/* Copyright (c) 2017-2018 Mozilla */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #include #include "kiss_fft.h" #include "common.h" #include #include "freq.h" #include "pitch.h" #include "arch.h" #include "celt_lpc.h" #include #include "lpcnet.h" #include "lpcnet_private.h" static void biquad(float *y, float mem[2], const float *x, const float *b, const float *a, int N) { int i; for (i=0;ifeatures[k][2*NB_BANDS+3+j]*st->sig_mem[j]; e = lin2ulaw(pcm[k*FRAME_SIZE+i] - p); /* Signal. */ data[4*i] = lin2ulaw(st->sig_mem[0]); /* Prediction. */ data[4*i+1] = lin2ulaw(p); /* Excitation in. */ data[4*i+2] = st->exc_mem; /* Excitation out. */ data[4*i+3] = e; /* Simulate error on excitation. */ e += noise[k*FRAME_SIZE+i]; e = IMIN(255, IMAX(0, e)); RNN_MOVE(&st->sig_mem[1], &st->sig_mem[0], LPC_ORDER-1); st->sig_mem[0] = p + ulaw2lin(e); st->exc_mem = e; } fwrite(data, 4*FRAME_SIZE, 1, file); } } static short float2short(float x) { int i; i = (int)floor(.5+x); return IMAX(-32767, IMIN(32767, i)); } int main(int argc, char **argv) { int i; int count=0; static const float a_hp[2] = {-1.99599, 0.99600}; static const float b_hp[2] = {-2, 1}; float a_sig[2] = {0}; float b_sig[2] = {0}; float mem_hp_x[2]={0}; float mem_resp_x[2]={0}; float mem_preemph=0; float x[FRAME_SIZE]; int gain_change_count=0; FILE *f1; FILE *ffeat; FILE *fpcm=NULL; short pcm[FRAME_SIZE]={0}; short pcmbuf[FRAME_SIZE*4]={0}; int noisebuf[FRAME_SIZE*4]={0}; short tmp[FRAME_SIZE] = {0}; float savedX[FRAME_SIZE] = {0}; float speech_gain=1; int last_silent = 1; float old_speech_gain = 1; int one_pass_completed = 0; LPCNetEncState *st; float noise_std=0; int training = -1; int encode = 0; int decode = 0; int quantize = 0; st = lpcnet_encoder_create(); if (argc == 5 && strcmp(argv[1], "-train")==0) training = 1; if (argc == 5 && strcmp(argv[1], "-qtrain")==0) { training = 1; quantize = 1; } if (argc == 4 && strcmp(argv[1], "-test")==0) training = 0; if (argc == 4 && strcmp(argv[1], "-qtest")==0) { training = 0; quantize = 1; } if (argc == 4 && strcmp(argv[1], "-encode")==0) { training = 0; quantize = 1; encode = 1; } if (argc == 4 && strcmp(argv[1], "-decode")==0) { training = 0; decode = 1; } if (training == -1) { fprintf(stderr, "usage: %s -train \n", argv[0]); fprintf(stderr, " or %s -test \n", argv[0]); return 1; } f1 = fopen(argv[2], "r"); if (f1 == NULL) { fprintf(stderr,"Error opening input .s16 16kHz speech input file: %s\n", argv[2]); exit(1); } ffeat = fopen(argv[3], "w"); if (ffeat == NULL) { fprintf(stderr,"Error opening output feature file: %s\n", argv[3]); exit(1); } if (decode) { float vq_mem[NB_BANDS] = {0}; while (1) { int ret; unsigned char buf[8]; float features[4][NB_TOTAL_FEATURES]; //int c0_id, main_pitch, modulation, corr_id, vq_end[3], vq_mid, interp_id; //ret = fscanf(f1, "%d %d %d %d %d %d %d %d %d\n", &c0_id, &main_pitch, &modulation, &corr_id, &vq_end[0], &vq_end[1], &vq_end[2], &vq_mid, &interp_id); ret = fread(buf, 1, 8, f1); if (ret != 8) break; decode_packet(features, vq_mem, buf); for (i=0;i<4;i++) { fwrite(features[i], sizeof(float), NB_TOTAL_FEATURES, ffeat); } } return 0; } if (training) { fpcm = fopen(argv[4], "w"); if (fpcm == NULL) { fprintf(stderr,"Error opening output PCM file: %s\n", argv[4]); exit(1); } } while (1) { float E=0; int silent; for (i=0;i=10000000 && one_pass_completed) break; if (training && ++gain_change_count > 2821) { float tmp; speech_gain = pow(10., (-20+(rand()%40))/20.); if (rand()%20==0) speech_gain *= .01; if (rand()%100==0) speech_gain = 0; gain_change_count = 0; rand_resp(a_sig, b_sig); tmp = (float)rand()/RAND_MAX; noise_std = 4*tmp*tmp; } biquad(x, mem_hp_x, x, b_hp, a_hp, FRAME_SIZE); biquad(x, mem_resp_x, x, b_sig, a_sig, FRAME_SIZE); preemphasis(x, &mem_preemph, x, PREEMPHASIS, FRAME_SIZE); for (i=0;ipcount*FRAME_SIZE], pcm, FRAME_SIZE); if (fpcm) { compute_noise(&noisebuf[st->pcount*FRAME_SIZE], noise_std); } st->pcount++; /* Running on groups of 4 frames. */ if (st->pcount == 4) { unsigned char buf[8]; process_superframe(st, buf, ffeat, encode, quantize); if (fpcm) write_audio(st, pcmbuf, noisebuf, fpcm); st->pcount = 0; } //if (fpcm) fwrite(pcm, sizeof(short), FRAME_SIZE, fpcm); for (i=0;i