jpemartins / speex.js

Speex codec in Javascript. Mirror from https://code.ua.pt/projects/speex-js
215 stars 64 forks source link

Trying to decode raw Int8Array #19

Closed phoboslab closed 9 years ago

phoboslab commented 9 years ago

I'm trying to stream Audio over the network via WebSockets. I'm encoding Audio using libspeex on an iOS device, which also acts as the stream server.

Encoding on iOS is set up like so:

// setup
void *speexEncoder = speex_encoder_init(&speex_nb_mode);
int quality = 8;
speex_encoder_ctl(speexEncoder, SPEEX_SET_QUALITY, &quality);

SpeexBits speexBits;
speex_bits_init(&speexBits);

// resampling step (to 8000hz, mono) omitted

// encoding
speex_bits_reset(&speexBits);
speex_encode_int(speexEncoder, (spx_int16_t*)inputBuffer, &speexBits);
size_t bytesWritten = speex_bits_write(&speexBits, outputBuffer, bufferSize);

As far as I can tell, the encoding and sending the encoded stream over the network works fine.

I'm then decoding and playing the decoded stream using WebAudio, like so:

var audioCtx = new AudioContext();
var speexDecoder = new SpeexDecoder({quality: 8, sample_rate: 8000, mode: 0});
speexDecoder.init();

websocketReceiveMessageCallback = function( event ) {
    var bytes = new Int8Array(event.data);
    var decoded = this.speexDecoder.process(bytes);

    var audioBuffer = audioCtx.createBuffer(1, decoded.length, 8000);
    var bufferData = audioBuffer.getChannelData(0);
    bufferData.set(decoded);

    var source = this.audioCtx.createBufferSource();
    source.buffer = audioBuffer;
    source.connect(this.audioCtx.destination);
    source.start();
}

However, all I can hear with this is stuttering white noise. I believe I'm doing something wrong in the decoding step, because the decoded data looks wrong. It's a Float32Array but often clamped hard against the [-1,1] limit. From the source I gathered that the Decoder's process function expects an Int8Array (as opposed to a Uint8Array for example). Is this correct?

Is there anything else I could try to decode a raw Typed Array Buffer?

phoboslab commented 9 years ago

I appologize. The issue was actually with the resampling step on iOS. I treated the in and out size for speex_resampler_process_int as byte sizes, but they actually denote the size in samples. Decoding in JS works fine now.

Thanks for making this lib :)

sassyn commented 8 years ago

Will you might share your code?

I'm trying to do the same, and I got thing working on PC - Chrome/FireFox/IE Also manage to run it on Android.

I stream a live FFMPEG MP3 or AAC using websocket and decode it on the end point.

However IOS Safari - can't decode it . I get no error, nothing.....

Maybe u could help?

wilsonssss commented 4 years ago

could u share the js code ? we need it, thank you man @phoboslab