Forums

play sound in C or C++

Started by ldkha1979 June 9, 2008
hi all,
I want to know how to play sound form an array of value with a given
frequency sampling (Fs = 16000Hz).
I have an array of value (16 bit) then I wwant to play like with
sound.m in matlab.
Thks a lot!

ldkha1979 wrote:

> hi all, > I want to know how to play sound form an array of value with a given > frequency sampling (Fs = 16000Hz). > I have an array of value (16 bit) then I wwant to play like with > sound.m in matlab. > Thks a lot!
This will help you: waveformatex.wFormatTag = WAVE_FORMAT_PCM; waveformatex.nChannels = WAVEOUT_CHANNELS; waveformatex.nSamplesPerSec = (int)WAVEOUT_FS; waveformatex.nAvgBytesPerSec = (int)(WAVEOUT_FS*2*WAVEOUT_CHANNELS); waveformatex.nBlockAlign = 2*WAVEOUT_CHANNELS; waveformatex.wBitsPerSample = 16; waveformatex.cbSize = 0; for(i=0;i<WAVEOUT_NUM_OF_BUFFERS;i++) { wh_data[i] = new char[WAVEOUT_BUFFER_LENGTH]; memset(wh_data[i],0,WAVEOUT_BUFFER_LENGTH); wh[i].lpData = wh_data[i]; wh[i].dwBufferLength = WAVEOUT_BUFFER_LENGTH; wh[i].dwBytesRecorded = 0; wh[i].dwUser = 0; wh[i].dwFlags = 0; wh[i].dwLoops = 0; } waveOutOpen(&hwo, WAVEOUT_DEVICE_ID, &waveformatex, (DWORD)waveOutProc, (DWORD)this, // Callbackinstance CALLBACK_FUNCTION|WAVE_FORMAT_DIRECT); for(i = 0; i < (WAVEOUT_NUM_OF_BUFFERS - 1); i++) { waveOutPrepareHeader(hwo,&wh[i],sizeof(wh[i])); waveOutWrite(hwo,&wh[i],sizeof(wh[i])); } waveout_buffer_id = WAVEOUT_NUM_OF_BUFFERS - 1; waveout_flag = 0; //------------------------------------ Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
On Jun 9, 5:36 pm, Vladimir Vassilevsky <antispam_bo...@hotmail.com>
wrote:

> waveOutOpen(&hwo, > WAVEOUT_DEVICE_ID, > &waveformatex, > (DWORD)waveOutProc, > (DWORD)this, // Callbackinstance > CALLBACK_FUNCTION|WAVE_FORMAT_DIRECT);
Will probably also be necessary to link winmm.lib There may also be #include requirements though I'm not sure (looks like I may be getting by with windows.h and windowsx.h) MSDN is one's friend. The example code there isn't a bad starting point when trying to figure out how to make a widoze box do something.
On Jun 9, 5:36&#2013266080;pm, Vladimir Vassilevsky <antispam_bo...@hotmail.com>
wrote:
> ldkha1979 wrote: > > hi all, > > I want to know how to play sound form an array of value with a given > > frequency sampling (Fs = 16000Hz). > > I have an array of value (16 bit) then I wwant to play like with > > sound.m in matlab. > > Thks a lot! > > This will help you: > > waveformatex.wFormatTag = WAVE_FORMAT_PCM; > waveformatex.nChannels &#2013266080;= WAVEOUT_CHANNELS; > waveformatex.nSamplesPerSec &#2013266080;= (int)WAVEOUT_FS; > waveformatex.nAvgBytesPerSec = (int)(WAVEOUT_FS*2*WAVEOUT_CHANNELS); > waveformatex.nBlockAlign = 2*WAVEOUT_CHANNELS; > waveformatex.wBitsPerSample = 16; > waveformatex.cbSize = 0; > > for(i=0;i<WAVEOUT_NUM_OF_BUFFERS;i++) > &#2013266080; { > &#2013266080; wh_data[i] = new char[WAVEOUT_BUFFER_LENGTH]; > &#2013266080; memset(wh_data[i],0,WAVEOUT_BUFFER_LENGTH); > &#2013266080; wh[i].lpData = wh_data[i]; > &#2013266080; wh[i].dwBufferLength = WAVEOUT_BUFFER_LENGTH; > &#2013266080; wh[i].dwBytesRecorded = &#2013266080;0; > &#2013266080; wh[i].dwUser = 0; > &#2013266080; wh[i].dwFlags = 0; > &#2013266080; wh[i].dwLoops = 0; > &#2013266080; } > > waveOutOpen(&hwo, > &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080;WAVEOUT_DEVICE_ID, > &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080;&waveformatex, > &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080;(DWORD)waveOutProc, > &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080;(DWORD)this, // Callbackinstance > &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080; &#2013266080;CALLBACK_FUNCTION|WAVE_FORMAT_DIRECT); > > for(i = 0; i < (WAVEOUT_NUM_OF_BUFFERS - 1); i++) > &#2013266080; { > &#2013266080; waveOutPrepareHeader(hwo,&wh[i],sizeof(wh[i])); > &#2013266080; waveOutWrite(hwo,&wh[i],sizeof(wh[i])); > &#2013266080; } > > waveout_buffer_id = WAVEOUT_NUM_OF_BUFFERS - 1; > waveout_flag = 0; > //------------------------------------ > > Vladimir Vassilevsky > DSP and Mixed Signal Design Consultanthttp://www.abvolt.com
Thks for your answer, but could you please explain more in detail? Because I'm new with this. Here is my code to read from a binary file the data that i want to play: #include <iostream> #include <fstream> using namespace std; void main() { std::fstream f_in; short speech, value[10000]; f_in.open ("myfile.pcm", std::ios::in | std::ios::binary); int i = 0; while (i < 10000) { f_in.read((char *)&speech, 2); value[i] = speech; cout << speech << std::endl; i++; } } From this, I have an array of data (name value) with 10000 values. Now I want to play them in sound with Fs = 16000 Hz, mono. It would be great if you could you me a little bit more in detail. Thank you very much!
On Jun 10, 11:36 am, ldkha1979 <ledinh...@gmail.com> wrote:

> Thks for your answer, but could you please explain more in detail?
You have your data in a data buffer. Windows wants to play a collection of one or more "wave headers". Each of these is a structure of items describing a chunk of audio data. One of the items in the structure is a pointer to the actual payload data. You'll have to make that point to your buffer of audio data. And then fill in the other items. You call a function to "prepare" the header, and then you call a function to play it. See MSDN (just google one of the function names from Vladimir's example), I think their example code has something that will play from a wave file, if you just have raw data then delete the bit about the file header.
On Jun 10, 12:23&#2013266080;pm, cs_post...@hotmail.com wrote:
> On Jun 10, 11:36 am, ldkha1979 <ledinh...@gmail.com> wrote: > > > Thks for your answer, but could you please explain more in detail? > > You have your data in a data buffer. > > Windows wants to play a collection of one or more "wave headers". > Each of these is a structure of items describing a chunk of audio > data. &#2013266080;One of the items in the structure is a pointer to the actual > payload data. &#2013266080;You'll have to make that point to your buffer of audio > data. &#2013266080;And then fill in the other items. &#2013266080;You call a function to > "prepare" the header, and then you call a function to play it. > > See MSDN (just google one of the function names from Vladimir's > example), I think their example code has something that will play from > a wave file, if you just have raw data then delete the bit about the > file header.
Thks for all of your answer. I found my answer here: http://www.insomniavisions.com/documents/tutorials/wave.php Thks Vladimir!
See also such examples (play & record a sound) & makefile in the zipped 
file 'Ddrec.exe' to be downloaded from
http://support.microsoft.com/kb/116293
or
http://download.microsoft.com/download/platformsdk/sample9/3.1/w31/en-us/ddrec.exe