2

我已经创建了一个振荡器(如下图),就像 MDN 说的:

// from : https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Using_Web_Audio_API

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
oscillator.type = 'sine'; // sine wave — other values are 'square', 'sawtooth', 'triangle' and 'custom'
oscillator.frequency.value = 2500; // value in hertz
oscillator.start();

有没有办法改变音量,比如我改变了频率值?

4

2 回答 2

0

您可以使用控制输出频率的滑块获得可变频率控制:

document.getElementById('slider99').addEventListener('input', slider99change, false);
function slider99change(e) {
    var x = document.getElementById("slider99");
    var s = x.value.toString();
    oz.frequency.value = s;
}

您的振荡器在其他地方全局定义:

var ozcontext = new (window.AudioContext || window.webkitAudioContext)();
var oz = ozcontext.createOscillator();
var gainNode = ozcontext.createGain();
oz.connect(gainNode);
gainNode.connect(ozcontext.destination);
gainNode.gain.value = 0;
oz.type = 'sine';
oz.frequency.value = 440;
oz.start();
于 2017-08-19T08:30:09.827 回答
0

您需要查看修改 audioContext 和 gainNode 以更改音量。以下链接可能会有所帮助:

https://github.com/mdn/voice-change-o-matic/blob/gh-pages/scripts/app.js

于 2015-12-20T18:40:33.880 回答