[Speex-dev] Hello Guys

Mohammed Ibrahim snouto at gmail.com
Tue Feb 13 04:52:03 PST 2007


hello everybody in this great mailing list , i have some difficulties to follow my code .

i solved some problems thanks to Carine Liang , but i still have one problem and i think it is fatal one.

when i encode the voice data in a wav file it is decoded without any errors it gives me 84 bytes wav file size for 139 kbytes wav audio data .Naturally i wanted to return my file back to its normal state by decoding it i made up the decoder , Both are the same like the samples encoder and decoder.
Then , It gives me back bad file i can't open it whereas in the samples encoder and decoder it retrieve for me the file back to its original data format.

here is my code :
for both the encoder and the decoder.
=============================
Speex Encoder ::
===========
#include "speex.h"

#include <stdio.h>

/*The frame size in hardcoded for this sample code but it doesn't have to be*/

#define FRAME_SIZE 160

int main(int argc, char **argv)

{

char *inFile;

FILE *fin;

FILE *fout;

short in[FRAME_SIZE];

float input[FRAME_SIZE];

char cbits[200];

int nbBytes;

/*Holds the state of the encoder*/

void *state;

/*Holds bits so they can be read and written to by the Speex routines*/

SpeexBits bits;

int i, tmp;

/*Create a new encoder state in narrowband mode*/

state = speex_encoder_init(&speex_nb_mode);

/*Set the quality to 8 (15 kbps)*/

tmp=8;

speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);

//inFile = argv[1];

inFile = new char[10];

inFile = "ahmed.wav";

fin = fopen(inFile, "r");

fout = fopen("ahmed2.wav","w");

/*Initialization of the structure that holds the bits*/

speex_bits_init(&bits);

while (1)

{

/*Read a 16 bits/sample audio frame*/

fread(in, sizeof(short), FRAME_SIZE, fin);

if (feof(fin))

break;

/*Copy the 16 bits values to float so Speex can work on them*/

for (i=0;i<FRAME_SIZE;i++)

input[i]=in[i];

/*Flush all the bits in the struct so we can encode a new frame*/

speex_bits_reset(&bits);

/*Encode the frame*/

speex_encode(state, input, &bits);

/*Copy the bits to an array of char that can be written*/

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

/*Write the size of the frame first. This is what sampledec expects but

it's likely to be different in your own application*/

fwrite(&nbBytes, sizeof(int), 1, fout/*stdout*/);

/*Write the compressed data*/

fwrite(cbits, 1, nbBytes, fout/*stdout*/);


}


/*Destroy the encoder state*/

speex_encoder_destroy(state);

/*Destroy the bit-packing struct*/

speex_bits_destroy(&bits);

fclose(fin);

fclose(fout);

//delete inFile;

return 0;

}



Here is the decoder  ::

================

// consSpexDec.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "speex.h"

#include <stdio.h>

#define FRAME_SIZE 160

int _tmain(int argc, _TCHAR* argv[])

{

char *outFile;

FILE *fout;

FILE *fin;

/*Holds the audio that will be written to file (16 bits per sample)*/

short out[FRAME_SIZE];

/*Speex handle samples as float, so we need an array of floats*/

float output[FRAME_SIZE];

char cbits[200];

int nbBytes;

/*Holds the state of the decoder*/

void *state;

/*Holds bits so they can be read and written to by the Speex routines*/

SpeexBits bits;

int i, tmp;

/*Create a new decoder state in narrowband mode*/

state = speex_decoder_init(&speex_nb_mode);

/*Set the perceptual enhancement on*/

tmp=1;

speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);

//outFile = argv[1];

outFile = "ahmed3.wav\0";

fout = fopen(outFile, "w");

fin = fopen("ahmed2.wav","r");

/*Initialization of the structure that holds the bits*/

speex_bits_init(&bits);

while (1)

{

/*Read the size encoded by sampleenc, this part will likely be 

different in your application*/

fread(&nbBytes, sizeof(int), 1, fin/*stdin*/);

fprintf (stderr, "nbBytes: %d\n", nbBytes);

if (feof(fin/*stdin*/))

break;


/*Read the "packet" encoded by sampleenc*/

fread(cbits, 1, nbBytes, fin/*stdin*/);

/*Copy the data into the bit-stream struct*/

speex_bits_read_from(&bits, cbits, nbBytes);

/*Decode the data*/

speex_decode(state, &bits, output);

/*Copy from float to short (16 bits) for output*/

for (i=0;i<FRAME_SIZE;i++)

out[i]=output[i];

/*Write the decoded audio to file*/

fwrite(out, sizeof(short), FRAME_SIZE, fout);

}


/*Destroy the decoder state*/

speex_decoder_destroy(state);

/*Destroy the bit-stream truct*/

speex_bits_destroy(&bits);

fclose(fout);

return 0;

}



Please help me guys please
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.xiph.org/pipermail/speex-dev/attachments/20070213/a0aaa13e/attachment.htm


More information about the Speex-dev mailing list