简体   繁体   中英

How can I wait for an (audio play) command to finish before executing the next one, in Octave?

I'm running the following simple audio playing script that plays some white noise, using 2 different methods. However, unless I put the pause (T+1) after the first play command, the second one (seem to be?) is executed at the same time.

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)

How can I wait for the 1st play command to finish before starting the 2nd and without using the artificial pause ?

PS. This is Octave 5.1.0 running on Windows.

You could do a busy loop on the play status:

while strcmpi(player.Running, 'on')
  pause(.1);
endwhile

The pause isn't required, or could be doing something else instead - I just use in the example to limit the CPU usage.

Or

while isplaying(player)
  pause(.1);
endwhile

You could also use playblocking(player) instead


From 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM