Home


 
Sound DAQ


Sound card is the cheapest  A/D card. It has a A/D which can be used to acquire signal. It has a D/A which can be used to output a desired signal.  For freshman, Sound card is the best one to learn PC based test and measurement. See "Sound Cards Work in Some Data-Acquisition Applications" by Brad Thompson.
The control of sound card is complicate, it need some skills and knowledges. A DAQ dll of sound card is designed by me to overcome these problems. By help of it, you can record or play a signal from your program directly.
The Sound DAQ is originally designed to help my students to design Sound Card based virtual instrument by VB. I wish it can also give you a help. 



Sound DAQ is a freeware, you can use and distribute it freely. Press here to download.


Sound DAQ package include four file:
1.sounddaq.h
2.sounddaq.lib
3.sounddaq.dll
5.sound_vb (VB Example program)
6.sound_vc (VC++ Example program)
.
Functions in  Sound DAQ:

2. int recording(long fs,long len,short *wave)
This function record a mono signal from sound card.
Fs is sample frequency, it must be 8000, 11025, 22050, or 44100Hz.
Len is data sample length, it must less than the dimension of array wave[];
Wave is a 16bit array to store sample data.
Example: recording(11025,4096,wave);
..
3. void sinewave(long fs,long len,short *wave,double f,double a,double p);
This function generate a sine signal.
Fs is sample frequency,
Len is data sample length, it must less than the dimension of array wave[];
Wave is a 16bit array to store signal data.
(f,a,p) is frequency, amplitude and phase of sine signal.
Example: sinewave(11025,4096,wave,500,5000,0);
.
4. long  jpg_to_bmp(char *jpgfilename, char *bmpfilename)
This function copy a JPG file to a BMP file.  If return 1, then all is ok.
Example:rs=jpg_to_bmp("c:\jpgdemo\demo.jpg","c:\temp\demo.bmp")
.
5. void Playfile(char *filename,long type) 
Play a signal from a windows WAV file.
Type=1: play once, Type=2: play circle
Example: Playfile("1000.wav",2);
.
6. void playmono(long int type,long int fs,long int len,short *wave)
Play a mono signal from a array.
Type=1: play once, Type=2: play circle
Fs is play frequency, it is equal to sample frequency of the signal,
Len is signal length;
Wave is a array that store signal data.
Example: playmono(2,11025,4096,wave);
.
7. void playstero(long int type,long int fs,long int len,short *ch1,short *ch2)
Play a stero signal from two array.
Type=1: play once, Type=2: play circle
Fs is play frequency, it is equal to sample frequency of the signal,
Len is signal length;
CH1 and CH2 are the array that store signal data.
Example: playstero(2,11025,4096,ch1,ch2);
.
8. void stopplay();
This function stop play from sound card.
Example:stopplay();
.
9. long int openwav(char *filename,long *chn,long *fs, long *len,long maxlen,short *ch1,short *ch2)
This function read a windows WAV file to memory.
Chn=1: mono signal, Chn=2: stero signal;
Fs: sample frequency of the signal;
Len: signal length;
Maxlen: dimension of ch1 and ch2
Ch1: array to store mono signal or left channel of stero signal; 
Ch2: array to store right channel of stero signal;
Example:openwav("example.wav",&chn,&fs,&len,16384,ch1,ch2);
 .
10. long int savewav16(char *name,long int ch,long int fs,long int len,short *ch1,short *ch2)
This function save signal in memory to a windown WAV file.
Chn=1: mono signal, Chn=2: stero signal;
Fs: sample frequency of the signal;
Len: signal length;
Ch1: array to store mono signal or left channel of stero signal; 
Ch2: array to store right channel of stero signal;
Example:savewav16("demo.wav",1,fs,len,ch1,ch2);
.
11.long int Recordflie(char *name,long int fs,long int len)
This function record signal to a windown WAV file.
Fs: sample frequency of the signal;
Len: signal length;
Example:Recordfile("demo.wav",fs,len);

 
Example of VC++:

.
Below is main souce code of this program:
//====================================================
BOOL CSoundtestDlg::OnInitDialog()
{CDialog::OnInitDialog();
 SetIcon(m_hIcon, TRUE); 
 SetIcon(m_hIcon, FALSE);
 SetWindowPos(&wndTop,0,0,500,300,SWP_NOMOVE); // Set size of Dialog 
  return TRUE; 
}

void CSoundtestDlg::OnMenuitem32772() // Close 
{CDialog::OnCancel();}

void CSoundtestDlg::OnMenuitem32771() // OPEN a wave file
{CClientDC dc(this); // Get handle of dc
 long rc,max=64534;
 rc=openwav("example.wav",&chn,&fs,&len,max,wave[0],wave[1]); // Read WAV FILE
 if (rc!=1) {outword(&dc,10,10,"Read file failed !"); return;}
 draewave(&dc); // Show signal
}

void CSoundtestDlg::OnMenuitem32773() // Save to a wav file
{savewav16("demo.wav",1,fs,len,wave[0],wave[1]);}

void CSoundtestDlg::OnMenuitem32775() // Play from a wav file
{Playfile("1000.wav",2);}

void CSoundtestDlg::OnMenuitem32776() // Stop play
{stopplay();}

void CSoundtestDlg::OnMenuitem32779()  // Recording
{CClientDC dc(this); 
 chn=1; fs=11025; len=4396;
 recording(fs,len,wave[0]);
 draewave(&dc); 
}
 

 

 
Example of VB:

Below is main souce code of this program:
Private Declare Function Playfile Lib "c:\temp\sounddaq.dll" 
(ByVal name As String, ByVal w As Long) As Long
Private Declare Function Recordflie Lib "c:\temp\sounddaq.dll" 
(ByVal name As String, ByVal fs As Long, ByVal length As Long) As Long
.
Private Sub play_Click()
Dim rc As Long
Dim name As String
name = Text1.Text
rc = Playfile(name, 1)
End Sub
.
Private Sub record_Click()
Dim rc As Long
Dim fs As Long
Dim length As Long
Dim name As String
fs = 11025
length = 110250
name = Text2.Text
rc = Recordflie(name, fs, length)
End Sub.


1999.12.20