diff --git a/dnn/lpcnet.c b/dnn/lpcnet.c index 800be8a0..782613ec 100644 --- a/dnn/lpcnet.c +++ b/dnn/lpcnet.c @@ -33,6 +33,9 @@ #define NB_FEATURES 38 +#define PITCH_GAIN_FEATURE 37 +#define PDF_FLOOR 0.002 + #define FRAME_INPUT_SIZE (NB_FEATURES + EMBED_PITCH_OUT_SIZE) #define SAMPLE_INPUT_SIZE (2*EMBED_SIG_OUT_SIZE + EMBED_EXC_OUT_SIZE + FEATURE_DENSE2_OUT_SIZE) @@ -79,11 +82,10 @@ void run_frame_network(NNetState *net, float *condition, float *lpc, const float RNN_CLEAR(lpc, LPC_ORDER); } -int run_sample_network(NNetState *net, const float *condition, int last_exc, int last_sig, int pred) +void run_sample_network(NNetState *net, float *pdf, const float *condition, int last_exc, int last_sig, int pred) { float in_a[SAMPLE_INPUT_SIZE]; float in_b[GRU_A_STATE_SIZE+FEATURE_DENSE2_OUT_SIZE]; - float pdf[DUAL_FC_OUT_SIZE]; compute_embedding(&embed_sig, &in_a[0], last_sig); compute_embedding(&embed_sig, &in_a[EMBED_SIG_OUT_SIZE], pred); compute_embedding(&embed_exc, &in_a[2*EMBED_SIG_OUT_SIZE], last_exc); @@ -93,8 +95,6 @@ int run_sample_network(NNetState *net, const float *condition, int last_exc, int RNN_COPY(&in_b[GRU_A_STATE_SIZE], condition, FEATURE_DENSE2_OUT_SIZE); compute_gru(&gru_b, net->gru_b_state, in_b); compute_mdense(&dual_fc, pdf, net->gru_b_state); - /* FIXME: Do the actual sampling here. */ - return 0; } void generate_samples(LPCNetState *lpcnet, short *output, const float *features, int pitch, int N) @@ -102,6 +102,7 @@ void generate_samples(LPCNetState *lpcnet, short *output, const float *features, int i; float condition[FEATURE_DENSE2_OUT_SIZE]; float lpc[LPC_ORDER]; + float pdf[DUAL_FC_OUT_SIZE]; run_frame_network(&lpcnet->nnet, condition, lpc, features, pitch); for (i=0;ilast_sig[0]); pred_ulaw = lin2ulaw(pred); - exc = run_sample_network(&lpcnet->nnet, condition, lpcnet->last_exc, last_sig_ulaw, pred_ulaw); + run_sample_network(&lpcnet->nnet, pdf, condition, lpcnet->last_exc, last_sig_ulaw, pred_ulaw); + exc = sample_from_pdf(pdf, DUAL_FC_OUT_SIZE, MAX16(0, 1.5f*features[PITCH_GAIN_FEATURE] - .5f), PDF_FLOOR); output[i] = pred + ulaw2lin(exc); RNN_MOVE(&lpcnet->last_sig[1], &lpcnet->last_sig[0], LPC_ORDER-1); lpcnet->last_sig[0] = output[i]; diff --git a/dnn/nnet.c b/dnn/nnet.c index 6ab1725c..e8e621d1 100644 --- a/dnn/nnet.c +++ b/dnn/nnet.c @@ -30,6 +30,7 @@ #include "config.h" #endif +#include #include #include "opus_types.h" #include "arch.h" @@ -236,3 +237,32 @@ void compute_embedding(const EmbeddingLayer *layer, float *output, int input) } } +int sample_from_pdf(const float *pdf, int N, float exp_boost, float pdf_floor) +{ + int i; + float sum, norm; + float r; + float tmp[DUAL_FC_OUT_SIZE]; + celt_assert(N <= DUAL_FC_OUT_SIZE); + sum = 0; + /* Decrease the temperature of the sampling. */ + for (i=0;i tmp[i]) return r; + } + return N-1; +} diff --git a/dnn/nnet.h b/dnn/nnet.h index a3666825..b87bdd1f 100644 --- a/dnn/nnet.h +++ b/dnn/nnet.h @@ -89,5 +89,6 @@ void compute_conv1d(const Conv1DLayer *layer, float *output, float *mem, const f void compute_embedding(const EmbeddingLayer *layer, float *output, int input); +int sample_from_pdf(const float *pdf, int N, float exp_boost, float pdf_floor); #endif /* _MLP_H_ */