Home | History | Annotate | Download | only in lib
      1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 #include "tensorflow/lite/experimental/microfrontend/lib/frontend_util.h"
     16 
     17 #include <stdio.h>
     18 #include <string.h>
     19 
     20 #include "tensorflow/lite/experimental/microfrontend/lib/bits.h"
     21 
     22 void FrontendFillConfigWithDefaults(struct FrontendConfig* config) {
     23   WindowFillConfigWithDefaults(&config->window);
     24   FilterbankFillConfigWithDefaults(&config->filterbank);
     25   NoiseReductionFillConfigWithDefaults(&config->noise_reduction);
     26   PcanGainControlFillConfigWithDefaults(&config->pcan_gain_control);
     27   LogScaleFillConfigWithDefaults(&config->log_scale);
     28 }
     29 
     30 int FrontendPopulateState(const struct FrontendConfig* config,
     31                           struct FrontendState* state, int sample_rate) {
     32   memset(state, 0, sizeof(*state));
     33 
     34   if (!WindowPopulateState(&config->window, &state->window, sample_rate)) {
     35     fprintf(stderr, "Failed to populate window state\n");
     36     return 0;
     37   }
     38 
     39   if (!FftPopulateState(&state->fft, state->window.size)) {
     40     fprintf(stderr, "Failed to populate fft state\n");
     41     return 0;
     42   }
     43   FftInit(&state->fft);
     44 
     45   if (!FilterbankPopulateState(&config->filterbank, &state->filterbank,
     46                                sample_rate, state->fft.fft_size / 2 + 1)) {
     47     fprintf(stderr, "Failed to populate filterbank state\n");
     48     return 0;
     49   }
     50 
     51   if (!NoiseReductionPopulateState(&config->noise_reduction,
     52                                    &state->noise_reduction,
     53                                    state->filterbank.num_channels)) {
     54     fprintf(stderr, "Failed to populate noise reduction state\n");
     55     return 0;
     56   }
     57 
     58   int input_correction_bits =
     59       MostSignificantBit32(state->fft.fft_size) - 1 - (kFilterbankBits / 2);
     60   if (!PcanGainControlPopulateState(&config->pcan_gain_control,
     61                                     &state->pcan_gain_control,
     62                                     state->noise_reduction.estimate,
     63                                     state->filterbank.num_channels,
     64                                     state->noise_reduction.smoothing_bits,
     65                                     input_correction_bits)) {
     66     fprintf(stderr, "Failed to populate pcan gain control state\n");
     67     return 0;
     68   }
     69 
     70   if (!LogScalePopulateState(&config->log_scale, &state->log_scale)) {
     71     fprintf(stderr, "Failed to populate log scale state\n");
     72     return 0;
     73   }
     74 
     75   FrontendReset(state);
     76 
     77   // All good, return a true value.
     78   return 1;
     79 }
     80 
     81 void FrontendFreeStateContents(struct FrontendState* state) {
     82   WindowFreeStateContents(&state->window);
     83   FftFreeStateContents(&state->fft);
     84   FilterbankFreeStateContents(&state->filterbank);
     85   NoiseReductionFreeStateContents(&state->noise_reduction);
     86   PcanGainControlFreeStateContents(&state->pcan_gain_control);
     87 }
     88