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?
댓글 수: 0
답변 (2개)
Devineni Aslesha
2019년 8월 12일
Play and stop functions are applicable for audioplayer and audiorecorder objects. 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
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.
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File 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!