MATLAB:有audioplayer()后继续函数结束打 [英] MATLAB: Having audioplayer() continue to play after function ends
问题描述
我使用的使用下面的子功能code:
I am using code that uses the following subfunction:
function playTone (duration, toneFreq)
% Generate a tone
samplesPerSecond = 44100; % the bit rate of the tone
y = sin(linspace(0, duration * toneFreq * 2 * pi, round(duration * samplesPerSecond))); % the equation of the sound wave
player = audioplayer(y, samplesPerSecond); % create an audio object from the sound wave at the specified bit rate
playblocking(player) % play the audio, blocking control until the sound completes
此函数被调用,例如,具有以下
This function is called, for example, with the following:
playTone(4, 400);
这会导致一个声音在400Hz的发挥,持续4秒。
This causes a sound to play at 400Hz, lasting for 4 seconds.
的问题是,函数playblocking()限制控制,直到声音已完成。另一种方法是使用播放(),这意味着没有声音播放可言(因为声音只要函数完成停止)。
The problem is, the function playblocking() restricts control until the sound has completed. The alternative is to use play(), which means that no sound plays at all (because sound stops as soon as the function completes).
我不能使用声音()函数,由于我的版本的MATLAB ...的已知错误,我怎样才能让audioplayer()函数播放声音没有考虑系统的控制权,如果创建的声音子函数内?
I can't use the sound() function due to a known bug in my version of MATLAB... How can I make the audioplayer() function play a sound without taking control of the system, if the sound is created within a subfunction?
推荐答案
您可以定义播放
变量作为全球。只要把该线路函数的开头:
You can define player
variable as global. Just put this line in the beginning of the function:
global player
虽然它被认为不是一个好的编程习惯,它可能为你工作。
Although it's considered not a good programming practice, it may work for you.
这篇关于MATLAB:有audioplayer()后继续函数结束打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!