int spx_encode_frame(int handle ,short *const pcm_data ,char *speex_data){ // char cbits[200]; float input[FRAME_SIZE]; speex_encode_union_t * speex_encode_u = (speex_encode_union_t *)handle; /*Flush all the bits in the struct so we can encode a new frame*/ speex_bits_reset(&speex_encode_u->bits); /*Encode the frame*/ // memcpy(input,pcm_data,FRAME_SIZE*2); int i; for (i = 0; i < FRAME_SIZE; i++) input[i] = pcm_data[i]; speex_encode(speex_encode_u->state, input, &speex_encode_u->bits); ////write rtp packet header!!!!!RTP HEADER //memcpy(speex_data ,&speex_encode_u->rtp_header ,RTP_HEADER_SIZE); ///*Copy the bits to an array of char that can be written*/ //int nbBytes = speex_bits_write(&speex_encode_u->bits, &speex_data[RTP_HEADER_SIZE], 38); //fwrite(&speex_data[RTP_HEADER_SIZE] ,1 ,38 ,fp_speex_send); int nbBytes = speex_bits_write(&speex_encode_u->bits, speex_data, 38); fwrite(speex_data ,1 ,38 ,fp_speex_send); printf("after speex_encode ,write nbBytes = %d to file !\n" ,nbBytes); return 0; }
int spx_encode_frame(int handle ,short *pcm_data ,char *speex_data){ // char cbits[200]; float input[FRAME_SIZE]; speex_encode_union_t * speex_encode_u = (speex_encode_union_t *)handle; /*Flush all the bits in the struct so we can encode a new frame*/ speex_bits_reset(&speex_encode_u->bits); /*Encode the frame*/ // memcpy(input,pcm_data,FRAME_SIZE*2); int i; for (i = 0; i < FRAME_SIZE; i++) input[i] = pcm_data[i]; speex_encode(speex_encode_u->state, input, &speex_encode_u->bits); /*Copy the bits to an array of char that can be written*/ // nbBytes = speex_bits_write(&speex_encode_u->bits, cbits, 200); int nbBytes = speex_bits_write(&speex_encode_u->bits, speex_data, 38); printf("nbBytes = %d \n" ,nbBytes); // /*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, stdout); // printf("nbBytes = %d \n" ,nbBytes); // /*Write the compressed data*/ // //fwrite(cbits, 1, nbBytes, stdout); }
int main(int argc, char **argv) { char *inFile; FILE *fin; short in[FRAME_SIZE]; float input[FRAME_SIZE]; char cbits[2000]; 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; /*Create a new encoder state in narrowband mode*/ state = speex_encoder_init(&speex_nb_mode); //printf("inited\n"); /*Set the quality to 8 (15 kbps)*/ tmp=8; speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp); inFile = argv[1]; fin = fopen(inFile, "r"); /*Initialization of the structure that holds the bits*/ speex_bits_init(&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++) 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, 2000); /*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, stdout); /*Write the compressed data*/ fwrite(cbits, 1, nbBytes, stdout); } /*Destroy the encoder state*/ speex_encoder_destroy(state); /*Destroy the bit-packing struct*/ speex_bits_destroy(&bits); fclose(fin); return 0; }
/* encode PCM data to speex frames */ static void speexout_encode(t_speexout *x) { if ( x->x_bytesbuffered > x->x_framesize ) { speex_bits_reset(&x->x_bits); { t_int sp=0, rp=0; while( sp < x->x_framesize ) { rp=(x->x_outp+sp)%IN_BUFFER_SIZE; // post( "speexout~ : sp=%d : rp=%d", sp, rp ); x->x_encchunk[ sp++ ] = *(x->x_inbuf+rp); } speex_encode(x->x_encstate, x->x_encchunk, &x->x_bits); } x->x_outp = (x->x_outp+x->x_framesize)%IN_BUFFER_SIZE; x->x_bytesbuffered -= x->x_framesize; x->x_encsize = speex_bits_write(&x->x_bits, x->x_outbuf+1, OUT_BUFFER_SIZE ); if ( x->x_encsize < 127 ) { *(x->x_outbuf) = (char)x->x_encsize; } else { post( "speexout~ : encoding error : frame is more than 127 bytes" ); x->x_encsize = -1; } x->x_bytesemitted += x->x_encsize; #ifdef DATADEBUG { t_int si; printf( "speexout~ : encoded : " ); for ( si=0; si<x->x_encsize; si++ ) { printf( "%d ", *(x->x_outbuf+si) ); } printf( "\n" ); } #endif } else { x->x_encsize = -1; } }
int main(int argc, char **argv) { char *inFile; FILE *fin; short in[FRAME_SIZE]; float input[FRAME_SIZE]; char cbits[200]; int nbBytes; /*保存编码的状态*/ void *state; /*保存字节因此他们可以被speex常规读写*/ SpeexBits bits; int i, tmp; //新建一个新的编码状态在窄宽(narrowband)模式下 state = speex_encoder_init(&speex_nb_mode); //设置质量为8(15kbps) tmp=8; speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp); inFile = argv[1]; fin = fopen(inFile, "r"); //初始化结构使他们保存数据 speex_bits_init(&bits); while (1) { //读入一帧16bits的声音 fread(in, sizeof(short), FRAME_SIZE, fin); if (feof(fin)) break; //把16bits的值转化为float,以便speex库可以在上面工作 for (i=0;i<FRAME_SIZE;i++) input[i]=in[i]; //清空这个结构体里所有的字节,以便我们可以编码一个新的帧 speex_bits_reset(&bits); //对帧进行编码 speex_encode(state, input, &bits); //把bits拷贝到一个利用写出的char型数组 nbBytes = speex_bits_write(&bits, cbits, 200); //首先写出帧的大小,这是sampledec文件需要的一个值,但是你的应用程序中可能不一样 fwrite(&nbBytes, sizeof(int), 1, stdout); //写出压缩后的数组 fwrite(cbits, 1, nbBytes, stdout); } //释放编码器状态量 speex_encoder_destroy(state); //释放bit_packing结构 speex_bits_destroy(&bits); fclose(fin); return 0; }
void gviSpursSpeexEncode(SpursSpeexTaskOutput *spuTaskOut) { short *inBuffer; float *speexBuffer; char *outBuffer; unsigned int i; spuTaskOut->mSpeexEncodedFrameSize = 0; spuTaskOut->mSpeexInitialized = 1; spuTaskOut->mSpeexSamplesPerFrame = 0; spuTaskOut->mSpeexReturnCode = 0; spuTaskOut->mSpeexOutBufferSize = 0; speexBuffer = (float *)memalign(16, gviSpursSpeexTaskDesc.mInputBufferSize * sizeof(float)); inBuffer = (short *)memalign(16, gviSpursSpeexTaskDesc.mInputBufferSize * sizeof(short)); outBuffer = (char *)memalign(16, gviSpursSpeexTaskDesc.mOutputBufferSize); memset(speexBuffer, 0, gviSpursSpeexTaskDesc.mInputBufferSize * sizeof(float)); memset(inBuffer, 0, gviSpursSpeexTaskDesc.mInputBufferSize * sizeof(short)); memset(outBuffer, 0, gviSpursSpeexTaskDesc.mOutputBufferSize); cellDmaGet(inBuffer, (uint64_t)gviSpursSpeexTaskDesc.mInputBuffer, gviSpursSpeexTaskDesc.mInputBufferSize * sizeof(short), DMA_TAG(1), 0,0); cellDmaWaitTagStatusAll(DMA_MASK(1)); // convert the input to floats for encoding for(i = 0 ; i < gviSpursSpeexTaskDesc.mInputBufferSize ; i++) speexBuffer[i] = inBuffer[i]; // (re)initialize the bits struct speex_bits_init_buffer(&gviSpursSpeexBits,gviSpursSpeexBitsBuffer,sizeof(gviSpursSpeexBitsBuffer)); // flush the bits speex_bits_reset(&gviSpursSpeexBits); // encode the frame speex_encode(gviSpursSpeexStateBuffer, speexBuffer, &gviSpursSpeexBits); // write the bits to the output spuTaskOut->mSpeexOutBufferSize = speex_bits_write(&gviSpursSpeexBits, (char *)outBuffer, gviSpursSpeexTaskDesc.mEncodedFrameSize); //spuDebugPrintf("[Speex][SPU] transferring data back, output size should be: %d\n", gviSpursSpeexTaskDesc.mOutputBufferSize>16?gviSpursSpeexTaskDesc.mOutputBufferSize:16); cellDmaPut(outBuffer, (uint64_t)gviSpursSpeexTaskDesc.mOutputBuffer, gviSpursSpeexTaskDesc.mOutputBufferSize, DMA_TAG(1), 0, 0); cellDmaWaitTagStatusAll(DMA_MASK(1)); //spuDebugPrintf("[Speex][SPU] done transferring data back\n"); free(speexBuffer); free(inBuffer); free(outBuffer); spuTaskOut->mSpeexReturnCode = 0; }
void gviSpeexEncode(GVByte * out, const GVSample * in) { int bytesWritten; int i; // convert the input to floats for encoding for(i = 0 ; i < gviSpeexSamplesPerFrame ; i++) gviSpeexBuffer[i] = in[i]; // flush the bits speex_bits_reset(&gviSpeexBits); // encode the frame speex_encode(gviSpeexEncoderState, gviSpeexBuffer, &gviSpeexBits); // write the bits to the output bytesWritten = speex_bits_write(&gviSpeexBits, (char *)out, gviSpeexEncodedFrameSize); GS_ASSERT(bytesWritten == gviSpeexEncodedFrameSize); }
static int codec_drv_control( ErlDrvData handle, unsigned int command, char *buf, int len, char **rbuf, int rlen) { codec_data* d = (codec_data*)handle; int i; int ret = 0; ErlDrvBinary *out; *rbuf = NULL; float frame[FRAME_SIZE]; char cbits[200]; switch(command) { case CMD_ENCODE: for (i=0; i < len / 2; i++){ frame[i] = (buf[2*i] & 0xff) | (buf[2*i+1] << 8); } speex_bits_reset(&d->bits); speex_encode(d->estate, frame, &d->bits); ret = speex_bits_write(&d->bits, cbits, 200); out = driver_alloc_binary(ret); memcpy(out->orig_bytes, cbits, ret); *rbuf = (char *) out; break; case CMD_DECODE: out = driver_alloc_binary(2*FRAME_SIZE); speex_bits_read_from(&d->bits, buf, len); speex_decode_int(d->dstate, &d->bits, (spx_int16_t *)out->orig_bytes); ret = 2*FRAME_SIZE; *rbuf = (char *) out; break; default: break; } return ret; }
void Transmitter::sendAudioPacket(float *fsamples, int samples) { int tot = 0; speex_bits_reset(&bits); if (state) { speex_encode(state, fsamples, &bits); tot = speex_bits_write(&bits, out, MAXBUFSIZE); } if (tot>0) { int size; char type = DRTA_INFO_AUDIO; try { if (blowfish) { size = PacketHandler::calculateCryptedSize(tot); type = DRTA_INFO_CRYPTED_AUDIO; } else size = PacketHandler::calculateSize(tot); Packet *p = new Packet (size); PacketHandler::buildModePacket(p, out, tot, type, speexmode); if (blowfish) p->crypt(blowfish); if( _enable_sctp) sendPacket(p , MAPLE_STM_AUDIO); else sendPacket(p); delete p; } catch (Error e) { emitError(e.getText()); } } }
hsBool plSpeex::Encode(short *data, int numFrames, int *packedLength, hsRAMStream *out) { *packedLength = 0; short *pData = data; // pointer to input data float *input = new float[fFrameSize]; // input to speex - used as am intermediate array since speex requires float data uint8_t frameLength; // number of bytes speex compressed frame to uint8_t *frameData = new uint8_t[fFrameSize]; // holds one frame of encoded data // encode data for( int i = 0; i < numFrames; i++ ) { // convert input data to floats for( int j = 0; j < fFrameSize; j++ ) { input[j] = pData[j]; } speex_bits_reset(fBits); // reset bit structure // encode data using speex speex_encode(fEncoderState, input, fBits); frameLength = speex_bits_write(fBits, (char *)frameData, fFrameSize); // write data - length and bytes out->WriteLE(frameLength); *packedLength += sizeof(frameLength); // add length of encoded frame out->Write(frameLength, frameData); *packedLength += frameLength; // update length pData += fFrameSize; // move input pointer } delete[] frameData; delete[] input; return true; }
void Phone::send(float *fsamples, int samples) { int tot = 0; speex_bits_reset(&enc_bits); if (enc_state) { speex_encode(enc_state, fsamples, &enc_bits); tot = speex_bits_write(&enc_bits, out, MAXBUFSIZE); } if (tot > 0) { try { for (int i=0; i<maxcall; i++) { if (calls[i]) calls[i]->send(out, tot); } } catch (Error e) { abortCall(e.getCallId(), e.getText()); } } }
void *os_sound_start_out_thread(void *_ca) { jcall_t *ca = (jcall_t*)_ca; char data_out[10000]; short sp_data_out_s[640]; float sp_data_out_f[640]; int timestamp = 0; int i; while (ca->enable_audio != -1) { int k; speex_bits_reset(&ca->speex_bits); for (k=0; k<ca->speex_nb_packet;k++) { int j; i=read(fd, sp_data_out_s, ca->speex_fsize * sizeof(short)); if (i>0) { for (j=0; j<ca->speex_fsize;j++) { /* convert to float */ sp_data_out_f[j] = sp_data_out_s[j]; } speex_encode(ca->speex_enc, sp_data_out_f, &ca->speex_bits); } } speex_bits_insert_terminator(&ca->speex_bits); /* convert to char */ i = speex_bits_write(&ca->speex_bits, data_out, sizeof(data_out)); rtp_session_send_with_ts(ca->rtp_session, data_out, i,timestamp); timestamp+=i; } return NULL; }
/*! \brief convert work buffer and produce output frame */ static struct ast_frame *lintospeex_frameout(struct ast_trans_pvt *pvt) { struct speex_coder_pvt *tmp = pvt->pvt; int is_speech=1; int datalen = 0; /* output bytes */ int samples = 0; /* output samples */ /* We can't work on anything less than a frame in size */ if (pvt->samples < tmp->framesize) return NULL; speex_bits_reset(&tmp->bits); while (pvt->samples >= tmp->framesize) { #ifdef _SPEEX_TYPES_H /* Preprocess audio */ if (preproc) is_speech = speex_preprocess(tmp->pp, tmp->buf + samples, NULL); /* Encode a frame of data */ if (is_speech) { /* If DTX enabled speex_encode returns 0 during silence */ is_speech = speex_encode_int(tmp->speex, tmp->buf + samples, &tmp->bits) || !dtx; } else { /* 5 zeros interpreted by Speex as silence (submode 0) */ speex_bits_pack(&tmp->bits, 0, 5); } #else { float fbuf[1024]; int x; /* Convert to floating point */ for (x = 0; x < tmp->framesize; x++) fbuf[x] = tmp->buf[samples + x]; /* Encode a frame of data */ is_speech = speex_encode(tmp->speex, fbuf, &tmp->bits) || !dtx; } #endif samples += tmp->framesize; pvt->samples -= tmp->framesize; } /* Move the data at the end of the buffer to the front */ if (pvt->samples) memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2); /* Use AST_FRAME_CNG to signify the start of any silence period */ if (is_speech) { tmp->silent_state = 0; } else { if (tmp->silent_state) { return NULL; } else { tmp->silent_state = 1; speex_bits_reset(&tmp->bits); memset(&pvt->f, 0, sizeof(pvt->f)); pvt->f.frametype = AST_FRAME_CNG; pvt->f.samples = samples; /* XXX what now ? format etc... */ } } /* Terminate bit stream */ speex_bits_pack(&tmp->bits, 15, 5); datalen = speex_bits_write(&tmp->bits, pvt->outbuf.c, pvt->t->buf_size); return ast_trans_frameout(pvt, datalen, samples); }
static int output_data(char *buf, int32 nbytes) { char cbits[MAX_FRAME_BYTES]; Speex_ctx *ctx = speex_ctx; int nbBytes; int16 *s; int i, j; int ret; int nbytes_left; if (dpm.fd < 0) return 0; ctx->in_bytes += nbytes; /* Main encoding loop (one frame per iteration) */ nbytes_left = nbytes; s = (int16 *)buf; while (1) { ctx->ogg_packetid++; /* packing 16 bit -> float sample */ for (i = ctx->input_idx; i < ctx->frame_size * ctx->channels; i++) { /* stream is ended, and buffer is not full. wait next spooling */ if (nbytes_left < 0) { /* canceling ogg packet */ ctx->ogg_packetid--; return 0; } ctx->input[i] = *s++; nbytes_left -= 2; /* -16 bit*/ ctx->input_idx++; } /* buffer is full. encode now. */ ctx->input_idx = 0; if (ctx->channels == 2) speex_encode_stereo(ctx->input, ctx->frame_size, &ctx->bits); /* Encode the frame */ speex_encode(ctx->state, ctx->input, &ctx->bits); if ((ctx->ogg_packetid + 1) % ctx->nframes != 0) continue; speex_bits_insert_terminator(&ctx->bits); /* Copy the bits to an array of char that can be written */ nbBytes = speex_bits_write(&ctx->bits, cbits, MAX_FRAME_BYTES); /* Flush all the bits in the struct so we can encode a new frame */ speex_bits_reset(&ctx->bits); /* ogg packet setup */ ctx->op.packet = (unsigned char *)cbits; ctx->op.bytes = nbBytes; ctx->op.b_o_s = 0; ctx->op.e_o_s = 0; ctx->op.granulepos = (ctx->ogg_packetid + ctx->nframes) * ctx->frame_size; ctx->op.packetno = 2 + ctx->ogg_packetid / ctx->nframes; ogg_stream_packetin(&ctx->os, &ctx->op); /* Write all new pages (most likely 0 or 1) */ while (ogg_stream_pageout(&ctx->os, &ctx->og)) { ret = oe_write_page(&ctx->og, dpm.fd); if (ret != ctx->og.header_len + ctx->og.body_len) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "failed writing header to output stream"); return -1; } else ctx->out_bytes += ret; } } return 0; }
static void close_output(void) { int i; char cbits[MAX_FRAME_BYTES]; Speex_ctx *ctx = speex_ctx; int nbBytes; int ret; if (ctx == NULL) return; if (dpm.fd < 0) return; /* Write last frame */ if (speex_ctx != NULL) { if ((ctx->ogg_packetid + 1) % ctx->nframes != 0) { while ((ctx->ogg_packetid + 1) % ctx->nframes != 0) { ctx->ogg_packetid++; speex_bits_pack(&ctx->bits, 15, 5); } nbBytes = speex_bits_write(&ctx->bits, cbits, MAX_FRAME_BYTES); ctx->op.packet = (unsigned char *)cbits; ctx->op.bytes = nbBytes; ctx->op.b_o_s = 0; ctx->op.e_o_s = 1; ctx->op.granulepos = (ctx->ogg_packetid + ctx->nframes) * ctx->frame_size; ctx->op.packetno = 2 + ctx->ogg_packetid / ctx->nframes; ogg_stream_packetin(&ctx->os, &ctx->op); } for (i = ctx->input_idx; i < ctx->frame_size * ctx->channels; i++) { /* left is zero-cleaned */ ctx->input[i] = 0; } if (ctx->channels == 2) speex_encode_stereo(ctx->input, ctx->frame_size, &ctx->bits); /* Encode the frame */ speex_encode(ctx->state, ctx->input, &ctx->bits); speex_bits_insert_terminator(&ctx->bits); /* Copy the bits to an array of char that can be written */ nbBytes = speex_bits_write(&ctx->bits, cbits, MAX_FRAME_BYTES); /* Flush all the bits in the struct so we can encode a new frame */ speex_bits_reset(&ctx->bits); /* ogg packet setup */ ctx->op.packet = (unsigned char *)cbits; ctx->op.bytes = nbBytes; ctx->op.b_o_s = 0; ctx->op.e_o_s = 1; ctx->op.granulepos = (ctx->ogg_packetid + ctx->nframes) * ctx->frame_size; ctx->op.packetno = 2 + ctx->ogg_packetid / ctx->nframes; ogg_stream_packetin(&ctx->os, &ctx->op); /* Write all new pages (most likely 0 or 1) */ while (ogg_stream_pageout(&ctx->os, &ctx->og)) { ret = oe_write_page(&ctx->og, dpm.fd); if (ret != ctx->og.header_len + ctx->og.body_len) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "failed writing header to output stream"); return; } else ctx->out_bytes += ret; } ogg_stream_clear(&speex_ctx->os); speex_bits_destroy(&speex_ctx->bits); speex_encoder_destroy(speex_ctx->state); close(dpm.fd); dpm.fd = -1; free(speex_ctx->input); ctl->cmsg(CMSG_INFO, VERB_NORMAL, "Wrote %lu/%lu bytes(%g%% compressed)", ctx->out_bytes, ctx->in_bytes, ((double)ctx->out_bytes / (double)ctx->in_bytes)) * 100.; speex_ctx->input = NULL; free(speex_ctx); speex_ctx = NULL; } return; }
int main(int argc, char **argv) { char *inFile, *outFile, *bitsFile; FILE *fin, *fout, *fbits=NULL; short in_short[FRAME_SIZE]; short out_short[FRAME_SIZE]; float in_float[FRAME_SIZE]; float sigpow,errpow,snr, seg_snr=0; int snr_frames = 0; char cbits[200]; int nbBits; int i; void *st; void *dec; SpeexBits bits; int tmp; int bitCount=0; int skip_group_delay; SpeexCallback callback; sigpow = 0; errpow = 0; st = speex_encoder_init(&speex_uwb_mode); dec = speex_decoder_init(&speex_uwb_mode); callback.callback_id = SPEEX_INBAND_CHAR; callback.func = speex_std_char_handler; callback.data = stderr; speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback); callback.callback_id = SPEEX_INBAND_MODE_REQUEST; callback.func = speex_std_mode_request_handler; callback.data = st; speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback); tmp=0; speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp); tmp=0; speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp); tmp=7; speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp); tmp=1; speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp); speex_mode_query(&speex_nb_mode, SPEEX_MODE_FRAME_SIZE, &tmp); fprintf (stderr, "frame size: %d\n", tmp); skip_group_delay = 509; if (argc != 4 && argc != 3) { fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc); exit(1); } inFile = argv[1]; fin = fopen(inFile, "r"); outFile = argv[2]; fout = fopen(outFile, "w+"); if (argc==4) { bitsFile = argv[3]; fbits = fopen(bitsFile, "w"); } speex_bits_init(&bits); while (!feof(fin)) { fread(in_short, sizeof(short), FRAME_SIZE, fin); if (feof(fin)) break; for (i=0;i<FRAME_SIZE;i++) in_float[i]=in_short[i]; speex_bits_reset(&bits); speex_encode(st, in_short, &bits); nbBits = speex_bits_write(&bits, cbits, 200); bitCount+=bits.nbBits; if (argc==4) fwrite(cbits, 1, nbBits, fbits); speex_bits_rewind(&bits); speex_decode(dec, &bits, out_short); speex_bits_reset(&bits); fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout); skip_group_delay = 0; } fprintf (stderr, "Total encoded size: %d bits\n", bitCount); speex_encoder_destroy(st); speex_decoder_destroy(dec); rewind(fin); rewind(fout); while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin) && FRAME_SIZE == fread(out_short, sizeof(short), FRAME_SIZE,fout) ) { float s=0, e=0; for (i=0;i<FRAME_SIZE;++i) { s += (float)in_short[i] * in_short[i]; e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]); } seg_snr += 10*log10((s+1)/(e+1)); sigpow += s; errpow += e; snr_frames++; } fclose(fin); fclose(fout); snr = 10 * log10( sigpow / errpow ); seg_snr /= snr_frames; fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr); #ifdef FIXED_DEBUG printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames)); #endif return 1; }
int SpeexEncoder::encoder_encode(const short *data, char **output) { ////fprintf(stderr, "encoder_encode1\n"); //for (int i = 0; i < FRAME_SIZE; i++){ // encoder_input[i] = data[i]; //} //speex_encode_int(encoder_state,(short*) encoder_input, &encoder_bits); //speex_bits_insert_terminator(&encoder_bits); ////fprintf(stderr, "encoder_encode2\n"); //speex_bits_reset(&encoder_bits); ////fprintf(stderr, "encoder_encode3\n"); //speex_encode(encoder_state, encoder_input, &encoder_bits); ////fprintf(stderr, "encoder_encode4\n"); //char outputBuffer[200]; //int size; ////fprintf(stderr, "encoder_encode4.5\n"); //size = speex_bits_write(&encoder_bits, outputBuffer, 200); char *inFile; FILE *fin; short in[FRAME_SIZE]; inFile = "gate10.decode.raw";//argv[1]; fin = fopen(inFile, "r"); float input[FRAME_SIZE]; int i; /*Initialization of the structure that holds the bits*/ speex_bits_init(&bits); typedef std::vector<char> buffer_type; buffer_type myData; 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++) 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(encoder_state, input, &bits); /*Copy the bits to an array of char that can be written*/ nbBytes = speex_bits_write(&bits, cbits, MAX_FRAME_BYTES); /*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, stdout); /*Write the compressed data*/ fwrite(cbits, 1, nbBytes, stdout); fprintf(stderr, "EncodeTest2 %i\n" , myData.size()); myData.insert(myData.end(), cbits, cbits + nbBytes); } char *buffer = new char[myData.size()]; std::copy(myData.begin(), myData.end(), buffer); fprintf(stderr, "EncodeTest2 %i\n", sizeof(buffer)); //fprintf(stderr, "encoder_encode5\n"); *output = buffer; //fprintf(stderr, "encoder_encode6 %i\n", size); return myData.size(); }
int main(int argc, char **argv) { char *inFile, *outFile, *bitsFile; FILE *fin, *fout, *fbits=NULL; short in[FRAME_SIZE]; float input[FRAME_SIZE], bak[FRAME_SIZE], bak2[FRAME_SIZE]; char cbits[200]; int nbBits; int i; void *st; void *dec; SpeexBits bits; int tmp; int bitCount=0; SpeexCallback callback; for (i=0;i<FRAME_SIZE;i++) bak2[i]=0; st = speex_encoder_init(&speex_nb_mode); dec = speex_decoder_init(&speex_nb_mode); callback.callback_id = SPEEX_INBAND_CHAR; callback.func = speex_std_char_handler; callback.data = stderr; speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback); callback.callback_id = SPEEX_INBAND_MODE_REQUEST; callback.func = speex_std_mode_request_handler; callback.data = st; speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback); tmp=0; speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp); tmp=0; speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp); tmp=8; speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp); tmp=1; speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp); speex_mode_query(&speex_nb_mode, SPEEX_MODE_FRAME_SIZE, &tmp); fprintf (stderr, "frame size: %d\n", tmp); if (argc != 4 && argc != 3) { fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc); exit(1); } inFile = argv[1]; fin = fopen(inFile, "r"); outFile = argv[2]; fout = fopen(outFile, "w"); if (argc==4) { bitsFile = argv[3]; fbits = fopen(bitsFile, "w"); } speex_bits_init(&bits); while (!feof(fin)) { fread(in, sizeof(short), FRAME_SIZE, fin); if (feof(fin)) break; for (i=0;i<FRAME_SIZE;i++) bak[i]=input[i]=in[i]; speex_bits_reset(&bits); /* speex_bits_pack(&bits, 14, 5); speex_bits_pack(&bits, SPEEX_INBAND_CHAR, 4); speex_bits_pack(&bits, 'A', 8); speex_bits_pack(&bits, 14, 5); speex_bits_pack(&bits, SPEEX_INBAND_MODE_REQUEST, 4); speex_bits_pack(&bits, 7, 4); speex_bits_pack(&bits, 15, 5); speex_bits_pack(&bits, 2, 4); speex_bits_pack(&bits, 0, 16); */ speex_encode(st, input, &bits); nbBits = speex_bits_write(&bits, cbits, 200); bitCount+=bits.nbBits; printf ("Encoding frame in %d bits\n", nbBits*8); if (argc==4) fwrite(cbits, 1, nbBits, fbits); { float enoise=0, esig=0, snr; for (i=0;i<FRAME_SIZE;i++) { enoise+=(bak2[i]-input[i])*(bak2[i]-input[i]); esig += bak2[i]*bak2[i]; } snr = 10*log10((esig+1)/(enoise+1)); printf ("real SNR = %f\n", snr); } speex_bits_rewind(&bits); speex_decode(dec, &bits, input); /* Save the bits here */ for (i=0;i<FRAME_SIZE;i++) { if (input[i]>32000) input[i]=32000; else if (input[i]<-32000) input[i]=-32000; } speex_bits_reset(&bits); for (i=0;i<FRAME_SIZE;i++) in[i]=(short)input[i]; for (i=0;i<FRAME_SIZE;i++) bak2[i]=bak[i]; fwrite(in, sizeof(short), FRAME_SIZE, fout); } fprintf (stderr, "Total encoded size: %d bits\n", bitCount); speex_encoder_destroy(st); speex_decoder_destroy(dec); return 1; }
int main(int argc, char **argv) { char *inFile, *outFile, *bitsFile; FILE *fin, *fout, *fbits=NULL; short in[FRAME_SIZE]; float input[FRAME_SIZE], bak[FRAME_SIZE], bak2[FRAME_SIZE]; char cbits[200]; int nbBits; int i; void *st; void *dec; SpeexBits bits; int tmp; int bitCount=0; for (i=0;i<FRAME_SIZE;i++) bak2[i]=0; st = speex_encoder_init(&speex_uwb_mode); dec = speex_decoder_init(&speex_uwb_mode); tmp=0; /*speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp); tmp=0; speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp); tmp=10; speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp); tmp=3; speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp); tmp=3; speex_encoder_ctl(st, SPEEX_SET_HIGH_MODE, &tmp); tmp=6; speex_encoder_ctl(st, SPEEX_SET_LOW_MODE, &tmp); */ if (argc != 4 && argc != 3) { fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc); exit(1); } inFile = argv[1]; fin = fopen(inFile, "r"); outFile = argv[2]; fout = fopen(outFile, "w"); if (argc==4) { bitsFile = argv[3]; fbits = fopen(bitsFile, "w"); } speex_bits_init(&bits); while (!feof(fin)) { fread(in, sizeof(short), FRAME_SIZE, fin); if (feof(fin)) break; for (i=0;i<FRAME_SIZE;i++) bak[i]=input[i]=in[i]; speex_bits_reset(&bits); speex_encode(st, input, &bits); nbBits = speex_bits_write(&bits, cbits, 200); bitCount+=bits.nbBits; printf ("Encoding frame in %d bits\n", nbBits*8); if (argc==4) fwrite(cbits, 1, nbBits, fbits); { float enoise=0, esig=0, snr; for (i=0;i<FRAME_SIZE;i++) { enoise+=(bak2[i]-input[i])*(bak2[i]-input[i]); esig += bak2[i]*bak2[i]; } snr = 10*log10((esig+1)/(enoise+1)); printf ("real SNR = %f\n", snr); } speex_bits_rewind(&bits); speex_decode(dec, &bits, input); /* Save the bits here */ for (i=0;i<FRAME_SIZE;i++) { if (input[i]>32000) input[i]=32000; else if (input[i]<-32000) input[i]=-32000; } speex_bits_reset(&bits); for (i=0;i<FRAME_SIZE;i++) in[i]=(short)input[i]; for (i=0;i<FRAME_SIZE;i++) bak2[i]=bak[i]; fwrite(in, sizeof(short), FRAME_SIZE, fout); } fprintf (stderr, "Total encoded size: %d bits\n", bitCount); speex_encoder_destroy(st); speex_decoder_destroy(dec); speex_bits_destroy(&bits); fclose(fin); fclose(fout); return 1; }
/*! \brief convert work buffer and produce output frame */ static struct ast_frame *lintospeex_frameout(struct ast_trans_pvt *pvt) { struct speex_coder_pvt *tmp = pvt->pvt; int is_speech=1; int datalen = 0; /* output bytes */ int samples = 0; /* output samples */ /* We can't work on anything less than a frame in size */ if (pvt->samples < tmp->framesize) return NULL; speex_bits_reset(&tmp->bits); while (pvt->samples >= tmp->framesize) { #ifdef _SPEEX_TYPES_H /* Preprocess audio */ if (preproc) is_speech = speex_preprocess(tmp->pp, tmp->buf + samples, NULL); /* Encode a frame of data */ if (is_speech) { /* If DTX enabled speex_encode returns 0 during silence */ is_speech = speex_encode_int(tmp->speex, tmp->buf + samples, &tmp->bits) || !dtx; } else { /* 5 zeros interpreted by Speex as silence (submode 0) */ speex_bits_pack(&tmp->bits, 0, 5); } #else { float fbuf[1024]; int x; /* Convert to floating point */ for (x = 0; x < tmp->framesize; x++) fbuf[x] = tmp->buf[samples + x]; /* Encode a frame of data */ is_speech = speex_encode(tmp->speex, fbuf, &tmp->bits) || !dtx; } #endif samples += tmp->framesize; pvt->samples -= tmp->framesize; } /* Move the data at the end of the buffer to the front */ if (pvt->samples) memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2); /* Use AST_FRAME_CNG to signify the start of any silence period */ if (is_speech) { tmp->silent_state = 0; } else { if (tmp->silent_state) { return NULL; } else { struct ast_frame frm = { .frametype = AST_FRAME_CNG, .src = pvt->t->name, }; /* * XXX I don't think the AST_FRAME_CNG code has ever * really worked for speex. There doesn't seem to be * any consumers of the frame type. Everyone that * references the type seems to pass the frame on. */ tmp->silent_state = 1; /* XXX what now ? format etc... */ return ast_frisolate(&frm); } } /* Terminate bit stream */ speex_bits_pack(&tmp->bits, 15, 5); datalen = speex_bits_write(&tmp->bits, pvt->outbuf.c, pvt->t->buf_size); return ast_trans_frameout(pvt, datalen, samples); } static void speextolin_destroy(struct ast_trans_pvt *arg) { struct speex_coder_pvt *pvt = arg->pvt; speex_decoder_destroy(pvt->speex); speex_bits_destroy(&pvt->bits); } static void lintospeex_destroy(struct ast_trans_pvt *arg) { struct speex_coder_pvt *pvt = arg->pvt; #ifdef _SPEEX_TYPES_H if (preproc) speex_preprocess_state_destroy(pvt->pp); #endif speex_encoder_destroy(pvt->speex); speex_bits_destroy(&pvt->bits); } static struct ast_translator speextolin = { .name = "speextolin", .src_codec = { .name = "speex", .type = AST_MEDIA_TYPE_AUDIO, .sample_rate = 8000, }, .dst_codec = { .name = "slin", .type = AST_MEDIA_TYPE_AUDIO, .sample_rate = 8000, }, .format = "slin",