/* * auencoder-speex.c * * Speex encoder sound system interface code * * Copyright (c) 2005 Todd MacDermid */ #include #include cutlass_speex_t * cutlass_speex_handle_init(cutlass_t *cut_handle, int speex_type) { int quality = CUTLASS_DEFAULT_QUALITY; int allow = 1; cutlass_speex_t *speex_handle; if((NULL == cut_handle)) { cutlass_sysmsg(cut_handle, CUT_ERROR, "cutlass_speex_handle_init: Passed a NULL pointer\n"); return(NULL); } speex_handle = calloc(1, sizeof(cutlass_speex_t)); if(NULL == speex_handle) { cutlass_sysmsg(cut_handle, CUT_ERROR, "cutlass_speex_handle_init: Memory allocation failed\n"); return(NULL); } speex_handle->speex_type = speex_type; if(speex_handle->speex_type == CUTLASS_SPEEX_ENCODER) { speex_handle->state = speex_encoder_init(&speex_nb_mode); speex_encoder_ctl(speex_handle->state, SPEEX_SET_QUALITY, &quality); speex_encoder_ctl(speex_handle->state, SPEEX_SET_VBR, &allow); speex_encoder_ctl(speex_handle->state, SPEEX_SET_VAD, &allow); speex_encoder_ctl(speex_handle->state, SPEEX_SET_DTX, &allow); } else if(speex_handle->speex_type == CUTLASS_SPEEX_DECODER) { speex_handle->state = speex_decoder_init(&speex_nb_mode); } else { cutlass_sysmsg(cut_handle, CUT_ERROR, "cutlass_speex_handle_init: Unknown coder type\n"); free(speex_handle); return(NULL); } speex_bits_init(&(speex_handle->bits)); return(speex_handle); } int cutlass_speex_handle_destroy(cutlass_t *cut_handle, cutlass_speex_t *speex_handle) { if((NULL == cut_handle) || (NULL==speex_handle)) { cutlass_sysmsg(cut_handle, CUT_ERROR, "cutlass_speex_handle_destroy: Passed a NULL pointer\n"); return(-1); } speex_bits_destroy(&(speex_handle->bits)); if(speex_handle->speex_type == CUTLASS_SPEEX_ENCODER) { speex_encoder_destroy(speex_handle->state); } else if(speex_handle->speex_type == CUTLASS_SPEEX_DECODER) { speex_decoder_destroy(speex_handle->state); } else { cutlass_sysmsg(cut_handle, CUT_ERROR, "cutlass_speex_handle_destroy: Unknown coder type\n"); } free(speex_handle); return(0); } int cutlass_speex_handle_framesize(cutlass_t *cut_handle, cutlass_speex_t *speex_handle) { int framesize; if(speex_handle->speex_type == CUTLASS_SPEEX_ENCODER) { speex_encoder_ctl(speex_handle->state, SPEEX_GET_FRAME_SIZE, &framesize); } else if(speex_handle->speex_type == CUTLASS_SPEEX_DECODER) { speex_decoder_ctl(speex_handle->state, SPEEX_GET_FRAME_SIZE, &framesize); } else { cutlass_sysmsg(cut_handle, CUT_ERROR, "cutlass_speex_handle_framesize: Unknown coder type\n"); } return(framesize); } int cutlass_speex_encode(cutlass_t *cut_handle, cutlass_speex_t *speex_handle, float *samples, uint8_t *frame, int max_frame_bytes) { int num_bytes; if((NULL == cut_handle) || (NULL==speex_handle) || (NULL == samples) || (NULL == frame)) { cutlass_sysmsg(cut_handle, CUT_ERROR, "cutlass_speex_encode: Passed a NULL pointer\n"); return(-1); } if(speex_handle->speex_type != CUTLASS_SPEEX_ENCODER) { cutlass_sysmsg(cut_handle, CUT_ERROR, "cutlass_speex_encode: Speex handle is not an encoder\n"); return(-1); } speex_bits_reset(&(speex_handle->bits)); speex_encode(speex_handle->state, samples, &(speex_handle->bits)); num_bytes = speex_bits_write(&(speex_handle->bits), frame, max_frame_bytes); return(num_bytes); } int cutlass_speex_decode(cutlass_t *cut_handle, cutlass_speex_t *speex_handle, float *samples, uint8_t *frame, int num_frame_bytes) { if((NULL == cut_handle) || (NULL==speex_handle) || (NULL == samples) || (NULL == frame)) { cutlass_sysmsg(cut_handle, CUT_ERROR, "cutlass_speex_decode: Passed a NULL pointer\n"); return(-1); } if(speex_handle->speex_type != CUTLASS_SPEEX_DECODER) { cutlass_sysmsg(cut_handle, CUT_ERROR, "cutlass_speex_decode: Speex handle is not an decoder\n"); return(-1); } speex_bits_read_from(&(speex_handle->bits), frame, num_frame_bytes); speex_decode(speex_handle->state, &(speex_handle->bits), samples); return(0); }