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

标签 multithreading audio octave

我正在运行以下使用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/

相关文章:

使用 GCD 或 NSThread 的 iOS 长时间运行操作?

java - java中多线程构造不可变树的算法

java - 使用 JLayer 分割和淡入淡出 MP3 文件

java - Android信号分析+一些过滤器

performance - 以更干净/更有效的方式过滤数据

c++ - 这是 C++ 条件变量中的错误吗?

c++ - 为什么 "non-standard syntax; use ' &' to create a pointer to member"在 CTOR 中使用线程?

cocoa - 以编程方式录制发送到内置输出的声音,Mac OS X

matlab - 将 .mat 转换为 .csv octave/matlab

matlab - Octave 函数应该返回一个向量,而不是 'ans'