[Speex-dev] Newbie - Encoding PCM

Michele "O-Zone" Pinassi liste at zerozone.it
Wed Jun 22 03:31:50 PDT 2005


Hi all,
i've to encode voice from a voicemodem. I choose speex 1.0.5 for its quality 
in voice encoding. I've tried to implement an encoder but unsuccesfully. 
Here's my code:

/* ============ SPEEX stream ENCODER 
============================================ */
int SPEEX_EncodePCM(struct _IDA_ClientSocket *IDA,char *buffer,unsigned char 
*PCM,int num_samples) {
/* buffer point to the output buffer 
PCM point to the numsamples bytes 8bit - 8Khz PCM from the modem 
num_samples is, commonly, 16000
*/
    int ret,c,d;
    short int PCM_F[160];
    for(c=0;c<num_samples;c++) {
	/* tempbuf 0<-127->256 */
	/* pcmbuff -32786 <- 0 -> +32786 */
	PCM_F[d] = (PCM[c] << 8)^0x8000;
	d++;
	if(d > 160) {
	    speex_bits_reset(&IDA->speex_bits);
	    speex_encode_int(IDA->speex_state,PCM_F,&IDA->speex_bits);
	    speex_bits_insert_terminator(&IDA->speex_bits);
	    d = speex_bits_write(&IDA->speex_bits, buffer, 200);
	    *buffer += d;
	    ret += d;
	    d = 0;
	}
    }
    return ret;
}

int SPEEX_Init(struct _IDA_ClientSocket *IDA) {
    /* speex_init_header(&IDA->speex_header,8000,1,IDA->speex_mode); */
    IDA->speex_mode = speex_lib_get_mode(SPEEX_MODEID_NB);
    speex_bits_init(&IDA->speex_bits);
    IDA->speex_state = speex_encoder_init(IDA->speex_mode);
}	

int SPEEX_Close(struct _IDA_ClientSocket *IDA) {
    speex_bits_destroy(&IDA->speex_bits);
    speex_encoder_destroy(IDA->speex_state);
}

"IDA" struct containts:

struct _IDA_ClientSocket {
[...]
     void *speex_state;
     SpeexHeader speex_header;
     SpeexBits speex_bits;
     SpeexMode *speex_mode;
};

so i call SPEEX_Init before start encoding and SPEEX_Encode on every 16000 
Bits of the PCM stream from the modem.

The result is a strange file that contains a lot of zeros and no audio 
data :-(

Someon can help me pointing to the right direction ? Thanks !

-- 
----
O-Zone ! No (C) 2005
www.zerozone.it
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.xiph.org/pipermail/speex-dev/attachments/20050622/ade2cd2d/attachment.pgp


More information about the Speex-dev mailing list