我正在运行以下使用2种不同方法播放一些白噪声的简单音频播放脚本。但是,除非我将pause (T+1)
放在第一个播放命令之后,否则第二个(似乎是?)将同时执行。
fs = 44100; % sampling frequency, Hz
T = 5; % signal duration, s
N = round(fs*T); % number of samples
% use rand to create noise, wave is then normalized to a max of 1:
wave = 2*(rand(N,1)-0.5);
wave = wave./max(abs(wave));
disp ("Now playing: White noise 1")
player = audioplayer (wave, 44100, 8);
play (player);
pause(T+1) % We need pause, otherwise multi thread will play next command at the same time!
disp ("Now playing: White noise 2")
soundsc(wave, fs)
如何在开始第二次播放之前等待第一次播放命令完成而又不使用人工暂停?
PS。这是Windows上运行的Octave 5.1.0 。
最佳答案
您可以对播放状态进行忙循环:
while strcmpi(player.Running, 'on')
pause(.1);
endwhile
暂停不是必需的,或者可以做其他事情-我仅在示例中使用以限制CPU使用率。
要么
while isplaying(player)
pause(.1);
endwhile
您也可以使用playblocking(player)代替
从here:
33.3.1 Playback
The following methods are used to control player playback.
...
playblocking (player)
playblocking (player, start)
playblocking (player, limits)
Play audio stored in the audioplayer object player with blocking.
Given optional argument start, begin playing at start samples in
the recording. Given a two-element vector limits, begin and end
playing at the number of samples specified by the elements of the
vector.
isplaying (player)
Return true if the audioplayer object player is currently playing
back audio and false otherwise.
关于multithreading - 如何在Octave中执行下一个命令之前,等待(音频播放)命令完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55276922/