如何用JavaScript改变音调? [英] How to change the pitch with JavaScript?

查看:79
本文介绍了如何用JavaScript改变音调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let’s say you have an audio variable called audio and it stores a sound.

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

I know how to change the speed for example:

audio.playBackRate = 2; 

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

But I don't know how to change the pitch.

是否有一个 audio.pitch 属性,还是我必须自己创建它?

Is there an audio.pitch attribute or do I have to create it myself?

我想做这样的事情:

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的此JSFiddle .我从下面的这个小提琴中复制粘贴了大部分的导入和部分:

I think you need to use a library to apply pitch shifting to your audio signal. You could use the Tone.js PitchShift. See this JSFiddle of GitHub user Jexim for a working example. I copy-pasted the most importand parts from this fiddle below:

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>

这篇关于如何用JavaScript改变音调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