[Speex-dev] Problem with encoding and decoding

Jean-Marc Valin jean-marc.valin at usherbrooke.ca
Thu Oct 12 22:44:55 PDT 2006


How about starting from the sampleenc/sampledec examples in the manual
(or directly from speexenc/speexdec). I'm not an automatic on-line
debugger (nor is anyone else on this list I'm sure).

	Jean-Marc

Tomasz Walenczak wrote:
> I have problem with coding and decoding.  I record voice from mic coding
> and write to file.
> And when I read from file and put it on sound card it's terrible sound.
> Have sameone can help me?
> 
> #include <speex.h>
> #include <stdio.h>
> #include <fcntl.h>
> #include <sys/ioctl.h>
> #include <sys/soundcard.h>
> 
> /*The frame size in hardcoded for this sample code but it doesn't have
> to be*/
> #define FRAME_SIZE 160
> #define false -1
> int    card;
> 
> int main(int argc, char **argv)
> {
>    //char *inFile;
>    FILE *fin;
>    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;
>     char file[100];
> int channels,format;
> 
> 
>    
>     while (argc>1){
>         argc--;
>         argv++;
>         if(!strncmp(argv[0],"-f",2)){
>                 sscanf(argv[1], "%s", (char*)&file);
>                 argc--;
>                 argv++;
>         };
>     }
> 
>     card=open("/dev/dsp", O_RDWR, 0);
>     if (card<0) {
>  
>     return false;
>     }
>     else{
>     channels=1;
>     if (ioctl (card, SNDCTL_DSP_CHANNELS, &channels)==-1) {
>     return false;
>     }
>     format = AFMT_S16_NE;
>     tmp = ioctl (card, SNDCTL_DSP_SETFMT, &format);
>     if (tmp==-1) {
>     return false;
>     }
>     // set speed
>     tmp = 16000;
>     ioctl(card, SNDCTL_DSP_SPEED, &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];
>    fin = fopen(file, "w");
> 
>    /*Initialization of the structure that holds the bits*/
>    speex_bits_init(&bits);
>    while (1)
>    {
>       /*Read a 16 bits/sample audio frame*/
>       read(card,in,FRAME_SIZE);
>       //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, fin);
>       /*Write the compressed data*/
>       fwrite(cbits, 1, nbBytes, fin);
>  
>    }
>   
>    /*Destroy the encoder state*/
>    speex_encoder_destroy(state);
>    /*Destroy the bit-packing struct*/
>    speex_bits_destroy(&bits);
>    fclose(fin);
>    return 0;
> }
> 
> 
> 
> and decoding
> 
> 
> 
> #include <speex.h>
> #include <stdio.h>
> #include <sys/ioctl.h>
> #include <sys/soundcard.h>
> #include <fcntl.h>
> 
> /*The frame size in hardcoded for this sample code but it doesn't have
> to be*/
> #define FRAME_SIZE 160
> #define false -1
> int card;
> int main(int argc, char **argv)
> {
>    //char *outFile;
>    FILE *fout;
>    /*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,channels,format;
>    char    path[255];
>    /*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;
> 
>     while (argc>1){
>     argc--;
>     argv++;
>     if(!strncmp(argv[0],"-f",2)){
>         sscanf(argv[1], "%s", (char*)&path);
>         argc--;
>         argv++;
>          };
>     }
> 
>     card=open("/dev/dsp", O_RDWR, 0);
>     if (card<0) {
>     return false;
>     }
>     else{
>     channels=1;
>     if (ioctl (card, SNDCTL_DSP_CHANNELS, &channels)==-1) {
>     return false;
>     }
>     format = AFMT_S16_NE;
>     tmp = ioctl (card, SNDCTL_DSP_SETFMT, &format);
>     if (tmp==-1) {
>     return false;
>     }
>     // set speed
>     tmp = 16000;
>     ioctl(card, SNDCTL_DSP_SPEED, &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];
>    fout = fopen(path, "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, fout);
>       //fprintf (stderr, "nbBytes: %d\n", nbBytes);
>       if (feof(stdin))
>          break;
>      
>       /*Read the "packet" encoded by sampleenc*/
>       fread(cbits, 1, nbBytes, fout);
>       /*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);
>     write(card,out,FRAME_SIZE);
>    
>    
>    }
>   
>    /*Destroy the decoder state*/
>    speex_encoder_destroy(state);
>    /*Destroy the bit-stream truct*/
>    speex_bits_destroy(&bits);
>    fclose(fout);
>    return 0;
> }
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Speex-dev mailing list
> Speex-dev at xiph.org
> http://lists.xiph.org/mailman/listinfo/speex-dev


More information about the Speex-dev mailing list