i've tested, now you can too

This commit is contained in:
Brian West
2012-12-20 20:08:42 -06:00
parent d3fcfa8245
commit d67b96af8a
94 changed files with 25305 additions and 2005 deletions

View File

@@ -1,41 +1,105 @@
AM_CFLAGS = -I../src -g -DFLOATING_POINT -DVAR_ARRAYS
AUTOMAKE_OPTIONS = gnu
AM_CFLAGS = -I../src -fPIC -g -DFLOATING_POINT -DVAR_ARRAYS -O2 -Wall
AUTOMAKE_OPTS = gnu
NAME = libcodec2
AM_CPPFLAGS = $(AM_CFLAGS)
bin_PROGRAMS = genres genlsp extract vqtrain tnlp tinterp tquant tcodec2
noinst_PROGRAMS = genres genlsp extract vqtrain vqtrainjnd tnlp tinterp tquant vq_train_jvm scalarlsptest tfdmdv t48_8 lspsync create_interleaver tlspsens vqtrainph genphdata genampdata polar2rect vqtrainsp tprede pre de tfifo
genres_SOURCES = genres.c ../src/lpc.c ../src/codebook.c
genres_SOURCES = genres.c ../src/lpc.c
genres_LDADD = $(lib_LTLIBRARIES)
genres_LDFLAGS = $(LIBS)
genlsp_SOURCES = genlsp.c ../src/lpc.c ../src/lsp.c ../src/codebook.c
genlsp_SOURCES = genlsp.c ../src/lpc.c ../src/lsp.c
genlsp_LDADD = $(lib_LTLIBRARIES)
genlsp_LDFLAGS = $(LIBS)
extract_SOURCES = extract.c ../src/codebook.c
extract_SOURCES = extract.c
extract_LDADD = $(lib_LTLIBRARIES)
extract_LDFLAGS = $(LIBS)
vqtrain_SOURCES = vqtrain.c ../src/codebook.c
vqtrain_SOURCES = vqtrain.c
vqtrain_LDADD = $(lib_LTLIBRARIES)
vqtrain_LDFLAGS = $(LIBS)
tnlp_SOURCES = tnlp.c ../src/sine.c ../src/nlp.c ../src/four1.c ../src/dump.c ../src/codebook.c
vqtrainjnd_SOURCES = vqtrainjnd.c
vqtrainjnd_LDADD = $(lib_LTLIBRARIES)
vqtrainjnd_LDFLAGS = $(LIBS)
vqtrainph_SOURCES = vqtrainph.c
vqtrainph_LDADD = $(lib_LTLIBRARIES)
vqtrainph_LDFLAGS = $(LIBS)
vqtrainsp_SOURCES = vqtrainsp.c
vqtrainsp_LDADD = $(lib_LTLIBRARIES)
vqtrainsp_LDFLAGS = $(LIBS)
genphdata_SOURCES = genphdata.c
genphdata_LDADD = $(lib_LTLIBRARIES)
genphdata_LDFLAGS = $(LIBS)
genampdata_SOURCES = genampdata.c
genampdata_LDADD = $(lib_LTLIBRARIES)
genampdata_LDFLAGS = $(LIBS)
polar2rect_SOURCES = polar2rect.c
polar2rect_LDADD = $(lib_LTLIBRARIES)
polar2rect_LDFLAGS = $(LIBS)
vq_train_jvm_SOURCES = vq_train_jvm.c
vq_train_jvm_LDADD = $(lib_LTLIBRARIES)
vq_train_jvm_LDFLAGS = $(LIBS)
CODEBOOKS = ../src/codebook.c ../src/codebookd.c ../src/codebookvq.c ../src/codebookjnd.c ../src/codebookdt.c ../src/codebookjvm.c ../src/codebookvqanssi.c ../src/codebookge.c
tnlp_SOURCES = tnlp.c ../src/sine.c ../src/nlp.c ../src/kiss_fft.c ../src/dump.c
tnlp_LDADD = $(lib_LTLIBRARIES)
tnlp_LDFLAGS = $(LIBS)
tinterp_SOURCES = tinterp.c ../src/sine.c ../src/four1.c ../src/interp.c ../src/codebook.c
tinterp_SOURCES = tinterp.c ../src/sine.c ../src/kiss_fft.c ../src/interp.c ../src/lpc.c ../src/lsp.c ../src/quantise.c $(CODEBOOKS) ../src/dump.c
tinterp_LDADD = $(lib_LTLIBRARIES)
tinterp_LDFLAGS = $(LIBS)
tquant_SOURCES = tquant.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/four1.c ../src/codebook.c
tquant_SOURCES = tquant.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/kiss_fft.c $(CODEBOOKS)
tquant_LDADD = $(lib_LTLIBRARIES)
tquant_LDFLAGS = $(LIBS)
tcodec2_SOURCES = tcodec2.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/four1.c \
../src/codec2.c ../src/sine.c ../src/nlp.c ../src/postfilter.c ../src/phase.c ../src/interp.c ../src/pack.c ../src/codebook.c
tcodec2_LDADD = $(lib_LTLIBRARIES)
tcodec2_LDFLAGS = $(LIBS)
scalarlsptest_SOURCES = scalarlsptest.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/kiss_fft.c $(CODEBOOKS)
scalarlsptest_LDADD = $(lib_LTLIBRARIES)
scalarlsptest_LDFLAGS = $(LIBS)
tfdmdv_SOURCES = tfdmdv.c ../src/fdmdv.c ../src/kiss_fft.c ../src/octave.c
tfdmdv_LDADD = $(lib_LTLIBRARIES)
tfdmdv_LDFLAGS = $(LIBS)
t48_8_SOURCES = t48_8.c ../src/fdmdv.c ../src/kiss_fft.c
t48_8_LDADD = $(lib_LTLIBRARIES)
t48_8_LDFLAGS = $(LIBS)
lspsync_SOURCES = lspsync.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/kiss_fft.c \
../src/codec2.c ../src/sine.c ../src/nlp.c ../src/postfilter.c ../src/phase.c ../src/interp.c ../src/pack.c $(CODEBOOKS)
lspsync_LDADD = $(lib_LTLIBRARIES)
lspsync_LDFLAGS = $(LIBS)
create_interleaver_SOURCES = create_interleaver.c
create_interleaver_LDADD = $(lib_LTLIBRARIES)
create_interleaver_LDFLAGS = $(LIBS)
tlspsens_SOURCES = tlspsens.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/kiss_fft.c ../src/codec2.c ../src/sine.c ../src/nlp.c ../src/pack.c ../src/interp.c ../src/postfilter.c ../src/phase.c $(CODEBOOKS)
tlspsens_LDADD = $(lib_LTLIBRARIES)
tlspsens_LDFLAGS = $(LIBS)
tprede_SOURCES = tprede.c ../src/lpc.c
tprede_LDADD = $(lib_LTLIBRARIES)
tprede_LDFLAGS = $(LIBS)
pre_SOURCES = pre.c ../src/lpc.c
pre_LDADD = $(lib_LTLIBRARIES)
pre_LDFLAGS = $(LIBS)
de_SOURCES = de.c ../src/lpc.c
de_LDADD = $(lib_LTLIBRARIES)
de_LDFLAGS = $(LIBS)
tfifo_SOURCES = tfifo.c ../src/fifo.c
tfifo_LDADD = $(lib_LTLIBRARIES) -lpthread
tfifo_LDFLAGS = $(LIBS)

