[Speex-dev] Speex and DS

Rafal Bielec rafal at dial.net.pl
Thu Jun 23 03:38:38 PDT 2005


Thank you for the quick response Thorvald, but I think that's not the 
problem here :[
I know how to capture the buffer and how to play it in the output buffer of 
the DS.
The problem is (probably) with same kind of short/floats/bytes error in 
conversion/copying
that the coder doesn't get.
I can have my buffer locked during the compression, it not the problem at 
the moment.
What I really am focusing right now on is to get the same sound out that 
went into the speex codec.
This is what I cannot do :[.
If could look at the code or send me a piece that compresses VOID* buffer to 
chars and in reverse,
because I think there's something omitted in the speex manual.
Still everything works without the codec, with the buffer set to 
4*160*sizeof(short)

Rafal

////////////////CODE///////////

// These should be equal
if( dwDSOutputLockedBufferSize != dwDSCaptureLockedBufferSize )
return E_FAIL; // Sanity check unhandled case

int size = dwDSOutputLockedBufferSize; //size in bytes

char* buffer = new char[size]; // buffer of bytes

CopyMemory( buffer, //fill the buffer
pDSCaptureLockedBuffer,
dwDSOutputLockedBufferSize );

void* enc_state; //states
void* dec_state;

#define FRAME_SIZE 160 //frame size in shorts,
#define BUFFERSIZE 4*FRAME_SIZE*sizeof(short)
#define SS sizeof(short)

short in[FRAME_SIZE];
float input[FRAME_SIZE];

int nbBytesenc;
int nbBytesdec;

SpeexBits bitsenc; //bits for encoding
SpeexBits bitsdec; //bits for decoding

enc_state = speex_encoder_init(&speex_nb_mode);
int tmp=10;
speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);
speex_bits_init(&bitsenc);

FILE* fout = fopen("_ee.raw","w");
char* pbuffer = buffer;
int nFrames = BUFFERSIZE/FRAME_SIZE;
char cbitsenc[200];
for(int i = 0 ; i < nFrames ; i++)
{
memcpy(in,pbuffer,FRAME_SIZE*SS); //fill in array with shorts
pbuffer+=FRAME_SIZE*SS; // from array of char

for (i=0;i<FRAME_SIZE;i++)
        input[i]=in[i];

speex_bits_reset(&bitsenc);
speex_encode(enc_state, input, &bitsenc);

/*Copy the bits to an array of char that can be written*/
nbBytesenc = speex_bits_write(&bitsenc, cbitsenc, 200);
fwrite(&nbBytesenc, sizeof(int), 1, fout);

/*Write the compressed data*/
fwrite(cbitsenc, 1, nbBytesenc, fout);

}
fclose(fout);
speex_encoder_destroy(enc_state);

/*Destroy the bit-packing struct*/
speex_bits_destroy(&bitsenc);

/* decoding */
FILE* fin = fopen("_ee.raw","r");  //get the file

char cbitsdec[200];
short out[FRAME_SIZE];
float output[FRAME_SIZE];
dec_state = speex_decoder_init(&speex_nb_mode);

speex_bits_init(&bitsdec);
char* buffer2 = new char[size]; // the output buffer
char* pbuffer2 = buffer2; // pointer to the output buffer
while (1)
{

//Read the size encoded by sampleenc, this part will likely be
//different in your application
fread(&nbBytesdec, sizeof(int), 1, fin);
    if (feof(fin))
        break;

//Read the "packet" encoded by sampleenc
    fread(cbitsdec, 1, nbBytesdec, fin);

//Copy the data into the bit-stream struct
    speex_bits_read_from(&bitsdec, cbitsdec, nbBytesdec);

//Decode the data
    speex_decode(dec_state, &bitsdec, output);

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

for (i=0;i<FRAME_SIZE;i++)
out[i]=output[i];

//write the decoded bytes to buffer2
memcpy(pbuffer2,out,FRAME_SIZE*SS);
pbuffer2+=FRAME_SIZE*SS;
}
fclose(fin);

speex_decoder_destroy(dec_state);
/*Destroy the bit-packing struct*/
speex_bits_destroy(&bitsdec);
/* end of decoding */

/*Fill output buffer with the buffer2 that was created from the file */
CopyMemory( pDSOutputLockedBuffer,
    buffer2 ,
dwDSOutputLockedBufferSize
);

delete[] buffer;
delete[] buffer2;

// Unlock the capture buffer 




More information about the Speex-dev mailing list