Ixaudio2voicecallback Callback of XAudio2 Learning

Source: Internet
Author: User

The advantage of using the Ixaudio2voicecallback callback is that the external program can be notified of other operations when the audio data is finished.

Using Ixaudio2voicecallback requires inheriting this interface and then re-implementing it, because all functions inside are pure virtual functions. The Onstreamend function is used in all functions, and onstreamend triggers an event when the audio data is played.

Inheritance implements the Ixaudio2voicecallback interface:

Class Voicecallback:public Ixaudio2voicecallback{public:    HANDLE hbufferendevent;    Voicecallback (): hbufferendevent (CreateEvent (null, FALSE, FALSE, NULL)) {}    ~voicecallback () {CloseHandle ( Hbufferendevent); }    //called When the voice has just finished playing a contiguous audio stream.    void Onstreamend () {SetEvent (hbufferendevent);}    Unused methods is stubs    void Onvoiceprocessingpassend () {}    void Onvoiceprocessingpassstart (UINT32 samplesrequired) {    }    void Onbufferend (void * pbuffercontext)    {}    void Onbufferstart (void * Pbuffercontext) {    }    void Onloopend (void * pbuffercontext) {    }    void Onvoiceerror (void * Pbuffercontext, HRESULT Error) {}};

How do I use callbacks?

Ixaudio2voicecallback is used for Createsourcevoice when creating Ixaudio2sourcevoice objects, and the fifth parameter is The pointer or address of the Ixaudio2voicecallback object.

Voicecallback pcallback; Ixaudio2sourcevoice *psourcevoice = Null;hr = Pengine->createsourcevoice (&psourcevoice, WaveFormat, 0, 1.0f, &pcallback);//create source sound to submit data if (FAILED (HR)) return 0;
then submit the audio data, call Ixaudio2sourcevoice start playback. Use WaitForSingleObject to wait for the audio data for this submission to complete: The first parameter is the event handle of the callback, and when the submitted audio data is played, the callback triggers onstreamend to set an event to Hbufferendevent, Then WaitForSingleObject will return; the second argument is the length of the wait, and infinite represents an infinite wait until the handle returns. Can also be based on the length of the data submitted by themselves, set a suitable value, such as 20, note unit is Ms.
WaitForSingleObject (Pcallback.hbufferendevent, INFINITE);
If you need to commit the data multiple times, you need to call submitsourcebuffer very often, but the maximum number of audio data queues Ixaudio2sourcevoice waiting to play cannot exceed xaudio2_max_queued_ Buffers is 64, otherwise it will cause a crash. In this case, you can do the following:
Xaudio2_voice_state state;psourcevoice->getstate (&state);//Gets the status while (state. buffersqueued > Xaudio2_max_queued_buffers-1) {waitforsingleobject (pcallback.hbufferendevent, INFINITE); Psourcevoice->getstate (&state);}
when the queue is over 63 o'clock, wait until the currently played buff plays, or it will block.

Note: The data in the Ixaudio2sourcevoice queue remains valid until playback is complete, or it crashes.

Test code (Wavefile.h/wavefile.cpp see previous article: XAudio2 Learning Shizhi Wave file format):

#pragma once#include "WaveFile.h" #include "XAudio2.h" class Voicecallback:public Ixaudio2voicecallback{public:handle Hbufferendevent; Voicecallback (): hbufferendevent (CreateEvent (null, FALSE, FALSE, null)) {}~voicecallback () {CloseHandle ( Hbufferendevent); }//called when the voice has just finished playing a contiguous audio stream.void onstreamend () {SetEvent (hbufferendevent );    }//unused methods is Stubsvoid onvoiceprocessingpassend () {}void Onvoiceprocessingpassstart (UINT32 samplesrequired) { }void onbufferend (void * pbuffercontext) {}void onbufferstart (void * pbuffercontext) {}void onloopend (void * pBu Ffercontext) {}void onvoiceerror (void * pbuffercontext, HRESULT Error) {}}; int main (int argc, char *argv[]) {HRESULT hr = CoInitializeEx (NULL, coinit_multithreaded);;  /com Initialize if (FAILED (HR)) return 0; IXAudio2 *pengine = NULL;  hr = Xaudio2create (&pengine);//Create Engine if (FAILED (HR)) return 0; Ixaudio2masteringvoice *pmastervoice = NULL; hr = Pengine->createmasterIngvoice (&pmastervoice);//Create a master sound, the default is to output the current speaker if (FAILED (HR)) return 0; Cwavefile Wavefile;  hr = Wavefile.open (L "f:\\ desktop \\24bit-48khz.wav", NULL, Wavefile_read);//Load file if (FAILED (HR)) return 0; WaveFormatEx *waveformat = Wavefile.getformat ();//Get file format Voicecallback pcallback; Ixaudio2sourcevoice *psourcevoice = Null;hr = Pengine->createsourcevoice (&psourcevoice, WaveFormat, 0, 1.0f,  &pcallback);//create source sound to submit data if (FAILED (HR)) return 0; DWORD size = Wavefile.getsize ();//Gets the size of the file BYTE *pdata = new byte[size];//request memory space for saving data hr = Wavefile.read (pData, size, &am p;size);//Read the file contents if (FAILED (HR)) return 0; Xaudio2_buffer buffer = {0};//will read the file data, assign the value Xaudio2_buffer buffer. audiobytes = size; Buffer.paudiodata = Pdata;buffer.  Flags = Xaudio2_end_of_stream;  hr = Psourcevoice->submitsourcebuffer (&buffer);//Submit memory Data if (FAILED (HR)) return 0; hr = Psourcevoice->start (0);//Start source Sound if (FAILED (HR)) return 0; Xaudio2_voice_state state;psourcevoice->getstate (&state);//Gets the status while (the state. buffersqueued > Xaudio2_max_queued_buffers-1) {waitforsingleobject (pcallback.hbufferendevent, INFINITE);  Psourcevoice->getstate (&state);} Pmastervoice->destroyvoice ();//Release Resources psourcevoice->destroyvoice ();//Release Resources pengine->release ();//Release resources CoUninitialize ();//release resource delete []pdata;//release resource pData = NULL; return 0; }
AC qq:1245178753
This address: http://blog.csdn.net/u011417605/article/details/50970574

SOURCE Download: http://download.csdn.net/detail/u011417605/9471383

Ixaudio2voicecallback Callback of XAudio2 Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.