在Octave中执行下一个命令之前,如何等待(音频播放)命令完成?

时间:2019-03-21 09:06:45

标签: multithreading audio octave

我正在运行以下简单的音频播放脚本,该脚本使用2种不同的方法播放一些白噪声。但是,除非我将pause (T+1)放在第一个 play 命令之后,否则第二个(似乎是?)命令会同时执行。

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

1 个答案:

答案 0 :(得分:2)

您可以对播放状态进行忙循环:

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.