/*---------------------------------------------------------------------------*\
FILE........: c2sim.c
AUTHOR......: David Rowe
DATE CREATED: 20/8/2010
Codec2 simulation. Combines encoder and decoder and allows
switching in and out various algorithms and quantisation steps. Used
for algorithm development.
\*---------------------------------------------------------------------------*/
/*
Copyright (C) 2009 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 .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include "defines.h"
#include "sine.h"
#include "nlp.h"
#include "dump.h"
#include "lpc.h"
#include "lsp.h"
#include "quantise.h"
#include "phase.h"
#include "postfilter.h"
#include "interp.h"
#include "ampexp.h"
#include "phaseexp.h"
void synth_one_frame(kiss_fft_cfg fft_inv_cfg, short buf[], MODEL *model, float Sn_[], float Pn[], int prede, float *de_mem, float gain);
void print_help(const struct option *long_options, int num_opts, char* argv[]);
/*---------------------------------------------------------------------------*\
MAIN
\*---------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
FILE *fout = NULL; /* output speech file */
FILE *fin; /* input speech file */
short buf[N]; /* input/output buffer */
float Sn[M]; /* float input speech samples */
float Sn_pre[M]; /* pre-emphasised input speech samples */
COMP Sw[FFT_ENC]; /* DFT of Sn[] */
kiss_fft_cfg fft_fwd_cfg;
kiss_fft_cfg fft_inv_cfg;
float w[M]; /* time domain hamming window */
COMP W[FFT_ENC]; /* DFT of w[] */
MODEL model;
float Pn[2*N]; /* trapezoidal synthesis window */
float Sn_[2*N]; /* synthesised speech */
int i; /* loop variable */
int frames;
float prev_Wo, prev__Wo, uq_Wo, prev_uq_Wo;
float pitch;
int voiced1 = 0;
char out_file[MAX_STR];
char ampexp_arg[MAX_STR];
char phaseexp_arg[MAX_STR];
float snr;
float sum_snr;
int lpc_model = 0, order = LPC_ORD;
int lsp = 0, lspd = 0, lspvq = 0;
int lspres = 0;
int lspdt = 0, lspdt_mode = LSPDT_ALL;
int dt = 0, lspjvm = 0, lspanssi = 0, lspjnd = 0, lspmel = 0;
int prede = 0;
float pre_mem = 0.0, de_mem = 0.0;
float ak[LPC_MAX];
COMP Sw_[FFT_ENC];
COMP Ew[FFT_ENC];
int phase0 = 0;
float ex_phase[MAX_AMP+1];
int postfilt;
float bg_est;
int hand_voicing = 0, phaseexp = 0, ampexp = 0, hi = 0, simlpcpf = 0;
int lpcpf = 0;
FILE *fvoicing = 0;
MODEL prev_model, interp_model;
int decimate = 0;
float lsps[LPC_MAX];
float prev_lsps[LPC_MAX], prev_lsps_[LPC_MAX];
float lsps__prev[LPC_MAX];
float lsps__prev2[LPC_MAX];
float e, prev_e;
float ak_interp[LPC_MAX];
int lsp_indexes[LPC_MAX];
float lsps_[LPC_MAX];
float Woe_[2];
void *nlp_states;
float hpf_states[2];
int scalar_quant_Wo_e = 0;
int vector_quant_Wo_e = 0;
int dump_pitch_e = 0;
FILE *fjvm = NULL;
#ifdef DUMP
int dump;
#endif
struct PEXP *pexp = NULL;
struct AEXP *aexp = NULL;
float gain = 1.0;
char* opt_string = "ho:";
struct option long_options[] = {
{ "lpc", required_argument, &lpc_model, 1 },
{ "lspjnd", no_argument, &lspjnd, 1 },
{ "lspmel", no_argument, &lspmel, 1 },
{ "lsp", no_argument, &lsp, 1 },
{ "lspd", no_argument, &lspd, 1 },
{ "lspvq", no_argument, &lspvq, 1 },
{ "lspres", no_argument, &lspres, 1 },
{ "lspdt", no_argument, &lspdt, 1 },
{ "lspdt_mode", required_argument, NULL, 0 },
{ "lspjvm", no_argument, &lspjvm, 1 },
{ "lspanssi", no_argument, &lspanssi, 1 },
{ "phase0", no_argument, &phase0, 1 },
{ "phaseexp", required_argument, &phaseexp, 1 },
{ "ampexp", required_argument, &exp, 1 },
{ "postfilter", no_argument, &postfilt, 1 },
{ "hand_voicing", required_argument, &hand_voicing, 1 },
{ "dec", no_argument, &decimate, 1 },
{ "dt", no_argument, &dt, 1 },
{ "hi", no_argument, &hi, 1 },
{ "simlpcpf", no_argument, &simlpcpf, 1 },
{ "lpcpf", no_argument, &lpcpf, 1 },
{ "prede", no_argument, &prede, 1 },
{ "dump_pitch_e", required_argument, &dump_pitch_e, 1 },
{ "sq_pitch_e", no_argument, &scalar_quant_Wo_e, 1 },
{ "vq_pitch_e", no_argument, &vector_quant_Wo_e, 1 },
{ "rate", required_argument, NULL, 0 },
{ "gain", required_argument, NULL, 0 },
#ifdef DUMP
{ "dump", required_argument, &dump, 1 },
#endif
{ "help", no_argument, NULL, 'h' },
{ NULL, no_argument, NULL, 0 }
};
int num_opts=sizeof(long_options)/sizeof(struct option);
for(i=0; i 20)) {
fprintf(stderr, "Error in LPC order: %s\n", optarg);
exit(1);
}
#ifdef DUMP
} else if(strcmp(long_options[option_index].name, "dump") == 0) {
if (dump)
dump_on(optarg);
#endif
} else if(strcmp(long_options[option_index].name, "lsp") == 0
|| strcmp(long_options[option_index].name, "lspd") == 0
|| strcmp(long_options[option_index].name, "lspvq") == 0) {
assert(order == LPC_ORD);
} else if(strcmp(long_options[option_index].name, "lspdt_mode") == 0) {
if (strcmp(optarg,"all") == 0)
lspdt_mode = LSPDT_ALL;
else if (strcmp(optarg,"low") == 0)
lspdt_mode = LSPDT_LOW;
else if (strcmp(optarg,"high") == 0)
lspdt_mode = LSPDT_HIGH;
else {
fprintf(stderr, "Error in lspdt_mode: %s\n", optarg);
exit(1);
}
} else if(strcmp(long_options[option_index].name, "hand_voicing") == 0) {
if ((fvoicing = fopen(optarg,"rt")) == NULL) {
fprintf(stderr, "Error opening voicing file: %s: %s.\n",
optarg, strerror(errno));
exit(1);
}
} else if(strcmp(long_options[option_index].name, "dump_pitch_e") == 0) {
if ((fjvm = fopen(optarg,"wt")) == NULL) {
fprintf(stderr, "Error opening pitch & energy dump file: %s: %s.\n",
optarg, strerror(errno));
exit(1);
}
} else if(strcmp(long_options[option_index].name, "phaseexp") == 0) {
strcpy(phaseexp_arg, optarg);
} else if(strcmp(long_options[option_index].name, "ampexp") == 0) {
strcpy(ampexp_arg, optarg);
} else if(strcmp(long_options[option_index].name, "gain") == 0) {
gain = atof(optarg);
} else if(strcmp(long_options[option_index].name, "rate") == 0) {
if(strcmp(optarg,"3200") == 0) {
lpc_model = 1; order = 10;
scalar_quant_Wo_e = 1;
lspd = 1;
phase0 = 1;
postfilt = 1;
decimate = 1;
lpcpf = 1;
} else if(strcmp(optarg,"2400") == 0) {
lpc_model = 1; order = 10;
vector_quant_Wo_e = 1;
lsp = 1;
phase0 = 1;
postfilt = 1;
decimate = 1;
lpcpf = 1;
} else if(strcmp(optarg,"1400") == 0) {
lpc_model = 1; order = 10;
vector_quant_Wo_e = 1;
lsp = 1; lspdt = 1;
phase0 = 1;
postfilt = 1;
decimate = 1;
dt = 1;
lpcpf = 1;
} else if(strcmp(optarg,"1200") == 0) {
lpc_model = 1; order = 10;
scalar_quant_Wo_e = 1;
lspjvm = 1; lspdt = 1;
phase0 = 1;
postfilt = 1;
decimate = 1;
dt = 1;
lpcpf = 1;
} else {
fprintf(stderr, "Error: invalid output rate %s\n", optarg);
exit(1);
}
}
break;
case 'h':
print_help(long_options, num_opts, argv);
break;
case 'o':
if (strcmp(optarg, "-") == 0) fout = stdout;
else if ((fout = fopen(optarg,"wb")) == NULL) {
fprintf(stderr, "Error opening output speech file: %s: %s.\n",
optarg, strerror(errno));
exit(1);
}
strcpy(out_file,optarg);
break;
default:
/* This will never be reached */
break;
}
}
/* Input file */
if ((fin = fopen(argv[optind],"rb")) == NULL) {
fprintf(stderr, "Error opening input speech file: %s: %s.\n",
argv[optind], strerror(errno));
exit(1);
}
ex_phase[0] = 0;
bg_est = 0.0;
Woe_[0] = Woe_[1] = 1.0;
/*
printf("lspd: %d lspdt: %d lspdt_mode: %d phase0: %d postfilt: %d "
"decimate: %d dt: %d\n",lspd,lspdt,lspdt_mode,phase0,postfilt,
decimate,dt);
*/
/* Initialise ------------------------------------------------------------*/
fft_fwd_cfg = kiss_fft_alloc(FFT_ENC, 0, NULL, NULL); /* fwd FFT,used in several places */
fft_inv_cfg = kiss_fft_alloc(FFT_DEC, 1, NULL, NULL); /* inverse FFT, used just for synth */
make_analysis_window(fft_fwd_cfg, w, W);
make_synthesis_window(Pn);
quantise_init();
if (phaseexp)
pexp = phase_experiment_create();
if (ampexp)
aexp = amp_experiment_create();
/*----------------------------------------------------------------*\
Main Loop
\*----------------------------------------------------------------*/
frames = 0;
sum_snr = 0;
while(fread(buf,sizeof(short),N,fin)) {
frames++;
//printf("frame: %d ", frames);
/* Read input speech */
for(i=0; i 32767.0)
buf[i] = 32767;
else if (Sn_[i] < -32767.0)
buf[i] = -32767;
else
buf[i] = Sn_[i];
}
}
void print_help(const struct option* long_options, int num_opts, char* argv[])
{
int i;
char *option_parameters;
fprintf(stderr, "\nCodec2 - low bit rate speech codec - Simulation Program\n"
"\thttp://rowetel.com/codec2.html\n\n"
"usage: %s [OPTIONS] \n\n"
"Options:\n"
"\t-o \n", argv[0]);
for(i=0; i