产生特定频率的声音 [英] Generate a sound of specific frequency

查看:180
本文介绍了产生特定频率的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个库: HTTP://$c$cbase.es/riffwave/ 其中生成即时在JavaScript声音。

I found this library : http://codebase.es/riffwave/ which generate sounds on-the-fly in javascript.

我想是有其产生的特定频率的正弦波的功能。

What I want is to have a function which generates a sine wave of a specific frequency.

但是,我们使用这个库的方法是有很多的数学,我无法使它工作。

But the way we use the library is with a lot of math and I wasn't able to make it work.

我试过

for(var i=0;i<10000;i++)data.push(127*(Math.sin(2*Math.PI*f*i)));

但它没有工作。

我该怎么办呢?

推荐答案

试试这个:

var freq = 440;  // Frequency (cycles per second)
var rate = 44100;  // Sample rate (samples per second)
for(var i = 0; i < 10000; i++) {
    var time = i / rate;       
    data[i] = 128 + Math.round(127 * (Math.sin(2 * Math.PI * freq * time)));
}

这篇关于产生特定频率的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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