static switch_status_t switch_lpc10_decode(switch_codec_t *codec, switch_codec_t *other_codec, void *encoded_data, uint32_t encoded_data_len, uint32_t encoded_rate, void *decoded_data, uint32_t *decoded_data_len, uint32_t *decoded_rate, unsigned int *flag) { struct lpc10_context *context = codec->private_info; if (!context) { return SWITCH_STATUS_FALSE; } *decoded_data_len = (2 * lpc10_decode(context->decoder_object, (int16_t *) decoded_data, (uint8_t *) encoded_data, encoded_data_len)); return SWITCH_STATUS_SUCCESS; }
static int lpc10tolin_framein(struct cw_translator_pvt *tmp, struct cw_frame *f) { /* Assuming there's space left, decode into the current buffer at the tail location */ int len = 0; int16_t *sd; if (f->datalen == 0) { /* Perform PLC with nominal framesize of LPC10_SAMPLES_PER_FRAME */ if ((tmp->tail + LPC10_SAMPLES_PER_FRAME) > sizeof(tmp->buf)/sizeof(int16_t)) { cw_log(LOG_WARNING, "Out of buffer space\n"); return -1; } if (useplc) { plc_fillin(&tmp->plc, tmp->buf+tmp->tail, LPC10_SAMPLES_PER_FRAME); tmp->tail += LPC10_SAMPLES_PER_FRAME; } return 0; } while (len + LPC10_BYTES_IN_COMPRESSED_FRAME <= f->datalen) { if (tmp->tail + LPC10_SAMPLES_PER_FRAME >= sizeof(tmp->buf)/sizeof(int16_t)) { cw_log(LOG_WARNING, "Out of buffer space\n"); return -1; } sd = tmp->buf + tmp->tail; if (lpc10_decode(tmp->lpc10.dec, sd, f->data + len, 7) < LPC10_SAMPLES_PER_FRAME) { cw_log(LOG_WARNING, "Invalid lpc10 data\n"); return -1; } if (useplc) plc_rx(&tmp->plc, tmp->buf + tmp->tail, LPC10_SAMPLES_PER_FRAME); tmp->tail += LPC10_SAMPLES_PER_FRAME; len += LPC10_BYTES_IN_COMPRESSED_FRAME; } if (len != f->datalen) cw_log(LOG_WARNING, "Decoded %d, expected %d\n", len, f->datalen); return 0; }
int main(int argc, char *argv[]) { AFfilehandle inhandle; AFfilehandle refhandle; AFfilehandle outhandle; AFfilesetup filesetup; int frames; int outframes; float x; double pre_energy; double post_energy; double ref_energy; double diff_energy; int16_t pre_amp[BLOCKS_PER_READ*BLOCK_LEN]; int16_t post_amp[BLOCKS_PER_READ*BLOCK_LEN]; int16_t ref_amp[BLOCKS_PER_READ*BLOCK_LEN]; int16_t log_amp[BLOCKS_PER_READ*BLOCK_LEN*3]; uint8_t lpc10_data[BLOCKS_PER_READ*7]; double xx; lpc10_encode_state_t *lpc10_enc_state; lpc10_decode_state_t *lpc10_dec_state; int i; int block_no; int log_error; int compress; int decompress; const char *in_file_name; int compress_file; int decompress_file; int len; int enc_len; int dec_len; compress = FALSE; decompress = FALSE; log_error = TRUE; in_file_name = IN_FILE_NAME; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-c") == 0) { compress = TRUE; continue; } if (strcmp(argv[i], "-d") == 0) { decompress = TRUE; continue; } if (strcmp(argv[i], "-i") == 0) { in_file_name = argv[++i]; continue; } if (strcmp(argv[i], "-l") == 0) { log_error = FALSE; continue; } } compress_file = -1; decompress_file = -1; inhandle = AF_NULL_FILEHANDLE; refhandle = AF_NULL_FILEHANDLE; outhandle = AF_NULL_FILEHANDLE; if (!decompress) { if ((inhandle = afOpenFile(in_file_name, "r", 0)) == AF_NULL_FILEHANDLE) { fprintf(stderr, " Cannot open wave file '%s'\n", in_file_name); exit(2); } if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0) { fprintf(stderr, " Unexpected frame size in wave file '%s'\n", in_file_name); exit(2); } if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE) { fprintf(stderr, " Unexpected sample rate in wave file '%s'\n", in_file_name); exit(2); } if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0) { fprintf(stderr, " Unexpected number of channels in wave file '%s'\n", in_file_name); exit(2); } if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP) { fprintf(stderr, " Failed to create file setup\n"); exit(2); } if ((refhandle = afOpenFile(REF_FILE_NAME, "r", 0)) == AF_NULL_FILEHANDLE) { fprintf(stderr, " Cannot open wave file '%s'\n", REF_FILE_NAME); exit(2); } if ((x = afGetFrameSize(refhandle, AF_DEFAULT_TRACK, 1)) != 2.0) { fprintf(stderr, " Unexpected frame size in wave file '%s'\n", REF_FILE_NAME); exit(2); } if ((x = afGetRate(refhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE) { fprintf(stderr, " Unexpected sample rate in wave file '%s'\n", REF_FILE_NAME); exit(2); } if ((x = afGetChannels(refhandle, AF_DEFAULT_TRACK)) != 1.0) { fprintf(stderr, " Unexpected number of channels in wave file '%s'\n", REF_FILE_NAME); exit(2); } } else { if ((decompress_file = open(DECOMPRESS_FILE_NAME, O_RDONLY)) < 0) { fprintf(stderr, " Cannot open decompressed data file '%s'\n", DECOMPRESS_FILE_NAME); exit(2); } } if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP) { fprintf(stderr, " Failed to create file setup\n"); exit(2); } afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE); afInitFileFormat(filesetup, AF_FILE_WAVE); afInitChannels(filesetup, AF_DEFAULT_TRACK, 1); if ((outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE) { fprintf(stderr, " Cannot create wave file '%s'\n", OUT_FILE_NAME); exit(2); } if ((lpc10_enc_state = lpc10_encode_init(NULL, TRUE)) == NULL) { fprintf(stderr, " Cannot create encoder\n"); exit(2); } if ((lpc10_dec_state = lpc10_decode_init(NULL, TRUE)) == NULL) { fprintf(stderr, " Cannot create decoder\n"); exit(2); } if (compress) { if ((compress_file = open(COMPRESS_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { fprintf(stderr, " Cannot create compressed data file '%s'\n", COMPRESS_FILE_NAME); exit(2); } } pre_energy = 0.0; post_energy = 0.0; ref_energy = 0.0; diff_energy = 0.0; if (decompress) { while ((len = read(decompress_file, lpc10_data, BLOCKS_PER_READ*7)) > 0) { lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, len/7); outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, BLOCK_LEN*len/7); } } else { block_no = 0; while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, pre_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN && (frames = afReadFrames(refhandle, AF_DEFAULT_TRACK, ref_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN) { enc_len = lpc10_encode(lpc10_enc_state, lpc10_data, pre_amp, BLOCKS_PER_READ*BLOCK_LEN); if (compress) write(compress_file, lpc10_data, enc_len); dec_len = lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, enc_len); for (i = 0; i < dec_len; i++) { pre_energy += (double) pre_amp[i]*(double) pre_amp[i]; post_energy += (double) post_amp[i]*(double) post_amp[i]; ref_energy += (double) ref_amp[i]*(double) ref_amp[i]; /* The reference file has some odd clipping, so eliminate this from the energy measurement. */ if (ref_amp[i] == 32767 || ref_amp[i] == -32768) xx = 0.0; else xx = post_amp[i] - ref_amp[i]; diff_energy += (double) xx*(double) xx; log_amp[i] = xx; } block_no++; if (log_error) outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, log_amp, dec_len); else outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, dec_len); } if (afCloseFile(inhandle) != 0) { fprintf(stderr, " Cannot close wave file '%s'\n", in_file_name); exit(2); } if (afCloseFile(refhandle) != 0) { fprintf(stderr, " Cannot close wave file '%s'\n", REF_FILE_NAME); exit(2); } } if (afCloseFile(outhandle) != 0) { fprintf(stderr, " Cannot close wave file '%s'\n", OUT_FILE_NAME); exit(2); } afFreeFileSetup(filesetup); if (compress) close(compress_file); if (decompress) close(decompress_file); lpc10_encode_release(lpc10_enc_state); lpc10_decode_release(lpc10_dec_state); if (!decompress) { printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy); printf("Difference energy is %f%% of the total.\n", 100.0*diff_energy/ref_energy); if (fabs(1.0 - post_energy/pre_energy) > 0.05 || fabs(diff_energy/post_energy) > 0.03) { printf("Tests failed.\n"); exit(2); } printf("Tests passed.\n"); } return 0; }
int main(int argc, char *argv[]) { SNDFILE *inhandle; SNDFILE *refhandle; SNDFILE *outhandle; int frames; double pre_energy; double post_energy; double ref_energy; double diff_energy; int16_t pre_amp[BLOCKS_PER_READ*BLOCK_LEN]; int16_t post_amp[BLOCKS_PER_READ*BLOCK_LEN]; int16_t ref_amp[BLOCKS_PER_READ*BLOCK_LEN]; int16_t log_amp[BLOCKS_PER_READ*BLOCK_LEN*3]; uint8_t lpc10_data[BLOCKS_PER_READ*7]; double xx; lpc10_encode_state_t *lpc10_enc_state; lpc10_decode_state_t *lpc10_dec_state; int i; int block_no; int log_error; int compress; int decompress; const char *in_file_name; int compress_file; int decompress_file; int len; int opt; int enc_len; int dec_len; compress = FALSE; decompress = FALSE; log_error = TRUE; in_file_name = IN_FILE_NAME; while ((opt = getopt(argc, argv, "cdi:l")) != -1) { switch (opt) { case 'c': compress = TRUE; break; case 'd': decompress = TRUE; break; case 'i': in_file_name = optarg; break; case 'l': log_error = FALSE; break; default: //usage(); exit(2); } } compress_file = -1; decompress_file = -1; inhandle = NULL; refhandle = NULL; outhandle = NULL; if (!decompress) { if ((inhandle = sf_open_telephony_read(in_file_name, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", in_file_name); exit(2); } if ((refhandle = sf_open_telephony_read(REF_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", REF_FILE_NAME); exit(2); } } else { if ((decompress_file = open(DECOMPRESS_FILE_NAME, O_RDONLY)) < 0) { fprintf(stderr, " Cannot open decompressed data file '%s'\n", DECOMPRESS_FILE_NAME); exit(2); } } if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME); exit(2); } if ((lpc10_enc_state = lpc10_encode_init(NULL, TRUE)) == NULL) { fprintf(stderr, " Cannot create encoder\n"); exit(2); } if ((lpc10_dec_state = lpc10_decode_init(NULL, TRUE)) == NULL) { fprintf(stderr, " Cannot create decoder\n"); exit(2); } if (compress) { if ((compress_file = open(COMPRESS_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { fprintf(stderr, " Cannot create compressed data file '%s'\n", COMPRESS_FILE_NAME); exit(2); } } pre_energy = 0.0; post_energy = 0.0; ref_energy = 0.0; diff_energy = 0.0; if (decompress) { while ((len = read(decompress_file, lpc10_data, BLOCKS_PER_READ*7)) > 0) { lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, len/7); sf_writef_short(outhandle, post_amp, BLOCK_LEN*len/7); } } else { block_no = 0; while ((frames = sf_readf_short(inhandle, pre_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN && (frames = sf_readf_short(refhandle, ref_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN) { enc_len = lpc10_encode(lpc10_enc_state, lpc10_data, pre_amp, BLOCKS_PER_READ*BLOCK_LEN); if (compress) write(compress_file, lpc10_data, enc_len); dec_len = lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, enc_len); for (i = 0; i < dec_len; i++) { pre_energy += (double) pre_amp[i]*(double) pre_amp[i]; post_energy += (double) post_amp[i]*(double) post_amp[i]; ref_energy += (double) ref_amp[i]*(double) ref_amp[i]; /* The reference file has some odd clipping, so eliminate this from the energy measurement. */ if (ref_amp[i] == 32767 || ref_amp[i] == -32768) xx = 0.0; else xx = post_amp[i] - ref_amp[i]; diff_energy += (double) xx*(double) xx; log_amp[i] = xx; } block_no++; if (log_error) sf_writef_short(outhandle, log_amp, dec_len); else sf_writef_short(outhandle, post_amp, dec_len); } if (sf_close_telephony(inhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", in_file_name); exit(2); } if (sf_close_telephony(refhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", REF_FILE_NAME); exit(2); } } if (sf_close_telephony(outhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME); exit(2); } if (compress) close(compress_file); if (decompress) close(decompress_file); lpc10_encode_release(lpc10_enc_state); lpc10_decode_release(lpc10_dec_state); if (!decompress) { printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy); printf("Difference energy is %f%% of the total.\n", 100.0*diff_energy/ref_energy); if (fabs(1.0 - post_energy/pre_energy) > 0.05 || fabs(diff_energy/post_energy) > 0.03) { printf("Tests failed.\n"); exit(2); } printf("Tests passed.\n"); } return 0; }
void main(int argc, char *argv[]) { int n; float speech[LPC10_SAMPLES_PER_FRAME]; INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; struct lpc10_decoder_state *st1; struct lpc10_decoder_state *st2; FILE *infile1, *outfile1; FILE *infile2, *outfile2; int done_with_file_1; int done_with_file_2; if (argc != 5) { fprintf(stderr, "Usage: %s infile1 outfile1 infile2 outfile2\n", argv[0]); exit(1); } infile1 = fopen(argv[1], "r"); if (infile1 == NULL) { fprintf(stderr, "%s: Could not open file '%s' for reading.\n", argv[0], argv[1]); exit(1); } outfile1 = fopen(argv[2], "w"); if (outfile1 == NULL) { fprintf(stderr, "%s: Could not open file '%s' for writing.\n", argv[0], argv[2]); exit(1); } infile2 = fopen(argv[3], "r"); if (infile2 == NULL) { fprintf(stderr, "%s: Could not open file '%s' for reading.\n", argv[0], argv[3]); exit(1); } outfile2 = fopen(argv[4], "w"); if (outfile2 == NULL) { fprintf(stderr, "%s: Could not open file '%s' for writing.\n", argv[0], argv[4]); exit(1); } st1 = create_lpc10_decoder_state(); st2 = create_lpc10_decoder_state(); if (st1 == 0 || st2 == 0) { fprintf(stderr, "Couldn't allocate %d bytes for two sets of decoder state.\n", 2 * sizeof(struct lpc10_decoder_state)); exit(1); } done_with_file_1 = 0; done_with_file_2 = 0; do { /* Read and process one frame from file 1. */ n = read_bits(infile1, bits, LPC10_BITS_IN_COMPRESSED_FRAME); if (n != LPC10_BITS_IN_COMPRESSED_FRAME) { done_with_file_1 = 1; } if (!done_with_file_1) { lpc10_decode(bits, speech, st1); n = write_16bit_samples(outfile1, speech, LPC10_SAMPLES_PER_FRAME); } /* Now do the same thing for file 2. */ n = read_bits(infile2, bits, LPC10_BITS_IN_COMPRESSED_FRAME); if (n != LPC10_BITS_IN_COMPRESSED_FRAME) { done_with_file_2 = 1; } if (!done_with_file_2) { lpc10_decode(bits, speech, st2); n = write_16bit_samples(outfile2, speech, LPC10_SAMPLES_PER_FRAME); } } while (!(done_with_file_1 && done_with_file_2)); exit(0); }