gpt4 book ai didi

javascript生成方波声音

转载 作者:行者123 更新时间:2023-12-03 08:52:37 26 4
gpt4 key购买 nike

我想使用信号“1 0 0 0”生成方波声音。每个代码 (0,1) 都有一个模式,如 picture 所示。 .

例如,代码 1 将产生持续 500μs 的声音,然后停止 1000μs。信号应从零到最大正振幅,并且不会有任何负振幅。声音的频率为 10KHz。

基本上我需要从移动设备(iPhone、Android 和 Windows Phone 8)生成这种声音。我正在使用 Cordova 框架。有什么建议吗?

最佳答案

检查这个

var frequency = 10000;
var data = {
1: {duration:500, sleep:1000},
0: {duration:500, sleep:500}
}
var audio = new window.webkitAudioContext();


//function creates an Oscillator. In this code we are creating an Oscillator for every tune, which help you control the gain.
//If you want, you can try creating the Oscillator once and stopping/starting it as you wish.
function createOscillator(freq, duration) {
var attack = 10, //duration it will take to increase volume full sound volume, makes it more natural
gain = audio.createGain(),
osc = audio.createOscillator();

gain.connect(audio.destination);
gain.gain.setValueAtTime(0, audio.currentTime); //change to "1" if you're not fadding in/out
gain.gain.linearRampToValueAtTime(1, audio.currentTime + attack / 1000); //remove if you don't want to fade in
gain.gain.linearRampToValueAtTime(0, audio.currentTime + duration / 1000); //remove if you don't want to fade out

osc.frequency.value = freq;
osc.type = "square";
osc.connect(gain);
osc.start(0);


setTimeout(function() {
osc.stop(0);
osc.disconnect(gain);
gain.disconnect(audio.destination);
}, duration)
}

function play() {
//your pattern
var song = [1,0,1,1];

timeForNext = 0;
for (i=0;i<song.length;i++){
duration = data[song[i]].duration;
//use timeout to delay next tune sound
window.setTimeout(function(){
createOscillator(frequency, duration);
},timeForNext);
timeForNext+=data[song[i]].sleep;
}
}

//play the music
play();

此链接有一些很好的信息 http://www.bit-101.com/blog/?p=3896不久前我用它和 Cordova 创建了一个钢琴应用程序。不过还没发布。

关于javascript生成方波声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32630211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com