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

6pp0gazn  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(430)

我想同时完成两件事:
1)将声音文件的播放速度改为1/2速度
2)将音高降低五分之一
最后,而不是使用一个单独的按钮来播放结果,我想挂钩到一个音频标签,并使用播放按钮那里。
下面是我到目前为止使用Tone.js得到的最接近的结果。我真的很难理解这个文档,但是我理解Tone.Transport.bpm.value = 60;改变了从120到60的bpm和音调。音高变换变换的歌曲。我只是不知道如何合并这两个'堆叠'的效果。
下面是我关注的一些链接:
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>
f8rj6qna

f8rj6qna1#

更新:工作溶液
我找到了一个有效的方法:首先设置播放速率。然后,当设置音高时,你需要计算增加/减少多少音高偏移来补偿播放速率。在我的例子中,我必须为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播放器到一个音频元素。任何帮助将不胜感激。请看我的帖子在这里:create html element from Tone.js object

相关问题