return 0;
}
+void synth_one_frame(short buf[], MODEL *model, float Sn_[], float Pn[]);
+
/*---------------------------------------------------------------------------*\
MAIN
int frames;
float prev_Wo;
float pitch;
+ int voiced1;
+ float phi1[MAX_AMP];
char out_file[MAX_STR];
int arg;
int lpc_model, order;
int lsp, lsp_quantiser;
- float ak[LPC_MAX+1];
+ float ak[LPC_MAX];
+ COMP Sw_[FFT_ENC];
int dump;
int hand_voicing;
FILE *fvoicing;
- MODEL model_1, model_2, model_3, model_synth, model_a, model_b;
- int transition, decimate;
+ MODEL prev_model, interp_model, tmp_model;
+ int decimate;
+ float tmp_phi = 0;
+ float ak_phase[LPC_ORD+1];
for(i=0; i<M; i++)
Sn[i] = 1.0;
prev_Wo = TWO_PI/P_MAX;
- model_1.Wo = TWO_PI/P_MIN;
- model_1.L = floor(PI/model_1.Wo);
- for(i=1; i<=model_1.L; i++) {
- model_1.A[i] = 0.0;
- model_1.phi[i] = 0.0;
+ prev_model.Wo = TWO_PI/P_MIN;
+ prev_model.L = floor(PI/prev_model.Wo);
+ for(i=1; i<=prev_model.L; i++) {
+ prev_model.A[i] = 0.0;
+ prev_model.phi[i] = 0.0;
}
for(i=1; i<=MAX_AMP; i++) {
ex_phase[i] = 0.0;
}
- model_synth = model_3 = model_2 = model_1;
if (argc < 2) {
printf("\nCodec2 - 2400 bit/s speech codec - Simulation Program\n");
postfilt = switch_present("--postfilter",argc,argv);
decimate = switch_present("--dec",argc,argv);
- transition = 0;
/* Initialise ------------------------------------------------------------*/
Sn[i] = Sn[i+N];
for(i=0; i<N; i++)
Sn[i+M-N] = buf[i];
- dump_Sn(Sn);
/* Estimate pitch */
/* estimate model parameters */
- dft_speech(Sw, Sn, w); dump_Sw(Sw);
+ dft_speech(Sw, Sn, w);
two_stage_pitch_refinement(&model, Sw);
estimate_amplitudes(&model, Sw, W);
dump_Sn(Sn); dump_Sw(Sw); dump_model(&model);
if (phase0) {
float Wn[M]; /* windowed speech samples */
- float ak_phase[LPC_ORD+1]; /* autocorrelation coeffs */
float Rk[LPC_ORD+1]; /* autocorrelation coeffs */
- COMP Sw_[FFT_ENC];
dump_phase(&model.phi[0], model.L);
for(i=0; i<MAX_AMP; i++)
model.phi[i] = 0;
+
if (hand_voicing) {
fscanf(fvoicing,"%d\n",&model.voiced);
}
- phase_synth_zero_order(&model, ak_phase, ex_phase);
-
- if (postfilt)
- postfilter(&model, &bg_est);
}
/* optional LPC model amplitudes */
/* option decimation to 20ms rate, which enables interpolation
routine to synthesise in between frame */
-#ifdef FIX_ME_FOR_NEW_INTERP_ROUTINE
+
if (decimate) {
- if (frames%2) {
-
- /* odd frames use the original model parameters */
-
- model_synth = model_2;
- transition = 0;
- }
- else {
- interp(&model_3, &model_1, &model_synth, &model_a, &model_b,
- &transition);
-
- /* phase need to be supplied outside of this routine, e.g. via
- a phase model */
-
- for(i=1; i<=model_synth.L; i++)
- model_synth.phi[i] = model_2.phi[i];
+ if (!phase0) {
+ printf("needs --phase0 to resample phase for interpolated Wo\n");
+ exit(0);
}
- model_3 = model_2;
- model_2 = model_1;
- model_1 = model;
- model = model_synth;
- }
-#endif
- /*
- Simulate Wo quantisation noise
- model.Wo += 2.0*(PI/8000)*(1.0 - 2.0*(float)rand()/RAND_MAX);
- if (model.Wo > TWO_PI/20.0) model.Wo = TWO_PI/20.0;
- if (model.Wo < TWO_PI/160.0) model.Wo = TWO_PI/160.0;
- model.L = floor(PI/model.Wo);
- */
-
- /* Synthesise speech */
-
- if (fout != NULL) {
-
- if (transition) {
- synthesise(Sn_,&model_a,Pn,1);
- synthesise(Sn_,&model_b,Pn,0);
- }
- else {
- synthesise(Sn_,&model,Pn,1);
- }
+ /* odd frame - interpolate */
- /* Save output speech to disk */
+ if (frames%2) {
- for(i=0; i<N; i++) {
- if (Sn_[i] > 32767.0)
- buf[i] = 32767;
- else if (Sn_[i] < -32767.0)
- buf[i] = -32767;
+ #ifdef TEST
+ model.voiced = 1;
+ prev_model.voiced = 1;
+ if (fabs(prev_model.Wo - model.Wo) < 0.1*model.Wo) {
+ interp_model.voiced = 1;
+ interpolate(&interp_model, &prev_model, &model);
+ for(i=0; i<=interp_model.L; i++) {
+ interp_model.phi[i] = phi1[i];
+ }
+ printf("interp\n");
+ }
else
- buf[i] = Sn_[i];
+ interp_model = tmp_model;
+ #endif
+
+ interp_model.voiced = voiced1;
+ interpolate(&interp_model, &prev_model, &model);
+
+ if (phase0)
+ phase_synth_zero_order(&interp_model, ak_phase, ex_phase);
+ if (postfilt)
+ postfilter(&interp_model, &bg_est);
+ synth_one_frame(buf, &interp_model, Sn_, Pn);
+ fwrite(buf,sizeof(short),N,fout);
+
+ if (phase0)
+ phase_synth_zero_order(&model, ak_phase, ex_phase);
+ if (postfilt)
+ postfilter(&model, &bg_est);
+ synth_one_frame(buf, &model, Sn_, Pn);
+ fwrite(buf,sizeof(short),N,fout);
+
+ prev_model = model;
+ }
+ else {
+ voiced1 = model.voiced;
}
+ }
+ else {
+ if (phase0)
+ phase_synth_zero_order(&model, ak_phase, ex_phase);
+ if (postfilt)
+ postfilter(&model, &bg_est);
+ synth_one_frame(buf, &model, Sn_, Pn);
fwrite(buf,sizeof(short),N,fout);
- }
+ }
}
if (fout != NULL)
return 0;
}
+void synth_one_frame(short buf[], MODEL *model, float Sn_[], float Pn[])
+{
+ int i;
+
+ synthesise(Sn_, model, Pn, 1);
+
+ for(i=0; i<N; i++) {
+ if (Sn_[i] > 32767.0)
+ buf[i] = 32767;
+ else if (Sn_[i] < -32767.0)
+ buf[i] = -32767;
+ else
+ buf[i] = Sn_[i];
+ }
+
+}
#include <stdio.h>
#include <string.h>
#include <math.h>
+#include <ctype.h>
#include "defines.h"
-#include "dump.h"
+#include "sine.h"
#include "interp.h"
-int main() {
- MODEL prev, next, interp;
- int l;
+void make_amp(MODEL *model, float f0, float cdB, float mdBHz)
+{
+ int i;
+ float mdBrad = mdBHz*FS/TWO_PI;
+
+ model->Wo = f0*TWO_PI/FS;
+ model->L = PI/model->Wo;
+ for(i=0; i<=model->L; i++)
+ model->A[i] = pow(10.0,(cdB + (float)i*model->Wo*mdBrad)/20.0);
+ model->voiced = 1;
+}
+
+void write_amp(char file[], MODEL *model)
+{
FILE *f;
+ int i;
- f = fopen("interp.txt","wt");
+ f = fopen(file,"wt");
+ for(i=1; i<=model->L; i++)
+ fprintf(f, "%f\t%f\n", model->Wo*i, model->A[i]);
+ fclose(f);
+}
- prev.Wo = PI/20;
- prev.L = PI/prev.Wo;
- prev.voiced = 1;
- for(l=1;l<=prev.L; l++)
- prev.A[l] = 1000.0;
+char *get_next_float(char *s, float *num)
+{
+ char *p = s;
+ char tmp[MAX_STR];
- next.Wo = PI/30;
- next.L = PI/next.Wo;
- next.voiced = 1;
- for(l=1;l<=next.L; l++)
- next.A[l] = 2000.0;
+ while(*p && !isspace(*p))
+ p++;
+ memcpy(tmp, s, p-s);
+ tmp[p-s] = 0;
+ *num = atof(tmp);
- interp.voiced = 1.0;
- interpolate(&interp, &prev, &next);
- printf("Wo = %f\n", interp.Wo);
-
- for(l=1; l<=interp.L; l++)
- fprintf(f, "%f\n", interp.A[l]);
+ return p+1;
+}
+
+char *get_next_int(char *s, int *num)
+{
+ char *p = s;
+ char tmp[MAX_STR];
+
+ while(*p && !isspace(*p))
+ p++;
+ memcpy(tmp, s, p-s);
+ tmp[p-s] = 0;
+ *num = atoi(tmp);
+
+ return p+1;
+}
+void load_amp(MODEL *model, char file[], int frame)
+{
+ FILE *f;
+ int i;
+ char s[1024];
+ char *ps;
+
+ f = fopen(file,"rt");
+
+ for(i=0; i<frame; i++)
+ fgets(s, 1023, f);
+
+ ps = s;
+ ps = get_next_float(ps, &model->Wo);
+ ps = get_next_int(ps, &model->L);
+ for(i=1; i<=model->L; i++)
+ ps = get_next_float(ps, &model->A[i]);
+
fclose(f);
+}
+
+int main() {
+ MODEL prev, next, interp;
+
+ //make_amp(&prev, 50.0, 60.0, 6E-3);
+ //make_amp(&next, 50.0, 40.0, 6E-3);
+ load_amp(&prev, "../src/hts1a_model.txt", 32);
+ load_amp(&next, "../src/hts1a_model.txt", 34);
+
+ interp.voiced = 1;
+ interpolate(&interp, &prev, &next);
+
+ write_amp("tinterp_prev.txt", &prev);
+ write_amp("tinterp_interp.txt", &interp);
+ write_amp("tinterp_next.txt", &next);
+
return 0;
}