mattdiamond / Recorderjs

A plugin for recording/exporting the output of Web Audio API nodes
4.18k stars 1.46k forks source link

Custom channel, sample rate, sample size? #11

Open heshiming opened 11 years ago

heshiming commented 11 years ago

I'm trying to put Recorderjs into my application. I noticed however, Recorderjs sends 44khz stereo 16bit wave, which is boatload of data. In my voice application, a typical requirement would be 16khz mono 16bit.

I've been digging deeper. It appears webkitAudioContext constructor do not accept arguments. And because sample rate cannot be changed once AudioContext is created, I'm stuck with the default format.

It looks like the problem to address this is to use the OfflineAudioContext, which accepts 3 argument of format when constructed. However, even though this function is internally available, it's not exposed to the Web Audio API for Javascript yet.

I'm wondering if anyone has any insight on this. Is there any way to change wave format at the moment?

cwilso commented 11 years ago

Hi He-

Recorderjs is actually just encoding the raw data that's coming from the audio system. The audio system transcodes (if necessary) the audio input to be the same as the output rate set in hardware - in your case, your output device must be 44kHz stereo 16-bit. offlineContext would give you the flexibility to control this, but hooking up audio input to that will be a little odd.

It would be pretty trivial to do a sample rate conversion on save - that is, drop your 44kHz stereo 16-bit buffers to 22kHz mono 16-bit in the "exportWAV()" function in recorderworker.js - just don't interleave the buffers, instead create a function that averages every two values in one side's array to cut the sample rate in half, and of course change encodeWAV() to drop the sample rate and change stereo->mono.

Sample rate conversion to an arbitrary rate - e.g. to specifically 16kHz - might be a bit more complex: http://en.wikipedia.org/wiki/Sample_rate_conversion.

heshiming commented 11 years ago

Thanks Chris. I just thought it would make sense to specify a recording format, given 16khz mono 16bit is the common wideband format for voice applications. Do you mean when OfflineAudioContext becomes available, there would be problems connecting the microphone input to the context?

cwilso commented 11 years ago

Yes. OfflineAudioContext is really designed to render audio in non-realtime - faster than realtime, normally. It likely will not work with media elements or getUserMedia.

kkxlkkxllb commented 11 years ago

I found the wav file exported is too large with default config,3 seconds duration cost 500kb. Common 1 sec mpeg audio just cost 5kb. The problem is the output rate of my audio devise ,right?

cwilso commented 11 years ago

Well, no, the problem is that it's uncompressed audio - we don't have a built in compressing encoder in Web Audio, and Recorderjs doesn't implement one in JavaScript (it's a big challenge, for licensing reasons as well as technical).

stereo * 16-bit (aka 2 byte) * 44.1kHz = 2 * 2 * 44100 = 176400 bytes per second. For three seconds at CD quality, that would be about 500kb. That's why a raw CD uncompressed is around 600mb.

kkxlkkxllb commented 11 years ago

@cwilso Thanks. I think i should try server-side compression for the output.

jaredfolkins commented 11 years ago

@cwilso I'm assuming many of us (and many more) are going to want client side compression. At least of some sort.

Since I'm a newb concerning the state of html5 and the audio APIs any insight into where this is headed (links)?

If a JS compressor were to be ported, would something like libvorbis be the safest route (at least where licensing is concerned)? Is this even feasible?

Thanks in advance dude.

cwilso commented 11 years ago

I expect in the relatively near term someone will port libvorbis to Javascript. Vorbis is the safest route in terms of licensing, yes - the only problem is Apple has thus far failed to ship any implementation, so it doesn't decode on Safari.

On Fri, Feb 15, 2013 at 9:34 AM, Jared Folkins notifications@github.comwrote:

@cwilso https://github.com/cwilso I'm assuming many of us (and many more) are going to want client side compression. At least of some sort.

Since I'm a newb concerning the state of html5 and the audio APIs any insight into where this is headed (links)?

If a JS compressor were to be ported, would something like libvorbis be the safest route (at least where licensing is concerned)? Is this even feasible?

Thanks in advance dude.

— Reply to this email directly or view it on GitHubhttps://github.com/mattdiamond/Recorderjs/issues/11#issuecomment-13618090.

kosso commented 11 years ago

I wonder if this work might be of use eventually : http://labs.official.fm/codecs/

sheerun commented 10 years ago

What about libopus?