View File

@@ -25,15 +25,16 @@
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define MAX_STR 256 /* maximum string length */
#define MAX_STR 2048 /* maximum string length */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
void scan_line(FILE *fp, float f[], int n);
@@ -45,8 +46,8 @@ int main(int argc, char *argv[]) {
long lines; /* lines read so far */
if (argc != 5) {
printf("usage: extract TextFile FloatFile start end\n");
exit(0);
printf("usage: %s TextFile FloatFile start(1 .. 10) end(1 .. 10)\n", argv[0]);
exit(1);
}
/* read command line arguments and open files */
@@ -75,8 +76,10 @@ int main(int argc, char *argv[]) {
lines = 0;
while(!feof(ftext)) {
scan_line(ftext, buf, en);
fwrite(&buf[st-1], sizeof(float), en-st+1, ffloat);
printf("\r%ld lines",lines++);
if (!feof(ftext)) {
fwrite(&buf[st-1], sizeof(float), en-st+1, ffloat);
printf("\r%ld lines",++lines);
}
}
printf("\n");
@@ -108,9 +111,11 @@ void scan_line(FILE *fp, float f[], int n)
char s[MAX_STR];
char *ps,*pe;
int i;
fgets(s,MAX_STR,fp);
ps = pe = s;
memset(s, 0, MAX_STR);
ps = pe = fgets(s,MAX_STR,fp);
if (ps == NULL)
return;
for(i=0; i<n; i++) {
while( isspace(*pe)) pe++;
while( !isspace(*pe)) pe++;

View File

@@ -23,15 +23,15 @@
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define P 10 /* LP order */
#define LSP_DELTA1 0.05 /* grid spacing for LSP root searches */
#define P 12 /* LP order */
#define LSP_DELTA1 0.01 /* grid spacing for LSP root searches */
#define NW 279 /* frame size in samples */
#define N 80 /* frame to frame shift */
#define THRESH 40.0 /* threshold energy/sample for frame inclusion */
#define PI 3.141592654 /* mathematical constant */
#include <stdio.h>
#include <stdlib.h>
@@ -61,19 +61,22 @@ int main(int argc, char *argv[]) {
float Sn[NW]; /* float input speech samples */
float ak[P+1]; /* LPCs for current frame */
float lsp[P]; /* LSPs for current frame */
float lsp_prev[P]; /* LSPs for previous frame */
float E; /* frame energy */
long f; /* number of frames */
long af; /* number frames with "active" speech */
float Eres; /* LPC residual energy */
int i;
int roots;
int unstables;
int lspd;
int lspd, log, lspdt;
float diff;
/* Initialise ------------------------------------------------------*/
if (argc < 3) {
printf("usage: gentest RawFile LSPTextFile [--lspd]\n");
exit(0);
printf("usage: %s RawFile LSPTextFile [--lspd] [--log] [--lspdt] \n", argv[0]);
exit(1);
}
/* Open files */
@@ -91,13 +94,15 @@ int main(int argc, char *argv[]) {
}
lspd = switch_present("--lspd", argc, argv);
log = switch_present("--log", argc, argv);
lspdt = switch_present("--lspdt", argc, argv);
for(i=0; i<NW; i++)
Sn[i] = 0.0;
/* Read SPC file, and determine aks[] for each frame ------------------*/
af = 0;
f = af = 0;
unstables = 0;
while(fread(buf,sizeof(short),N,fspc) == N) {
@@ -117,30 +122,59 @@ int main(int argc, char *argv[]) {
/* If energy high enough, include this frame */
f++;
if (E > THRESH) {
af++;
printf("Active Frame: %ld unstables: %d\n",af, unstables);
find_aks(Sn, ak, NW, P, &Eres);
roots = lpc_to_lsp(&ak[1], P , lsp, 5, LSP_DELTA1);
roots = lpc_to_lsp(ak, P , lsp, 5, LSP_DELTA1);
if (roots == P) {
if (lspd) {
fprintf(flsp,"%f ",lsp[0]);
for(i=1; i<P; i++)
fprintf(flsp,"%f ",lsp[i]-lsp[i-1]);
if (log) {
fprintf(flsp,"%f ",log10(lsp[0]));
for(i=1; i<P; i++) {
diff = lsp[i]-lsp[i-1];
if (diff < (PI/4000.0)*25.0) diff = (PI/4000.0)*25.0;
fprintf(flsp,"%f ",log10(diff));
}
}
else {
fprintf(flsp,"%f ",lsp[0]);
for(i=1; i<P; i++)
fprintf(flsp,"%f ",lsp[i]-lsp[i-1]);
}
fprintf(flsp,"\n");
}
else if (lspdt) {
for(i=0; i<P; i++)
fprintf(flsp,"%f ",lsp[i]-lsp_prev[i]);
fprintf(flsp,"\n");
}
else {
for(i=0; i<P; i++)
fprintf(flsp,"%f ",lsp[i]);
fprintf(flsp,"\n");
}
if (log) {
for(i=0; i<P; i++)
fprintf(flsp,"%f ",log10(lsp[i]));
fprintf(flsp,"\n");
}
else {
for(i=0; i<P; i++)
fprintf(flsp,"%f ",lsp[i]);
fprintf(flsp,"\n");
}
}
memcpy(lsp_prev, lsp, sizeof(lsp));
}
else
unstables++;
}
}
printf("%3.2f %% active frames\n", 100.0*(float)af/f);
fclose(fspc);
fclose(flsp);

View File

@@ -22,8 +22,7 @@
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
@@ -47,7 +46,7 @@ int main(int argc, char *argv[])
if (argc < 3) {
printf("usage: %s InputFile ResidualFile\n", argv[0]);
exit(0);
exit(1);
}
/* Open files */

View File

@@ -22,8 +22,7 @@
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define MAX_N 2048 /* maximum DFT size */

View File

@@ -0,0 +1,33 @@
/*--------------------------------------------------------------------------*\
FILE........: sd.h
AUTHOR......: David Rowe
DATE CREATED: 22/7/93
Function to determine spectral distortion between two sets of LPCs.
\*--------------------------------------------------------------------------*/
/*
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, 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/>.
*/
#ifndef __SD__
#define __SD__
float spectral_dist(float ak1[], float ak2[], int p, int n);
#endif /* __SD__ */

View File

@@ -22,8 +22,7 @@
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
@@ -32,39 +31,43 @@
#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 */
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;
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(CODEC2 *c2, MODEL *model, short speech[]);
void synthesise_one_frame(CODEC2 *c2, short speech[], MODEL *model, float ak[]);
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];
void *c2;
CODEC2 *c3;
struct CODEC2 *c2;
MODEL model;
float ak[LPC_ORD+1];
float lsps[LPC_ORD];
c2 = codec2_create();
c3 = (CODEC2*)c2;
c2 = codec2_create(CODEC2_MODE_2400);
fin = fopen("../raw/hts1a.raw", "rb");
assert(fin != NULL);
@@ -72,9 +75,9 @@ int test1()
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);
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);
}
@@ -90,22 +93,22 @@ int test2()
{
FILE *fin, *fout;
short buf[2*N];
void *c2;
CODEC2 *c3;
struct CODEC2 *c2;
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];
char *bits;
int nbit;
int i;
c2 = codec2_create();
c3 = (CODEC2*)c2;
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");
@@ -114,60 +117,57 @@ int test2()
while(fread(buf, sizeof(short), 2*N, fin) == 2*N) {
/* first 10ms analysis frame - we just want voicing */
analyse_one_frame(c3, &model, buf);
analyse_one_frame(c2, &model, buf);
voiced1 = model.voiced;
/* second 10ms analysis frame */
analyse_one_frame(c3, &model, &buf[N]);
analyse_one_frame(c2, &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);
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(bits, &nbit, Wo_index, WO_BITS);
pack((unsigned char*)bits, (unsigned *)&nbit, Wo_index, WO_BITS);
for(i=0; i<LPC_ORD; i++) {
pack(bits, &nbit, lsp_indexes[i], lsp_bits(i));
pack((unsigned char*)bits, (unsigned *)&nbit, lsp_indexes[i], lsp_bits(i));
}
pack(bits, &nbit, lpc_correction, 1);
pack(bits, &nbit, energy_index, E_BITS);
pack(bits, &nbit, voiced1, 1);
pack(bits, &nbit, voiced2, 1);
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(bits, &nbit, WO_BITS);
Wo_index = unpack((unsigned char*)bits, (unsigned *)&nbit, WO_BITS);
for(i=0; i<LPC_ORD; i++) {
lsp_indexes[i] = unpack(bits, &nbit, lsp_bits(i));
lsp_indexes[i] = unpack((unsigned char*)bits, (unsigned *)&nbit, lsp_bits(i));
}
lpc_correction = unpack(bits, &nbit, 1);
energy_index = unpack(bits, &nbit, E_BITS);
voiced1 = unpack(bits, &nbit, 1);
voiced2 = unpack(bits, &nbit, 1);
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,
lpc_correction,
energy_index);
energy_index,
lsps,
&e);
model.voiced = voiced2;
model_interp.voiced = voiced1;
interpolate(&model_interp, &c3->prev_model, &model);
interpolate(&model_interp, &c2->prev_model, &model);
synthesise_one_frame(c3, buf, &model_interp, ak);
synthesise_one_frame(c3, &buf[N], &model, ak);
synthesise_one_frame(c2, buf, &model_interp, ak);
synthesise_one_frame(c2, &buf[N], &model, ak);
memcpy(&c3->prev_model, &model, sizeof(MODEL));
memcpy(&c2->prev_model, &model, sizeof(MODEL));
fwrite(buf, sizeof(short), 2*N, fout);
}
free(bits);
codec2_destroy(c2);
fclose(fin);
@@ -181,10 +181,14 @@ int test3()
FILE *fin, *fout, *fbits;
short buf1[2*N];
short buf2[2*N];
char bits[CODEC2_BITS_PER_FRAME];
void *c2;
char *bits;
struct CODEC2 *c2;
c2 = codec2_create();
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);
@@ -194,12 +198,13 @@ int test3()
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_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);

