【发布时间】:2019-09-20 13:50:19
【问题描述】:
数组第一列是频率,第二列是播放时间。
// create web audio api context
var audioCtx = new(window.AudioContext || window.webkitAudioContext)();
function playNote(frequency, duration) {
// create Oscillator node
var oscillator = audioCtx.createOscillator();
oscillator.type = 'sawtooth';
oscillator.frequency.value = frequency; // value in hertz
oscillator.connect(audioCtx.destination);
oscillator.start();
setTimeout(
function() {
oscillator.stop();
playMelody();
}, duration);
}
function playMelody() {
if (notes.length > 0) {
note = notes.pop();
playNote(note[0],note[1]);
}
}
notes = [[67.40,14.84],
[58.60,17.06],
[69.80,14.33],
[69.80,14.33],
[66.30,15.08],
[62.30,16.05],
[66.90,14.95],
[65.00,15.38],
[66.00,15.15],
[88.40,11.31],
[60.60,16.50],
[63.90,15.65],
[114.20,8.76],
[114.20,8.76],
[99.70,10.03],
[344.90,2.90],
[344.90,2.90],
[70.00,14.29],
[310.90,3.22],
[310.90,3.22],
[68.30,14.64],
[71.30,14.03],
[69.40,14.41],
[101.70,9.83],
[70.40,14.20],
[67.20,14.88],
[76.00,13.16],
[59.60,16.78],
[73.30,13.64],
[62.10,16.10],
[72.60,13.77],
[76.60,13.05],
[76.80,13.02],
[52.90,18.90],
[69.50,14.39],
[72.90,13.72],
[69.90,14.31],
[69.60,14.37]];
notes.reverse();
tempo = 100;
playMelody();
我能够使用音频上下文以顺序方式播放频率代码,但我需要一种将这种方式转换为音频文件或 AudioBuffer 的方法。我想用这些频率设计一个频谱图。
【问题讨论】:
标签: javascript audiocontext audiobuffer