Its possible to merge two audio 'base64data' strings to create an unique audio file?
I have in strings two cyclic audio base64 wavs like this:
data:audio/x-wav;base64,UklGRuIfQVZFZm1R7SH$WP90AhICLwKT...
I guess I am doing something very stupid but I wonder if it is possible.
I am trying to merge the two wavs into one that I can play in an audio HTML element. I want to xtract the base64 data of second to merge to the one and then play it but the navigator return an error.
This is my javascript code:
var audio_Data1 = base64_audio1;
var audio_Data2 = base64_audio1.split(',')[1];
var audio_final = audio_Data1 + audio_Data2;
audioControl.src = audio_final;
audioControl.play();
I appreciate your suggestions Thank you !
EDIT:
I am trying to decode base64 chunks to buffer data and concat the buffers for encode to base64 again.
The problem: The base64 returned is "AA=="
I think "AA==" is 0 bytes. Then i am doing something bad when I concat the buffer:
var myB64Data = myB64WavString.split(',');
var myB64Chunk = myB64Data[1];
var myBuffer1 = base64ToArrayBuffer(myB64Chunk);
var myBuffer2 = ""; //Same process
var myFinalBuffer = [];
myFinalBuffer.push.apply(myFinalBuffer, myBuffer1);
myFinalBuffer.push.apply(myFinalBuffer, myBuffer2);
var myFinalB64 = 'data:audio/x-wav;base64,' + arrayBufferToBase64(myFinalBuffer);
console.log( myFinalB64 );
Its return by console the next: "data:audio/x-wav;base64,AA=="
My javascript working functions for encode/decode:
function base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array( len );
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
function arrayBufferToBase64( buffer ) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}