CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2016
    Posts
    2

    How to play wav file without blocking application with xaudio2

    Hi!
    I am using xaudio2 to trigger very simple 100 ms .wav files. The timing is very important in this application, and when the mouse is clicked, the application sends a trigger to the parallel port, and 40 ms later, plays the sound. To play sound, I used MSDN's sample code Xaudio2BasicSound (find it here: https://code.msdn.microsoft.com/wind...mples-024b3933). In that code, the PlayWave() function is defined, which reads the wave file, creates a source voice, submits the data to a buffer, and plays the sound. However, the big problem is that it blocks the application while the sound is playing. I need to send additional triggers to my parallel port while the sound is playing, for example 10 ms after the beginning of the sound. What is it in this playwave function which is blocking the rest of the application? How can I modify that so that it effectively plays my sound in the background and still processes my parallel port triggers?

    I am attaching my code, which contains all the xaudio2 functionalities as well as the timing for the different events, in the RunTrials() function.

    tactile2ifc_piezo script.txt
    Thank you so much.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to play wav file without blocking application with xaudio2

    However, the big problem is that it blocks the application while the sound is playing
    Yes, comments in the source say it will. It blocks as no messages are being processed whilst the sound is played - ie control isn't being returned to the OS to obtain the next message. What you need to do is have another thread that plays the sound. That way whilst the second thread is playing the sound it no effect on the main thread which continues to process messages etc.

    PS Is this code c++/cli as there are references in the code to forms?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to play wav file without blocking application with xaudio2

    The problem part must be while(isRunning) loop in PlayWave(). This is where the caller thread is blocked. The only purpose of the loop is waiting until playback finishes to destroy voice not earlier than playback completes. In case you regroup the code to do the waiting and destruction in some other thread, PlayWave becomes non blocked. However, to do the trick you must be familiar with COM multithreading. Or you may run PlayWave itself as it now is in a separate thread along with all the COM stuff involved.
    Last edited by Igor Vartanov; October 3rd, 2016 at 04:20 PM.
    Best regards,
    Igor

  4. #4
    Join Date
    Oct 2016
    Posts
    2

    Re: How to play wav file without blocking application with xaudio2

    Quote Originally Posted by Igor Vartanov View Post
    In case you regroup the code to do the waiting and destruction in some other thread, PlayWave becomes non blocked. However, to do the trick you must be familiar with COM multithreading. Or you may run PlayWave itself as it now is in a separate thread along with all the COM stuff involved.
    Igor and 2kaud, thank you very much for your responses. 2kaud, I am indeed using C++ code with Visual Studio 2013.
    Could you give me some advice on how I would re-arrange my code so that waiting and destruction are processed in a separate thread? Your other suggestion was to run PlayWave itself with the COM stuff involved, but I'm not sure what you mean by this.
    Right now, I am calling the file name with PlayWave as part of my RunTrials() function, but would it help to pre-initialize the wav files as part of my PlayWave function, or will this not affect the block on the program?
    I am unfamiliar with COM multithreading, as you said, and any advice would be extremely helpful!
    Thanks very much!
    Last edited by koekoe_nut; October 4th, 2016 at 09:47 AM.

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to play wav file without blocking application with xaudio2

    Quote Originally Posted by koekoe_nut View Post
    Could you give me some advice on how I would re-arrange my code so that waiting and destruction are processed in a separate thread?
    You create a thread, marshal the voice pointer and do the waiting and destruction there in the thread.

    Your other suggestion was to run PlayWave itself with the COM stuff involved, but I'm not sure what you mean by this.
    You create a thread that runs PlayWave internally.

    Right now, I am calling the file name with PlayWave as part of my RunTrials() function, but would it help to pre-initialize the wav files as part of my PlayWave function, or will this not affect the block on the program?
    No it won't. So far I can see the while loop must be a blocker, but not wav preparations.

    I am unfamiliar with COM multithreading, as you said, and any advice would be extremely helpful!
    The advice I always give in a situation like yours: put aside your current project and get familiar with the subject. Find some good reading on COM and study it top to bottom. Do some test project to see if you understand the topic good enough, and only then get back to your original issue.

    COM is not a couple of recipes, it's a technology.
    Best regards,
    Igor

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to play wav file without blocking application with xaudio2

    I am indeed using C++ code with Visual Studio 2013
    Yes, but there is a difference between c++ and c++/cli. As your code contains references to forms, it suggests that this is c++/cli code for which there is a dedicated forum.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured