我听说通过改变播放速度我们可以修改音频的频率。我在这里测试过:https://teropa.info/blog/2016/08/10/Frequency-and-pitch.html https://teropa.info/blog/2016/08/10/frequency-and-pitch.html
但问题是我需要一个录制的音频文件才能做到这一点。据我发现,网络音频无法改变现场音频的播放速度。我一直在想,如果我们将音频保存在缓冲区中,我们就可以改变它的播放速度,从而改变频率。
我是网络音频 API 的新手。我发现一篇文章通过将实时音频保存到缓冲区来记录它。https://docs.sumerian.amazonaws.com/articles/webaudio-1/ https://docs.sumerian.amazonaws.com/articles/webaudio-1/
我想要的是:-
- 从麦克风获取音频。
- 将其保存到缓冲区中。
- 更改播放速度。
- 在扬声器上播放。
演示如何更改 buffernode 的播放速度https://mdn.github.io/webaudio-examples/decode-audio-data/ https://mdn.github.io/webaudio-examples/decode-audio-data/
但我希望使用现场麦克风音频来代替录制的声音。
这是我对小提琴的尝试
https://jsfiddle.net/5dza62b8/13/ https://jsfiddle.net/5dza62b8/13/
var audioContext = new(window.AudioContext || window.webkitAudioContext)();
var streamSource, scriptNode, bufferSource, audioBuffer;
var playbackControl = document.querySelector('#playback-rate-control');
var playbackValue = document.querySelector('#playback-rate-value');
// define variables
window.start_audio = function() {
navigator.mediaDevices.getUserMedia({
audio: true
}).then((stream) => {
alert("Got audio stream from microphone!");
audioContext = new AudioContext();
// Create an AudioNode from the stream.
streamSource = audioContext.createMediaStreamSource(stream);
scriptNode = audioContext.createScriptProcessor(2048, 1, 1);
bufferSource = audioContext.createBufferSource();
// Whenever onaudioprocess event is dispatched it creates a buffer array with the length bufferLength
scriptNode.onaudioprocess = (audioProcessingEvent) => {
realtimeBuffer = audioProcessingEvent.inputBuffer.getChannelData(0);
// Create an array of buffer array
audioBuffer.push(realtimeBuffer);
}
bufferSource.buffer = audioBuffer;
bufferSource.playbackRate.value = 0.8;
streamSource.connect(scriptNode);
bufferSource.connect(audioContext.destination);
bufferSource.start();
}).catch((e) => {
alert(e.name + ". " + e.message);
});
}
// wire up buttons to stop and play audio, and range slider control
playbackControl.addEventListener('input', function() {
bufferSource.playbackRate.value = playbackControl.value;
playbackValue.innerHTML = playbackControl.value;
});