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 <stdio.h>
     16 
     17 #include "tensorflow/lite/experimental/microfrontend/lib/frontend.h"
     18 #include "tensorflow/lite/experimental/microfrontend/lib/frontend_util.h"
     19 
     20 int main(int argc, char** argv) {
     21   struct FrontendConfig frontend_config;
     22   FrontendFillConfigWithDefaults(&frontend_config);
     23 
     24   char* filename = argv[1];
     25   int sample_rate = 16000;
     26 
     27   struct FrontendState frontend_state;
     28   if (!FrontendPopulateState(&frontend_config, &frontend_state, sample_rate)) {
     29     fprintf(stderr, "Failed to populate frontend state\n");
     30     FrontendFreeStateContents(&frontend_state);
     31     return 1;
     32   }
     33 
     34 
     35   FILE* fp = fopen(filename, "r");
     36   if (fp == NULL) {
     37     fprintf(stderr, "Failed to open %s for read\n", filename);
     38     return 1;
     39   }
     40   fseek(fp, 0L, SEEK_END);
     41   size_t audio_file_size = ftell(fp) / sizeof(int16_t);
     42   fseek(fp, 0L, SEEK_SET);
     43   int16_t* audio_data = malloc(audio_file_size * sizeof(int16_t));
     44   int16_t* original_audio_data = audio_data;
     45   if (audio_file_size !=
     46       fread(audio_data, sizeof(int16_t), audio_file_size, fp)) {
     47     fprintf(stderr, "Failed to read in all audio data\n");
     48     return 1;
     49   }
     50 
     51   while (audio_file_size > 0) {
     52     size_t num_samples_read;
     53     struct FrontendOutput output = FrontendProcessSamples(
     54         &frontend_state, audio_data, audio_file_size, &num_samples_read);
     55     audio_data += num_samples_read;
     56     audio_file_size -= num_samples_read;
     57 
     58     if (output.values != NULL) {
     59       int i;
     60       for (i = 0; i < output.size; ++i) {
     61         printf("%d ", output.values[i]);
     62       }
     63       printf("\n");
     64     }
     65   }
     66 
     67   FrontendFreeStateContents(&frontend_state);
     68   free(original_audio_data);
     69   return 0;
     70 }
     71