Sound & Music

Moderators: Sephiroth
Number of threads: 611
Number of posts: 1220

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Sound in C/C++ application Posted by kianhong on 9 Sept 2005 at 12:46 AM
I have tried PlaySound both synchronous and asynchronous but my c application seems to use the same resource as PlaySound as the wav file stop playing(although running in ASYNC mode) when it reaches other parts of the application code.

My question now is whether i can still use PlaySound to do the sound by changing a few options in PlaySound.I'm not quite sure what option like SND_APPLICATION AND SND_MEMORY does even after reading the documentation at microsoft.com.Can someone explain them to me?

If the problem is not solvable by PlaySound then i wish to use c code to open/play a wav file using realPlayer or mediaPlayer.Can someone show me?
Report
Re: Sound in C/C++ application Posted by katman on 12 Sept 2005 at 9:18 AM
I create a literal image of a wave file in memory and pass a pointer to this memory buffer and use the SND_MEMORY option. The PlaySound call I use looks something like this.

PlaySound((LPCSTR)wavePtr, 0, SND_MEMORY | SND_ASYNC | SND_LOOP)

This statement starts playing the sound pointed to by wavePtr in an endless loop (SND_LOOP) but returns immediately (SND_ASYNC) to my calling routine so that I can do other things.

I end the sound intentionally with:

PlaySound(NULL, NULL, SND_PURGE); // End any ongoing sounds.


I haven't used SND_APPLICATION, but the documentation indicates that the pszSound field of the PlaySound call identifies an application-specific association. Sorry I can't be more help there, but if you tinker with it you can probably figure it out.


: I have tried PlaySound both synchronous and asynchronous but my c application seems to use the same resource as PlaySound as the wav file stop playing(although running in ASYNC mode) when it reaches other parts of the application code.
:
: My question now is whether i can still use PlaySound to do the sound by changing a few options in PlaySound.I'm not quite sure what option like SND_APPLICATION AND SND_MEMORY does even after reading the documentation at microsoft.com.Can someone explain them to me?
:
: If the problem is not solvable by PlaySound then i wish to use c code to open/play a wav file using realPlayer or mediaPlayer.Can someone show me?
:
Report
Re: Sound in C/C++ application Posted by kianhong on 14 Sept 2005 at 12:29 AM
Hmm,how do i do a literal image of the wav file?I'm not quite sure how to do it.

: I create a literal image of a wave file in memory and pass a pointer to this memory buffer and use the SND_MEMORY option. The PlaySound call I use looks something like this.
:
: PlaySound((LPCSTR)wavePtr, 0, SND_MEMORY | SND_ASYNC | SND_LOOP)
:
: This statement starts playing the sound pointed to by wavePtr in an endless loop (SND_LOOP) but returns immediately (SND_ASYNC) to my calling routine so that I can do other things.
:
: I end the sound intentionally with:
:
: PlaySound(NULL, NULL, SND_PURGE); // End any ongoing sounds.
:
:
: I haven't used SND_APPLICATION, but the documentation indicates that the pszSound field of the PlaySound call identifies an application-specific association. Sorry I can't be more help there, but if you tinker with it you can probably figure it out.
:
:
: : I have tried PlaySound both synchronous and asynchronous but my c application seems to use the same resource as PlaySound as the wav file stop playing(although running in ASYNC mode) when it reaches other parts of the application code.
: :
: : My question now is whether i can still use PlaySound to do the sound by changing a few options in PlaySound.I'm not quite sure what option like SND_APPLICATION AND SND_MEMORY does even after reading the documentation at microsoft.com.Can someone explain them to me?
: :
: : If the problem is not solvable by PlaySound then i wish to use c code to open/play a wav file using realPlayer or mediaPlayer.Can someone show me?
: :
:

Report
Re: Sound in C/C++ application Posted by katman on 14 Sept 2005 at 10:02 AM
The image in memory has the same format as a wave file. E.G.,

"RIFF"
int filesize
"WAVE"
"fmt "
long chunksize
short wFormatTag
unsigned short wChannels
unsigned long dwSamplesPerSec
unsigned long dwAvgBytesPerSec
unsigned short wBlockAlign
unsigned short wBitsPerSample
"data"
long chunksize
.
.
.
wave data
.
.
.

You can allocate some memory with:

wavePtr = (char *)VirtualAlloc(0, waveSize, MEM_COMMIT, PAGE_READWRITE)

Then generate your waveform (in my case a sinewave) or read in a wave file and modify it by using wavePtr.

In my case I split up a short int into 2 bytes for a 16 bit mono waveform stored in little Endian.

This looks something like the following for a single 16bit sound value, where intamp is my short int wave value, and I previously assigned tempptr = wavePtr.

intampl = intamp&255;
intamph = intamp >> 8; // shift hibyte to lobyte
intamph = intamph&255;
*tempptr = (unsigned char)intampl;
++tempptr;
*tempptr = (unsigned char)intamph;
++tempptr;


: Hmm,how do i do a literal image of the wav file?I'm not quite sure how to do it.
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.