Loadsound()/Playsound() problem (Solved).

Just starting out? Need help? Post your questions and find answers here.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Loadsound()/Playsound() problem (Solved).

Post by RASHAD »

Hi again heartbone
You do not have to pack unpack sound files to hard disk(No need for IncludeBinary)
You can play sound files direct from memory as follows :

1- Save the next resource file as "Sound_Files.rc"
2- Add the resource file to your code using Compiler -->Compiler Options --> Resources
3- Save your code
4- Run any file from memory using the following snippet

Sound_Files.rc :

;*************************************************
// LANGUAGE LANG_ENGLISH,SUBLANG_ENGLISH_US

SOUND_1 WAVE "e:\\flourishGSM.wav"
SOUND_2 WAVE "e:\\flourishIMA.wav"
SOUND_3 WAVE "e:\\flourishMPG.wav"
SOUND_4 WAVE "e:\\flourishMSA.wav"
;*************************************************

Code: Select all

hInst = GetModuleHandle_(#Null)
hResInfo = FindResource_(hInst, "SOUND_1", "WAVE")
hRes = LoadResource_(hInst, hResInfo)
lpRes = LockResource_(hRes)

sndPlaySound_(lpRes,#SND_MEMORY | #SND_SYNC | #SND_NODEFAULT)

hResInfo = FindResource_(hInst, "SOUND_3", "WAVE")
hRes = LoadResource_(hInst, hResInfo)
lpRes = LockResource_(hRes)

sndPlaySound_(lpRes,#SND_MEMORY | #SND_SYNC | #SND_NODEFAULT)

Egypt my love
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Loadsound()/Playsound() problem (Solved).

Post by heartbone »

Thank you RASHAD for the detailed reply.
I learned something significant about the PureBasic environment's toolset from it, and I will try to use your approach in the future.
However as you already have given me a very simple Windows® solution for this particular issue,
before I implement your suggestion above to play non-PCM .wavs,
I just wanted to make sure... are you suggesting that your code will work in Linux?

Shardik just gave me another possible approach to use to play these .wav files in Linux, but I'd prefer not to have to muck around with calling OS level commands which might fail on certain installations, if I can find a PureBasic/API solution.
Keep it BASIC.
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Loadsound()/Playsound() problem (Solved).

Post by Shardik »

heartbone wrote:At this point my executable is 829 KB and using the PCM wav is out of the question if I intend to deliver a less than 1 MB final executable.
If I see that finishing the implementation will put me over the desired 1 MB limit, (my best guess is 50-50 at this point), then I'll go ahead and use that large PCM file.
But if I can squeeze the entire program in under a megabyte without that unnecessary flourish, then that sound won't make it.
Whereas an 8 KB .wav file, plus your code fix, would make the desired flourish much more likely to be included in a 1 MB executable.

In my opinion one of PureBasic strengths is the small executable file size.
If I can deliver a fully working, non-trivial three player networked game in less than 1 megabyte, then that might garner some deserved notice.
Why don't you convert your WAV files into the OGG format which will even shrink the size of your WAV files considerably. PureBasic offers UseOGGSoundDecoder() to play OGG sound files without problems cross-platform. Just try the following simple example for Linux (should run without any change on Ubuntu 14.04) and Windows (for Windows I have copied the Ubuntu OGG sound file to drive H:) which plays an OGG sound file from memory:

Code: Select all

UseOGGSoundDecoder()

OpenWindow(0, 100, 100, 200, 45, "OGG sound demo")
ButtonGadget(0, 10, 10, 180, 25, "Play OGG sound")

If InitSound()
  If CatchSound(0, ?SoundStart, ?SoundEnd - ?SoundStart)
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          StopSound(0)
          Break
        Case #PB_Event_Gadget
          If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
            PlaySound(0)
          EndIf
      EndSelect
    ForEver
  EndIf
EndIf

End

DataSection
  SoundStart:
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux
        IncludeBinary "/usr/share/sounds/ubuntu/stereo/phone-incoming-call.ogg"
      CompilerCase #PB_OS_Windows
        IncludeBinary "H:\phone-incoming-call.ogg"
    CompilerEndSelect
  SoundEnd:
EndDataSection
If sound quality is very important to you and the degradation due to the conversion into the OGG format is not tolerable, you may try the lossless FLAC decoder using PB's UseFLACDecoder() which also will shrink the size of your WAV files although only to a smaller amount... :wink:
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Loadsound()/Playsound() problem (Solved).

Post by heartbone »

Shardik wrote:Why don't you convert your WAV files into the OGG format which will even shrink the size of your WAV files considerably. PureBasic offers UseOGGSoundDecoder() to play OGG sound files without problems cross-platform. Just try the following simple example for Linux (should run without any change on Ubuntu 14.04) and Windows (for Windows I have copied the Ubuntu OGG sound file to drive H:) which plays an OGG sound file from memory:
If sound quality is very important to you and the degradation due to the conversion into the OGG format is not tolerable, you may try the lossless FLAC decoder using PB's UseFLACDecoder() which also will shrink the size of your WAV files although only to a smaller amount... :wink:
No the quality is not as important as size.
That packet of 6 wavs that I linked to above, was actually 5 wavs and 1 ogg.
I included the ogg instead of the higher quality mp3 encoded wav for sound quality comparison purposes,
and because the higher bitrate mp3 was never considered for use (again size).
In yet another game that I'm working on, I did a comparison in Windows® to see how much got pulled in using ogg.
(That 179 Windows® KBs flourish.wav = 183 Linux KBs. I'm on Linux as usual, so these files sizes are Linux numbers.)

Sv2.exe using the 183KB flourish.wav 741 KB
Sv2.exe using the 58KB flourish.ogg 972 KB

So in Windows®, over 250K RAM of binary libraries were needed to play a 4 second ogg.

I know that I am not alone in liking to keep things compact.
There seems to be great interest in packing, punching, and crunching on this forum,
because PureBasic seems such a great tool for achieving those purposes.

Also the 8KB mp3 wav file that I can use in Windows® is considerably smaller than the 179KB PCM wav, or the ~58KB ogg.

Thanks and peace.
Keep it BASIC.
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Loadsound()/Playsound() problem (Solved).

Post by Shardik »

heartbone,

sorry, I was not able to download your dropbox file because dropbox is blocked by our corporate firewall at my working place. But It just looked as if your download link wasn't available anymore and I were not able to take a look at your sound files. But I am now at home and have just successfully downloaded your files...
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Loadsound()/Playsound() problem (Solved).

Post by RASHAD »

Again for windows only
- You can play many sound formats like midi,mp3,wav,wma,ogg and maybe more
- You can set the sound volume from 0 to 1000
For sound format other than WAVE it depends on sound codec

Code: Select all

;Volume from 0 To 1000
Volume = 1000

Filename$ = OpenFileRequester("","","ALL|*.*;*.mid|Wave|*.wav|mp3|*.mp3| OGG|*.OGG|MID|*.MID",0) 
If Filename$
      mciSendString_("OPEN "+Chr(34)+Filename$+Chr(34)+" Type MPEGVideo ALIAS "+Str(0),0,0,0) 
      Length$ = Space(#MAX_PATH) 
      mciSendString_("Status "+Str(0)+" length",@Length$,#MAX_PATH, 0)
      mciSendString_("SetAudio "+Str(0)+" volume to "+Str(Volume),0,0, 0)
      mciSendString_("Play "+Str(Nb),0,0,0)
      Delay(Val(Length$))
 EndIf
Egypt my love
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Loadsound()/Playsound() problem (Solved).

Post by heartbone »

Once again thank you very much RASHAD.
Now I don't even have to muck with a compressed .wav file, because now I can play the original .mid in full fidelity and with the correct volume level. :D

I assume that in the code above, Nb can be substituted with the number zero.

Although now I owe you a big one, I doubt if I'll ever have any valuable OS information to repay you in kind. Sorry.
Keep it BASIC.
SeregaZ
Enthusiast
Enthusiast
Posts: 619
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Loadsound()/Playsound() problem (Solved).

Post by SeregaZ »

this code can somehow convert wav to another format? i mean was 44kGz, stereo come to 11025kGz, mono for example
Post Reply