网络音频api以不同的速率播放提示音,提示音... [英] web audio api plays beep, beep,... beep at different rate
问题描述
我正在尝试根据浏览器窗口中的某些传感器读数以不同的速率播放哔"声.
I am trying to play "beep" sound at different rate based on some sensor readings inside a browser window.
该想法是在传感器读数较高时嘟嘟",哔哔",哔哔","...哔哔"更快,而在传感器读数低时嘟嘟......哔哔"较慢,这都是实时的.
The idea is to "beep, beep, beep, ... beep" faster when the sensor reading is high, and "beep,...beep" slower when the sensor reading is low, all in real-time.
传感器读数通过socket.io输入到浏览器中.我已经可以控制进度栏的上下移动.音频反馈是一项额外功能.
The sensor reading is fed into the browser via socket.io. I can already control a progress bar moving up and down. The audio feedback is an extra feature.
经过一番谷歌搜索之后,我正在考虑使用Web音频api,创建一个正弦波振荡器并通过增益节点连接/断开来打开/关闭它.
After some googling, I am thinking about using web audio api, creating a sin-wave oscillator, and to turn it on/off with a gain node connect/disconnect.
我的问题是我如何以正确的方式控制时序,比如说我试图在1 Hz到20 Hz的频率范围内发出蜂鸣声,并且能够动态更改频率.
My question is how do I control the timing in the right way, say I am trying to beep at a range of frequencies from 1 Hz to 20 Hz, and be able to change the frequency dynamically.
推荐答案
我最不希望通过连接和断开振荡器来打开和关闭振荡器-您必须在主线程中执行此操作,所以不要使用超级-可预测的.
I would most specifically NOT turn an oscillator on and off by connecting and disconnecting it - you'd have to do that from the main thread, so not super-predictable.
您实际上可以使用调制低频振荡器来执行此操作:请查看以下代码:
You can actually do this with a modulating low-frequency oscillator: check out this code:
var context = new AudioContext();
//defaults to A440Hz, sine wave
var src = context.createOscillator();
// Now let's create a modulator to turn beeps on and off
var mod = context.createOscillator();
mod.type="square";
mod.frequency.value = "2"; // Start at 2Hz
var gain = context.createGain();
var scaler = context.createGain();
src.connect(gain);
gain.connect(context.destination);
mod.connect(scaler); // Mod signal is [-1,1]
scaler.gain.value = 0.5; // we need it to be [-0.5,0.5]
gain.gain.value = 0.5; // then it's summed with 0.5, so [0,1]
scaler.connect(gain.gain);
//start it up
src.start(0);
mod.start(0);
// to change rate, change mod.frequency.value to desired frequency
这篇关于网络音频api以不同的速率播放提示音,提示音...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!