From 3b2dd696e15b1724a3edb178885ef03e6daad83f Mon Sep 17 00:00:00 2001 From: Jan Buethe Date: Wed, 13 Dec 2023 10:10:01 +0100 Subject: added 10ms-cross-fade after osce reset --- dnn/osce.c | 21 ++++-- dnn/osce_features.c | 202 ++++++++++++++++++++++++++++------------------------ dnn/osce_features.h | 2 + dnn/osce_structs.h | 1 + src/opus_demo.c | 2 + 5 files changed, 130 insertions(+), 98 deletions(-) diff --git a/dnn/osce.c b/dnn/osce.c index 02e11cb2..0bb8fb8c 100644 --- a/dnn/osce.c +++ b/dnn/osce.c @@ -335,7 +335,7 @@ static int init_nolace(NoLACE *hNoLACE, const WeightArray *weights) for (i = 0; i < NOLACE_OVERLAP_SIZE; i ++) { - hNoLACE->window[i] = 0.5 + 0.5 * cos(M_PI * (i + 0.5) / LACE_OVERLAP_SIZE); + hNoLACE->window[i] = 0.5f + 0.5f * cos(M_PI * (i + 0.5f) / LACE_OVERLAP_SIZE); } return ret; @@ -343,6 +343,7 @@ static int init_nolace(NoLACE *hNoLACE, const WeightArray *weights) static void reset_nolace_state(NoLACEState *state) { + printf("nolace reset\n"); OPUS_CLEAR(state, 1); init_adacomb_state(&state->cf1_state); @@ -794,6 +795,7 @@ void osce_reset(silk_OSCE_struct *hOSCE, int method) celt_assert(0 && "method not defined"); /* Question: return error code? */ } hOSCE->method = method; + hOSCE->features.reset = 1; } @@ -859,6 +861,11 @@ void osce_enhance_frame( int i; (void) arch; + static int counter = 0; + + if (counter ++ % 50 == 0){ + osce_reset(&psDec->osce, psDec->osce.method); + } /* enhancement only implemented for 20 ms frame at 16kHz */ if (psDec->fs_kHz != 16 || psDec->nb_subfr != 4) @@ -954,19 +961,25 @@ void osce_enhance_frame( fwrite(xq, psDec->nb_subfr * psDec->subfr_length, sizeof(xq[0]), fnoisy16k); #endif + if (psDec->osce.features.reset) + { + osce_cross_fade_10ms(out_buffer, in_buffer, 320); + psDec->osce.features.reset = 0; + } + /* scale output */ for (i = 0; i < 320; i++) { float tmp = round((1U<<15) * out_buffer[i]); - if (tmp > 32767) tmp = 32767; - if (tmp < -32768) tmp = -32768; + + if (tmp > INT16_MAX) tmp = INT16_MAX; + if (tmp < INT16_MIN) tmp = INT16_MIN; xq[i] = (opus_int16) tmp; } } - #if 0 #include diff --git a/dnn/osce_features.c b/dnn/osce_features.c index d3d79785..ae6703fd 100644 --- a/dnn/osce_features.c +++ b/dnn/osce_features.c @@ -22,7 +22,7 @@ #include #endif -static int center_bins_clean[64] = { +static const int center_bins_clean[64] = { 0, 2, 5, 8, 10, 12, 15, 18, 20, 22, 25, 28, 30, 33, 35, 38, 40, 42, 45, 48, 50, 52, 55, 58, @@ -33,107 +33,107 @@ static int center_bins_clean[64] = { 140, 142, 145, 148, 150, 152, 155, 160 }; -static int center_bins_noisy[18] = { +static const int center_bins_noisy[18] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 136, 160 }; -static float band_weights_clean[64] = { - 0.666666666667, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.400000000000, 0.400000000000, 0.400000000000, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.400000000000, 0.400000000000, 0.400000000000, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.333333333333, 0.400000000000, - 0.500000000000, 0.400000000000, 0.250000000000, 0.333333333333 +static const float band_weights_clean[64] = { + 0.666666666667f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.400000000000f, 0.400000000000f, 0.400000000000f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.400000000000f, 0.400000000000f, 0.400000000000f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.333333333333f, 0.400000000000f, + 0.500000000000f, 0.400000000000f, 0.250000000000f, 0.333333333333f }; -static float band_weights_noisy[18] = { - 0.400000000000, 0.250000000000, 0.250000000000, 0.250000000000, - 0.250000000000, 0.250000000000, 0.250000000000, 0.250000000000, - 0.166666666667, 0.125000000000, 0.125000000000, 0.125000000000, - 0.083333333333, 0.062500000000, 0.062500000000, 0.050000000000, - 0.041666666667, 0.080000000000 +static const float band_weights_noisy[18] = { + 0.400000000000f, 0.250000000000f, 0.250000000000f, 0.250000000000f, + 0.250000000000f, 0.250000000000f, 0.250000000000f, 0.250000000000f, + 0.166666666667f, 0.125000000000f, 0.125000000000f, 0.125000000000f, + 0.083333333333f, 0.062500000000f, 0.062500000000f, 0.050000000000f, + 0.041666666667f, 0.080000000000f }; static float osce_window[320] = { - 0.004908718808, 0.014725683311, 0.024541228523, 0.034354408400, 0.044164277127, - 0.053969889210, 0.063770299562, 0.073564563600, 0.083351737332, 0.093130877450, - 0.102901041421, 0.112661287575, 0.122410675199, 0.132148264628, 0.141873117332, - 0.151584296010, 0.161280864678, 0.170961888760, 0.180626435180, 0.190273572448, - 0.199902370753, 0.209511902052, 0.219101240157, 0.228669460829, 0.238215641862, - 0.247738863176, 0.257238206902, 0.266712757475, 0.276161601717, 0.285583828929, - 0.294978530977, 0.304344802381, 0.313681740399, 0.322988445118, 0.332264019538, - 0.341507569661, 0.350718204573, 0.359895036535, 0.369037181064, 0.378143757022, - 0.387213886697, 0.396246695891, 0.405241314005, 0.414196874117, 0.423112513073, - 0.431987371563, 0.440820594212, 0.449611329655, 0.458358730621, 0.467061954019, - 0.475720161014, 0.484332517110, 0.492898192230, 0.501416360796, 0.509886201809, - 0.518306898929, 0.526677640552, 0.534997619887, 0.543266035038, 0.551482089078, - 0.559644990127, 0.567753951426, 0.575808191418, 0.583806933818, 0.591749407690, - 0.599634847523, 0.607462493302, 0.615231590581, 0.622941390558, 0.630591150148, - 0.638180132051, 0.645707604824, 0.653172842954, 0.660575126926, 0.667913743292, - 0.675187984742, 0.682397150168, 0.689540544737, 0.696617479953, 0.703627273726, - 0.710569250438, 0.717442741007, 0.724247082951, 0.730981620454, 0.737645704427, - 0.744238692572, 0.750759949443, 0.757208846506, 0.763584762206, 0.769887082016, - 0.776115198508, 0.782268511401, 0.788346427627, 0.794348361383, 0.800273734191, - 0.806121974951, 0.811892519997, 0.817584813152, 0.823198305781, 0.828732456844, - 0.834186732948, 0.839560608398, 0.844853565250, 0.850065093356, 0.855194690420, - 0.860241862039, 0.865206121757, 0.870086991109, 0.874883999665, 0.879596685080, - 0.884224593137, 0.888767277786, 0.893224301196, 0.897595233788, 0.901879654283, - 0.906077149740, 0.910187315596, 0.914209755704, 0.918144082372, 0.921989916403, - 0.925746887127, 0.929414632439, 0.932992798835, 0.936481041442, 0.939879024058, - 0.943186419177, 0.946402908026, 0.949528180593, 0.952561935658, 0.955503880820, - 0.958353732530, 0.961111216112, 0.963776065795, 0.966348024735, 0.968826845041, - 0.971212287799, 0.973504123096, 0.975702130039, 0.977806096779, 0.979815820533, - 0.981731107599, 0.983551773378, 0.985277642389, 0.986908548290, 0.988444333892, - 0.989884851171, 0.991229961288, 0.992479534599, 0.993633450666, 0.994691598273, - 0.995653875433, 0.996520189401, 0.997290456679, 0.997964603026, 0.998542563469, - 0.999024282300, 0.999409713092, 0.999698818696, 0.999891571247, 0.999987952167, - 0.999987952167, 0.999891571247, 0.999698818696, 0.999409713092, 0.999024282300, - 0.998542563469, 0.997964603026, 0.997290456679, 0.996520189401, 0.995653875433, - 0.994691598273, 0.993633450666, 0.992479534599, 0.991229961288, 0.989884851171, - 0.988444333892, 0.986908548290, 0.985277642389, 0.983551773378, 0.981731107599, - 0.979815820533, 0.977806096779, 0.975702130039, 0.973504123096, 0.971212287799, - 0.968826845041, 0.966348024735, 0.963776065795, 0.961111216112, 0.958353732530, - 0.955503880820, 0.952561935658, 0.949528180593, 0.946402908026, 0.943186419177, - 0.939879024058, 0.936481041442, 0.932992798835, 0.929414632439, 0.925746887127, - 0.921989916403, 0.918144082372, 0.914209755704, 0.910187315596, 0.906077149740, - 0.901879654283, 0.897595233788, 0.893224301196, 0.888767277786, 0.884224593137, - 0.879596685080, 0.874883999665, 0.870086991109, 0.865206121757, 0.860241862039, - 0.855194690420, 0.850065093356, 0.844853565250, 0.839560608398, 0.834186732948, - 0.828732456844, 0.823198305781, 0.817584813152, 0.811892519997, 0.806121974951, - 0.800273734191, 0.794348361383, 0.788346427627, 0.782268511401, 0.776115198508, - 0.769887082016, 0.763584762206, 0.757208846506, 0.750759949443, 0.744238692572, - 0.737645704427, 0.730981620454, 0.724247082951, 0.717442741007, 0.710569250438, - 0.703627273726, 0.696617479953, 0.689540544737, 0.682397150168, 0.675187984742, - 0.667913743292, 0.660575126926, 0.653172842954, 0.645707604824, 0.638180132051, - 0.630591150148, 0.622941390558, 0.615231590581, 0.607462493302, 0.599634847523, - 0.591749407690, 0.583806933818, 0.575808191418, 0.567753951426, 0.559644990127, - 0.551482089078, 0.543266035038, 0.534997619887, 0.526677640552, 0.518306898929, - 0.509886201809, 0.501416360796, 0.492898192230, 0.484332517110, 0.475720161014, - 0.467061954019, 0.458358730621, 0.449611329655, 0.440820594212, 0.431987371563, - 0.423112513073, 0.414196874117, 0.405241314005, 0.396246695891, 0.387213886697, - 0.378143757022, 0.369037181064, 0.359895036535, 0.350718204573, 0.341507569661, - 0.332264019538, 0.322988445118, 0.313681740399, 0.304344802381, 0.294978530977, - 0.285583828929, 0.276161601717, 0.266712757475, 0.257238206902, 0.247738863176, - 0.238215641862, 0.228669460829, 0.219101240157, 0.209511902052, 0.199902370753, - 0.190273572448, 0.180626435180, 0.170961888760, 0.161280864678, 0.151584296010, - 0.141873117332, 0.132148264628, 0.122410675199, 0.112661287575, 0.102901041421, - 0.093130877450, 0.083351737332, 0.073564563600, 0.063770299562, 0.053969889210, - 0.044164277127, 0.034354408400, 0.024541228523, 0.014725683311, 0.004908718808 + 0.004908718808f, 0.014725683311f, 0.024541228523f, 0.034354408400f, 0.044164277127f, + 0.053969889210f, 0.063770299562f, 0.073564563600f, 0.083351737332f, 0.093130877450f, + 0.102901041421f, 0.112661287575f, 0.122410675199f, 0.132148264628f, 0.141873117332f, + 0.151584296010f, 0.161280864678f, 0.170961888760f, 0.180626435180f, 0.190273572448f, + 0.199902370753f, 0.209511902052f, 0.219101240157f, 0.228669460829f, 0.238215641862f, + 0.247738863176f, 0.257238206902f, 0.266712757475f, 0.276161601717f, 0.285583828929f, + 0.294978530977f, 0.304344802381f, 0.313681740399f, 0.322988445118f, 0.332264019538f, + 0.341507569661f, 0.350718204573f, 0.359895036535f, 0.369037181064f, 0.378143757022f, + 0.387213886697f, 0.396246695891f, 0.405241314005f, 0.414196874117f, 0.423112513073f, + 0.431987371563f, 0.440820594212f, 0.449611329655f, 0.458358730621f, 0.467061954019f, + 0.475720161014f, 0.484332517110f, 0.492898192230f, 0.501416360796f, 0.509886201809f, + 0.518306898929f, 0.526677640552f, 0.534997619887f, 0.543266035038f, 0.551482089078f, + 0.559644990127f, 0.567753951426f, 0.575808191418f, 0.583806933818f, 0.591749407690f, + 0.599634847523f, 0.607462493302f, 0.615231590581f, 0.622941390558f, 0.630591150148f, + 0.638180132051f, 0.645707604824f, 0.653172842954f, 0.660575126926f, 0.667913743292f, + 0.675187984742f, 0.682397150168f, 0.689540544737f, 0.696617479953f, 0.703627273726f, + 0.710569250438f, 0.717442741007f, 0.724247082951f, 0.730981620454f, 0.737645704427f, + 0.744238692572f, 0.750759949443f, 0.757208846506f, 0.763584762206f, 0.769887082016f, + 0.776115198508f, 0.782268511401f, 0.788346427627f, 0.794348361383f, 0.800273734191f, + 0.806121974951f, 0.811892519997f, 0.817584813152f, 0.823198305781f, 0.828732456844f, + 0.834186732948f, 0.839560608398f, 0.844853565250f, 0.850065093356f, 0.855194690420f, + 0.860241862039f, 0.865206121757f, 0.870086991109f, 0.874883999665f, 0.879596685080f, + 0.884224593137f, 0.888767277786f, 0.893224301196f, 0.897595233788f, 0.901879654283f, + 0.906077149740f, 0.910187315596f, 0.914209755704f, 0.918144082372f, 0.921989916403f, + 0.925746887127f, 0.929414632439f, 0.932992798835f, 0.936481041442f, 0.939879024058f, + 0.943186419177f, 0.946402908026f, 0.949528180593f, 0.952561935658f, 0.955503880820f, + 0.958353732530f, 0.961111216112f, 0.963776065795f, 0.966348024735f, 0.968826845041f, + 0.971212287799f, 0.973504123096f, 0.975702130039f, 0.977806096779f, 0.979815820533f, + 0.981731107599f, 0.983551773378f, 0.985277642389f, 0.986908548290f, 0.988444333892f, + 0.989884851171f, 0.991229961288f, 0.992479534599f, 0.993633450666f, 0.994691598273f, + 0.995653875433f, 0.996520189401f, 0.997290456679f, 0.997964603026f, 0.998542563469f, + 0.999024282300f, 0.999409713092f, 0.999698818696f, 0.999891571247f, 0.999987952167f, + 0.999987952167f, 0.999891571247f, 0.999698818696f, 0.999409713092f, 0.999024282300f, + 0.998542563469f, 0.997964603026f, 0.997290456679f, 0.996520189401f, 0.995653875433f, + 0.994691598273f, 0.993633450666f, 0.992479534599f, 0.991229961288f, 0.989884851171f, + 0.988444333892f, 0.986908548290f, 0.985277642389f, 0.983551773378f, 0.981731107599f, + 0.979815820533f, 0.977806096779f, 0.975702130039f, 0.973504123096f, 0.971212287799f, + 0.968826845041f, 0.966348024735f, 0.963776065795f, 0.961111216112f, 0.958353732530f, + 0.955503880820f, 0.952561935658f, 0.949528180593f, 0.946402908026f, 0.943186419177f, + 0.939879024058f, 0.936481041442f, 0.932992798835f, 0.929414632439f, 0.925746887127f, + 0.921989916403f, 0.918144082372f, 0.914209755704f, 0.910187315596f, 0.906077149740f, + 0.901879654283f, 0.897595233788f, 0.893224301196f, 0.888767277786f, 0.884224593137f, + 0.879596685080f, 0.874883999665f, 0.870086991109f, 0.865206121757f, 0.860241862039f, + 0.855194690420f, 0.850065093356f, 0.844853565250f, 0.839560608398f, 0.834186732948f, + 0.828732456844f, 0.823198305781f, 0.817584813152f, 0.811892519997f, 0.806121974951f, + 0.800273734191f, 0.794348361383f, 0.788346427627f, 0.782268511401f, 0.776115198508f, + 0.769887082016f, 0.763584762206f, 0.757208846506f, 0.750759949443f, 0.744238692572f, + 0.737645704427f, 0.730981620454f, 0.724247082951f, 0.717442741007f, 0.710569250438f, + 0.703627273726f, 0.696617479953f, 0.689540544737f, 0.682397150168f, 0.675187984742f, + 0.667913743292f, 0.660575126926f, 0.653172842954f, 0.645707604824f, 0.638180132051f, + 0.630591150148f, 0.622941390558f, 0.615231590581f, 0.607462493302f, 0.599634847523f, + 0.591749407690f, 0.583806933818f, 0.575808191418f, 0.567753951426f, 0.559644990127f, + 0.551482089078f, 0.543266035038f, 0.534997619887f, 0.526677640552f, 0.518306898929f, + 0.509886201809f, 0.501416360796f, 0.492898192230f, 0.484332517110f, 0.475720161014f, + 0.467061954019f, 0.458358730621f, 0.449611329655f, 0.440820594212f, 0.431987371563f, + 0.423112513073f, 0.414196874117f, 0.405241314005f, 0.396246695891f, 0.387213886697f, + 0.378143757022f, 0.369037181064f, 0.359895036535f, 0.350718204573f, 0.341507569661f, + 0.332264019538f, 0.322988445118f, 0.313681740399f, 0.304344802381f, 0.294978530977f, + 0.285583828929f, 0.276161601717f, 0.266712757475f, 0.257238206902f, 0.247738863176f, + 0.238215641862f, 0.228669460829f, 0.219101240157f, 0.209511902052f, 0.199902370753f, + 0.190273572448f, 0.180626435180f, 0.170961888760f, 0.161280864678f, 0.151584296010f, + 0.141873117332f, 0.132148264628f, 0.122410675199f, 0.112661287575f, 0.102901041421f, + 0.093130877450f, 0.083351737332f, 0.073564563600f, 0.063770299562f, 0.053969889210f, + 0.044164277127f, 0.034354408400f, 0.024541228523f, 0.014725683311f, 0.004908718808f }; -static void apply_filterbank(float *x_out, float *x_in, int *center_bins, float* band_weights, int num_bands) +static void apply_filterbank(float *x_out, float *x_in, const int *center_bins, const float* band_weights, int num_bands) { int b, i; float frac; @@ -231,7 +231,7 @@ static void calculate_cepstrum(float *cepstrum, float *signal) /* log domain conversion */ for (n = 0; n < OSCE_NOISY_SPEC_NUM_BANDS; n++) { - spec[n] = log(spec[n] + 1e-9); + spec[n] = log(spec[n] + 1e-9f); #ifdef DEBUG_PRINT printf("logspec[%d]: %f\n", n, spec[n]); #endif @@ -260,7 +260,7 @@ static void calculate_acorr(float *acorr, float *signal, int lag) yy += signal[n - lag + k] * signal[n - lag + k]; xy += signal[n] * signal[n - lag + k]; } - acorr[k+2] = xy / sqrt(xx * yy + 1e-9); + acorr[k+2] = xy / sqrt(xx * yy + 1e-9f); } } @@ -345,7 +345,7 @@ void osce_calculate_features( psFeatures = &psDec->osce.features; /* smooth bit count */ - psFeatures->numbits_smooth = 0.9 * psFeatures->numbits_smooth + 0.1 * num_bits; + psFeatures->numbits_smooth = 0.9f * psFeatures->numbits_smooth + 0.1f * num_bits; numbits[0] = num_bits; #ifdef OSCE_NUMBITS_BUGFIX numbits[1] = psFeatures->numbits_smooth; @@ -399,7 +399,7 @@ void osce_calculate_features( } /* frame gain */ - pfeatures[OSCE_LOG_GAIN_START] = log((float) psDecCtrl->Gains_Q16[k] / (1UL << 16) + 1e-9); + pfeatures[OSCE_LOG_GAIN_START] = log((float) psDecCtrl->Gains_Q16[k] / (1UL << 16) + 1e-9f); #ifdef WRITE_FEATURES fwrite(pfeatures, sizeof(*pfeatures), 93, f_feat); @@ -409,3 +409,17 @@ void osce_calculate_features( /* buffer update */ OPUS_COPY(psFeatures->signal_history, &buffer[num_samples], OSCE_FEATURES_MAX_HISTORY); } + + +void osce_cross_fade_10ms(float *x_enhanced, float *x_in, int length) +{ + int i; + celt_assert(length >= 160); + + for (i = 0; i < 160; i++) + { + x_enhanced[i] = osce_window[i] * x_enhanced[i] + (1 - osce_window[i]) * x_in[i]; + } + + +} \ No newline at end of file diff --git a/dnn/osce_features.h b/dnn/osce_features.h index 124209fd..f9dd02a9 100644 --- a/dnn/osce_features.h +++ b/dnn/osce_features.h @@ -18,4 +18,6 @@ void osce_calculate_features( ); +void osce_cross_fade_10ms(float *x_enhanced, float *x_in, int length); + #endif \ No newline at end of file diff --git a/dnn/osce_structs.h b/dnn/osce_structs.h index 67fbf811..3b102e29 100644 --- a/dnn/osce_structs.h +++ b/dnn/osce_structs.h @@ -20,6 +20,7 @@ typedef struct { int last_lag; int last_type; float signal_history[OSCE_FEATURES_MAX_HISTORY]; + int reset; } OSCEFeatureState; diff --git a/src/opus_demo.c b/src/opus_demo.c index 8ea61360..fc81de89 100644 --- a/src/opus_demo.c +++ b/src/opus_demo.c @@ -692,8 +692,10 @@ int main(int argc, char *argv[]) } #ifdef ENABLE_OSCE +#if 0 /* force encoder to use SILK for the time being */ opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_SILK_ONLY)); +#endif #endif } if (!encode_only) -- cgit v1.2.3