For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > October 2004 > Playing a WAV sound loaded into RAM









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Playing a WAV sound loaded into RAM
Sean

2004-10-28, 3:55 am

Perhaps someone can help me here. I am trying to load a WAV file into RAM
and play it from there. I can't seem to get it right though. Any help is
appreciated, here's my code so far:


Dim retval As Long ' return value
Dim hMem As Long, pMem As Long ' handle and pointer to memory block
Dim WavLen As Long 'length of wave file

'>>>>--------> Entire Contents of Wave File is loaded into WAVString(1) in
another sub

'Get the byte length of the string
WavLen = LenB(WAVString(1))
Debug.Print "WavLen: " & WavLen

' Allocate a moveable block of memory (returns a handle) (Integer type = 2
bytes)
hMem = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, WavLen)

' Lock the memory block, returning a pointer to it
pMem = GlobalLock(hMem)
Debug.Print "pMem: " & pMem

'Copy contents of wave file into memory location
CopyMemory ByVal pMem, WAVString(1), WavLen

'Play Sound from memory location
retval = PlaySound(pMem, frmMenu.hWnd, SND_MEMORY)
Debug.Print "RetVal: " & retval

' Unlock the memory block, destroying the pointer and freeing resources
retval = GlobalUnlock(hMem)

' Free the memory block (de-allocate it)
retval = GlobalFree(hMem)



Larry Serflaten

2004-10-28, 3:55 am


"Sean" <Sean@NOSPAMEROONIE.net> wrote
> Perhaps someone can help me here. I am trying to load a WAV file into RAM
> and play it from there. I can't seem to get it right though. Any help is
> appreciated, here's my code so far:


That looks rather short. I saw nothing about preparing a header, or writing
the wave to the device, etc.

For another take on creating sound from raw data, check out this demo:

www.usinternet.com/users/serflaten/makesound.zip

LFS
Tom Esh

2004-10-28, 3:55 am

On Thu, 28 Oct 2004 15:33:40 +1000, "Sean" <Sean@NOSPAMEROONIE.net>
wrote:

>Perhaps someone can help me here. I am trying to load a WAV file into RAM
>and play it from there. I can't seem to get it right though. Any help is
>appreciated, here's my code so far:
>
>
>Dim retval As Long ' return value
>Dim hMem As Long, pMem As Long ' handle and pointer to memory block
>Dim WavLen As Long 'length of wave file
>
>'>>>>--------> Entire Contents of Wave File is loaded into WAVString(1) in
>another sub
>
>'Get the byte length of the string
>WavLen = LenB(WAVString(1))
>Debug.Print "WavLen: " & WavLen
>
>' Allocate a moveable block of memory (returns a handle) (Integer type = 2
>bytes)
>hMem = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, WavLen)


Looks painful :-)
It's actually much easier than that if you're dealing with a wav file
or wav file image. The String type adds unneccessary complication
here, and you can also dispense with the mem management with the
proper PlaySound declaration.

Public Const SND_SYNC = &H0
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_ALIAS = &H10000
Public Const SND_FILENAME = &H20000
Public Const SND_RESOURCE = &H40004
Public Const SND_ALIAS_ID = &H110000
Public Const SND_ALIAS_START = 0
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10
Public Const SND_VALID = &H1F
Public Const SND_NOWAIT = &H2000
Public Const SND_VALIDFLAGS = &H17201F
Public Const SND_TYPE_MASK = &H170007

Public Declare Function PlaySoundAny Lib "winmm.dll" _
Alias "PlaySoundA" _
(lpSound As Any, ByVal hModule As Long, _
ByVal dwFlags As Long) As Long


Dim bWavData() As Byte

'just load the wav data into a byte array...
'bWavData = LoadResData(...whatever..)
'...and pass the 1st byte ByRef...
PlaySoundAny bWavData(0), 0, SND_MEMORY Or SND_ASYNC Or SND_NODEFAULT


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
Sean

2004-10-28, 8:55 am

"Tom Esh" <tjeshGibberish@earthlink.net> wrote in message
news:0071o05nafd8m46pvmi0dacsefsk691j5e@
4ax.com...
> PlaySoundAny bWavData(0), 0, SND_MEMORY Or SND_ASYNC Or SND_NODEFAULT
> -Tom
> MVP - Visual Basic
> (please post replies to the newsgroup)


