XAudio2 Learning Sample Rate Conversion

Source: Internet
Author: User

The wave file can be converted to sample rate using Ixaduio2submixvoice to meet the device's requirements for waveform audio sample rate. In XAudio2 learn three to get the audio output device information you can see that the audio playback device has its own support for playing the wave file format.

Take the 2-channel/44.1k sample-rate wave to 2-channel/48k wave as an example:

First of all, initialize the COM component;

HRESULT hr = CoInitializeEx (NULL, coinit_multithreaded);; /com Initialize if (FAILED (HR)) return 0;

Then, then create the IXAudio2 engine;

IXAudio2 *pengine = NULL; hr = Xaudio2create (&pengine);//Create Engine if (FAILED (HR)) return 0;

Then, create the main sound, and specify the number of channels is 2, the sampling rate is 48K;

WaveFormatEx waveformatex;//Sets the format of the main sound waveformatex.nchannels = 2;waveformatex.nsamplespersec = 48000; Waveformatex.wbitspersample = 32;waveformatex.navgbytespersec = 384000;waveformatex.nblockalign = 8; Waveformatex.wformattag = WAVE_FORMAT_PCM; Ixaudio2masteringvoice *pmastervoice = NULL; hr = Pengine->createmasteringvoice (&pmastervoice,waveformatex.nchannels,waveformatex.nsamplespersec);// Create a master sound, the default is to output the current speaker if (FAILED (HR)) return 0;
then, read the wave file and get the file format information;

Cwavefile Wavefile; hr = Wavefile.open (L "c:\\users\\xdd\\desktop\\ the time has gone. wav", NULL, Wavefile_read);//Load file if (FAILED (HR)) return 0; WaveFormatEx *waveformat = Wavefile.getformat ();//Get file format
then, create a Ixaudio2submixvoice object that specifies the number of channels to get the number of channels in the file format when created, the sample rate for the file's sample rate, and the output voice of the specified object as mastering voice:

Xaudio2_send_descriptor psend;psend.poutputvoice = pmastervoice;//Specify Output as mastering Voicepsend.flags = XAUDIO2_SEND_ Usefilter; Xaudio2_voice_sends psendlist;psendlist.psends = &psend;psendlist.sendcount = 1;IXAudio2SubmixVoice * Psubmixvoice = NULL;HR = Pengine->createsubmixvoice (&psubmixvoice, Waveformat->nchannels, waveFormat-> Nsamplespersec, 0, 0, &psendlist);//Specify Output as mastering Voiceif (FAILED (HR)) return 0;
In addition to specifying the output voice directly above on creation, you can also use the following method:

Psubmixvoice->setoutputvoices (&psendlist);
then, create the Ixaudio2sourcevoice object, create the format information using the obtained file, and specify the output voice for the Ixaduio2submixvoice object created above, Use Ixaudio2voicecallback callback at the same time for easy playback:
Psend.poutputvoice = psubmixvoice;//The specified output is submixvoicepsend.flags = Xaudio2_send_usefilter;psendlist.psends = & Psend;psendlist.sendcount = 1; Voicecallback Voicecallback; Ixaudio2sourcevoice *psourcevoice = Null;hr = Pengine->createsourcevoice (&psourcevoice, WaveFormat, 0, 1.0f, &voicecallback,&psendlist);//Create a source sound to submit data. Specify the output as Submixvoice if (FAILED (HR))

Then, read the wave audio data and use the Ixaudio2sourcevoice object to submit the data and start the playback:

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;
then, using the callback, wait for the playback to complete:
Xaudio2_voice_state state;psourcevoice->getstate (&state);//Gets the status while (state. buffersqueued) {WaitForSingleObject (voicecallback.hbufferendevent, INFINITE);p sourcevoice->getstate (& State);}
Finally, release the resources.
Pmastervoice->destroyvoice ();//Release Resources psubmixvoice->destroyvoice ();//Psourcevoice->destroyvoice ();//Release resources Pengine->release ();//Release Resource couninitialize ();//release resource delete []pdata;//release resource pData = NULL;
compile, run. Can hear a very clear sound.


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

This address: http://blog.csdn.net/u011417605/article/details/50990215
AC qq:1245178753

XAudio2 Learning Sample Rate Conversion

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.