09-28-2008, 07:52 AM
Audio recording and level meter example. Measures sound power (sorry, don't know how it is in English) instead of max amplitude. Liner, not dB.
Function dlg_wave_meter
Function dlg_wave_meter
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages
str controls = "3"
str e3
if(!ShowDialog("dlg_wave_meter" &dlg_wave_meter &controls)) ret
;BEGIN DIALOG
;0 "" 0x90C80A44 0x100 0 0 223 135 "Wave In Level"
;1 Button 0x54030001 0x4 120 116 48 14 "OK"
;2 Button 0x54030000 0x4 170 116 48 14 "Cancel"
;3 Edit 0x54030080 0x200 2 16 42 14 ""
;4 msctls_progress32 0x54030000 0x0 2 2 220 12 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2030002 "*" "" ""
ret
;messages
int- hwi
WAVEHDR- hd
sel message
,case WM_INITDIALOG
,;SendMessage id(4 hDlg) PBM_SETRANGE 0 0x8000<<16
,
,WAVEFORMATEX wf.cbSize=sizeof(wf)
,wf.wFormatTag=WAVE_FORMAT_PCM
,wf.nChannels=1 ;;mono
,wf.nSamplesPerSec=44.1*1000 ;;44.1 kHz
,wf.wBitsPerSample=16
,wf.nBlockAlign=wf.nChannels*wf.wBitsPerSample/8
,wf.nAvgBytesPerSec=wf.nSamplesPerSec*wf.nBlockAlign
,
,int rc=waveInOpen(&hwi WAVE_MAPPER &wf hDlg 0 CALLBACK_WINDOW)
,if(rc) out "waveInOpen: %i" rc; ret
,
,hd.dwBufferLength=wf.nAvgBytesPerSec/10 ;;10 buffers/s
,hd.lpData=q_malloc(hd.dwBufferLength)
,rc=waveInPrepareHeader(hwi &hd sizeof(hd))
,if(rc) out "waveInPrepareHeader: %i" rc; ret
,
,rc=waveInStart(hwi)
,if(rc) out "waveInStart: %i" rc; ret
,
,case WM_DESTROY
,waveInStop(hwi)
,waveInClose(hwi)
,0
,
,case MM_WIM_OPEN
,out "open"
,rc=waveInAddBuffer(hwi &hd sizeof(hd))
,if(rc) out "waveInAddBuffer: %i" rc; ret
,
,case MM_WIM_CLOSE
,out "close"
,rc=waveInUnprepareHeader(hwi &hd sizeof(hd))
,q_free hd.lpData
,
,case MM_WIM_DATA
,;out "%i %i 0x%X" hd.dwBytesRecorded hd.lpData hd.dwFlags
,int i dc p k
,word* w=hd.lpData; int nsamples=hd.dwBytesRecorded/2
,if(!nsamples) ret
,;calc direct current level
,for(i 0 nsamples) dc+w[i]
,dc/nsamples
,;calc power
,for(i 0 nsamples) p+abs(w[i]-dc)
,p/nsamples
,;out "%i %i" dc p
,p=p*100/0x8000 ;;normalize to max 100. Near 100 can be only for square waveform or when peaks are cut. For nondistorted sinus waveform would be max about 60 or 70.
,if(p>100) p=100
,SendMessage id(4 hDlg) PBM_SETPOS p 0
,SetDlgItemInt hDlg 3 p 0
,rc=waveInAddBuffer(hwi &hd sizeof(hd))
,
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1