From c923a9163412402d1b3e60fe91df61b870105910 Mon Sep 17 00:00:00 2001 From: drowe67 Date: Tue, 24 Aug 2010 07:02:39 +0000 Subject: [PATCH] alpha version fully quantised codec at 51 bits per 20ms frame working, yaaaaaaayyyyy git-svn-id: https://svn.code.sf.net/p/freetel/code@185 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2/src/c2dec.c | 5 +- codec2/src/c2enc.c | 3 + codec2/src/c2sim.c | 57 ++++++---- codec2/src/codec2.c | 12 ++- codec2/src/quantise.c | 8 +- codec2/src/quantise.h | 13 +++ codec2/unittest/Makefile | 9 +- codec2/unittest/tcodec2.c | 215 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 292 insertions(+), 30 deletions(-) create mode 100644 codec2/unittest/tcodec2.c diff --git a/codec2/src/c2dec.c b/codec2/src/c2dec.c index 815a7746..642ba41d 100644 --- a/codec2/src/c2dec.c +++ b/codec2/src/c2dec.c @@ -39,6 +39,7 @@ int main(int argc, char *argv[]) FILE *fout; short buf[CODEC2_SAMPLES_PER_FRAME]; char bits[CODEC2_BITS_PER_FRAME]; + int i; if (argc != 3) { printf("usage: %s InputBitFile OutputRawSpeechFile\n", argv[0]); @@ -59,8 +60,10 @@ int main(int argc, char *argv[]) codec2 = codec2_create(); - while(fread(bits, sizeof(buf), CODEC2_BITS_PER_FRAME, fin) == + while(fread(bits, sizeof(char), CODEC2_BITS_PER_FRAME, fin) == CODEC2_BITS_PER_FRAME) { + //for(i=0; iw); pack(bits, &nbit, Wo_index, WO_BITS); - for(i=0; iprev_model, &model); diff --git a/codec2/src/quantise.c b/codec2/src/quantise.c index f44a099e..b5ba7fae 100644 --- a/codec2/src/quantise.c +++ b/codec2/src/quantise.c @@ -74,7 +74,7 @@ LSP_CB lsp_q[] = { {1,4,16, "../unittest/lsp7.txt"}, {1,3,8, "../unittest/lsp8.txt"}, {1,3,8, "../unittest/lsp9.txt"}, - {1,3,4, "../unittest/lsp10.txt"}, + {1,2,4, "../unittest/lsp10.txt"}, {0,0,0, ""} }; @@ -697,7 +697,7 @@ void bw_expand_lsps(float lsp[], /*---------------------------------------------------------------------------*\ - FUNCTION....: lpc_correction() + FUNCTION....: need_lpc_correction() AUTHOR......: David Rowe DATE CREATED: 22/8/2010 @@ -834,6 +834,7 @@ void encode_amplitudes(int lsp_indexes[], float lsps[LPC_ORD]; float ak[LPC_ORD+1]; float e; + int i; e = speech_to_uq_lsps(lsps, ak, Sn, w, LPC_ORD); encode_lsps(lsp_indexes, lsps, LPC_ORD); @@ -865,6 +866,7 @@ float decode_amplitudes(MODEL *model, decode_lsps(lsps, lsp_indexes, LPC_ORD); bw_expand_lsps(lsps, LPC_ORD); + lsp_to_lpc(lsps, ak, LPC_ORD); e = decode_energy(energy_index); aks_to_M2(ak, LPC_ORD, model, e, &snr); apply_lpc_correction(model, lpc_correction); @@ -911,7 +913,7 @@ int unpack(char bits[], int *nbit, int index_bits) for(i=0; i +#include +#include +#include +#include +#include "defines.h" +#include "codec2.h" +#include "quantise.h" +#include "interp.h" + +/* CODEC2 struct copies from codec2.c to help with testing */ + +typedef struct { + float Sn[M]; /* input speech */ + float w[M]; /* time domain hamming window */ + COMP W[FFT_ENC]; /* DFT of w[] */ + float Pn[2*N]; /* trapezoidal synthesis window */ + float Sn_[2*N]; /* synthesised speech */ + float prev_Wo; /* previous frame's pitch estimate */ + float ex_phase; /* excitation model phase track */ + float bg_est; /* background noise estimate for post filter */ + MODEL prev_model; /* model parameters from 20ms ago */ +} CODEC2; + +void analyse_one_frame(CODEC2 *c2, MODEL *model, short speech[]); +void synthesise_one_frame(CODEC2 *c2, short speech[], MODEL *model, float ak[]); + +int test1() +{ + FILE *fin, *fout; + short buf[N]; + void *c2; + CODEC2 *c3; + MODEL model; + float ak[LPC_ORD+1]; + float lsps[LPC_ORD]; + + c2 = codec2_create(); + c3 = (CODEC2*)c2; + + fin = fopen("../raw/hts1a.raw", "rb"); + assert(fin != NULL); + fout = fopen("hts1a_test.raw", "wb"); + assert(fout != NULL); + + while(fread(buf, sizeof(short), N, fin) == N) { + analyse_one_frame(c3, &model, buf); + speech_to_uq_lsps(lsps, ak, c3->Sn, c3->w, LPC_ORD); + synthesise_one_frame(c3, buf, &model, ak); + fwrite(buf, sizeof(short), N, fout); + } + + codec2_destroy(c2); + + fclose(fin); + fclose(fout); + + return 0; +} + +int test2() +{ + FILE *fin, *fout; + short buf[2*N]; + void *c2; + CODEC2 *c3; + MODEL model, model_interp; + float ak[LPC_ORD+1]; + int voiced1, voiced2; + int lsp_indexes[LPC_ORD]; + int lpc_correction; + int energy_index; + int Wo_index; + char bits[CODEC2_BITS_PER_FRAME]; + int nbit; + int i; + + c2 = codec2_create(); + c3 = (CODEC2*)c2; + + fin = fopen("../raw/hts1a.raw", "rb"); + assert(fin != NULL); + fout = fopen("hts1a_test.raw", "wb"); + assert(fout != NULL); + + while(fread(buf, sizeof(short), 2*N, fin) == 2*N) { + /* first 10ms analysis frame - we just want voicing */ + + analyse_one_frame(c3, &model, buf); + voiced1 = model.voiced; + + /* second 10ms analysis frame */ + + analyse_one_frame(c3, &model, &buf[N]); + voiced2 = model.voiced; + + Wo_index = encode_Wo(model.Wo); + encode_amplitudes(lsp_indexes, + &lpc_correction, + &energy_index, + &model, + c3->Sn, + c3->w); + nbit = 0; + pack(bits, &nbit, Wo_index, WO_BITS); + for(i=0; iprev_model, &model); + + synthesise_one_frame(c3, buf, &model_interp, ak); + synthesise_one_frame(c3, &buf[N], &model, ak); + + memcpy(&c3->prev_model, &model, sizeof(MODEL)); + fwrite(buf, sizeof(short), 2*N, fout); + } + + codec2_destroy(c2); + + fclose(fin); + fclose(fout); + + return 0; +} + +int test3() +{ + FILE *fin, *fout, *fbits; + short buf1[2*N]; + short buf2[2*N]; + char bits[CODEC2_BITS_PER_FRAME]; + void *c2; + + c2 = codec2_create(); + + fin = fopen("../raw/hts1a.raw", "rb"); + assert(fin != NULL); + fout = fopen("hts1a_test.raw", "wb"); + assert(fout != NULL); + fbits = fopen("hts1a_test3.bit", "wb"); + assert(fout != NULL); + + while(fread(buf1, sizeof(short), 2*N, fin) == 2*N) { + codec2_encode(c2, bits, buf1); + fwrite(bits, sizeof(char), CODEC2_BITS_PER_FRAME, fbits); + codec2_decode(c2, buf2, bits); + fwrite(buf2, sizeof(short), CODEC2_SAMPLES_PER_FRAME, fout); + } + + codec2_destroy(c2); + + fclose(fin); + fclose(fout); + fclose(fbits); + + return 0; +} + +int main() { + test3(); + return 0; +} -- 2.25.1