Stop playing audio using audioDeviceWriter.

9 次查看(过去 30 天)
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?

回答(2 个)

Devineni Aslesha
Devineni Aslesha 2019-8-12
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 个评论
Devineni Aslesha
Devineni Aslesha 2019-8-12
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.
Sudhee
Sudhee 2019-8-12
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.

请先登录,再进行评论。


Charlie DeVane
Charlie DeVane 2019-12-9
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

类别

Help CenterFile Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by