Loading wave file from datasection, then playing it

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Loading wave file from datasection, then playing it

Post 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 
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Loading wave file from datasection, then playing it

Post by ts-soft »

You should add a messagerequester or anythink else, to make sure your application doesn't end before the sound comes.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Loading wave file from datasection, then playing it

Post 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.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: Loading wave file from datasection, then playing it

Post 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
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Loading wave file from datasection, then playing it

Post 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
Post Reply