Procedure StartRecording ;
Const Buffer_Frames = NBPoints; // in Main =16384
LABEL DoItAgain ;
Var i,err : Integer;
//Samplerate : Int32 = 8192 ; defined in Main
buffer : Pchar ;
// Capture_handle : Psnd_pcm_t ; // declared as global to unit
hw_params : Psnd_pcm_hw_params_t ;
format : snd_pcm_format_t = SND_PCM_FORMAT_S16_LE ; // 16 bits signed little endian
direction : integer = 0 ;
//Channels : int32 = 1 ; //mono In Main
begin
new(Capture_handle) ;
new(hw_params) ;
// volume adjust
//snd_mixer_selem_id_set_index(sid, 100);
//snd_mixer_selem_id_set_name(sid, Pchar(deviceTable[MainForm.CBListDevices.itemIndex].iname));
//originally 0
err := snd_pcm_open(@capture_handle,Pchar(deviceTable[MainForm.CBListDevices.itemIndex].iname),SND_PCM_STREAM_CAPTURE,SND_PCM_NONBLOCK) ;
If err < 0 then begin PostError('Error opening PCM') ; Exit ;end;
err := snd_pcm_hw_params_malloc (@hw_params) ;
If err < 0 then begin PostError('Error hwparams'); Exit ;end;
err := snd_pcm_hw_params_any (capture_handle, hw_params) ;
if err< 0 then begin PostError('Cannot initialize hardware parameter structure') ; Exit ; end ;
// SND_PCM_ACCESS_MMAP_NONINTERLEAVED
err := snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
if err < 0 then begin PostError('Cannot access PCM type');Exit;end;
err := snd_pcm_hw_params_set_format (capture_handle, hw_params, format) ;
if err < 0 then begin Posterror('Cannot set format') ; Exit; end;
err := snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, @samplerate, @direction);
if err < 0 then begin Posterror('Cannot set rate') ; Exit ; end;
err := snd_pcm_hw_params_set_channels (capture_handle, hw_params, channels) ; // 1 channel = mono
if err < 0 then begin PostError('Cannot set channel count') ; Exit ; end;
err := snd_pcm_hw_params (capture_handle, hw_params) ;
if err < 0 then Begin PostError('Cannot set parameters') ; Exit ; end ;
snd_pcm_hw_params_free (hw_params);
err := snd_pcm_prepare (capture_handle) ;
if err < 0 then begin PostError('Cannot prepare audio interface for use'); Exit ; end;
buffer := malloc (Buffer_Frames * snd_pcm_format_width(format) DIV 8 * Channels);
// showmessage('Buffer allocated');
DoItAgain:
snd_pcm_wait(capture_handle, 2000);
err := snd_pcm_readi (capture_handle, buffer, buffer_frames);
if err <> buffer_frames then
begin
PostError ('Read from audio interface failed. Error = '+inttostr(err));
//*****************error -11 is reported here ************
exit ;
end
else
begin //we have a full buffer
// ShowMessage('Read successfull');
// here I need to transfer the buffer in the process buffer
FillWord(SavedFrameData,length(SavedFrameData),0); // remove garbage
move(buffer^,savedFrameData,buffer_frames*2) ;// CopyMemory(@savedFrameData, @buffer, buffer_frames*2 ); // 2 bytes per frame
Showmessage('Buffer copied') ;
// Find the max value
gMax := 0 ;
gMaxIndex := 0 ;
For i := 0 to length(SavedFrameData)-1 do
begin
if abs(SavedFrameData[i])> gmax then // use only positives values
begin
gMax := Abs( SavedFrameData[i]);
gMaxIndex := i ;
End;
End;
PostError('Gmax ' + inttostr(gmax));
showmessage('gMAx ' + inttostr(gmax)) ;
If gMax > gSensitivity then // Check if level is higher than sensitivity set level
begin
MainForm.Timer1.Enabled := False ; // stop visual buffer
MainForm.ProgressBar1.Position := 0;
MainForm.ProgressBar1.Update ;
MainForm.RBdataReady.checked := true ; // set an event to draw graphs
end // Max > sensivity
else
begin
MainForm.ProgressBar1.Position := 0;
MainForm.ProgressBar1.Update ;
goto DoItAgain ;
end;
End ; // end buffer complet
begin
MainForm.ProgressBar1.Position := 0;
MainForm.ProgressBar1.Update ;
goto DoItAgain ;
end;
Processing := False ;
free(Buffer) ; // should it be free or freemem?
//showmessage('Buffer freed') ;
//snd_pcm_close (Capture_handle);
// ShowMessage( 'audio interface closed');
// if assigned(capture_handle) then free(capture_handle) ;
// if assigned(hw_params) then free(hw_params) ;
end;