diff --git a/dnn/dump_lpcnet.py b/dnn/dump_lpcnet.py index c52b8836..23b41fa5 100755 --- a/dnn/dump_lpcnet.py +++ b/dnn/dump_lpcnet.py @@ -190,7 +190,7 @@ hf.write('#define MAX_MDENSE_TMP {}\n\n'.format(max_mdense_tmp)) hf.write('typedef struct {\n') for i, name in enumerate(layer_list): hf.write(' float {}_state[{}_STATE_SIZE];\n'.format(name, name.upper())) -hf.write('} LPCNetState;\n') +hf.write('} NNetState;\n') hf.write('\n\n#endif\n') diff --git a/dnn/lpcnet.c b/dnn/lpcnet.c index ee1ca0ab..3492c5e6 100644 --- a/dnn/lpcnet.c +++ b/dnn/lpcnet.c @@ -24,16 +24,41 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include "nnet_data.h" #include "nnet.h" #include "common.h" #include "arch.h" +#include "lpcnet.h" #define NB_FEATURES 38 #define FRAME_INPUT_SIZE (NB_FEATURES + EMBED_PITCH_OUT_SIZE) -void run_frame_network(LPCNetState *net, float *out, const float *features, int pitch) +static int ulaw2lin(int u) +{ + float s; + float scale_1 = 32768.f/255.f; + u = u - 128; + s = u >= 0 ? 1 : -1; + u = abs(u); + return s*scale_1*(exp(u/128.*log(256))-1); +} + +static int lin2ulaw(int x) +{ + float u; + float scale = 255.f/32768.f; + int s = x >= 0 ? 1 : -1; + x = abs(x); + u = (s*(128*log(1+scale*x)/log(256))); + u = 128 + u; + if (u < 0) u = 0; + if (u > 255) u = 255; + return (int)floor(.5 + u); +} + +void run_frame_network(NNetState *net, float *condition, float *lpc, const float *features, int pitch) { int i; float in[FRAME_INPUT_SIZE]; @@ -47,6 +72,38 @@ void run_frame_network(LPCNetState *net, float *out, const float *features, int celt_assert(FRAME_INPUT_SIZE == FEATURE_CONV2_OUT_SIZE); for (i=0;innet, condition, lpc, features, pitch); + for (i=0;ilast_sig[j]*lpc[j]; + pred = (int)floor(.5f + sum); + last_sig_ulaw = lin2ulaw(lpcnet->last_sig[0]); + pred_ulaw = lin2ulaw(pred); + exc = run_sample_network(&lpcnet->nnet, condition, lpc, lpcnet->last_exc, last_sig_ulaw, pred_ulaw); + output[i] = pred + ulaw2lin(exc); + RNN_MOVE(&lpcnet->last_sig[1], &lpcnet->last_sig[0], LPC_ORDER-1); + lpcnet->last_sig[0] = output[i]; + lpcnet->last_exc = exc; + } +} diff --git a/dnn/lpcnet.h b/dnn/lpcnet.h new file mode 100644 index 00000000..9d2836f8 --- /dev/null +++ b/dnn/lpcnet.h @@ -0,0 +1,44 @@ +/* Copyright (c) 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. +*/ + +#ifndef _LPCNET_H_ +#define _LPCNET_H_ + + +#include "nnet_data.h" + +#define LPC_ORDER 16 + +typedef struct { + NNetState nnet; + int last_exc; + short last_sig[LPC_ORDER]; +} LPCNetState; + + + + +#endif