waveOutWrite - sound duration
-
Hi, I found some code to play sounds. It works well but I can't figure out how to change the duration of the sound, e.g. to play for 100ms, 1000ms, 5000ms etc. Here is the code. I added the DurationMillisecs parameter but I don't know how to utilize it.
BOOL Test1(long FREQUENCY, long DurationMillisecs)
{
// taken from http://www.tmsoft.com/tutorial-sound.html#define BUFFERSIZE 4860 // 4k sound buffer
#define PI 3.14159265358979
HWAVEOUT hWaveOut; // Handle to sound card output
WAVEFORMATEX WaveFormat; // The sound format
WAVEHDR WaveHeader; // WAVE header for our sound datachar Data\[BUFFERSIZE\]; // Sound data buffer HANDLE Done; // Event Handle that tells us the sound has finished being played. // This is a real efficient way to put the program to sleep // while the sound card is processing the sound buffer double x; int i; // \*\* Initialize the sound format we will request from sound card \*\* WaveFormat.wFormatTag = WAVE\_FORMAT\_PCM; // Uncompressed sound format WaveFormat.nChannels = 2; // 1=Mono 2=Stereo WaveFormat.wBitsPerSample = 8; // Bits per sample per channel WaveFormat.nSamplesPerSec = 11025; //1000\*(DurationMillisecs/1000); // Sample Per Second WaveFormat.nBlockAlign = WaveFormat.nChannels \* WaveFormat.wBitsPerSample / 8; WaveFormat.nAvgBytesPerSec = (WaveFormat.nSamplesPerSec \* WaveFormat.nBlockAlign); WaveFormat.cbSize = 0; // \*\* Create our "Sound is Done" event \*\* Done = CreateEvent (0, FALSE, FALSE, 0); // \*\* Open the audio device \*\* if (waveOutOpen(&hWaveOut,0,&WaveFormat,(DWORD) Done,0,CALLBACK\_EVENT) != MMSYSERR\_NOERROR) { MessageBox(0,"Sound card cannot be opened.", "error", 0); return TRUE; } // \*\* Make the sound buffer \*\* for (i=0; i < BUFFERSIZE; i++) { // \*\* Generate the sound wave based on FREQUENCY define // \*\* x will have a range of -1 to +1 x = sin(i\*2.0\*PI\*(FREQUENCY)/(double)WaveFormat.nSamplesPerSec); // \*\* scale x to a range of 0-255 (signed char) for 8 bit sound reproduction \*\* Data\[i\] = (char)(127\*x+128); } // \*\* Create the wave header for our sound buffer \*\* WaveHeader.lpDa
-
Hi, I found some code to play sounds. It works well but I can't figure out how to change the duration of the sound, e.g. to play for 100ms, 1000ms, 5000ms etc. Here is the code. I added the DurationMillisecs parameter but I don't know how to utilize it.
BOOL Test1(long FREQUENCY, long DurationMillisecs)
{
// taken from http://www.tmsoft.com/tutorial-sound.html#define BUFFERSIZE 4860 // 4k sound buffer
#define PI 3.14159265358979
HWAVEOUT hWaveOut; // Handle to sound card output
WAVEFORMATEX WaveFormat; // The sound format
WAVEHDR WaveHeader; // WAVE header for our sound datachar Data\[BUFFERSIZE\]; // Sound data buffer HANDLE Done; // Event Handle that tells us the sound has finished being played. // This is a real efficient way to put the program to sleep // while the sound card is processing the sound buffer double x; int i; // \*\* Initialize the sound format we will request from sound card \*\* WaveFormat.wFormatTag = WAVE\_FORMAT\_PCM; // Uncompressed sound format WaveFormat.nChannels = 2; // 1=Mono 2=Stereo WaveFormat.wBitsPerSample = 8; // Bits per sample per channel WaveFormat.nSamplesPerSec = 11025; //1000\*(DurationMillisecs/1000); // Sample Per Second WaveFormat.nBlockAlign = WaveFormat.nChannels \* WaveFormat.wBitsPerSample / 8; WaveFormat.nAvgBytesPerSec = (WaveFormat.nSamplesPerSec \* WaveFormat.nBlockAlign); WaveFormat.cbSize = 0; // \*\* Create our "Sound is Done" event \*\* Done = CreateEvent (0, FALSE, FALSE, 0); // \*\* Open the audio device \*\* if (waveOutOpen(&hWaveOut,0,&WaveFormat,(DWORD) Done,0,CALLBACK\_EVENT) != MMSYSERR\_NOERROR) { MessageBox(0,"Sound card cannot be opened.", "error", 0); return TRUE; } // \*\* Make the sound buffer \*\* for (i=0; i < BUFFERSIZE; i++) { // \*\* Generate the sound wave based on FREQUENCY define // \*\* x will have a range of -1 to +1 x = sin(i\*2.0\*PI\*(FREQUENCY)/(double)WaveFormat.nSamplesPerSec); // \*\* scale x to a range of 0-255 (signed char) for 8 bit sound reproduction \*\* Data\[i\] = (char)(127\*x+128); } // \*\* Create the wave header for our sound buffer \*\* WaveHeader.lpDa
Duration(msec) / 1000.0 = durationSec durationSec * samplesPerSecond = numOfSamplesRequired for (i=0; i<numOfSamplesRequired; i++) { // ** Generate the sound wave based on FREQUENCY define } WaveHeader.dwBufferLength = numOfSamplesRequired * bytesPerSample
-
Duration(msec) / 1000.0 = durationSec durationSec * samplesPerSecond = numOfSamplesRequired for (i=0; i<numOfSamplesRequired; i++) { // ** Generate the sound wave based on FREQUENCY define } WaveHeader.dwBufferLength = numOfSamplesRequired * bytesPerSample
Thanks. I didn't quite understand what the code does. It seems to me you mean that I should loop for the number of seconds I want to sound to last. But that creates a non-smooth sounds, like noise. But I understood from your code that by playing with the size of BUFFERSIZE I can make the sound shorter or longer, and that seems to work well. Thank you.
-
Thanks. I didn't quite understand what the code does. It seems to me you mean that I should loop for the number of seconds I want to sound to last. But that creates a non-smooth sounds, like noise. But I understood from your code that by playing with the size of BUFFERSIZE I can make the sound shorter or longer, and that seems to work well. Thank you.
-
That's okay. I hope you can forgive me for my poorly formatted (i.e - not at all) answer. I had hoped you would understand as much as I do from what I wrote, and it seems to have served its purpose. Have a nice weekend. :)
It certainly did serve it's purpose. Thank you, it was helpful.