Re: wav files

Discussions related to sound, music, video and other multimedia applications
Hated Moron

Re: wav files

Post by Hated Moron »

On 05/03/2023 07:57, geoffevenden via groups.io wrote (cross-posted from the Discussion Group):
Hi
this may seem simple but i am learning.
could anyone write an example of how to play a .wav file in my program. my file name is test1.wav
You don't say what version of BBC BASIC you are using, so I will assume it's BBC BASIC for SDL 2.0; you also don't say what the path to the file is, so I will assume it's in the same directory as the BASIC program itself. Additionally I have assumed CD-quality audio (44.1 kHz sampling, 2 channels). With those assumptions here is some code to play the WAV file:

Code: Select all

      INSTALL @lib$ + "audiolib"
      PROC_initAudio
      wav%% = FN_loadWAV(@dir$ + "test1.wav", 44100, 2)
      IF wav%% = 0 ERROR 100, "Couldn't open WAV file"
      PROC_playSound(wav%%)
If the duration of the WAV file is greater than about 27 seconds you must periodically call PROC_trackMusic() to keep the buffer 'topped up' and control what happens when the end of the music is reached. Here I am assuming you just want it to stop playing:

Code: Select all

      REPEAT
        REM do whatever your program needs to do in the foreground here
        PROC_trackMusic(wav%%, FALSE, FALSE, TRUE)
        WAIT 10
      UNTIL whatever_condition_terminates_your_program
The WAIT 10 is an arbitrary delay for the purposes of illustration, in practice it will depend on what your program is doing. But never spin around a tight loop without a delay of some sort.
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: wav files

Post by KenDown »

On the other hand, if you are using BB4W, you could try the following:

Code: Select all

      PROCloadsound
      SYS"PlaySound",tada1%,0,5
      WAIT 340
      SYS"PlaySound",tada1%,0,5
      END
      :
      DEFPROCloadsound
      snd%=0
      wave$="bang01.wav"
      REM It is assumed that the .wav file is in the same directory as your program.
      file%=OPENIN(wave$)
      size%=EXT#file%
      CLOSE#file%
      DIMtada1%size%-1
      OSCLI("LOAD "+CHR$34+@dir$+wave$+CHR$34+" "+STR$~tada1%)
      ENDPROC
As the sound file is loaded into memory, you will need more than the trail version of BB4W. I'm not saying this is the best way of doing it, but it does work.
Hated Moron

Re: wav files

Post by Hated Moron »

On 05/03/2023 12:16, geoffevenden via groups.io wrote (cross-posted from the Discussion Group):
thanks for that I am using bb4w
Oh, you should have said! I thought it was a pretty safe bet that you were not using BBC BASIC for Windows since the Help manual has a section on playing WAV files and therefore I would not have expected that you would need to ask. The manual lists this code:

Code: Select all

SND_ASYNC = 1
SND_FILENAME = &20000
wave$ = "\windows\media\tada.wav"
SYS "PlaySound", wave$, 0, SND_FILENAME + SND_ASYNC
It can be found simply by typing WAV into the search box in the Help viewer.
Hated Moron

Re: wav files

Post by Hated Moron »

KenDown wrote: Sun 05 Mar 2023, 10:56 As the sound file is loaded into memory, you will need more than the trail version of BB4W. I'm not saying this is the best way of doing it, but it does work.
I don't see any benefit of loading the file into memory. If you are playing it multiple times (such as with a sound effect in a game) it might slightly reduce the overhead of repeatedly playing it straight from the file, but since files are automatically cached in memory by Windows that is doubtful.

Incidentally, although the trial version of BB4W has a memory limit, remember that you don't have to pay anything for a version that doesn't. SDLIDE_for_BB4W is completely free and provides a fully-functional version of BBC BASIC for Windows without any memory limits (up to 512 Mbytes).

The only thing it can't do (without the full version of BB4W being installed as well) is create standalone exes. But it does several things better:
  • it has breakpoints, which the BB4W IDE doesn't.
  • It runs your program in a separate process, meaning it is far better protected from crashing.
  • The listing of functions and procedures in your program is (optionally) alphabetical.
  • The program comparator and profiler are built-in rather than separate utilities.
  • It has a Dark Mode, something that many people have asked for.
  • It is written in BBC BASIC itself, so is in principle far more customisable.
Since the interface is identical to that provided by the desktop editions of BBC BASIC for SDL 2.0 it can make switching between the versions, depending on the application, more straightforward.