At a quick glance, I can see two possible problems:<br><br>1) The files should be opened in binary mode ("rb" on the sending side, "wb" on the receiving side). This will only be an issue on Windows, whic treats text and binary files differentlly. On Linux, Unix and OS X this shouldn't matter.
<br><br>2) A wav file has a header on it. You should either read and process the header, or fseek() past it when you read the file. Otherwise you'll be treating the header as data (44 bytes, if I recall correctly). More importantly, you also need to write the header when you save your output file on the receiving side, otherwise the application you use to play the file won't know things like the samping rate, the bit depth, the number of channels and so on.
<br><br><br><br><br><div><span class="gmail_quote">On 2/13/07, <b class="gmail_sendername">Mohammed Ibrahim</b> <<a href="mailto:snouto@gmail.com">snouto@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div bgcolor="#ffffff">
<div><font face="Arial" size="2">hello everybody in this great mailing list , i have
some difficulties to follow my code .</font></div>
<div><font face="Arial" size="2"></font> </div>
<div><font face="Arial" size="2">i solved some problems thanks to Carine Liang , but
i still have one problem and i think it is fatal one.</font></div>
<div><font face="Arial" size="2"></font> </div>
<div><font face="Arial" size="2">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.</font></div>
<div><font face="Arial" size="2">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.</font></div>
<div><font face="Arial" size="2"></font> </div>
<div><font face="Arial" size="2">here is my code :</font></div>
<div><font face="Arial" size="2">for both the encoder and the decoder.</font></div>
<div><font face="Arial" size="2">=============================</font></div>
<div><font face="Arial" size="2">Speex Encoder ::</font></div>
<div><font face="Arial" size="2">===========</font></div>
<div><font face="Arial" size="2"><font color="#0000ff" size="2">
<p>#include</p></font><font size="2"> "speex.h"</font><font color="#0000ff" size="2">
<p>#include</p></font><font size="2"> <stdio.h></font><font color="#008000" size="2">
<p>/*The frame size in hardcoded for this sample code but it doesn't have to
be*/</p></font><font color="#0000ff" size="2">
<p>#define</p></font><font size="2"> FRAME_SIZE 160</font><font color="#0000ff" size="2">
<p>int</p></font><font size="2"> main(</font><font color="#0000ff" size="2">int</font><font size="2"> argc, </font><font color="#0000ff" size="2">char</font><font size="2"> **argv)
<p>{</p>
<p></p></font><font color="#0000ff" size="2">char</font><font size="2"> *inFile;
<p>FILE *fin;</p>
<p>FILE *fout;</p>
<p></p></font><font color="#0000ff" size="2">short</font><font size="2">
in[FRAME_SIZE];
<p></p></font><font color="#0000ff" size="2">float</font><font size="2">
input[FRAME_SIZE];
<p></p></font><font color="#0000ff" size="2">char</font><font size="2"> cbits[200];
<p></p></font><font color="#0000ff" size="2">int</font><font size="2"> nbBytes;
<p></p></font><font color="#008000" size="2">/*Holds the state of the
encoder*/</font><font size="2">
<p></p></font><font color="#0000ff" size="2">void</font><font size="2"> *state;
<p></p></font><font color="#008000" size="2">/*Holds bits so they can be read and
written to by the Speex routines*/</font><font size="2">
<p>SpeexBits bits;</p>
<p></p></font><font color="#0000ff" size="2">int</font><font size="2"> i, tmp;
<p></p></font><font color="#008000" size="2">/*Create a new encoder state in narrowband
mode*/</font><font size="2">
<p>state = speex_encoder_init(&speex_nb_mode);</p>
<p></p></font><font color="#008000" size="2">/*Set the quality to 8 (15
kbps)*/</font><font size="2">
<p>tmp=8;</p>
<p>speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);</p>
<p></p></font><font color="#008000" size="2">//inFile = argv[1];</font><font size="2">
<p>inFile = </p></font><font color="#0000ff" size="2">new</font><font size="2">
</font><font color="#0000ff" size="2">char</font><font size="2">[10];
<p>inFile = "ahmed.wav";</p>
<p>fin = fopen(inFile, "r");</p>
<p>fout = fopen("ahmed2.wav","w");</p>
<p></p></font><font color="#008000" size="2">/*Initialization of the structure that
holds the bits*/</font><font size="2">
<p>speex_bits_init(&bits);</p>
<p></p></font><font color="#0000ff" size="2">while</font><font size="2"> (1)
<p>{</p>
<p></p></font><font color="#008000" size="2">/*Read a 16 bits/sample audio
frame*/</font><font size="2">
<p>fread(in, </p></font><font color="#0000ff" size="2">sizeof</font><font size="2">(</font><font color="#0000ff" size="2">short</font><font size="2">),
FRAME_SIZE, fin);
<p></p></font><font color="#0000ff" size="2">if</font><font size="2"> (feof(fin))
<p></p></font><font color="#0000ff" size="2">break</font><font size="2">;
<p></p></font><font color="#008000" size="2">/*Copy the 16 bits values to float so Speex
can work on them*/</font><font size="2">
<p></p></font><font color="#0000ff" size="2">for</font><font size="2">
(i=0;i<FRAME_SIZE;i++)
<p>input[i]=in[i];</p>
<p></p></font><font color="#008000" size="2">/*Flush all the bits in the struct so we
can encode a new frame*/</font><font size="2">
<p>speex_bits_reset(&bits);</p>
<p></p></font><font color="#008000" size="2">/*Encode the frame*/</font><font size="2">
<p>speex_encode(state, input, &bits);</p>
<p></p></font><font color="#008000" size="2">/*Copy the bits to an array of char that
can be written*/</font><font size="2">
<p>nbBytes = speex_bits_write(&bits, cbits, 200);</p>
<p></p></font><font color="#008000" size="2">/*Write the size of the frame first. This
is what sampledec expects but
<p>it's likely to be different in your own application*/</p></font><font size="2">
<p>fwrite(&nbBytes, </p></font><font color="#0000ff" size="2">sizeof</font><font size="2">(</font><font color="#0000ff" size="2">int</font><font size="2">), 1,
fout</font><font color="#008000" size="2">/*stdout*/</font><font size="2">);
<p></p></font><font color="#008000" size="2">/*Write the compressed
data*/</font><font size="2">
<p>fwrite(cbits, 1, nbBytes, fout</p></font><font color="#008000" size="2">/*stdout*/</font><font size="2">);
<p></p>
<p>}</p>
<p></p>
<p></p></font><font color="#008000" size="2">/*Destroy the encoder
state*/</font><font size="2">
<p>speex_encoder_destroy(state);</p>
<p></p></font><font color="#008000" size="2">/*Destroy the bit-packing
struct*/</font><font size="2">
<p>speex_bits_destroy(&bits);</p>
<p>fclose(fin);</p>
<p>fclose(fout);</p>
<p></p></font><font color="#008000" size="2">//delete inFile;</font><font size="2">
<p></p></font><font color="#0000ff" size="2">return</font><font size="2"> 0;
</font></font><p><font face="Arial" size="2"><font size="2">}</font></font></p>
<p><font face="Arial" size="2"><font size="2"> </font></font></p>
<p><font face="Arial" size="2"><font size="2">Here is the decoder ::</font></font></p>
<p><font face="Arial" size="2"><font size="2">================</font></font></p><font face="Arial" size="2"><font size="2"><font color="#008000" size="2">
<p>// consSpexDec.cpp : Defines the entry point for the console application.</p>
<p>//</p></font><font color="#0000ff" size="2">
<p>#include</p></font><font size="2"> "stdafx.h"</font><font color="#0000ff" size="2">
<p>#include</p></font><font size="2"> "speex.h"</font><font color="#0000ff" size="2">
<p>#include</p></font><font size="2"> <stdio.h></font><font color="#0000ff" size="2">
<p>#define</p></font><font size="2"> FRAME_SIZE 160</font><font color="#0000ff" size="2">
<p>int</p></font><font size="2"> _tmain(</font><font color="#0000ff" size="2">int</font><font size="2"> argc, _TCHAR* argv[])
<p>{</p>
<p></p></font><font color="#0000ff" size="2">char</font><font size="2"> *outFile;
<p>FILE *fout;</p>
<p>FILE *fin;</p>
<p></p></font><font color="#008000" size="2">/*Holds the audio that will be written to
file (16 bits per sample)*/</font><font size="2">
<p></p></font><font color="#0000ff" size="2">short</font><font size="2">
out[FRAME_SIZE];
<p></p></font><font color="#008000" size="2">/*Speex handle samples as float, so we need
an array of floats*/</font><font size="2">
<p></p></font><font color="#0000ff" size="2">float</font><font size="2">
output[FRAME_SIZE];
<p></p></font><font color="#0000ff" size="2">char</font><font size="2"> cbits[200];
<p></p></font><font color="#0000ff" size="2">int</font><font size="2"> nbBytes;
<p></p></font><font color="#008000" size="2">/*Holds the state of the
decoder*/</font><font size="2">
<p></p></font><font color="#0000ff" size="2">void</font><font size="2"> *state;
<p></p></font><font color="#008000" size="2">/*Holds bits so they can be read and
written to by the Speex routines*/</font><font size="2">
<p>SpeexBits bits;</p>
<p></p></font><font color="#0000ff" size="2">int</font><font size="2"> i, tmp;
<p></p></font><font color="#008000" size="2">/*Create a new decoder state in narrowband
mode*/</font><font size="2">
</font></font></font><p><font face="Arial" size="2"><font size="2"><font size="2">state = speex_decoder_init(&speex_nb_mode);</font></font></font></p>
<p></p><font face="Arial" size="2"><font size="2"><font color="#008000" size="2">/*Set the perceptual enhancement
on*/</font><font size="2">
<p>tmp=1;</p>
<p>speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);</p>
<p></p></font><font color="#008000" size="2">//outFile = argv[1];</font><font size="2">
<p>outFile = "ahmed3.wav\0";</p>
<p>fout = fopen(outFile, "w");</p>
<p>fin = fopen("ahmed2.wav","r");</p>
<p></p></font><font color="#008000" size="2">/*Initialization of the structure that
holds the bits*/</font><font size="2">
<p>speex_bits_init(&bits);</p>
<p></p></font><font color="#0000ff" size="2">while</font><font size="2"> (1)
<p>{</p>
<p></p></font><font color="#008000" size="2">/*Read the size encoded by sampleenc, this
part will likely be
<p>different in your application*/</p></font><font size="2">
<p>fread(&nbBytes, </p></font><font color="#0000ff" size="2">sizeof</font><font size="2">(</font><font color="#0000ff" size="2">int</font><font size="2">), 1,
fin</font><font color="#008000" size="2">/*stdin*/</font><font size="2">);
<p>fprintf (stderr, "nbBytes: %d\n", nbBytes);</p>
<p></p></font><font color="#0000ff" size="2">if</font><font size="2">
(feof(fin</font><font color="#008000" size="2">/*stdin*/</font><font size="2">))
<p></p></font><font color="#0000ff" size="2">break</font><font size="2">;
<p></p>
<p></p></font><font color="#008000" size="2">/*Read the "packet" encoded by
sampleenc*/</font><font size="2">
<p>fread(cbits, 1, nbBytes, fin</p></font><font color="#008000" size="2">/*stdin*/</font><font size="2">);
<p></p></font><font color="#008000" size="2">/*Copy the data into the bit-stream
struct*/</font><font size="2">
<p>speex_bits_read_from(&bits, cbits, nbBytes);</p>
<p></p></font><font color="#008000" size="2">/*Decode the data*/</font><font size="2">
<p>speex_decode(state, &bits, output);</p>
<p></p></font><font color="#008000" size="2">/*Copy from float to short (16 bits) for
output*/</font><font size="2">
<p></p></font><font color="#0000ff" size="2">for</font><font size="2">
(i=0;i<FRAME_SIZE;i++)
<p>out[i]=output[i];</p>
<p></p></font><font color="#008000" size="2">/*Write the decoded audio to
file*/</font><font size="2">
<p>fwrite(out, </p></font><font color="#0000ff" size="2">sizeof</font><font size="2">(</font><font color="#0000ff" size="2">short</font><font size="2">),
FRAME_SIZE, fout);
<p>}</p>
<p></p>
<p></p></font><font color="#008000" size="2">/*Destroy the decoder
state*/</font><font size="2">
<p>speex_decoder_destroy(state);</p>
<p></p></font><font color="#008000" size="2">/*Destroy the bit-stream
truct*/</font><font size="2">
<p>speex_bits_destroy(&bits);</p>
<p>fclose(fout);</p>
<p></p></font><font color="#0000ff" size="2">return</font><font size="2"> 0;
<p>}</p>
<p> </p>
<p>Please help me guys please</p></font></font></font></div></div>
<br>_______________________________________________<br>Speex-dev mailing list<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:Speex-dev@xiph.org">Speex-dev@xiph.org</a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://lists.xiph.org/mailman/listinfo/speex-dev" target="_blank">
http://lists.xiph.org/mailman/listinfo/speex-dev</a><br><br></blockquote></div><br><br clear="all"><br>-- <br> Bernie Roehl<br> Software Consultant<br> Mail: <a href="mailto:broehl@gmail.com">broehl@gmail.com</a><br>
Voice: (519) 577-8494<br>