LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

sound acquire

I have to acquire a signal from the sound card in LabWindows, simulating a voltmeter. How can I do


Thank you
Luigi
0 Kudos
Message 1 of 13
(7,338 Views)

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.

0 Kudos
Message 2 of 13
(7,305 Views)
Hi,
my problem is to send 2 signals to the sound card and later captured. Sending a signal to turn the sound card and using a jack male male I can acqusirli. Here are the two functions used

int AudioOut(int samples, short int wave[])

{
HWAVEOUT handle;
WAVEFORMATEX formato;
WAVEHDR BufferSuono;

char ErrorString[200];

formato.wFormatTag = WAVE_FORMAT_PCM;
formato.nChannels = 1;
formato.nSamplesPerSec = sampling;
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);

BufferSuono.lpData = (LPSTR)wave;
BufferSuono.dwBufferLength = samples * 2;
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);
}

nt AudioInAndReturn(int samples, short int wave[])


{
WAVEFORMATEX formato;

char ErrorString[200];

formato.wFormatTag = WAVE_FORMAT_PCM;
formato.nChannels = 1;
formato.nSamplesPerSec = sampling;
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 * 2;
BufferSuono.dwFlags = 0;

errore = waveInPrepareHeader(handle, &BufferSuono, sizeof(WAVEHDR));
if (errore)
waveInGetErrorText(errore, ErrorString, 500);

errore = waveInAddBuffer(handle, &BufferSuono, sizeof(WAVEHDR));
if (errore)
waveInGetErrorText(errore, ErrorString, 500);

errore = waveInStart(handle);
if (errore)
waveInGetErrorText(errore, ErrorString, 500);

return(0);
}

My problem is to send 2 signals simultaneously,with a stereo transmission,and then acquire them.
Help me. I do not know how to do
0 Kudos
Message 3 of 13
(7,168 Views)

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.

0 Kudos
Message 4 of 13
(7,160 Views)
Dummy_decoy has already answered the question but I might as well post an example based on your code:


#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;
}
Message 5 of 13
(7,153 Views)

Thank you very much Peter Ilberg,

 

You've helped me a lot. 

0 Kudos
Message 6 of 13
(7,115 Views)

Thank you,

I have a stereo output. In acquisition, I used the following function

WAVEFORMATEX formato;
 
 char ErrorString[200];

 formato.wFormatTag = WAVE_FORMAT_PCM;
 formato.nChannels = 2;
 formato.nSamplesPerSec = sampling;
 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)
     waveInGetErrorText(errore, ErrorString, 500);
 
 errore = waveInAddBuffer(handle, &BufferSuono, sizeof(WAVEHDR));
 if (errore)
     waveInGetErrorText(errore, ErrorString, 500);
 
 errore = waveInStart(handle);
 if (errore)
     waveInGetErrorText(errore, ErrorString, 500);

divides the left and right signals:

    for (i=0,j=0; i<samples_out; i++,j++)
                {
               
                     wave_outright[j]= (double)wave[i*2+1]/16383; 
                     wave_outleft[j]= (double)wave[i*2]/16383;     
                }

I get   wave_outleft=wave_outright

Why I do not distinguish the output signals
0 Kudos
Message 7 of 13
(7,072 Views)
I don't know why you get the same data on your left and right channels. Are you sure that the source is a stereo signal?

For testing, I took my program (above) and changed it to play the signal in an infinite loop:


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);

Then I connected the headphone-out to the microphone-in on my PC and analysed the input with this program (based on your code). As expected, the program tells me that there's a 440Hz signal on the left channel and a 329Hz signal on the right channel.


#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;
}
Message 8 of 13
(7,053 Views)
I made your code, but it does not work. Probably I'm wrong to include the code in the main.
Please, could you attach the whole program.

   
thank you very much
0 Kudos
Message 9 of 13
(7,023 Views)
Here are my three projects:

input.prj - record from the microphone for one second
output.prj - play to the speaker
combined.prj - play to the speaker and record from the microphone
0 Kudos
Message 10 of 13
(7,015 Views)