我正在尝试使用 NI DAQMx ANSI C 库输出包含在 wav 文件中的波形。我正在使用 libsnd 库读取 wav 文件,并且能够成功提取数据,但是输出波形的频率远高于实际的 wav 文件本身。有谁知道如何设置输出波形的频率。我正在使用 PCIe 6351 数据采集卡。
以下是我为完成此任务而编写的一些代码:
#include<stdio.h>
#include<conio.h>
#include <math.h>
#include <stdlib.h>
#include <windows.h>
#include "NIDAQmx.h"
#include "Sync_AIAO.h"
#include "sndfile.h"
#include "RIB2.h"
int32 fnCreateTask(TaskHandle *AOTaskHandle)
{
int32 error=0;
DAQmxErrChk(DAQmxCreateTask("", AOTaskHandle));
Error:
return error;
}
int main(int argc, char** argv)
{
int i=0;
int32 error=0;
TaskHandle AOtaskHandle = 0;
float64* AIOSample;
float *fWavSample;
SNDFILE *SoundFile;
SF_INFO SoundFileInfo;
int iNoOfSamples=0;
FILE* fp;
//Error code
//Handle to the tasks created
char errBuff[2048]={'\0'};
//DAQmxErrChk(DAQmxCreateTask("",AOtaskHandle));
fnCreateTask(&AOtaskHandle);
//Create an analog out channel
DAQmxErrChk (DAQmxCreateAOVoltageChan(* (&AOtaskHandle),"Dev1/ao1","",-10.0000000,+10.00000,DAQmx_Val_Volts,NULL));
//Set for
//DAQmxErrChk (DAQmxCfgDigEdgeStartTrig(&AOtaskHandle,"ai/StartTrigger",DAQmx_Val_Rising));
SoundFile=sf_open("sine.wav", SFM_READ, &SoundFileInfo);
//Check if file is opened sucessfully
if (SoundFile == NULL)
{
printf("Failed to open the file.\n");
exit(-1);
}
//allocate memory for the buffer that is to hold the wav data:
fWavSample = new float[SoundFileInfo.channels * SoundFileInfo.frames];
iNoOfSamples = SoundFileInfo.channels * SoundFileInfo.frames;
//Read data into the float structure
sf_readf_float(SoundFile, fWavSample, SoundFileInfo.frames);
printf("Float:%d, Float64:%d\n",sizeof(float),sizeof(float64));
//printf("%f\n",fWavSample[0]);
//printf("%f\n",fWavSample[200000]);
AIOSample = new float64[iNoOfSamples];
// fopen_s(&fp,"output.dat","w");
for(i=0;i<SoundFileInfo.channels * SoundFileInfo.frames;i++)
{
// fprintf(fp,"Data[%d]:%f\n",i,fWavSample[i]);
AIOSample[i] = (float64)fWavSample[i];
}
// fclose(fp);
int32 written;
/*calling function that will output the trigger on PFI6*/
//fnSrPlayElectric(); //play electric stimulus
while(1)
{
/*
DAQmxErrChk(DAQmxWriteAnalogF64(AOtaskHandle,(SoundFileInfo.channels * SoundFileInfo.frames),
true, 10.0, DAQmx_Val_GroupByChannel,AIOSample,&written,NULL));
*/
DAQmxErrChk(DAQmxWriteAnalogF64(AOtaskHandle,1000,
true, 10.0, DAQmx_Val_GroupByChannel,AIOSample,&written,NULL));
//Sleep(3000);
}
//Display the error to the user here.
Error:
if( DAQmxFailed(error) )
{
DAQmxGetExtendedErrorInfo(errBuff,2048);
puts(errBuff);
}
getch();
}
我会很感激我能得到的任何帮助。谢谢!
阿图尔