我发现了一个功能形式的代码片段,可以更改正在播放的音频的音调。 我想将其更改为使用麦克风网络音频live input时改变音调的功能。
我已经尝试过,但仍然无法正常工作。我需要帮助。
这是代码:
function playSoundpitch(file, speed = 2, pitchShift = 1, loop = false, autoplay = true) {
if (pitchShift) {
audioCtx = new(window.AudioContext || window.webkitAudioContext)();
source = audioCtx.createBufferSource();
var gainNodex = audioCtx.createGain();
gainNodex.gain.value = 2; // double the volume
request = new XMLHttpRequest();
request.open('GET', file, true);
request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = request.response;
audioCtx.decodeAudioData(audioData, function(buffer) {
myBuffer = buffer;
songLength = buffer.duration;
source.buffer = myBuffer;
source.playbackRate.value = speed;
source.loop = loop;
//source.connect(audioCtx.destination);
source.connect(gainNodex);
gainNodex.connect(audioCtx.destination);
source.onended = function() {};
},
function(e) {
"Error with decoding audio data" + e.error
});
}
request.send();
source.play = source.start
} else {
source = new Audio(file)
source.playbackRate = speed
source.loop = loop
}
if (autoplay) {
source.play()
}
return source
}
var source;
source = playSoundpitch('https://sampleswap.org/samples-ghost/VOCALS%20and%20SPOKEN%20WORD/Native%20American/1392[kb]sheshone-native-vox2.wav.mp3', pitch = 0.8);
答案 0 :(得分:0)
看看此页面:https://alligator.io/js/first-steps-web-audio-api/
与您的问题相关的部分位于底部:
/* The frequency (in Hz) of Bb4 is 466.16 */ oscillator .frequency .setValueAtTime(466.16, audioContext.currentTime);
值得注意的是,首先构建了“振荡器”对象,随后在代码中,可以使用上述代码在流中调整节点的间距。您已经创建了节点(正在调用audioCtx
对象)。该对象的频率值必须随时更改。以上是静态音高变化的情况。您将必须同时获取频率,然后通过所需音高的差来更改它,然后使用setValueAtTime
更新该值。
或者,您可以安装此软件包并使用它使音高转换工作更容易:
https://github.com/mmckegg/soundbank-pitch-shift
最后,还有一个使用AudioContext对象的解决方案(使用detune
方法):https://codepen.io/qur2/pen/emVQwW