本文介绍了MATLAB:audioplayer()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MATLAB中的声音()函数来产生一个音调。下面的函数在440Hz的使用为4秒的口气说:

I am using the sound() function in MATLAB to generate a tone. The following function plays a tone for 4 seconds at 440Hz:

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)一个已知的bug,因此一般建议似乎是使用的。所以,我已经更新了我的code以下内容:

Occasionally (after using the function a few times), I get an error from MATLAB saying "can't register sound window". Having looked around the internet a bit, I notice this is a known bug in MATLAB (version R14 SP3) and so the general advice seems to be to use the 'audioplayer' function instead. So, I have updated my code to the following:

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)

然而,这并不产生一个音调。在做这个新的code的工作谁能帮助?

However, this does not produce a tone. Can anyone help in making this new code work?

推荐答案

我已经找到了解决方案 - 这个问题似乎是音频播放停止功能退出时。所以,我不得不改变打法()来playblocking()。这prevents控制返回,直到声音结束。这不是理想的解决方案但是(这将是很好的控制传递回父函数,而声音依然饰演),但​​现在它会做。如果任何人都可以提出和办法通过控制返回的 的发挥整体的声音,我就AP preciate它。下面是最终code:

I have found the solution - the problem seems to be that the audio playback stops when the function exits. So, I have had to change play() to playblocking(). This prevents control returning until the sound finishes. It's not the ideal solution however (it would be nice to pass control back to the parent function whilst the sound still plays), but for now it will do. If anyone can suggest a way to pass control back and play the whole sound, I would appreciate it. Here is the final code:

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:有audioplayer()继续发挥功能结束后

这篇关于MATLAB:audioplayer()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 07:31