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

How can I wait for an (audio play) command to finish before executing the next one, in 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)

如何在不使用人为 pause 的情况下等待第一个播放命令完成后再开始第二个播放命令?

PS。这是 Octave 5.1.0 运行 在 Windows.

你可以在播放状态上做一个忙循环:

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.