3
votes

is it possible to convert a wav string to an html5 blob ?

( I'm not looking for a crossbrowser compatible solution, I just need it to work in Chrome ).

wavString = "RIFF  WAVEfmt D¬Xdata ¦®µ¼ÃÊÐÖÜáæêïòõøúüýþþþþüûøöóïëçâÜ×ÑËĽ¶¯§ xph`YQJC<5/~)~#~~~~~~  ~~~~~~~~~~~~~~~~~~!~'~,~3~9~@~GNV^emu}¥¬´»ÂÈÏÕÛàåéîñõøúüýþþþþýûùöóðìèãÞØÒÌÅ¿¸°©¡zrjb~Z~S~K~D~=~6~0~*~$}}}}}
}.....a lot of ugly chars.....";

I can convert the wavString to an html5 audio object and I can hear the wav sound if I play the sound:

var wave = new Audio('data:audio/wav;base64,' + btoa(wavString));
wave.controls = true;
document.body.appendChild(wave);

But I cannot create a wav blob from it, here is what I tried with no luck : ( I tried all combinations of type and blob vars ) :

var type = "audio/x-wav";
var type = "application/octet-stream";


var blob = new Blob([btoa(wavString)], {type: type});
var blob = new Blob(['data:audio/wav;base64,' + btoa(wavString)], {type: type});
var blob = new Blob([wavString], {type: type});

None of those works : they create blob, but I cannot save them to a server and listen to them as wav files. What I want to do is create a blob that I can export to a server as a wav file. Actually I can export a valid wav blob to a server, to the problem is really to convert this wavString to a valid wav blob.

1

1 Answers

8
votes

Creating ArrayBuffer may work.

var wavString = "RIFF  WAVEfmt.....a lot of ugly chars.....";
var len = wavString.length;
var buf = new ArrayBuffer(len);
var view = new Uint8Array(buf);
for (var i = 0; i < len; i++) {
  view[i] = wavString.charCodeAt(i) & 0xff;
}
var blob = new Blob([view], {type: "audio/x-wav"});