【问题标题】:Generating a tone using pure javascript with Chromium WebAudio API使用纯 JavaScript 和 Chromium WebAudio API 生成音调
【发布时间】:2011-10-15 13:26:15
【问题描述】:

如何仅使用 javascript 和 Chromium 的 WebAudio API 生成音调(例如纯正弦波)?

我想完成类似the Firefox equivalent 的事情。

Chromium WebAudio 演示 here 似乎都使用了预先录制的 <audio> 元素。

谢谢!

【问题讨论】:

    标签: html google-chrome audio chromium web-audio-api


    【解决方案1】:

    网络音频 API 具有所谓的振荡器接口来生成您正在谈论的音调。他们非常直截了当地开始......

    var context = new webkitAudioContext(),
        //Call function on context
        oscillator = context.createOscillator(); // Oscillator defaults to sine wave
    
    oscillator.connect(context.destination);
    oscillator.start();
    

    您可以通过以下方式更改波浪的类型:

    oscillator.type = 1; // Change to square wave.
    

    或者:

    oscillator.type = oscillator.SQUARE;
    

    an article about this very topic 写的比较详细,可能对你有用!

    【讨论】:

    • 没有比使用数字更好的改变振荡器类型的方法吗?它不会产生非常可读的代码。
    • @frankster 是的,你可以做我会更新答案。
    • 使用oscillator.start()而不是oscillator.noteOn(),它已经不存在了,见:jsfiddle.net/xMruN/322
    • 如何添加时间限制?
    • @Dewsworld 你可以做振荡器.start(context.currentTime),然后振荡器.stop(context.currentTime + 2) 在2秒后停止它。
    【解决方案2】:

    可能不是这些最好的方法,但我使用 dsp.js 生成不同类型的正弦曲线,然后在此演示中将它们传递给 Web Audio API:http://www.htmlfivewow.com/demos/waveform-generator/index.html

    【讨论】:

    • 啊对了,忘记放下了;我基本上做了同样的事情。当我问这个问题时,那个演示或多或少完全坏了。我和一些朋友一起破解了这个pastebin.com/7jEqJSCU 的黑客马拉松。它使用 DSP 生成正弦波。它还演示了一个异步加载架子鼓。我不知道它是否仍然有效;当我们构建它时它在 Canary 中运行,但如果现在需要进行调整,我不会感到惊讶。
    猜你喜欢
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    相关资源
    最近更新 更多