xiph / opus

Modern audio compression for the internet.
https://opus-codec.org/
Other
2.25k stars 601 forks source link

Crackled Audio - Web Assembly Compilation ( WASM ) #272

Open praveen-kd-23 opened 1 year ago

praveen-kd-23 commented 1 year ago

Hi , We have compiled opus into wasm and trying to decode an audio in browser through wasm and play it . Opus decoder produces distorted/bad audio .

Here are the steps to compile opus into wasm .

./autogen.sh

emconfigure ./configure --prefix=/home/local/ZOHOCORP/praveen-10261/opus-wasm/opus_libwasm/opus/opus/ --disable-rtcd --disable-intrinsics --disable-shared   --enable-static  --disable-stack-protector --disable-hardening

 emmake make

emmake make install

Below is the C Wrapper to call opus API's:

#include <opus/opus.h>
#include <emscripten.h>
#include <stdlib.h>

void *decoder;

void *malloc_(int size){
      return malloc(size);
}

int opus_decoder_create_(int sample_rate , int channels){
    int err;

    decoder = opus_decoder_create(sample_rate,channels,&err);

    if(err != OPUS_OK){
       return -1; 
    }

    return 1;
}

int opus_decode_(void *encoded_data,int data_len , float *decoded_data,int max_size){

    return opus_decode_float(decoder,encoded_data,data_len,decoded_data,max_size,0);
}
rillian commented 1 year ago

Buffering implementations are a common cause of playback distortion. I'd look there. In particular make sure you understand the data layout the opus_decode API uses and are advancing the read and write pointers correctly, and that the input and output buffers are big enough to avoid underrun.

You can also try dumping the decoded data and comparing the output against a native build of the same code to check for compatibility problems with the emscripten compiler, and feeding the same data to the Web Audio API decoder to eliminate issues with your data receiver.

nickolas52468 commented 1 year ago

did you REALLY got compiled to Webassembly? i´m trying for a lot of time... i need to compile then acces methods from a javascript but i´ve been found some errors.