在使用JavaScript动态产生声音/ HTML5 [英] Generating sound on the fly with javascript/html5

查看:295
本文介绍了在使用JavaScript动态产生声音/ HTML5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能产生的JavaScript / HTML恒定的声音流?例如,要产生一个永久的正弦波,我想有一个回调函数,那被称为每当输出缓冲区即将成为空的:

Is it possible to generate a constant sound stream with javascript/html? 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);
}

(我们的想法是用这个做一个互动的合成器。我不知道事先的关键有多长pressed,所以我不能使用固定长度的缓冲区)

(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)

推荐答案

JavaScript和音频元素目前还无法跨浏览器生成的持续音频,因为史蒂芬Wittens的笔记在博客上创建一个JavaScript合成器:

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:

...有没有办法排队无缝播放音频合成大块。

"...there is no way to queue up chunks of synthesized audio for seamless playback".

借助网络音频API 旨在方便的JavaScript音频合成。 Mozilla开发者网络有一个<一个href=\"https://wiki.mozilla.org/Audio_Data_API#Complete_Example%3a_Creating_a_Web_Based_Tone_Generator\">Web基于音频发生器在Firefox 4+ [演示1 ]工作。添加这两行至code和你有钥匙时preSS [演示2 生成音频持续工作合成器 - 在Firefox 4只,先单击结果区域,然后preSS任意键]:

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]:

window.onkeydown = start;  
window.onkeyup = stop;

在网络音频API 页面>是值得检讨了。不幸的是,网络音频API的支持不会扩展到其他浏览器呢。

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.

要创建在present一个跨浏览器的合成器,你可能必须通过依傍prerecorded音频:

To create a cross-browser synth at present, you'll likely have to fall back on prerecorded audio by:


  1. 使用长prerecorded OGG / MP3采样音色,在不同的音频它们嵌入元素以及启动和停止他们在关键preSS。

  2. 含嵌入音频元素和通过JavaScript控制播放的SWF文件。 (这似乎是在谷歌的Les Paul涂鸦采用的方式。)

  1. Using long prerecorded ogg/mp3 sample tones, embedding them in separate audio elements and starting and stopping them upon keypress.
  2. Embedding an swf file containing the audio elements and controlling playback via JavaScript. (This appears to be the method that the Google Les Paul Doodle employs.)

这篇关于在使用JavaScript动态产生声音/ HTML5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