07-28-2009 11:52 AM
07-29-2009 07:50 AM
you can use the waveIn*() set of functions which are part of the Windows SDK. here is the starting point into the MSDN for everything related to waveform audio on windows, and somewhere inside is the specific chapter you are searching for.
09-17-2009 12:02 PM
09-17-2009 02:02 PM
do you mean that you want to send one signal on the left side and another on the right side ?
if this is the case, your code is almost done. set formato.nChannels = 2, then you only have to interleave the 2 signals: fill a buffer with one sample of the first signal followed by one sample of the second signal, then the second sample of the first signal followed by the second sample of the second signal, etc. the sound subsystem will take care of sending samples on each side of the audio signal.
09-17-2009 02:12 PM
#include <windows.h>
#include <ansi_c.h>
#include <mmreg.h>
#include <mmsystem.h>
#include <analysis.h>
#define samples 44100
#define A4 440
#define E4 329.63
#define PI 3.14159
static double leftWave[samples];
static double rightWave[samples];
static short int wave[samples*2];
int main (int argc, char *argv[])
{
HWAVEOUT handle;
WAVEFORMATEX formato;
WAVEHDR BufferSuono;
MMRESULT errore;
double phase;
char ErrorString[200];
int i;
formato.wFormatTag = WAVE_FORMAT_PCM;
formato.nChannels = 2; /* stereo */
formato.nSamplesPerSec = samples;
formato.wBitsPerSample = 16;
formato.nBlockAlign = formato.nChannels * formato.wBitsPerSample / 8;
formato.nAvgBytesPerSec = formato.nSamplesPerSec * formato.nBlockAlign;
formato.cbSize = 0;
errore = waveOutOpen(&handle, WAVE_MAPPER, &formato, 0, 0, CALLBACK_NULL);
if (errore)
waveOutGetErrorText(errore, ErrorString, 500);
#if 1
/* left channel - A4 */
for (i = 0; i < samples; ++i)
leftWave[i] = sin (2*PI * (i*A4)/samples);
#else
/* left channel - silence */
for (i = 0; i < samples; ++i)
leftWave[i] = 0.0;
#endif
#if 1
/* right channel - E4 */
for (i = 0; i < samples; ++i)
rightWave[i] = sin (2*PI * (i*E4)/samples);
#else
/* right channel - silence */
for (i = 0; i < samples; ++i)
rightWave[i] = 0.0;
#endif
/* interleave the channels */
for (i = 0; i < samples; ++i) {
wave[i*2] = 16383 * leftWave[i];
wave[i*2+1] = 16383 * rightWave[i];
}
BufferSuono.lpData = (LPSTR)wave;
BufferSuono.dwBufferLength = samples * 4;
BufferSuono.dwFlags = 0;
errore = waveOutPrepareHeader(handle, &BufferSuono, sizeof(WAVEHDR));
if (errore)
waveOutGetErrorText(errore, ErrorString, 500);
errore = waveOutWrite(handle, &BufferSuono, sizeof(WAVEHDR));
if (errore)
waveOutGetErrorText(errore, ErrorString, 500);
while (!(BufferSuono.dwFlags & WHDR_DONE));
errore = waveOutReset(handle);
if (errore)
waveOutGetErrorText(errore, ErrorString, 500);
errore = waveOutClose(handle);
if (errore)
waveOutGetErrorText(errore, ErrorString, 500);
return 0;
}
09-19-2009 10:12 AM
Thank you very much Peter Ilberg,
You've helped me a lot.
09-23-2009 11:14 AM
Thank you,
09-23-2009 01:53 PM
do {
errore = waveOutWrite(handle, &BufferSuono, sizeof(WAVEHDR));
if (errore)
waveOutGetErrorText(errore, ErrorString, 500);
while (!(BufferSuono.dwFlags & WHDR_DONE));
errore = waveOutReset(handle);
if (errore)
waveOutGetErrorText(errore, ErrorString, 500);
} while (1);
#include <windows.h>
#include <analysis.h>
#include <ansi_c.h>
#include <mmreg.h>
#include <mmsystem.h>
#define samples 44100
static double leftWave[samples], leftSpectrum[samples/2];
static double rightWave[samples], rightSpectrum[samples/2];
static short int wave[samples*2];
int main (int argc, char *argv[])
{
HWAVEIN handle;
WAVEFORMATEX formato;
WAVEHDR BufferSuono;
MMRESULT errore;
double phase, ignore;
double *leftPeaks, *leftAmps, *leftDerivs;
double *rightPeaks, *rightAmps, *rightDerivs;
int leftCount, rightCount;
char ErrorString[200];
int i;
formato.wFormatTag = WAVE_FORMAT_PCM;
formato.nChannels = 2; /* stereo */
formato.nSamplesPerSec = samples;
formato.wBitsPerSample = 16;
formato.nBlockAlign = formato.nChannels * formato.wBitsPerSample / 8;
formato.nAvgBytesPerSec = formato.nSamplesPerSec * formato.nBlockAlign;
formato.cbSize = 0;
errore = waveInOpen(&handle, WAVE_MAPPER, &formato, 0, 0, CALLBACK_NULL);
if (errore)
waveInGetErrorText(errore, ErrorString, 500);
BufferSuono.lpData = (LPSTR)wave;
BufferSuono.dwBufferLength = samples * 4;
BufferSuono.dwFlags = 0;
errore = waveInPrepareHeader(handle, &BufferSuono, sizeof(WAVEHDR));
if (errore)
waveOutGetErrorText(errore, ErrorString, 500);
errore = waveInAddBuffer(handle, &BufferSuono, sizeof(WAVEHDR));
if (errore)
waveInGetErrorText(errore, ErrorString, 500);
errore = waveInStart(handle);
if (errore)
waveInGetErrorText(errore, ErrorString, 500);
while (!(BufferSuono.dwFlags & WHDR_DONE));
errore = waveInStop (handle);
if (errore)
waveInGetErrorText(errore, ErrorString, 500);
errore = waveInClose(handle);
if (errore)
waveInGetErrorText(errore, ErrorString, 500);
/* split channels */
for (i = 0; i < samples; ++i) {
leftWave[i] = wave[i*2]/16383.0;
rightWave[i] = wave[i*2+1]/16383.0;
}
/* frequency analysis */
AutoPowerSpectrum (leftWave, samples, 1.0, leftSpectrum, &ignore);
AutoPowerSpectrum (rightWave, samples, 1.0, rightSpectrum, &ignore);
PeakDetector (leftSpectrum, samples/2, 0.001, 3, 0, 1, 1, &leftCount,
&leftPeaks, &leftAmps, &leftDerivs);
PeakDetector (rightSpectrum, samples/2, 0.001, 3, 0, 1, 1, &rightCount,
&rightPeaks, &rightAmps, &rightDerivs);
for (i = 0; i < leftCount; ++i)
printf ("left channel: %lfHz\n", leftPeaks[i]);
for (i = 0; i < rightCount; ++i)
printf ("right channel: %lfHz\n", rightPeaks[i]);
getchar ();
FreeAnalysisMem (leftPeaks);
FreeAnalysisMem (leftAmps);
FreeAnalysisMem (leftDerivs);
FreeAnalysisMem (rightPeaks);
FreeAnalysisMem (rightAmps);
FreeAnalysisMem (rightDerivs);
return 0;
}
09-24-2009 01:31 PM
09-24-2009 03:06 PM