MATLAB Answers

Stop playing audio using audioDeviceWriter.

30 views (last 30 days)
Sudeep Surendran
Sudeep Surendran on 8 Aug 2019
Answered: Charlie DeVane on 9 Dec 2019
Hi,
How to stop playing an audio in between when using audioDeviceWriter?
deviceWriter = audioDeviceWriter
deviceWriter(Speech_sample); % Speech_sample is the desired speech variable.
For audioplayer,
play(audioplayer)
plays the audio while
stop(audioplayer)
stops it. Can an audio played by audioDeviceWriter be stopped in a similar way?

  0 Comments

Sign in to comment.

Answers (2)

Devineni Aslesha
Devineni Aslesha on 12 Aug 2019
Play and stop functions are applicable for audioplayer and audiorecorderobjects. An audio played by audioDeviceWriter can be stopped by using release function as audioDeviceWriter is a system object.
Use the below code for testing.
% Path for the below file is “C:\Program Files\MATLAB\R2019a\examples\audio\farspeech”
y = load('farspeech.mat')
deviceWriter = audioDeviceWriter('SampleRate',10000);
mySignal = y.x(1:20000);
for i = 1:10
deviceWriter(mySignal);
end
Use the command release(deviceWriter) to stop the audio after some delay.
Doc Link:

  3 Comments

Sudeep Surendran
Sudeep Surendran on 12 Aug 2019
Hi Devineni Aslesha,
Thank you for the response. My aim is to stop the devicewriter in the middle of playing. In audioplay, the control is released for the next instruction to be executed whereas for devicewriter, the system is in the busy state.
Matlab doesn't take the instruction release(deviceWriter) before it finishes the sample to play. Even in the example given above, unless the for loop is completed, release(deviceWriter) is not taken by Matlab. Am I missing something here or is this how it is meant to be?
Thanks.
Devineni Aslesha
Devineni Aslesha on 12 Aug 2019
Hi Sudeep Surendran,
Try to increase the number of iterations in the for loop and type release(deviceWriter) in the command window while the code is executing. The audio will be stopped after some delay.
Sudeep Surendran
Sudeep Surendran on 12 Aug 2019
Hi Devineni Aslesha,
I made the number of iterations to 1000 and did release(deviceWriter) . It doesn't stop in between and is still running.

Sign in to comment.


Charlie DeVane
Charlie DeVane on 9 Dec 2019
audioDeviceWriter is designed to play back audio incrementally, by repeatedly writing frames (small amounts of the signal) to the audio device. It sends one frame of audio each time you call its step function. This enables you, among other things, to play back an infinite stream of data. By contrast, you must give audioplayer the entire signal when you create it.
To make an audioDeviceWriter stop playing audio, you simply stop calling its step function.
A good example is in:
openExample('dsp/ReadFromFileAndWriteToAudioDeviceExample')
The heart of the example is this loop:
while ~isDone(fileReader)
audioData = fileReader(); % call fileReader step function to get a frame of audio from input file
deviceWriter(audioData); % call deviceWriter step function to play the frame of audio
end
This loop stops playing audio when the input file is exhausted (isDone(fileReader)).
You can change that logic to stop under whatever conditions make sense for your application. It might look something like this:
keepPlaying = true;
while ~isDone(fileReader) && keepPlaying
audioData = fileReader(); % get a frame of audio from input file
deviceWriter(audioData); % write the audio to the output device
keepPlaying = codeToDecideWhetherToKeepPlayingAudio; % whatever your application needs
end
hope this helps,
Charlie

  0 Comments

Sign in to comment.

Sign in to answer this question.