help using fbsound-1.1 for playing multiple wav files

New to FreeBASIC? Post your questions here.
ron77
Posts: 80
Joined: Feb 21, 2019 19:24
Location: Israel
Contact:

help using fbsound-1.1 for playing multiple wav files

Postby ron77 » Jun 28, 2020 14:33

hello all...

i've checked the bug in my game "hikikomori" that causes my program to crash and it seems to be related to playing wav music files using fbsound lib.

here is the code to correctly load and play wav sound with fbsound lib. in the test bas files which works perfectly:

Code: Select all

'  ################################
' # fbs_load_and_destroy_wav.bas #
'################################

' simple speed test
' loading and reloading WAV files.
'#include "../inc/fbsound.bi"
#include "../inc/fbsound_dynamic.bi"

const data_path = "../data/"

' only if not same as exe path
' fbs_Set_PlugPath("./")


dim as boolean ok
ok=fbs_Init()
if ok=false then
  ? "error: fbs_Init() !"
  beep:sleep:end 1
end if

dim as integer hWave

? "any key = quit"
while inkey=""
  ? "loading      : ";
  fbs_Load_WAVFile(data_path & "atem.wav",@hWave)
  ? "ok"
  ? "wait on start: ";
  fbs_Play_Wave(hWave)
  while fbs_Get_PlayingSounds()=0:sleep 10:wend
  ? "ok "
  ? "wait on end  : ";
  while fbs_Get_PlayingSounds()>0:sleep 10:wend
  ? "ok":?
  ' !!! don't add more sounds in the pool of waves !!!
  fbs_Destroy_Wave(@hWave)
  sleep 100
wend 
end


and now here is how i implemented the use of fbsound lib. in my program:

Code: Select all

#INCLUDE ".\fbsound-1.1\inc\fbsound_dynamic.bi"
DIM SHARED AS long hWave

SUB SOUND(f AS STRING , t AS INTEGER)
   DIM AS BOOLEAN ok
   ok = fbs_Init()
   'DIM AS INTEGER hWave
   fbs_Load_WAVFile(f , @hWave)
   fbs_Play_Wave(hWave , t)
   sleep
   fbs_Destroy_Wave(@hWave)
END SUB

SUB guitar()
   
   IF songcounter = 0 THEN
        SOUND("pray_song.wav" , 1)
        txtfile("guitar1.txt")
        songcounter += 1
    ELSEIF songcounter = 1 THEN
        SOUND("love_song.wav" , 1)
        txtfile("guitar2.txt")
        songcounter += 1
    ELSEIF songcounter = 2 THEN
        SOUND("blues_song.wav" , 1)
        txtfile("guitar3.txt")
        songcounter += 1
    ELSEIF songcounter = 3 THEN
        SOUND("pray_song2.wav" , 1)
        txtfile("pray2.txt")
        songcounter += 1
    ELSEIF songcounter = 4 THEN
        SOUND("guitar_song.wav" , 1)
        txtfile("guitar4.txt")
        songcounter = 0
    END IF
   
END SUB




as you can see i made hWave an Dim Shared variable and i use it in a sub called SOUND and i destroy the @hWave variable later on in the code (after sleep command)... my question is if this form of use which is different then the original use in the example is correct or not and if not how can i correct it to play music in my code?

thank you.
fxm
Posts: 9946
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: help using fbsound-1.1 for playing multiple wav files

Postby fxm » Jun 28, 2020 14:58

Do you compile in 32-bit or 64-bit?
Tourist Trap
Posts: 2933
Joined: Jun 02, 2015 16:24

Re: help using fbsound-1.1 for playing multiple wav files

Postby Tourist Trap » Jun 28, 2020 15:13

I used that times ago and it worked:

