I want to use the web audio OfflineAudioContext to save some short sound files into a new longer sound file. It seems that the OfflineAudioContext uses an audiobuffer to save the audio. I'm using the audio tag to load my sound files. How can I convert the sound files into an audio buffer that I can pass to the OfflineAudioContext buffer. I don't want to have to use the XMLHttpRequest to create the audio buffer. I wanted to be able to load each sound file then add it to a buffer, and then pass it to the offline context. I've tried several different ways, but the most I've got done is to pass an empty audio buffer to the context which just gives me a file with no sound.
I've tried several features of the web audio context to create a new buffer, but I either get errors, or no sound
<html>
<body>
<audio controls controlsList = "nodownload" id="audio"><source id="audioSource" src="" />
Your browser does not support the audio format.</audio>
<ol id="recordingsList"></ol>
<script>
// define online and offline audio context
var audioCtx1 = new AudioContext();
var offlineCtx = new OfflineAudioContext(2,44100*40,44100);
var channels = 2;
// Create an empty two second stereo buffer at the
// sample rate of the AudioContext
var frameCount = audioCtx1.sampleRate * 2.0;
var myArrayBuffer = audioCtx1.createBuffer(2, frameCount, audioCtx1.sampleRate);
source = offlineCtx.createBufferSource();
var array = ['sound1.mp3','sound2.mp3','sound3.mp3']
var audio = document.querySelector('audio');
audio.src = array[0];
source.buffer = myArrayBuffer;
source.connect(offlineCtx.destination);
source.start();
//source.loop = true;
offlineCtx.startRendering();
offlineCtx.oncomplete = function(e) {
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var song = audioCtx.createBufferSource();
song.buffer = e.renderedBuffer;
song.connect(audioCtx.destination);
song.start();
console.log("completed!");
}
</script>
</body>
</html>
I've changed the code several times during testing, I think with this code I posted I just get no sound playing.
Aucun commentaire:
Enregistrer un commentaire