View File

@@ -24,8 +24,7 @@
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define N 80 /* frame size */
@@ -103,8 +102,8 @@ char *argv[];
float f0;
if (argc < 3) {
printf("\nusage: tcontphase OutputRawSpeechFile F0\n");
exit(0);
printf("\nusage: %s OutputRawSpeechFile F0\n", argv[0]);
exit(1);
}
/* Output file */

View File

@@ -22,8 +22,7 @@
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
@@ -32,6 +31,9 @@
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "defines.h"
#include "sine.h"
@@ -60,13 +62,14 @@ void write_amp(char file[], MODEL *model)
fclose(f);
}
char *get_next_float(char *s, float *num)
const char *get_next_float(const char *s, float *num)
{
char *p = s;
const char *p = s;
char tmp[MAX_STR];
while(*p && !isspace(*p))
p++;
assert((p-s) < (int)(sizeof(tmp)-1));
memcpy(tmp, s, p-s);
tmp[p-s] = 0;
*num = atof(tmp);
@@ -74,13 +77,14 @@ char *get_next_float(char *s, float *num)
return p+1;
}
char *get_next_int(char *s, int *num)
const char *get_next_int(const char *s, int *num)
{
char *p = s;
const char *p = s;
char tmp[MAX_STR];
while(*p && !isspace(*p))
p++;
assert((p-s) < (int)(sizeof(tmp)-1));
memcpy(tmp, s, p-s);
tmp[p-s] = 0;
*num = atoi(tmp);
@@ -88,18 +92,20 @@ char *get_next_int(char *s, int *num)
return p+1;
}
void load_amp(MODEL *model, char file[], int frame)
void load_amp(MODEL *model, const char * file, int frame)
{
FILE *f;
int i;
char s[1024];
char *ps;
const char *ps;
f = fopen(file,"rt");
assert(f);
for(i=0; i<frame; i++)
fgets(s, 1023, f);
ps = fgets(s, 1023, f);
/// can frame ever be 0? what if fgets fails?
ps = s;
ps = get_next_float(ps, &model->Wo);
ps = get_next_int(ps, &model->L);
@@ -109,13 +115,30 @@ void load_amp(MODEL *model, char file[], int frame)
fclose(f);
}
void load_or_make_amp(MODEL *model,
const char * filename, int frame,
float f0, float cdB, float mdBHz)
{
struct stat buf;
int rc = stat(filename, &buf);
if (rc || !S_ISREG(buf.st_mode) || ((buf.st_mode & S_IRUSR) != S_IRUSR))
{
make_amp(model, f0, cdB, mdBHz);
}
else
{
load_amp(model, filename, frame);
}
}
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);
load_or_make_amp(&prev,
"../src/hts1a_model.txt", 32,
50.0, 60.0, 6E-3);
load_or_make_amp(&next,
"../src/hts1a_model.txt", 34,
50.0, 40.0, 6E-3);
interp.voiced = 1;
interpolate(&interp, &prev, &next);

