使用 javascript/html5 动态生成声音
是否可以使用 javascript/html5 生成恒定的声音流?例如,要生成永久正弦波,我将有一个回调函数,每当输出缓冲区即将变空时就会调用该函数:(
function getSampleAt(timestep)
{
return Math.sin(timestep);
}
想法是使用它来制作交互式合成器。我不知道在提前按下按键的时间,所以我不能使用固定长度的缓冲区)
Is it possible to generate a constant sound stream with javascript/html5? For example, to generate a perpetual sine wave, I would have a callback function, that would be called whenever the output buffer is about to become empty:
function getSampleAt(timestep)
{
return Math.sin(timestep);
}
(The idea is to use this to make an interactive synth. I don't know in advance how long a key will be pressed, so I can't use a fixed length buffer)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您现在可以在大多数浏览器中使用 Web Audio API ( IE 和 Opera Mini 除外)。
试试这个代码:
如果你想要降低音量,你可以这样做:
我在阅读 Web Audio API 工作草案,我从 @brainjam 的链接中找到的。
最后,在 chrome 检查器 (ctrl-shift-i) 中检查各种对象非常有帮助。
You can use the Web Audio API in most browsers now (excepting IE and Opera Mini).
Try out this code:
If you want the volume lower, you can do something like this:
I got most of this from experimenting in chromium while reading the Web Audio API Working Draft, which I found from @brainjam 's link.
Lastly, it is very helpful to inspect the various objects in the chrome inspector (ctrl-shift-i).
使用 HTML5 音频元素
使用 JavaScript 和
audio
元素跨浏览器生成持续音频目前是不可能的,正如 Steven Wittens 关于创建 JavaScript 合成器的博客文章中的注释:使用网络音频 API
网络音频 API 的设计促进 JavaScript 音频合成。 Mozilla 开发者网络有一个可在 Firefox 4+ 中运行的基于网络的音频生成器 [演示 1]。将这两行添加到该代码中,您就拥有了一个可以在按键时生成持续音频的工作合成器 [演示 2 - 有效仅在 Firefox 4 中,先单击“结果”区域,然后按任意键]:
BBC 的 Web Audio API 上的页面是也值得回顾。不幸的是,对 Web Audio API 的支持尚未扩展到其他浏览器。
可能的解决方法
目前要创建跨浏览器合成器,您可能必须依靠预先录制的音频
Using the HTML5 audio element
Cross-browser generative sustained audio using JavaScript and the
audio
element isn't currently possible, as Steven Wittens notes in a blog post on creating a JavaScript synth:Using the Web Audio API
The Web Audio API was designed to facilitate JavaScript audio synthesis. The Mozilla Developer Network has a Web Based Tone Generator that works in Firefox 4+ [demo 1]. Add these two lines to that code and you have a working synth with generative sustained audio upon keypress [demo 2 - works in Firefox 4 only, click the 'Results' area first, then press any key]:
The BBC's page on the Web Audio API is worth reviewing too. Unfortunately, support for the Web Audio API doesn't extend to other browsers yet.
Possible workarounds
To create a cross-browser synth at present, you'll likely have to fall back on prerecorded audio by:
audio
elements and starting and stopping them upon keypress.当然!您可以在此演示中使用音调合成器:
玩得开心!
我从 Houshalter 那里得到了解决方案:
如何发出 Javascript 蜂鸣声?
您可以在此处克隆和调整代码:
JS Bin 上的音调合成器演示
兼容浏览器:
不兼容
Sure! You could use the tone synthesizer in this demo:
Have fun!
I got the solution from Houshalter here:
How do I make Javascript beep?
You can clone and tweak the code here:
Tone synthesizer demo on JS Bin
Compatible browsers:
Not Compatible
Web Audio API 即将登陆 Chrome。请参阅 http://googlechrome.github.io/web-audio -samples/samples/audio/index.html
按照“入门”中的说明进行操作,然后查看令人印象深刻的演示。
更新(2017):到目前为止,这是一个更加成熟的界面。该 API 记录于 https://developer.mozilla.org/en -US/docs/Web/API/Web_Audio_API
Web Audio API is coming to Chrome. See http://googlechrome.github.io/web-audio-samples/samples/audio/index.html
Follow the directions in "Getting Started" there, and then check out the very impressive demos.
Update(2017): by now this is a much more mature interface. The API is documented at https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API
您可以即时生成 wav-e 文件并播放它(src)
这是可视化
You can generate wav-e file in the fly and play it (src)
Here is visualistation
这就是我一直在寻找的东西,最终我成功地做到了我想要的。也许你也会喜欢它。
带频率和推动开/关的简单滑块:
This is what I have looked for like forever and in the end I managed to do it myself like I wanted. Maybe you will like it too.
Simple slider with frequency and push on/off:
这不是您问题的真正答案,因为您已经要求使用 JavaScript 解决方案,但您可以使用 ActionScript。它应该可以在所有主要浏览器上运行。
您可以从 JavaScript 中调用 ActionScript 函数。
这样你就可以封装 ActionScript 声音生成函数并对其进行 JavaScript 实现。只需使用 Adobe Flex 构建一个小型 swf,然后将其用作 JavaScript 代码的后端。
This is not real answer on your question because you have asked for a JavaScript solution, but you can use ActionScript. It should run on all major browsers.
You can call ActionScript functions from within JavaScript.
In that way you can wrap the ActionScript sound generation functions and make a JavaScript implementation of them. Just use Adobe Flex to build a tiny swf and then use that as backend for your JavaScript code.
您可以使用以下代码来生成声音并尝试不同的频率来生成更多声音:
you can use the following code to generate sounds and try different frequencies to generate even more sounds: