[Speex-dev] just noise

zmorris at mac.com zmorris at mac.com
Sat Apr 21 11:36:04 PDT 2007

Hi, I tried both the stable and beta versions of the speex source  
code download on Mac OS 10.4.9.  I just do:

sudo make install

Then I added libspeex.a from /usr/local/lib and the headers to my  
xcode project.  My app compiles and I'm able to call all of the speex  
functions.  I copied the example code from the website and tweaked it  
to include the first 10000 bytes of the female.wav file (to include  
the header).

However, when I play the output file, I get the header and a second  
of audio, but the rest is just noise.  I've stared at it for hours  
and tried every combination of quality settings but it just doesn't  
work.  Can someone please tell me what I am doing wrong?  Thanx, and  
here is the code:



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

#define FRAME_SIZE 160

int	main( void )
	char *inFile = "female.wav";
	char *tempFile = "compressed.wav";
	char *outFile = "speex-result.wav";
	FILE *fin;
	FILE *fout;
	char	header[10000];	// storage for wav header
	short in[FRAME_SIZE];
	float input[FRAME_SIZE];
	/*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 encoder*/
	void *state;
	/*Holds bits so they can be read and written to by the Speex routines*/
	SpeexBits bits;
	int i, tmp;
	//////////////////// encoder ////////////////////
	/*Create a new encoder state in narrowband mode*/
	state = speex_encoder_init(&speex_nb_mode);

	/*Set the quality to 8 (15 kbps)*/
	speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);

	//inFile = argv[1];
	fin = fopen(inFile, "r");
	if( !fin ) return( -1 );	// couldn't find file
	fread(header, sizeof(char), 10000, fin);	// get wav header
	fout = fopen(tempFile, "w");

	/*Initialization of the structure that holds the 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++)

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

		/*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  
		it's likely to be different in your own application*/
		fwrite(&nbBytes, sizeof(int), 1, fout);
		/*Write the compressed data*/
		fwrite(cbits, 1, nbBytes, fout);

	/*Destroy the encoder state*/
	/*Destroy the bit-packing struct*/
	//////////////////// decoder ////////////////////
	/*Create a new decoder state in narrowband mode*/
	state = speex_decoder_init(&speex_nb_mode);

	/*Set the perceptual enhancement on*/
	speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);

	//outFile = argv[1];
	fin = fopen(tempFile, "r");
	fout = fopen(outFile, "w");
	fwrite(header, sizeof(char), 10000, fout);
	/*Initialization of the structure that holds the bits*/
	while (1)
		/*Read the size encoded by sampleenc, this part will likely be
		different in your application*/
		fread(&nbBytes, sizeof(int), 1, fin);
		fprintf (stderr, "nbBytes: %d\n", nbBytes);
		if (feof(fin))

		/*Read the "packet" encoded by sampleenc*/
		fread(cbits, 1, nbBytes, fin);
		/*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++)

		/*Write the decoded audio to file*/
		fwrite(out, sizeof(short), FRAME_SIZE, fout);

	/*Destroy the decoder state*/
	/*Destroy the bit-stream truct*/

More information about the Speex-dev mailing list