View File

@@ -22,8 +22,7 @@
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define N 80 /* frame size */
@@ -41,6 +40,7 @@
#include "dump.h"
#include "sine.h"
#include "nlp.h"
#include "kiss_fft.h"
int frames;
@@ -81,19 +81,22 @@ char *argv[];
FILE *fin,*fout;
short buf[N];
float Sn[M]; /* float input speech samples */
kiss_fft_cfg fft_fwd_cfg;
COMP Sw[FFT_ENC]; /* DFT of Sn[] */
float w[M]; /* time domain hamming window */
COMP W[FFT_ENC]; /* DFT of w[] */
float pitch;
int i;
int dump;
float prev_Wo;
void *nlp_states;
#ifdef DUMP
int dump;
#endif
if (argc < 3) {
printf("\nusage: tnlp InputRawSpeechFile OutputPitchTextFile "
"[--dump DumpFile]\n");
exit(0);
exit(1);
}
/* Input file */
@@ -110,12 +113,18 @@ char *argv[];
exit(1);
}
#ifdef DUMP
dump = switch_present("--dump",argc,argv);
if (dump)
dump_on(argv[dump+1]);
#else
/// TODO
/// #warning "Compile with -DDUMP if you expect to dump anything."
#endif
nlp_states = nlp_create();
make_analysis_window(w,W);
fft_fwd_cfg = kiss_fft_alloc(FFT_ENC, 0, NULL, NULL);
make_analysis_window(fft_fwd_cfg, w, W);
frames = 0;
prev_Wo = 0;
@@ -128,10 +137,12 @@ char *argv[];
Sn[i] = Sn[i+N];
for(i=0; i<N; i++)
Sn[i+M-N] = buf[i];
dft_speech(Sw, Sn, w);
dft_speech(fft_fwd_cfg, Sw, Sn, w);
#ifdef DUMP
dump_Sn(Sn); dump_Sw(Sw);
#endif
nlp(nlp_states,Sn,N,M,PITCH_MIN,PITCH_MAX,&pitch,Sw,&prev_Wo);
nlp(nlp_states,Sn,N,M,PITCH_MIN,PITCH_MAX,&pitch,Sw,W, &prev_Wo);
prev_Wo = TWO_PI/pitch;
fprintf(fout,"%f\n",pitch);
@@ -139,7 +150,9 @@ char *argv[];
fclose(fin);
fclose(fout);
#ifdef DUMP
if (dump) dump_off();
#endif
nlp_destroy(nlp_states);
return 0;

