如何使用 JavaScript / Tone.js 改变音调和播放速率?

How to change pitch AND playback rate with JavaScript / Tone.js?

我想同时完成两件事:

1) 将声音文件的播放速率更改为 1/2 速度

2) 将 音高 降低五分之一

最后,我不想使用单独的按钮来播放结果,而是想将其连接到音频标签并使用那里的播放按钮。

以下是我迄今为止使用 Tone.js 得到的最接近的结果。我真的很难处理文档,但我知道 Tone.Transport.bpm.value = 60;将 bpm 从 120 更改为 60 并 Tone.PitchShift 移调歌曲。我只是不知道如何将两者结合起来 'stack' 效果。

以下是我关注的一些链接:

How to change the pitch with JavaScript?

https://tonejs.github.io/docs/r12/PitchShift

https://tonejs.github.io/docs/13.8.25/Transport

在此先感谢您的帮助!

<audio id="myAudio" controls preload="none">
      <source src="my_tune.m4a" type="audio/mp4" >
</audio>

<script src="https://unpkg.com/tone@next/build/Tone.js"></script>

<script>

Tone.Transport.bpm.value = 60;  // setting the bpm like this is not working. where to put this?


var player = new Tone.Player("my_tune.m4a").sync().start(0);
// is it possible to use audio tag instead of creating this player?

var pitchShift = new Tone.PitchShift({
    pitch: -5 // this is working and lowers pitch by a fifth
}).toMaster();

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

<script>

<button onclick="setPlaySpeed()" type="button">separate button</button><br>

更新:可行的解决方案

我找到了一个可行的方法:先设置播放速率。然后,在设置音高时,您需要计算 add/subtract 的音高偏移量以补偿播放速率。在我的示例中,我必须为 0.5 的播放速率补偿 +12 的音高偏移。

player = new Tone.Player("url/to/audio.mp3");
playback_rate = 0.5 // or 1 (I only implemented these two options)
transpose_by = 7 // up 7 half steps, -7 would be down 7 half steps

// set playback rate
player.playbackRate = playback_rate
player.toDestination();


// set pitch shift
if (playback_rate == 1){
    pitch_shift = new Tone.PitchShift({
    pitch: transpose_by.toString()
    }).toDestination();
} else { //if playbackrate == 0.5 add +12 to pitch to correct
    trans = transpose_by + 12
    trans = trans.toString()
    pitch_shift = new Tone.PitchShift({
    pitch: trans
  }).toDestination();
}
player.disconnect(); // disconnect old player (get overlay of two players otherwise if you repeat this)
player.connect(pitch_shift);

player.start()

在我的网站上查看实际效果:https://www.lickstack.com

我仍然不知道如何将 Tone.js 播放器连接到音频元素。任何帮助,将不胜感激。在这里查看我的 post: create html <audio> element from Tone.js object