Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Using analog output in the background

Hi,

 

I need to use the analog output channel on a NI PCIe-6351 to play an audio signal, and while the signal plays i need make some changes the UI dialog box, is it possible for me to move the playing of the audio to the background and proceed with program execution?

 

Here is a simple piece of code that explains what I'm trying to do: 

 

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<memory.h>
#include<wchar.h>


#include "sndfile.h" //include file for the sound library
#include "NIDAQmx.h"

#define DAQmxErrChk(functionCall) { if( DAQmxFailed(error=(functionCall)) ) { goto Error; } }

 

float *fWavSample;
SNDFILE *SoundFile;
SF_INFO SoundFileInfo;
int iNoOfSamples=0;


int32 error=0;
TaskHandle AOtaskHandle = 0;
float64* AIOSample;
char errBuff[2048]={'\0'};

 


int32 fnCreateTask(TaskHandle *AOTaskHandle)
{
int32 error=0;
DAQmxErrChk(DAQmxCreateTask("", AOTaskHandle));
Error:
return error;
}

 

int ReadWavFile()
{

//Open the wav file for reading
SoundFile=sf_open("sin_10s.wav",SFM_READ,&SoundFileInfo);

//Check if file is opened sucessfully
if (SoundFile == NULL)
{
puts("File not opened");
return FALSE;
}

//allocate memory for the buffer that is to hold the wav data&colon;
fWavSample = new float[SoundFileInfo.channels * SoundFileInfo.frames];
iNoOfSamples = SoundFileInfo.channels * SoundFileInfo.frames;

//Read data into the float structure that has been copied in
sf_readf_float(SoundFile, fWavSample, SoundFileInfo.frames);


//Allocate memory for the structure that is to hold the sound samples
AIOSample = new float64[iNoOfSamples+660];

 

//Copy wavefile data into the new float64 array (needs to be typecasted to float64)
int i=0;
for(i=0;i<(SoundFileInfo.channels * SoundFileInfo.frames);i++)
{
AIOSample[i] = (float64)fWavSample[i];
}


//After the float64 array has been filled, release the memory used by fWavSample
free(fWavSample);
return 0;
}

int main(int argc, char** argv)
{
DAQmxErrChk(fnCreateTask(&AOtaskHandle));
//Create an analog out channel
DAQmxErrChk (DAQmxCreateAOVoltageChan(*(&AOtaskHandle),"Dev1/ao1","",-10.0000000,+10.00000,DAQmx_Val_Volts,NULL));
//16 bit output resolution needed, sampling rate matched to the WAV file's
DAQmxErrChk (DAQmxCfgSampClkTiming(AOtaskHandle,"",44100.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));
//Set the output to trigger on a rising edge on PFI6
//DAQmxErrChk (DAQmxCfgDigEdgeStartTrig (AOtaskHandle,"PFI6",DAQmx_Val_Rising));
//Start the task in the background
DAQmxStartTask(AOtaskHandle);
ReadWavFile();

DAQmxErrChk(DAQmxWriteAnalogF64(AOtaskHandle,(SoundFileInfo.channels * SoundFileInfo.frames),true,10.0, DAQmx_Val_GroupByChannel,AIOSample,NULL,NULL));
printf("Playing audio\n");

Error:
if( DAQmxFailed(error) )
{
DAQmxGetExtendedErrorInfo(errBuff,2048);
//puts(errBuff);
puts(errBuff);
}

return TRUE;//DefWindowProc(hwndmon,message,wParam,lParam);

 

getch();

}

 

The program reads a 10 seconds long 1kHz sine wave signal from a WAV file and plays it over AO0.

 

I want the program to  print "Playing Audio" on the console window while the audio is still playing, i.e when DAQmxWriteAnalogF64 is executing.

 

 

I'm using Visual Studio 2005 on Windows XP and an NI PCIe6351.

 

I'd really appreciate any help I can get.

 

Thanks a lot!

 

RaziM

 

 

 

 

 

 

0 Kudos
Message 1 of 2
(4,843 Views)

Hi RaziM, 

 

It sounds like you will need to implement some sort of multithreading to accomplish playing audio in the background and also making changes in the UI dialog box in the foreground. Since DAQmx is thread safe this should definitely be possible, but it will probably take a little more work on your part.

 

You may want to take a look at this page which gives a walkthrough to Multithreading with a Background component, http://msdn.microsoft.com/en-us/library/ywkkz4s1.aspx

Here is another forum post about how DAQmx runs multiple threads, http://forums.ni.com/t5/Measurement-Studio-for-VC/DAQmx-how-does-it-multithread/td-p/221953

 

I hope some of this helps!

 

Rachel M.
Applications Engineer
National Instruments
0 Kudos
Message 2 of 2
(4,807 Views)