View File

@@ -22,8 +22,7 @@
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
@@ -123,8 +122,8 @@ int test_lsp(int lsp_number, int levels, float max_error_hz) {
for(i=0; i<levels; i++) {
indexes_in[lsp_number] = i;
decode_lsps(lsp, indexes_in, LPC_ORD);
encode_lsps(indexes_out, lsp,LPC_ORD);
decode_lsps_scalar(lsp, indexes_in, LPC_ORD);
encode_lsps_scalar(indexes_out, lsp,LPC_ORD);
if (indexes_in[lsp_number] != indexes_out[lsp_number]) {
printf("freq: %f index_in: %d index_out: %d\n",
lsp[lsp_number]+1, indexes_in[lsp_number],
@@ -136,18 +135,18 @@ int test_lsp(int lsp_number, int levels, float max_error_hz) {
for(i=0; i<LPC_ORD; i++)
indexes[i] = 0;
indexes[lsp_number] = 0;
decode_lsps(lsp, indexes, LPC_ORD);
decode_lsps_scalar(lsp, indexes, LPC_ORD);
lowf = lsp[lsp_number];
indexes[lsp_number] = levels - 1;
decode_lsps(lsp, indexes, LPC_ORD);
decode_lsps_scalar(lsp, indexes, LPC_ORD);
highf = lsp[lsp_number];
sprintf(s,"lsp%d_err.txt", lsp_number+1);
flsp = fopen(s, "wt");
for(f=lowf; f<highf; f +=(highf-lowf)/1000.0) {
lsp[lsp_number] = f;
encode_lsps(indexes, lsp, LPC_ORD);
decode_lsps(lsp, indexes, LPC_ORD);
encode_lsps_scalar(indexes, lsp, LPC_ORD);
decode_lsps_scalar(lsp, indexes, LPC_ORD);
error = f - lsp[lsp_number];
fprintf(flsp, "%f\n", error);
if (fabs(error) > max_error_rads) {

View File

@@ -23,8 +23,7 @@
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/*-----------------------------------------------------------------------*\
@@ -38,6 +37,7 @@
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <assert.h>
/*-----------------------------------------------------------------------*\
@@ -59,7 +59,7 @@ void acc(float v1[], float v2[], int k);
void norm(float v[], int k, long n);
long quantise(float cb[], float vec[], int k, int m, float *se);
/*-----------------------------------------------------------------------*\
/*-----------------------------------------------------------------------* \
MAIN
@@ -79,12 +79,13 @@ int main(int argc, char *argv[]) {
float delta; /* improvement in distortion */
FILE *ftrain; /* file containing training set */
FILE *fvq; /* file containing vector quantiser */
int ret;
/* Interpret command line arguments */
if (argc != 5) {
printf("usage: vqtrain TrainFile K M VQFile\n");
exit(0);
printf("usage: %s TrainFile K(dimension) M(codebook size) VQFile\n", argv[0]);
exit(1);
}
/* Open training file */
@@ -99,7 +100,7 @@ int main(int argc, char *argv[]) {
k = atol(argv[2]);
m = atol(argv[3]);
printf("dimension K=%ld number of entries M=%ld\n", k,m);
printf("dimension K=%ld number of entries M=%ld\n", k, m);
vec = (float*)malloc(sizeof(float)*k);
cb = (float*)malloc(sizeof(float)*k*m);
cent = (float*)malloc(sizeof(float)*k*m);
@@ -112,14 +113,14 @@ int main(int argc, char *argv[]) {
/* determine size of training set */
J = 0;
while(fread(vec, sizeof(float), k, ftrain) == k)
while(fread(vec, sizeof(float), k, ftrain) == (size_t)k)
J++;
printf("J=%ld entries in training set\n", J);
/* set up initial codebook state from samples of training set */
rewind(ftrain);
fread(cb, sizeof(float), k*m, ftrain);
ret = fread(cb, sizeof(float), k*m, ftrain);
/* main loop */
@@ -140,7 +141,7 @@ int main(int argc, char *argv[]) {
se = 0.0;
rewind(ftrain);
for(i=0; i<J; i++) {
fread(vec, sizeof(float), k, ftrain);
ret = fread(vec, sizeof(float), k, ftrain);
ind = quantise(cb, vec, k, m, &se);
n[ind]++;
acc(&cent[ind*k], vec, k);
@@ -151,7 +152,7 @@ int main(int argc, char *argv[]) {
printf("\r Iteration %ld, Dn = %f, Delta = %e\n", j, Dn, delta);
j++;
/* determine new codebook from centriods */
/* determine new codebook from centroids */
if (delta > DELTAQ)
for(i=0; i<m; i++) {
@@ -171,6 +172,7 @@ int main(int argc, char *argv[]) {
exit(1);
}
fprintf(fvq,"%ld %ld\n",k,m);
for(j=0; j<m; j++) {
for(i=0; i<k; i++)
fprintf(fvq,"%f ",cb[j*k+i]);
@@ -277,13 +279,16 @@ long quantise(float cb[], float vec[], int k, int m, float *se)
float beste; /* best error so far */
long j;
int i;
float diff;
besti = 0;
beste = 1E32;
for(j=0; j<m; j++) {
e = 0.0;
for(i=0; i<k; i++)
e += pow(cb[j*k+i]-vec[i],2.0);
for(i=0; i<k; i++) {
diff = cb[j*k+i]-vec[i];
e += pow(diff,2.0);
}
if (e < beste) {
beste = e;
besti = j;