gpt4 book ai didi

matlab - MATLAB:audioplayer()

转载 作者:行者123 更新时间:2023-12-02 22:35:15 26 4
gpt4 key购买 nike

我在MATLAB中使用sound()函数生成音调。以下功能会以440Hz的频率播放4秒钟的声音:

duration = 4
toneFreq = 440
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
sound(y, samplesPerSecond); % play the sound wave at the specified bit rate

偶尔(在使用函数几次之后),我从MATLAB中收到一条错误消息,提示“无法注册声音窗口”。在互联网上浏览了一下之后,我注意到这是MATLAB(R14 SP3版本)中的一个已知错误,因此一般的建议似乎是改用 'audioplayer' function。因此,我将代码更新为以下内容:
duration = 4
toneFreq = 440
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); % play the sound wave at the specified bit rate
play(player)

但是,这不会发出声音。任何人都可以帮助使此新代码正常工作吗?

最佳答案

我找到了解决方案-问题似乎是当函数退出时音频播放停止。因此,我不得不将play()更改为playblocking()。这样可以防止控件返回直到声音结束。但是,这不是理想的解决方案(在声音仍播放的同时将控制权传递回父函数会很不错),但现在可以了。如果有人可以建议一种将控制权传递回去并播放整个声音的方法,我将不胜感激。这是最终代码:

duration = 4
toneFreq = 440
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); % play the sound wave at the specified bit rate
playblocking(player)

编辑:还找到了一种解决方案,该解决方案允许函数退出后继续播放。参见 MATLAB: Having audioplayer() continue to play after function ends

关于matlab - MATLAB:audioplayer(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9702454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com