我也想转换音频,经过多年我发现了这个。
我还重新格式化了代码以使其更易于自己使用(您需要将“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>