[Speex-dev] Hello guys Please help

Mohammed Ibrahim snouto at gmail.com
Tue Feb 13 05:15:30 PST 2007


Why my decoder doesn't want to return the data back to its original wav audio file that i can playback it correctly , is there something wrong in my code .


Encoder :
=======
#include "speex.h"

#include <stdio.h>

#include <iostream>

#include <conio.h>

using namespace std;

/*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 = "snouto.wav";

fin = fopen(inFile, "r");

fout = fopen("snouto2.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;

if(ferror(fin))

{

std::cout << "There was an error snouto " <<std::endl;

getch();

}

/*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;

}





decoder :

======

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

//

#include "stdafx.h"

#include "speex.h"

#include <stdio.h>

#include <conio.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("snouto2.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*/))

{

getch();

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;

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.xiph.org/pipermail/speex-dev/attachments/20070213/3581e5fc/attachment.htm


More information about the Speex-dev mailing list