title: speex support in android
body: <p>Can anybody help me on <strong>how to use speex or jspeex in android?</strong></p>
  
  <p>I have searched a lot but could not able to find anywhere.There are many issues regarding this in <a href="http://code.google.com/p/android/issues/detail?can=4&amp;q=&amp;colspec=I&amp;groupby=&amp;sort=&amp;id=354#makechanges" rel="nofollow noreferrer">code.google.com/android</a> but none have answered it. Here also this question did not got a good response as my another question regarding this is <a href="https://stackoverflow.com/questions/9029977/decoding-speex-encoded-byte-array-in-android-jniwrapper-for-speex-decodingsp">Decoding speex encoded byte array in Android</a>. So If you know something about this then Please provide me information regarding this.</p>
  
  <p>I need to encode and decode bytearray of audio file using this codec.</p>
  
  <p>I have tried <a href="http://andrewbrobinson.com/2011/11/28/a-jni-wrapper-for-speex-on-android/comment-page-1/#comment-1352" rel="nofollow noreferrer">Android-ndk and got encoding done,</a> but getting a <strong>problem in decoding the byte array.</strong> Is there any other alternative to achieve this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>my encoding Functions in native c file are as follow:</p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include "speex/speex.h"
  
  #define FRAME_SIZE 320
  
  int nbBytes;
  /*Holds the state of the encoder*/
  void *state;
  void *decod_state;
  
  
  /*Holds bits so they can be read and written to by the Speex routines*/
  
  SpeexBits decod_bits;
  SpeexBits bits;
  int i, tmp;
  
  void Java_com_mycom_speex_SpeexEncodingActivity_init(JNIEnv * env, jobject jobj) {
     /*Create a new encoder state in narrowband mode*/
     state = speex_encoder_init(&amp;speex_wb_mode);
  
     /*Set the quality to 8*/
     tmp=8;
     speex_encoder_ctl(state, SPEEX_SET_QUALITY, &amp;tmp);
  
     /*Initialization of the structure that holds the bits*/
     speex_bits_init(&amp;bits);
  }
  
  jbyteArray Java_com_mycom_speex_SpeexEncodingActivity_encode(JNIEnv * env, jobject jobj, jshortArray inputData) {
          jbyteArray ret;
  
          jshort * inputArrayElements = (*env)-&gt;GetShortArrayElements(env, inputData, 0);
  
          /*Flush all the bits in the struct so we can encode a new frame*/
          speex_bits_reset(&amp;bits);
  
          /*Encode the frame*/
          speex_encode_int(state, inputArrayElements, &amp;bits);
          /*Copy the bits to an array of char that can be written*/
          nbBytes = speex_bits_nbytes(&amp;bits);
  
          ret = (jbyteArray) ((*env)-&gt;NewByteArray(env, nbBytes));
          jbyte * arrayElements = (*env)-&gt;GetByteArrayElements(env, ret, 0);
  
          speex_bits_write(&amp;bits, arrayElements, nbBytes);
  
          (*env)-&gt;ReleaseShortArrayElements(env, inputData, inputArrayElements, JNI_ABORT);
          (*env)-&gt;ReleaseByteArrayElements(env, ret, arrayElements, 0);
          return ret;
  }
  </code></pre>
  
  <p>now for decoding i am sending the converted short array to <strong>decode function as follow:</strong></p>
  
  <pre><code>void Java_com_mycom_speex_SpeexEncodingActivity_initDecode(JNIEnv * env,
          jobject jobj) {
  
      decod_state = speex_decoder_init(&amp;speex_wb_mode);
  
      tmp = 1;
      speex_decoder_ctl(decod_state, SPEEX_SET_ENH, &amp;tmp);
  
      /*Initialization of the structure that holds the bits*/
      speex_bits_init(&amp;decod_bits);
  }
  
  jshortArray Java_com_mycom_speex_SpeexEncodingActivity_decode(JNIEnv * env,
          jobject jobj, jshortArray inputData) {
  
  
  
      jshort * inputArrayElements = (*env)-&gt;GetShortArrayElements(env, inputData,
              0);
  
      /*Flush all the bits in the struct so we can decode a new frame*/
      speex_bits_reset(&amp;decod_bits);
      /*Copy the bits to an array of char that can be written*/
      nbBytes = speex_bits_nbytes(&amp;decod_bits);
      speex_bits_read_from(&amp;decod_bits,inputArrayElements, nbBytes); // here it requires char * in second argument
      /*Decode the frame*/
      speex_decode_int(decod_state, &amp;decod_bits, inputArrayElements);
      (*env)-&gt;ReleaseShortArrayElements(env, encodedData, inputArrayElements,
              JNI_ABORT);
      return inputArrayElements;
  }
  </code></pre>
  
  <p>my Encoding functions are working fine the example is provided on the blog <a href="http://andrewbrobinson.com/2011/11/28/a-jni-wrapper-for-speex-on-android/comment-page-1/#comment-1352" rel="nofollow noreferrer">A JNI Wrapper for Speex on Android</a></p>
  
  <p><strong>Another attempt to decoding by passing char array and returning short array is as follow:</strong></p>
  
  <pre><code>jshortArray Java_com_mycom_speex_SpeexEncodingActivity_decode(JNIEnv * env,
          jobject jobj, jcharArray inputCharData) {
  
      jshortArray ret;
      jchar * inputArrayElements = (*env)-&gt;GetCharArrayElements(env,
              inputCharData, 0);
      /*Flush all the bits in the struct so we can decode a new frame*/
      speex_bits_reset(&amp;decod_bits);
      /*Copy the bits to an array of char that can be written*/
      nbBytes = speex_bits_nbytes(&amp;decod_bits);
      ret = (jshortArray)((*env)-&gt;NewShortArray(env, nbBytes));
      jshort * arrayElements = (*env)-&gt;GetShortArrayElements(env, ret, 0);
  
      speex_bits_read_from(&amp;decod_bits,(char *) inputArrayElements, nbBytes);
      /*Decode the frame*/
      speex_decode_int(decod_state, &amp;decod_bits, arrayElements);
  
      (*env)-&gt;ReleaseCharArrayElements(env, inputCharData, inputArrayElements,
              JNI_ABORT);
      (*env)-&gt;ReleaseShortArrayElements(env, ret, arrayElements, 0);
      return ret;
  }
  </code></pre>
  
  <p>the result is </p>
  
  <pre><code>Returned empty array of short if i return ret and if i return arrayElements it 
  gives an error Fatal signal 11 (SIGSEGV) at 0x00000018 (code=1)
  </code></pre>