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 
FrontendFillConfigWithDefaults(struct FrontendConfig * config)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 
FrontendPopulateState(const struct FrontendConfig * config,struct FrontendState * state,int sample_rate)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(
61           &config->pcan_gain_control, &state->pcan_gain_control,
62           state->noise_reduction.estimate, state->filterbank.num_channels,
63           state->noise_reduction.smoothing_bits, input_correction_bits)) {
64     fprintf(stderr, "Failed to populate pcan gain control state\n");
65     return 0;
66   }
67 
68   if (!LogScalePopulateState(&config->log_scale, &state->log_scale)) {
69     fprintf(stderr, "Failed to populate log scale state\n");
70     return 0;
71   }
72 
73   FrontendReset(state);
74 
75   // All good, return a true value.
76   return 1;
77 }
78 
FrontendFreeStateContents(struct FrontendState * state)79 void FrontendFreeStateContents(struct FrontendState* state) {
80   WindowFreeStateContents(&state->window);
81   FftFreeStateContents(&state->fft);
82   FilterbankFreeStateContents(&state->filterbank);
83   NoiseReductionFreeStateContents(&state->noise_reduction);
84   PcanGainControlFreeStateContents(&state->pcan_gain_control);
85 }
86