[Speex-dev] Audio file is corrupted after decoding

raneezn at icare4u.frih.org raneezn at icare4u.frih.org
Wed Mar 14 09:02:58 PDT 2012


I successfully encoded and decoded a wav file of PCM 16 , MONO and with
sample rate 8000 in Android OS. The size of the original wav file and
decoded file is near.
But i am not able to play the decoded audio file, the mediaplayer says
that the file has been corrupted.

ENCODING:

#include <jni.h>
#include <stdio.h>
#include "speex/speex.h"

 #define FRAME_SIZE 320


void Java_com_m2_iSmartDm_ISmartDMActivity_spxEnc(JNIEnv * env, jobject
jobj,jstring dir1,jstring dir2)
{
	const char *inFile= (*env)->GetStringUTFChars(env,dir1,0);
	const char *outFile= (*env)->GetStringUTFChars(env,dir2,0);


	 FILE *fin;
	 FILE *fout;
	 short in[FRAME_SIZE];
	 float input[FRAME_SIZE];
	 char cbits[360];
	 int nbBytes;


	 void *state;

	 SpeexBits bits;
	 int i, tmp;


	 state = speex_encoder_init(&speex_wb_mode);


	 tmp=8;
	 speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);

	  fin = fopen(inFile, "r");
	  fout=fopen(outFile,"w");
	  speex_bits_init(&bits);

	  int bool=1;


	  while (!feof(fin))
	  {
		  if(bool==1)
		  {
		    /*  skip wav header */
		    fseek ( fin , 44 , SEEK_SET );
		    bool=0;
		  }

	  	/*Read a 16 bits/sample audio frame*/
	  	 fread(in, sizeof(short), FRAME_SIZE, fin);
	 	 if (feof(fin))
	 		 break;


	 	 speex_bits_reset(&bits);


	 	 speex_encode_int(state, in, &bits);

		  nbBytes = speex_bits_write(&bits, cbits, 360);


	 	 fwrite(&nbBytes, sizeof(int), 1, fout);

	 	 /*Write the compressed data*/
	 	 fwrite(cbits, 1, nbBytes, fout);

	  }

	  speex_encoder_destroy(state);
	  speex_bits_destroy(&bits);
	  fclose(fin);
	  fclose(fout);

}


DECODING:

#include <jni.h>
#include <stdio.h>
#include "speex/speex.h"

 #define FRAME_SIZE 320


void Java_com_m2_iSmartDm_ISmartDMActivity_spxDec(JNIEnv * env, jobject
jobj,jstring dir1,jstring dir2)
{
	const char *inFile= (*env)->GetStringUTFChars(env,dir1,0);
	const char *outFile= (*env)->GetStringUTFChars(env,dir2,0);
	FILE *fin;
	FILE *fout;

 short out[FRAME_SIZE];

 short output[FRAME_SIZE];
 char cbits[360];
 int nbBytes;

 void *state;

 SpeexBits bits;
 int i, tmp;
 jbyteArray bytes;

  state = speex_decoder_init(&speex_wb_mode);


  tmp=1;
  speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);

  fin = fopen(inFile, "r");
  fout=fopen(outFile,"w");

  speex_bits_init(&bits);


  while (!feof(fin))
   {


   	fread(&nbBytes, sizeof(int), 1, fin);
   	if (feof(stdin))
   	 break;


   	fread(cbits, 1, nbBytes, fin);


  	speex_bits_read_from(&bits, cbits, nbBytes);


  	speex_decode_int(state, &bits, output);



    	fwrite(output, sizeof(short), FRAME_SIZE, fout);
   }

   speex_decoder_destroy(state);

   speex_bits_destroy(&bits);
   fclose(fout);
   fclose(fin);


}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: spx_enc.c
Type: application/octet-stream
Size: 1398 bytes
Desc: not available
Url : http://lists.xiph.org/pipermail/speex-dev/attachments/20120314/a1cd9577/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: spx_dec.c
Type: application/octet-stream
Size: 1167 bytes
Desc: not available
Url : http://lists.xiph.org/pipermail/speex-dev/attachments/20120314/a1cd9577/attachment-0001.obj 


More information about the Speex-dev mailing list