I have tried it this way too, but passing the variable containing the wave
file as the argument causes hiccups in the sound which make it an
unacceptable solution. I need to play the sound (asynchronously), and have
the play triggered again before the sound finishes playing. This is when the
hiccups (short staticy screeching sounds etc) happen if I pass the sound
this way. I was hoping if I could place the file in memory, and play it from
there, then the "hiccups" would be gone. I haven't tried loading and passing
the sound as a byte array (just a string), so maybe I'll try that next.
Can't see that it'll help though, but here's hoping!


Sean

2004-10-28, 8:55 am

"Larry Serflaten" <serflaten@usinternet.com> wrote in message
news:uDi14lLvEHA.1392@TK2MSFTNGP14.phx.gbl...
>
> That looks rather short. I saw nothing about preparing a header, or
> writing
> the wave to the device, etc.
>
> For another take on creating sound from raw data, check out this demo:
>
> www.usinternet.com/users/serflaten/makesound.zip
>
> LFS


Thanks, but the wave files are already created. I just have a need to play
them from memory (not file). I've had a look at that code, but can't see a
way I could use it to load and play a pre-existing wave file from memory.

Sean


Schmidt

2004-10-28, 8:55 am


"Sean" <Sean@NOSPAMEROONIE.net> schrieb im Newsbeitrag news:%
> Thanks, but the wave files are already created. I just have a need to play
> them from memory (not file). I've had a look at that code, but can't see a
> way I could use it to load and play a pre-existing wave file from memory.


DirectSound can create soundbuffers from wavefiles and can play them
independently (without 'hickups').
Here's an example:

http://tinyurl.com/4lgbm

Olaf


nrford

2004-10-28, 3:55 pm

"Sean" <Sean@NOSPAMEROONIE.net> wrote in message
news:%23uFEpLMvEHA.3840@TK2MSFTNGP12.phx.gbl...
> "Tom Esh" <tjeshGibberish@earthlink.net> wrote in message
> news:0071o05nafd8m46pvmi0dacsefsk691j5e@
4ax.com...
>
> I have tried it this way too, but passing the variable containing the wave
> file as the argument causes hiccups in the sound which make it an
> unacceptable solution. I need to play the sound (asynchronously), and have
> the play triggered again before the sound finishes playing. This is when
> the hiccups (short staticy screeching sounds etc) happen if I pass the
> sound this way. I was hoping if I could place the file in memory, and play
> it from there, then the "hiccups" would be gone. I haven't tried loading
> and passing the sound as a byte array (just a string), so maybe I'll try
> that next. Can't see that it'll help though, but here's hoping!



If you find a solution, I hope you'll post it here.

NF


Tom Esh

2004-10-28, 3:55 pm

On Thu, 28 Oct 2004 17:51:10 +1000, "Sean" <Sean@NOSPAMEROONIE.net>
wrote:

>
>I have tried it this way too, but passing the variable containing the wave
>file as the argument causes hiccups in the sound which make it an
>unacceptable solution....


Big wav sound? Seems that's come up before. IIRC one at least
partially successful work-around was to use the mci Api. A Google NG
search may turn up the thread(s). DirectSound, as Olaf suggested, is
another option, but would be my last choice only because you''ve got
another dependency which may not be installed on the system.


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
Sean

2004-10-28, 8:55 pm

"Schmidt" <sss@online.de> wrote in message
news:uDPKdZMvEHA.3152@TK2MSFTNGP14.phx.gbl...
>
> "Sean" <Sean@NOSPAMEROONIE.net> schrieb im Newsbeitrag news:%
>
> DirectSound can create soundbuffers from wavefiles and can play them
> independently (without 'hickups').
> Here's an example:
>
> http://tinyurl.com/4lgbm
>
> Olaf


Thanks Olaf, I'll look into it!


Sean

2004-10-28, 8:55 pm

"Tom Esh" <tjeshGibberish@earthlink.net> wrote in message
news:quc2o0hr8fuo3ca6dfmuj5j46firbjohn5@
4ax.com...
> On Thu, 28 Oct 2004 17:51:10 +1000, "Sean" <Sean@NOSPAMEROONIE.net>
> wrote:
>
>
> Big wav sound? Seems that's come up before. IIRC one at least
> partially successful work-around was to use the mci Api. A Google NG
> search may turn up the thread(s). DirectSound, as Olaf suggested, is
> another option, but would be my last choice only because you''ve got
> another dependency which may not be installed on the system.
>


Nope, the sound is only 1.2 kb, which I would think is tiny by any measure.
Thanks for the suggestion on using MCI, I'll look into that next.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com