Sound in C/C++ application - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Sound in C/C++ application

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?

Comments

  • katmankatman Posts: 24Member
    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?
    :
  • kianhongkianhong Posts: 2Member
    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?
    : :
    :

  • katmankatman Posts: 24Member
    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.
    :

Sign In or Register to comment.