2

我找到了一个函数形式的代码片段来改变正在播放的音频的音高。我想将其更改为使用麦克风网络音频进行实时输入时更改音高的功能。

我已经尝试过,但它仍然无法正常工作。我需要帮助。

这是代码:

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);

4

1 回答 1

1

看看这个页面: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

于 2019-03-18T15:31:47.633 回答