我是初学者学习javascript。我的网站上有一个与微调频率和行星频率相关的交互式页面,我有各种各样的项目。 我需要能够在循环中播放我的音频样本 .wav 文件,但是音频样本的时间延长以及相应的音高变化。
我尝试myAudio.playbackRate = 0.5;
播放音频0.5慢但保持音高相同。
我研究并找到了一些东西。但是我如何将preservesPitch
设置为false或true?这只适用于'谷歌Chrome'我认为,所以我找到的其他程序在这里:
https://github.com/janesconference/KievII/blob/master/dsp/pitchshift.js
似乎无法让它工作,我不知道我应该如何修改它,我在哪里粘贴我的音频.wav文件URL在程序中? 任何其他与此相关的提示将非常感激。 提前感谢您的时间和帮助。
答案 0 :(得分:2)
我也希望改变音频转换音量,经过多年的努力,我找到this。
我还重新格式化了代码,使其更易于使用(您需要将“decode-audio-data / viper.ogg”替换为您自己的WAV或OGG文件名。):
<script>
function playSound(file, speed=1, pitchShift=1, loop=false, autoplay=true) {
/*
Use the play() method to start the audio. if pitchShift is true
use the stop() method to stop the audio and destroy the object.
If pitchShift is false use the pause() method to pause and set
the attribute currentTime to 0 to reset the time.
*/
if(pitchShift) {
/*
After weeks of searching, I have finally found a way to pitch shift audio.
Thank you Mozilla.
2018/03/31:
https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/playbackRate
https://github.com/mdn/webaudio-examples/tree/master/decode-audio-data
https://www.w3schools.com/jsref/prop_audio_loop.asp
Original comments:
use XHR to load an audio track, and
decodeAudioData to decode it and stick it in a buffer.
Then we put the buffer into the source
*/
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
source = audioCtx.createBufferSource();
request = new XMLHttpRequest();
request.open('GET', file, true);
request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = request.response;
audioCtx.decodeAudioData(audioData, function(buffer) {
myBuffer = buffer;
songLength = buffer.duration;
source.buffer = myBuffer;
source.playbackRate.value = speed;
source.connect(audioCtx.destination);
source.loop = loop;
},
function(e){"Error with decoding audio data" + e.error});
}
request.send();
source.play=source.start
} else {
source=new Audio(file)
source.playbackRate=speed
source.loop=loop
}
if(autoplay) {
source.play()
}
return source
}
var source
function play() {
source=playSound('decode-audio-data/viper.ogg', pitch=2);
}
function stop() {
source.stop(0);
document.getElementById('play').href=''
document.getElementById('play').innerHTML='Refresh to play again'
}
</script>
<a id="play" href="#" onclick="play()">Play</a> | <a href="#" onclick="stop()">Stop</a>
答案 1 :(得分:1)
您可以将mozPreservesPitch
属性设置为false,以使用Firefox更改元素或声音文件的音高。 webkitPreservesPitch
应该与webkit浏览器一起使用,但请注意&#34;此API尚未标准化&#34; ...
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
这对我有用:
var soundPlayer = new Audio();
function playLowerNote() {
soundPlayer.src = "piano.mp3";
soundPlayer.mozPreservesPitch = false;
soundPlayer.playbackRate = 0.5;
soundPlayer.play();
}
playLowerNote();
仍在寻找一种更好的循环方式。
答案 2 :(得分:0)
有些问题,当你说I tried myAudio.playbackRate = 0.5;
时我们正在谈论Web Audio API
?
如果是,请再次听取音高以及样本的持续时间。
如果不是,可能就是你使用原生html5功能,如果你想保持原始速度并改变音高一种方式是在改变速度后应用某种类型的插值尝试线性插值与相同因子用于拉伸你音频。如果您需要更改速度和音高只需应用插值而不改变速度(原始声音),这相当于以不同的采样率播放您的音频Web Audio API可以做到。
代码pitchshift.js
是从Stephan Bernsee到javascript
的代码的端口(这会改变音高并保持速度不可触及),您需要在每个音频音频上调用此函数,所以首先你需要做一个用short int或float解码音频文件的函数。
答案 3 :(得分:0)
嘿,您的网络音频API现在似乎至少适用于正弦波,将会很快学会如何使用样本https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Using_Web_Audio_API来自此处的信息