imaya / zlib.js

compact zlib, deflate, inflate, zip library in JavaScript
Other
1.13k stars 351 forks source link

Can it compress Int16Array WebAudio pcm data? #51

Open Fallsleep opened 7 years ago

Fallsleep commented 7 years ago

I want to compress the pcm data produce by WebAudio API, it is Int16Array.

Can I use zlib.js?

CWBudde commented 7 years ago

It should be possible to use the underlying array buffer and cast it to an UInt8Array.

Something as simple as

var a = new UInt8Array(YourInt16Array);

should already do the trick.

However, speaking of the WebAudio API (where the data is often available as Float32Array), it might make sense to use a different intermediate representation. Personally I have made success converting the data into something like a Float16Array by using the inofficial half point floating point format where the converted bytes can be stored directly as a UInt8Array. Conversion from floating point to floating point format will better preserve the quality and suits better for audio (where the ear operates in a similar non-linear fashion as the exponential floating point format).

Fallsleep commented 7 years ago

I tried to compress pcm data such as :

var udata = new Uint8Array(data);// data is the pcm audio data
var gzip = new Zlib.Gzip(udata);
var udatac = gzip.compress();
data = udatac.buffer;
xhr.send(data);

For the code above, I got http://wx4.sinaimg.cn/large/715d71fdgy1fhyi6x3kpkj206x062mxl.jpg this result.The compressed Uint8Array's byteLength and the buffer's byteLength not the same. Should I slice it myself? Or I done something wrong? I think if the origin data were not enough? I'll try it tomorrow.