Page 1 of 1

Loading wave file from datasection, then playing it

Posted: Wed Jul 09, 2014 12:01 pm
by Fangbeast
I haven't done this before so started to design a small section to play wave files when they are loaded from a datasection. The below code doesn't fail but doesn't play either.

What boneheaded thing have I forgotten this time?

The code after it is the PureBasic example which play my wave file quite well.

Code: Select all

Enumeration
  #SndStandard  
EndEnumeration
 
If InitSound()  <> 0
  Debug "Sound environment okay"
  If CatchSound(#SndStandard, ?_SndStandard)  <> 0
    Debug "Loaded the sound from the data section"
    Debug IsSound(#SndStandard)
    If PlaySound(#SndStandard, #PB_Sound_Loop) <> 0
      Debug "Playing the sound, I hope"
    Else
      Debug "Could not play the sound"  
    EndIf
  Else
    Debug "Could not load sound from the datasection"
  EndIf
Else
  Debug "Sound environment stuffed"
EndIf

DataSection
  _SndStandard: : IncludeBinary  "tng-doorbell.wav"
EndDataSection
If InitSound() = 0
MessageRequester("Error", "Sound system is not available", 0)
End
EndIf

Code: Select all

SoundFileName$ = OpenFileRequester("Choose a .wav file", "", "Wave files|*.wav",0)
If SoundFileName$
  If LoadSound(0, SoundFileName$)
    PlaySound(0, #PB_Sound_Loop)
    MessageRequester("Sound", "Playing the sound (loop)..."+#LF$+"Click to quit..", 0)
  Else
    MessageRequester("Error", "Can't load the sound.", 0)
  EndIf
EndIf
End 

Re: Loading wave file from datasection, then playing it

Posted: Wed Jul 09, 2014 12:09 pm
by ts-soft
You should add a messagerequester or anythink else, to make sure your application doesn't end before the sound comes.

Re: Loading wave file from datasection, then playing it

Posted: Wed Jul 09, 2014 1:12 pm
by Fangbeast
ts-soft wrote:You should add a messagerequester or anythink else, to make sure your application doesn't end before the sound comes.
That's very interesting, i've never read that anywhere *But then I wouldn't know where to look.

Since I'm playing the sound automatically when an alarm event happens and I don't want to delay the program, what else could I add there to make sure the sound works other than a blocking messagerequester?

**Edit** I'll use Delay(1000) now to give the program time to play the sound but that will also block 2 of my timers running in the background I think.

Re: Loading wave file from datasection, then playing it

Posted: Wed Jul 09, 2014 2:11 pm
by LuCiFeR[SD]
A slight deviation from your original code, but easy enough to adapt to your coding style.
you could launch a message requester as a thread... that way it would be non-blocking.

Code: Select all

If InitSound()
  sound=CatchSound(#PB_Any,?_SndStandard)
  
  If PlaySound(sound)
    
    Debug "Playing sound!"
    
    While SoundStatus(sound)=#PB_Sound_Playing
      Debug GetSoundPosition(sound)
    Wend
  Else
    Debug "Problem playing sound!"
  EndIf
Else
  Debug "Problem initializing sound system!"
EndIf

Debug "Program ending"
End

DataSection
  _SndStandard: : IncludeBinary  "tng-doorbell.wav"
EndDataSection

Re: Loading wave file from datasection, then playing it

Posted: Wed Jul 09, 2014 3:06 pm
by infratec
Hi Fangbeast,

I don't understand the problem:

If your main program is still running, you should hear the sound.
So you can simply use the PlaySound() command.

Code: Select all

Enumeration
  #SndStandard 
EndEnumeration


InitSound()

If CatchSound(#SndStandard, ?_SndStandard) = 0
  MessageRequester("Error", " Sound not loaded")
  End
EndIf


OpenWindow(0, 0, 0, 300, 200, "Test")

PlaySound(#SndStandard, #PB_Sound_MultiChannel)

Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_Event_LeftClick
    PlaySound(#SndStandard, #PB_Sound_MultiChannel)
  EndIf
  
Until Event = #PB_Event_CloseWindow



DataSection
  _SndStandard: : IncludeBinary  "tng-doorbell.wav"
EndDataSection
Bernd