Amn10
Amn10

Reputation: 21

stop Audio recording on user prompt using dsp.AudioRecorder?

I'm new to matlab. I'm using the following code to record the sound from micro on a wav file:

AR = dsp.AudioRecorder('OutputNumOverrunSamples',true);
audiofile='test.wav';
nbsec=10;
AFW = dsp.AudioFileWriter(audiofile,'FileFormat', 'WAV');
disp('Recording starts now');
tic;
while toc < nbsec,
   [audioIn,nOverrun] = step(AR);
   [x, indexMax] = max(abs(fft(audioIn(:,1)-mean(audioIn(:,1)))));
  step(AFW,audioIn);
  if nOverrun > 0
    fprintf('Audio recorder queue was overrun by %d samples\n',nOverrun);
  end
end
release(AR);
release(AFW);
disp('Recording done!');

It works, but the problem is I don't know in advance for how long I should be recording so rather than using a time constraint (with tic toc), I would like to be able to finish the recording by pressing the 'f' button. But I have no idea how to implement this in matlab.

Many thanks!

Upvotes: 1

Views: 416

Answers (1)

ben
ben

Reputation: 1390

You can use the Audio Recorder Object. This has a method which records without blocking the controls. While this is running you can check for your keypress and use the stop method when it is detected

Upvotes: 1

Related Questions