2
0

tcodec2.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*---------------------------------------------------------------------------*\
  2. FILE........: tcodec2.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 24/8/10
  5. Test program for codec2.c functions.
  6. \*---------------------------------------------------------------------------*/
  7. /*
  8. Copyright (C) 2010 David Rowe
  9. All rights reserved.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU Lesser General Public License version 2, as
  12. published by the Free Software Foundation. This program is
  13. distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  16. License for more details.
  17. You should have received a copy of the GNU Lesser General Public License
  18. along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <assert.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #include "defines.h"
  26. #include "comp.h"
  27. #include "codec2.h"
  28. #include "quantise.h"
  29. #include "interp.h"
  30. /* CODEC2 struct copies from codec2.c to help with testing */
  31. struct CODEC2 {
  32. int mode;
  33. float w[M]; /* time domain hamming window */
  34. COMP W[FFT_ENC]; /* DFT of w[] */
  35. float Pn[2*N]; /* trapezoidal synthesis window */
  36. float Sn[M]; /* input speech */
  37. float hpf_states[2]; /* high pass filter states */
  38. void *nlp; /* pitch predictor states */
  39. float Sn_[2*N]; /* synthesised output speech */
  40. float ex_phase; /* excitation model phase track */
  41. float bg_est; /* background noise estimate for post filter */
  42. float prev_Wo; /* previous frame's pitch estimate */
  43. MODEL prev_model; /* previous frame's model parameters */
  44. float prev_lsps_[LPC_ORD]; /* previous frame's LSPs */
  45. float prev_energy; /* previous frame's LPC energy */
  46. };
  47. void analyse_one_frame(struct CODEC2 *c2, MODEL *model, short speech[]);
  48. void synthesise_one_frame(struct CODEC2 *c2, short speech[], MODEL *model, float ak[]);
  49. int test1()
  50. {
  51. FILE *fin, *fout;
  52. short buf[N];
  53. struct CODEC2 *c2;
  54. MODEL model;
  55. float ak[LPC_ORD+1];
  56. float lsps[LPC_ORD];
  57. c2 = codec2_create(CODEC2_MODE_2400);
  58. fin = fopen("../raw/hts1a.raw", "rb");
  59. assert(fin != NULL);
  60. fout = fopen("hts1a_test.raw", "wb");
  61. assert(fout != NULL);
  62. while(fread(buf, sizeof(short), N, fin) == N) {
  63. analyse_one_frame(c2, &model, buf);
  64. speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD);
  65. synthesise_one_frame(c2, buf, &model, ak);
  66. fwrite(buf, sizeof(short), N, fout);
  67. }
  68. codec2_destroy(c2);
  69. fclose(fin);
  70. fclose(fout);
  71. return 0;
  72. }
  73. int test2()
  74. {
  75. FILE *fin, *fout;
  76. short buf[2*N];
  77. struct CODEC2 *c2;
  78. MODEL model, model_interp;
  79. float ak[LPC_ORD+1];
  80. int voiced1, voiced2;
  81. int lsp_indexes[LPC_ORD];
  82. int energy_index;
  83. int Wo_index;
  84. char *bits;
  85. int nbit;
  86. int i;
  87. float lsps[LPC_ORD];
  88. float e;
  89. c2 = codec2_create(CODEC2_MODE_2400);
  90. bits = (char*)malloc(codec2_bits_per_frame(c2));
  91. assert(bits != NULL);
  92. fin = fopen("../raw/hts1a.raw", "rb");
  93. assert(fin != NULL);
  94. fout = fopen("hts1a_test.raw", "wb");
  95. assert(fout != NULL);
  96. while(fread(buf, sizeof(short), 2*N, fin) == 2*N) {
  97. /* first 10ms analysis frame - we just want voicing */
  98. analyse_one_frame(c2, &model, buf);
  99. voiced1 = model.voiced;
  100. /* second 10ms analysis frame */
  101. analyse_one_frame(c2, &model, &buf[N]);
  102. voiced2 = model.voiced;
  103. Wo_index = encode_Wo(model.Wo);
  104. e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD);
  105. encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD);
  106. energy_index = encode_energy(e);
  107. nbit = 0;
  108. pack((unsigned char*)bits, (unsigned *)&nbit, Wo_index, WO_BITS);
  109. for(i=0; i<LPC_ORD; i++) {
  110. pack((unsigned char*)bits, (unsigned *)&nbit, lsp_indexes[i], lsp_bits(i));
  111. }
  112. pack((unsigned char*)bits, (unsigned *)&nbit, energy_index, E_BITS);
  113. pack((unsigned char*)bits, (unsigned *)&nbit, voiced1, 1);
  114. pack((unsigned char*)bits, (unsigned *)&nbit, voiced2, 1);
  115. nbit = 0;
  116. Wo_index = unpack((unsigned char*)bits, (unsigned *)&nbit, WO_BITS);
  117. for(i=0; i<LPC_ORD; i++) {
  118. lsp_indexes[i] = unpack((unsigned char*)bits, (unsigned *)&nbit, lsp_bits(i));
  119. }
  120. energy_index = unpack((unsigned char*)bits, (unsigned *)&nbit, E_BITS);
  121. voiced1 = unpack((unsigned char*)bits, (unsigned *)&nbit, 1);
  122. voiced2 = unpack((unsigned char*)bits, (unsigned *)&nbit, 1);
  123. model.Wo = decode_Wo(Wo_index);
  124. model.L = PI/model.Wo;
  125. decode_amplitudes(&model,
  126. ak,
  127. lsp_indexes,
  128. energy_index,
  129. lsps,
  130. &e);
  131. model.voiced = voiced2;
  132. model_interp.voiced = voiced1;
  133. interpolate(&model_interp, &c2->prev_model, &model);
  134. synthesise_one_frame(c2, buf, &model_interp, ak);
  135. synthesise_one_frame(c2, &buf[N], &model, ak);
  136. memcpy(&c2->prev_model, &model, sizeof(MODEL));
  137. fwrite(buf, sizeof(short), 2*N, fout);
  138. }
  139. free(bits);
  140. codec2_destroy(c2);
  141. fclose(fin);
  142. fclose(fout);
  143. return 0;
  144. }
  145. int test3()
  146. {
  147. FILE *fin, *fout, *fbits;
  148. short buf1[2*N];
  149. short buf2[2*N];
  150. char *bits;
  151. struct CODEC2 *c2;
  152. c2 = codec2_create(CODEC2_MODE_2400);
  153. int numBits = codec2_bits_per_frame(c2);
  154. int numBytes = (numBits+7)>>3;
  155. bits = (char*)malloc(numBytes);
  156. fin = fopen("../raw/hts1a.raw", "rb");
  157. assert(fin != NULL);
  158. fout = fopen("hts1a_test.raw", "wb");
  159. assert(fout != NULL);
  160. fbits = fopen("hts1a_test3.bit", "wb");
  161. assert(fout != NULL);
  162. while(fread(buf1, sizeof(short), 2*N, fin) == 2*N) {
  163. codec2_encode(c2, (void*)bits, buf1);
  164. fwrite(bits, sizeof(char), numBytes, fbits);
  165. codec2_decode(c2, buf2, (void*)bits);
  166. fwrite(buf2, sizeof(short), numBytes, fout);
  167. }
  168. free(bits);
  169. codec2_destroy(c2);
  170. fclose(fin);
  171. fclose(fout);
  172. fclose(fbits);
  173. return 0;
  174. }
  175. int main() {
  176. test3();
  177. return 0;
  178. }