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!