假设您有一个名为
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();
我认为你需要使用一个库来将变调应用于你的音频信号。您可以使用 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>
这个答案以来,Tone 的 API 似乎已经发生了一些变化。以下代码反映了最新版本(截至 2022 年):
const url = "https://upload.wikimedia.org/wikipedia/commons/9/90/De-Wolfgang_Amadeus_Mozart.ogg"; // use any valid audio file if this disappears over time...
const btn = document.querySelector("button");
btn.addEventListener("click", async () => {
await Tone.start();
const player = new Tone.Player({
url,
loop: true,
autostart: false,
});
await Tone.loaded();
const pitchShift = new Tone
.PitchShift({pitch: -8})
.toDestination()
;
player.connect(pitchShift);
player.start();
});
<script src="https://unpkg.com/[email protected]/build/Tone.js"></script>
<button>Play</button>