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