Code: Select all

   #include "inc/fbsound_dynamic.bi"
   function FbSound_Init() as boolean
      dim as string  data_path
      data_path = exepath() & "/dta/"
      dim as boolean ok
      '
      ok=fbs_Init()
      if ok=false then
         ? "error: fbs_Init() !"
         ? FBS_Get_PlugError()
         beep
         sleep
      end if
      return ok
   end function
   function LoadWave(byref HWave as integer, byref WaveFileName as const string) as boolean
      dim as string  data_path
      data_path = exepath() & "/dta/"
      dim as boolean ok
      '
      ok=fbs_Load_WAVFile(data_path & WaveFileName,@HWave)
      if ok=false then
         ? "error: fbs_Load_WAVFile() !"
         beep
         sleep
      end if
      'get next free playback channel or create one
      'ok=fbs_Play_Wave(HWave)
      'if ok=false then
      '  ? "error: fbs_Play_Wave() !"
      '  beep
      '  sleep
      'end if
      return ok
   end function

Put there just in case it may help, it is not the most generic form due to assumption of sound data path (in execpath/dta/ here).
Also in execpath/lib/win32 I have the lib that suits for win32, and in execpath/lib/win64, the other.
I see also that I put the .BI file in execpath/inc/
Of course you must set up correctly the path in order to find the libs properly.
ron77
Posts: 80
Joined: Feb 21, 2019 19:24
Location: Israel
Contact:

Re: help using fbsound-1.1 for playing multiple wav files

Postby ron77 » Jun 28, 2020 15:53

i have both 32 and 64 compilers but i compile usually 32 bit compile
MrSwiss
Posts: 3610
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: help using fbsound-1.1 for playing multiple wav files

Postby MrSwiss » Jun 28, 2020 16:12

I'd suggest to use FBC 64 bit, whenever you are on a 64 bit operating system.
The advantage is: the 32 bit emulator (WoW on WIN) isn't needed, at all.
Typically: consumes more RAM and possibly, slows down execution speed.

From my point of view, the 32 bit version, is only good for legacy 32 bit OS's.
(or maybe: backwards compatibility testing)
fxm
Posts: 9946
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: help using fbsound-1.1 for playing multiple wav files

Postby fxm » Jun 28, 2020 16:22

It could be better to also try with:
DIM SHARED AS INTEGER hWave
Tourist Trap
Posts: 2933
Joined: Jun 02, 2015 16:24

Re: help using fbsound-1.1 for playing multiple wav files

Postby Tourist Trap » Jun 28, 2020 16:25

Have to add this, I simply copied my version from examples provided by D.J.Peters with the lib. Why not doing the same in order to avoid any issues?
badidea
Posts: 2149
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: help using fbsound-1.1 for playing multiple wav files

Postby badidea » Jun 28, 2020 17:37

ron77 wrote:

Code: Select all

...
fbs_Play_Wave(hWave , t)
sleep
fbs_Destroy_Wave(@hWave)
...

This seems dangerous to me. I assume the destroy deallocates memory linked to hWave. What happens if you do this while the 'wave' is still playing?

For a 'hSound', I once made this function:

Code: Select all

function my_fbs_Sound_playing(hSound as integer) as boolean
   dim as integer nLoops
   fbs_Get_SoundLoops(hSound, @nLoops)
   if nLoops = 0 then return false
   return true
end function

Some 'call back' function might also be possible, but too complicated for me at the moment.
ron77
Posts: 80
Joined: Feb 21, 2019 19:24
Location: Israel
Contact:

Re: help using fbsound-1.1 for playing multiple wav files

Postby ron77 » Jun 30, 2020 6:00

hello all...

i'm happy to say that the bug with fbsound has been fixed...

last night thanks to imortis who was kind enough to review my code he quickly found that the problem was that each time fbsound was used in the sub block of code it had "fbs_init()" in it which means that each time a wave file was played the program initialized fbsound THAT WAS A MISTAKE! the use if "fbs_init()" is supposed to be used only once! ever! and not every time a sound or music is played! that is why the program was crashing vause it run out of memory sooner or later... once we fixed that - the program became stable...

i thank you all for trying to help me...

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 6 guests