如何用JavaScript改变音调?

时间:2018-12-20 22:12:16

标签: javascript html audio pitch

假设您有一个名为audio的音频变量,它存储声音。

例如,我知道如何更改速度:

audio.playBackRate = 2; 

但是我不知道如何改变音高。

是否存在audio.pitch属性,还是必须自己创建?

我想做这样的事情:

var audio = new Audio();
audio.src = "sound_effect.wav";
audio.pitch = 2 //doubling the pitch but there is no pitch attribute
audio.play();

1 个答案:

答案 0 :(得分:0)

我认为您需要使用一个库来将pitch shifting应用于音频信号。您可以使用Tone.js PitchShift。有关工作示例,请参见GitHub用户Jexim的this JSFiddle。我从下面的这个小提琴中复制粘贴了大部分的导入和部分:

Javascript:

var player = new Tone.Player("http://example.com/my-audiofile.mp3").sync().start(0);

var pitchShift = new Tone.PitchShift({
    pitch: -5
}).toMaster();

player.connect(pitchShift);

Tone.Buffer.on('load', () => {
    alert('Ready for play');
});

window.play = function() {
    Tone.Transport.start();
}

HTML

<script src="https://unpkg.com/tone@next/build/Tone.js"></script>
<button onclick="play()">Play</button>
相关问题