Hongrui WeiSiri ManjunathKexi DangQiao He
Published

Wake Word Detection

Nowadays, it pays to have a way to get things done with voice activation to easily carry out daily tasks and this leads to our project.

BeginnerShowcase (no instructions)669
Wake Word Detection

Things used in this project

Hardware components

SparkFun Edge Development Board - Apollo3 Blue
SparkFun Edge Development Board - Apollo3 Blue
×1
SparkFun Serial Basic Breakout - CH340C and USB-C
SparkFun Serial Basic Breakout - CH340C and USB-C
×1
TinyML Machine Learning with TensorFlow on Arduino and Ultra-Low Power Micro-Controllers
×1

Hand tools and fabrication machines

TensorFlow Lite
TensorFlow Lite

Story

Read more

Schematics

untitled_diagram_wC2J32UK9S.jpg

Code

command_responder.h

C Header File
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// Provides an interface to take an action based on an audio command.

#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_COMMAND_RESPONDER_H_
#define TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_COMMAND_RESPONDER_H_

#include "tensorflow/lite/c/c_api_internal.h"
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h"

// Called every time the results of an audio recognition run are available. The
// human-readable name of any recognized command is in the `found_command`
// argument, `score` has the numerical confidence, and `is_new_command` is set
// if the previous command was different to this one.
void RespondToCommand(tflite::ErrorReporter* error_reporter,
                      int32_t current_time, const char* found_command,
                      uint8_t score, bool is_new_command);

#endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_COMMAND_RESPONDER_H_

feature_provider_mock_test.cc

C/C++
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/c/c_api_internal.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/feature_provider.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/no_micro_features_data.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/yes_micro_features_data.h"
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h"
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"

TF_LITE_MICRO_TESTS_BEGIN

TF_LITE_MICRO_TEST(TestFeatureProviderMockYes) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  uint8_t feature_data[kFeatureElementCount];
  FeatureProvider feature_provider(kFeatureElementCount, feature_data);

  int how_many_new_slices = 0;
  TfLiteStatus populate_status = feature_provider.PopulateFeatureData(
      error_reporter, /* last_time_in_ms= */ 0, /* time_in_ms= */ 970,
      &how_many_new_slices);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, populate_status);
  TF_LITE_MICRO_EXPECT_EQ(kFeatureSliceCount, how_many_new_slices);

  for (int i = 0; i < kFeatureElementCount; ++i) {
    TF_LITE_MICRO_EXPECT_EQ(g_yes_micro_f2e59fea_nohash_1_data[i],
                            feature_data[i]);
  }
}

TF_LITE_MICRO_TEST(TestFeatureProviderMockNo) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  uint8_t feature_data[kFeatureElementCount];
  FeatureProvider feature_provider(kFeatureElementCount, feature_data);

  int how_many_new_slices = 0;
  TfLiteStatus populate_status = feature_provider.PopulateFeatureData(
      error_reporter, /* last_time_in_ms= */ 4000, /* time_in_ms= */ 4970,
      &how_many_new_slices);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, populate_status);
  TF_LITE_MICRO_EXPECT_EQ(kFeatureSliceCount, how_many_new_slices);

  for (int i = 0; i < kFeatureElementCount; ++i) {
    TF_LITE_MICRO_EXPECT_EQ(g_no_micro_f9643d42_nohash_4_data[i],
                            feature_data[i]);
  }
}

TF_LITE_MICRO_TESTS_END

feature_provider_test.cc

C/C++
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/feature_provider.h"
#include "tensorflow/lite/c/c_api_internal.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h"
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"

TF_LITE_MICRO_TESTS_BEGIN

TF_LITE_MICRO_TEST(TestFeatureProvider) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  uint8_t feature_data[kFeatureElementCount];
  FeatureProvider feature_provider(kFeatureElementCount, feature_data);

  int how_many_new_slices = 0;
  TfLiteStatus populate_status = feature_provider.PopulateFeatureData(
      error_reporter, /* last_time_in_ms= */ 0, /* time_in_ms= */ 10000,
      &how_many_new_slices);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, populate_status);
  TF_LITE_MICRO_EXPECT_EQ(kFeatureSliceCount, how_many_new_slices);
}

TF_LITE_MICRO_TESTS_END

feature_provider.cc

C/C++
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/feature_provider.h"

#include "tensorflow/lite/experimental/micro/examples/micro_speech/audio_provider.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_features_generator.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"

FeatureProvider::FeatureProvider(int feature_size, uint8_t* feature_data)
    : feature_size_(feature_size),
      feature_data_(feature_data),
      is_first_run_(true) {
  // Initialize the feature data to default values.
  for (int n = 0; n < feature_size_; ++n) {
    feature_data_[n] = 0;
  }
}

FeatureProvider::~FeatureProvider() {}

TfLiteStatus FeatureProvider::PopulateFeatureData(
    tflite::ErrorReporter* error_reporter, int32_t last_time_in_ms,
    int32_t time_in_ms, int* how_many_new_slices) {
  if (feature_size_ != kFeatureElementCount) {
    error_reporter->Report("Requested feature_data_ size %d doesn't match %d",
                           feature_size_, kFeatureElementCount);
    return kTfLiteError;
  }

  // Quantize the time into steps as long as each window stride, so we can
  // figure out which audio data we need to fetch.
  const int last_step = (last_time_in_ms / kFeatureSliceStrideMs);
  const int current_step = (time_in_ms / kFeatureSliceStrideMs);

  int slices_needed = current_step - last_step;
  // If this is the first call, make sure we don't use any cached information.
  if (is_first_run_) {
    TfLiteStatus init_status = InitializeMicroFeatures(error_reporter);
    if (init_status != kTfLiteOk) {
      return init_status;
    }
    is_first_run_ = false;
    slices_needed = kFeatureSliceCount;
  }
  if (slices_needed > kFeatureSliceCount) {
    slices_needed = kFeatureSliceCount;
  }
  *how_many_new_slices = slices_needed;

  const int slices_to_keep = kFeatureSliceCount - slices_needed;
  const int slices_to_drop = kFeatureSliceCount - slices_to_keep;
  // If we can avoid recalculating some slices, just move the existing data
  // up in the spectrogram, to perform something like this:
  // last time = 80ms          current time = 120ms
  // +-----------+             +-----------+
  // | data@20ms |         --> | data@60ms |
  // +-----------+       --    +-----------+
  // | data@40ms |     --  --> | data@80ms |
  // +-----------+   --  --    +-----------+
  // | data@60ms | --  --      |  <empty>  |
  // +-----------+   --        +-----------+
  // | data@80ms | --          |  <empty>  |
  // +-----------+             +-----------+
  if (slices_to_keep > 0) {
    for (int dest_slice = 0; dest_slice < slices_to_keep; ++dest_slice) {
      uint8_t* dest_slice_data =
          feature_data_ + (dest_slice * kFeatureSliceSize);
      const int src_slice = dest_slice + slices_to_drop;
      const uint8_t* src_slice_data =
          feature_data_ + (src_slice * kFeatureSliceSize);
      for (int i = 0; i < kFeatureSliceSize; ++i) {
        dest_slice_data[i] = src_slice_data[i];
      }
    }
  }
  // Any slices that need to be filled in with feature data have their
  // appropriate audio data pulled, and features calculated for that slice.
  if (slices_needed > 0) {
    for (int new_slice = slices_to_keep; new_slice < kFeatureSliceCount;
         ++new_slice) {
      const int new_step = (current_step - kFeatureSliceCount + 1) + new_slice;
      const int32_t slice_start_ms = (new_step * kFeatureSliceStrideMs);
      int16_t* audio_samples = nullptr;
      int audio_samples_size = 0;
      // TODO(petewarden): Fix bug that leads to non-zero slice_start_ms
      GetAudioSamples(error_reporter, (slice_start_ms > 0 ? slice_start_ms : 0),
                      kFeatureSliceDurationMs, &audio_samples_size,
                      &audio_samples);
      if (audio_samples_size < kMaxAudioSampleSize) {
        error_reporter->Report("Audio data size %d too small, want %d",
                               audio_samples_size, kMaxAudioSampleSize);
        return kTfLiteError;
      }
      uint8_t* new_slice_data = feature_data_ + (new_slice * kFeatureSliceSize);
      size_t num_samples_read;
      TfLiteStatus generate_status = GenerateMicroFeatures(
          error_reporter, audio_samples, audio_samples_size, kFeatureSliceSize,
          new_slice_data, &num_samples_read);
      if (generate_status != kTfLiteOk) {
        return generate_status;
      }
    }
  }
  return kTfLiteOk;
}

feature_provider.h

C Header File
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_FEATURE_PROVIDER_H_
#define TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_FEATURE_PROVIDER_H_

#include "tensorflow/lite/c/c_api_internal.h"
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h"

// Binds itself to an area of memory intended to hold the input features for an
// audio-recognition neural network model, and fills that data area with the
// features representing the current audio input, for example from a microphone.
// The audio features themselves are a two-dimensional array, made up of
// horizontal slices representing the frequencies at one point in time, stacked
// on top of each other to form a spectrogram showing how those frequencies
// changed over time.
class FeatureProvider {
 public:
  // Create the provider, and bind it to an area of memory. This memory should
  // remain accessible for the lifetime of the provider object, since subsequent
  // calls will fill it with feature data. The provider does no memory
  // management of this data.
  FeatureProvider(int feature_size, uint8_t* feature_data);
  ~FeatureProvider();

  // Fills the feature data with information from audio inputs, and returns how
  // many feature slices were updated.
  TfLiteStatus PopulateFeatureData(tflite::ErrorReporter* error_reporter,
                                   int32_t last_time_in_ms, int32_t time_in_ms,
                                   int* how_many_new_slices);

 private:
  int feature_size_;
  uint8_t* feature_data_;
  // Make sure we don't try to use cached information if this is the first call
  // into the provider.
  bool is_first_run_;
};

#endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_FEATURE_PROVIDER_H_

main.cc

C/C++
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/main_functions.h"

// This is the default main used on systems that have the standard C entry
// point. Other devices (for example FreeRTOS or ESP32) that have different
// requirements for entry code (like an app_main function) should specialize
// this main.cc file in a target-specific subfolder.
int main(int argc, char* argv[]) {
  setup();
  while (true) {
    loop();
  }
}

micro_speech_test.cc

C/C++
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/no_micro_features_data.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/tiny_conv_micro_features_model_data.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/yes_micro_features_data.h"
#include "tensorflow/lite/experimental/micro/kernels/micro_ops.h"
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h"
#include "tensorflow/lite/experimental/micro/micro_interpreter.h"
#include "tensorflow/lite/experimental/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/version.h"

TF_LITE_MICRO_TESTS_BEGIN

TF_LITE_MICRO_TEST(TestInvoke) {
  // Set up logging.
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  // Map the model into a usable data structure. This doesn't involve any
  // copying or parsing, it's a very lightweight operation.
  const tflite::Model* model =
      ::tflite::GetModel(g_tiny_conv_micro_features_model_data);
  if (model->version() != TFLITE_SCHEMA_VERSION) {
    error_reporter->Report(
        "Model provided is schema version %d not equal "
        "to supported version %d.\n",
        model->version(), TFLITE_SCHEMA_VERSION);
  }

  // Pull in only the operation implementations we need.
  // This relies on a complete list of all the ops needed by this graph.
  // An easier approach is to just use the AllOpsResolver, but this will
  // incur some penalty in code space for op implementations that are not
  // needed by this graph.
  //
  // tflite::ops::micro::AllOpsResolver resolver;
  tflite::MicroMutableOpResolver micro_mutable_op_resolver;
  micro_mutable_op_resolver.AddBuiltin(
      tflite::BuiltinOperator_DEPTHWISE_CONV_2D,
      tflite::ops::micro::Register_DEPTHWISE_CONV_2D());
  micro_mutable_op_resolver.AddBuiltin(
      tflite::BuiltinOperator_FULLY_CONNECTED,
      tflite::ops::micro::Register_FULLY_CONNECTED());
  micro_mutable_op_resolver.AddBuiltin(tflite::BuiltinOperator_SOFTMAX,
                                       tflite::ops::micro::Register_SOFTMAX());

  // Create an area of memory to use for input, output, and intermediate arrays.
  const int tensor_arena_size = 10 * 1024;
  uint8_t tensor_arena[tensor_arena_size];

  // Build an interpreter to run the model with.
  tflite::MicroInterpreter interpreter(model, micro_mutable_op_resolver,
                                       tensor_arena, tensor_arena_size,
                                       error_reporter);
  interpreter.AllocateTensors();

  // Get information about the memory area to use for the model's input.
  TfLiteTensor* input = interpreter.input(0);

  // Make sure the input has the properties we expect.
  TF_LITE_MICRO_EXPECT_NE(nullptr, input);
  TF_LITE_MICRO_EXPECT_EQ(4, input->dims->size);
  TF_LITE_MICRO_EXPECT_EQ(1, input->dims->data[0]);
  TF_LITE_MICRO_EXPECT_EQ(49, input->dims->data[1]);
  TF_LITE_MICRO_EXPECT_EQ(40, input->dims->data[2]);
  TF_LITE_MICRO_EXPECT_EQ(1, input->dims->data[3]);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteUInt8, input->type);

  // Copy a spectrogram created from a .wav audio file of someone saying "Yes",
  // into the memory area used for the input.
  const uint8_t* yes_features_data = g_yes_micro_f2e59fea_nohash_1_data;
  for (int i = 0; i < input->bytes; ++i) {
    input->data.uint8[i] = yes_features_data[i];
  }

  // Run the model on this input and make sure it succeeds.
  TfLiteStatus invoke_status = interpreter.Invoke();
  if (invoke_status != kTfLiteOk) {
    error_reporter->Report("Invoke failed\n");
  }
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, invoke_status);

  // Get the output from the model, and make sure it's the expected size and
  // type.
  TfLiteTensor* output = interpreter.output(0);
  TF_LITE_MICRO_EXPECT_EQ(2, output->dims->size);
  TF_LITE_MICRO_EXPECT_EQ(1, output->dims->data[0]);
  TF_LITE_MICRO_EXPECT_EQ(4, output->dims->data[1]);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteUInt8, output->type);

  // There are four possible classes in the output, each with a score.
  const int kSilenceIndex = 0;
  const int kUnknownIndex = 1;
  const int kYesIndex = 2;
  const int kNoIndex = 3;

  // Make sure that the expected "Yes" score is higher than the other classes.
  uint8_t silence_score = output->data.uint8[kSilenceIndex];
  uint8_t unknown_score = output->data.uint8[kUnknownIndex];
  uint8_t yes_score = output->data.uint8[kYesIndex];
  uint8_t no_score = output->data.uint8[kNoIndex];
  TF_LITE_MICRO_EXPECT_GT(yes_score, silence_score);
  TF_LITE_MICRO_EXPECT_GT(yes_score, unknown_score);
  TF_LITE_MICRO_EXPECT_GT(yes_score, no_score);

  // Now test with a different input, from a recording of "No".
  const uint8_t* no_features_data = g_no_micro_f9643d42_nohash_4_data;
  for (int i = 0; i < input->bytes; ++i) {
    input->data.uint8[i] = no_features_data[i];
  }

  // Run the model on this "No" input.
  invoke_status = interpreter.Invoke();
  if (invoke_status != kTfLiteOk) {
    error_reporter->Report("Invoke failed\n");
  }
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, invoke_status);

  // Get the output from the model, and make sure it's the expected size and
  // type.
  output = interpreter.output(0);
  TF_LITE_MICRO_EXPECT_EQ(2, output->dims->size);
  TF_LITE_MICRO_EXPECT_EQ(1, output->dims->data[0]);
  TF_LITE_MICRO_EXPECT_EQ(4, output->dims->data[1]);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteUInt8, output->type);

  // Make sure that the expected "No" score is higher than the other classes.
  silence_score = output->data.uint8[kSilenceIndex];
  unknown_score = output->data.uint8[kUnknownIndex];
  yes_score = output->data.uint8[kYesIndex];
  no_score = output->data.uint8[kNoIndex];
  TF_LITE_MICRO_EXPECT_GT(no_score, silence_score);
  TF_LITE_MICRO_EXPECT_GT(no_score, unknown_score);
  TF_LITE_MICRO_EXPECT_GT(no_score, yes_score);

  error_reporter->Report("Ran successfully\n");
}

TF_LITE_MICRO_TESTS_END

recognize_commands.cc

C/C++
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/recognize_commands.h"

#include <limits>

RecognizeCommands::RecognizeCommands(tflite::ErrorReporter* error_reporter,
                                     int32_t average_window_duration_ms,
                                     uint8_t detection_threshold,
                                     int32_t suppression_ms,
                                     int32_t minimum_count)
    : error_reporter_(error_reporter),
      average_window_duration_ms_(average_window_duration_ms),
      detection_threshold_(detection_threshold),
      suppression_ms_(suppression_ms),
      minimum_count_(minimum_count),
      previous_results_(error_reporter) {
  previous_top_label_ = "silence";
  previous_top_label_time_ = std::numeric_limits<int32_t>::min();
}

TfLiteStatus RecognizeCommands::ProcessLatestResults(
    const TfLiteTensor* latest_results, const int32_t current_time_ms,
    const char** found_command, uint8_t* score, bool* is_new_command) {
  if ((latest_results->dims->size != 2) ||
      (latest_results->dims->data[0] != 1) ||
      (latest_results->dims->data[1] != kCategoryCount)) {
    error_reporter_->Report(
        "The results for recognition should contain %d elements, but there are "
        "%d in an %d-dimensional shape",
        kCategoryCount, latest_results->dims->data[1],
        latest_results->dims->size);
    return kTfLiteError;
  }

  if (latest_results->type != kTfLiteUInt8) {
    error_reporter_->Report(
        "The results for recognition should be uint8 elements, but are %d",
        latest_results->type);
    return kTfLiteError;
  }

  if ((!previous_results_.empty()) &&
      (current_time_ms < previous_results_.front().time_)) {
    error_reporter_->Report(
        "Results must be fed in increasing time order, but received a "
        "timestamp of %d that was earlier than the previous one of %d",
        current_time_ms, previous_results_.front().time_);
    return kTfLiteError;
  }

  // Add the latest results to the head of the queue.
  previous_results_.push_back({current_time_ms, latest_results->data.uint8});

  // Prune any earlier results that are too old for the averaging window.
  const int64_t time_limit = current_time_ms - average_window_duration_ms_;
  while ((!previous_results_.empty()) &&
         previous_results_.front().time_ < time_limit) {
    previous_results_.pop_front();
  }

  // If there are too few results, assume the result will be unreliable and
  // bail.
  const int64_t how_many_results = previous_results_.size();
  const int64_t earliest_time = previous_results_.front().time_;
  const int64_t samples_duration = current_time_ms - earliest_time;
  if ((how_many_results < minimum_count_) ||
      (samples_duration < (average_window_duration_ms_ / 4))) {
    *found_command = previous_top_label_;
    *score = 0;
    *is_new_command = false;
    return kTfLiteOk;
  }

  // Calculate the average score across all the results in the window.
  int32_t average_scores[kCategoryCount];
  for (int offset = 0; offset < previous_results_.size(); ++offset) {
    PreviousResultsQueue::Result previous_result =
        previous_results_.from_front(offset);
    const uint8_t* scores = previous_result.scores_;
    for (int i = 0; i < kCategoryCount; ++i) {
      if (offset == 0) {
        average_scores[i] = scores[i];
      } else {
        average_scores[i] += scores[i];
      }
    }
  }
  for (int i = 0; i < kCategoryCount; ++i) {
    average_scores[i] /= how_many_results;
  }

  // Find the current highest scoring category.
  int current_top_index = 0;
  int32_t current_top_score = 0;
  for (int i = 0; i < kCategoryCount; ++i) {
    if (average_scores[i] > current_top_score) {
      current_top_score = average_scores[i];
      current_top_index = i;
    }
  }
  const char* current_top_label = kCategoryLabels[current_top_index];

  // If we've recently had another label trigger, assume one that occurs too
  // soon afterwards is a bad result.
  int64_t time_since_last_top;
  if ((previous_top_label_ == kCategoryLabels[0]) ||
      (previous_top_label_time_ == std::numeric_limits<int32_t>::min())) {
    time_since_last_top = std::numeric_limits<int32_t>::max();
  } else {
    time_since_last_top = current_time_ms - previous_top_label_time_;
  }
  if ((current_top_score > detection_threshold_) &&
      ((current_top_label != previous_top_label_) ||
       (time_since_last_top > suppression_ms_))) {
    previous_top_label_ = current_top_label;
    previous_top_label_time_ = current_time_ms;
    *is_new_command = true;
  } else {
    *is_new_command = false;
  }
  *found_command = current_top_label;
  *score = current_top_score;

  return kTfLiteOk;
}

main_functions.cc

C/C++
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/main_functions.h"

#include "tensorflow/lite/experimental/micro/examples/micro_speech/audio_provider.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/command_responder.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/feature_provider.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/tiny_conv_micro_features_model_data.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/recognize_commands.h"
#include "tensorflow/lite/experimental/micro/kernels/micro_ops.h"
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h"
#include "tensorflow/lite/experimental/micro/micro_interpreter.h"
#include "tensorflow/lite/experimental/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/version.h"

// Globals, used for compatibility with Arduino-style sketches.
namespace {
tflite::ErrorReporter* error_reporter = nullptr;
const tflite::Model* model = nullptr;
tflite::MicroInterpreter* interpreter = nullptr;
TfLiteTensor* model_input = nullptr;
FeatureProvider* feature_provider = nullptr;
RecognizeCommands* recognizer = nullptr;
int32_t previous_time = 0;

// Create an area of memory to use for input, output, and intermediate arrays.
// The size of this will depend on the model you're using, and may need to be
// determined by experimentation.
constexpr int kTensorArenaSize = 10 * 1024;
uint8_t tensor_arena[kTensorArenaSize];
}  // namespace

// The name of this function is important for Arduino compatibility.
void setup() {
  // Set up logging. Google style is to avoid globals or statics because of
  // lifetime uncertainty, but since this has a trivial destructor it's okay.
  // NOLINTNEXTLINE(runtime-global-variables)
  static tflite::MicroErrorReporter micro_error_reporter;
  error_reporter = &micro_error_reporter;

  // Map the model into a usable data structure. This doesn't involve any
  // copying or parsing, it's a very lightweight operation.
  model = tflite::GetModel(g_tiny_conv_micro_features_model_data);
  if (model->version() != TFLITE_SCHEMA_VERSION) {
    error_reporter->Report(
        "Model provided is schema version %d not equal "
        "to supported version %d.",
        model->version(), TFLITE_SCHEMA_VERSION);
    return;
  }

  // Pull in only the operation implementations we need.
  // This relies on a complete list of all the ops needed by this graph.
  // An easier approach is to just use the AllOpsResolver, but this will
  // incur some penalty in code space for op implementations that are not
  // needed by this graph.
  //
  // tflite::ops::micro::AllOpsResolver resolver;
  // NOLINTNEXTLINE(runtime-global-variables)
  static tflite::MicroMutableOpResolver micro_mutable_op_resolver;
  micro_mutable_op_resolver.AddBuiltin(
      tflite::BuiltinOperator_DEPTHWISE_CONV_2D,
      tflite::ops::micro::Register_DEPTHWISE_CONV_2D());
  micro_mutable_op_resolver.AddBuiltin(
      tflite::BuiltinOperator_FULLY_CONNECTED,
      tflite::ops::micro::Register_FULLY_CONNECTED());
  micro_mutable_op_resolver.AddBuiltin(tflite::BuiltinOperator_SOFTMAX,
                                       tflite::ops::micro::Register_SOFTMAX());

  // Build an interpreter to run the model with.
  static tflite::MicroInterpreter static_interpreter(
      model, micro_mutable_op_resolver, tensor_arena, kTensorArenaSize,
      error_reporter);
  interpreter = &static_interpreter;

  // Allocate memory from the tensor_arena for the model's tensors.
  TfLiteStatus allocate_status = interpreter->AllocateTensors();
  if (allocate_status != kTfLiteOk) {
    error_reporter->Report("AllocateTensors() failed");
    return;
  }

  // Get information about the memory area to use for the model's input.
  model_input = interpreter->input(0);
  if ((model_input->dims->size != 4) || (model_input->dims->data[0] != 1) ||
      (model_input->dims->data[1] != kFeatureSliceCount) ||
      (model_input->dims->data[2] != kFeatureSliceSize) ||
      (model_input->type != kTfLiteUInt8)) {
    error_reporter->Report("Bad input tensor parameters in model");
    return;
  }

  // Prepare to access the audio spectrograms from a microphone or other source
  // that will provide the inputs to the neural network.
  // NOLINTNEXTLINE(runtime-global-variables)
  static FeatureProvider static_feature_provider(kFeatureElementCount,
                                                 model_input->data.uint8);
  feature_provider = &static_feature_provider;

  static RecognizeCommands static_recognizer(error_reporter);
  recognizer = &static_recognizer;

  previous_time = 0;
}

// The name of this function is important for Arduino compatibility.
void loop() {
  // Fetch the spectrogram for the current time.
  const int32_t current_time = LatestAudioTimestamp();
  int how_many_new_slices = 0;
  TfLiteStatus feature_status = feature_provider->PopulateFeatureData(
      error_reporter, previous_time, current_time, &how_many_new_slices);
  if (feature_status != kTfLiteOk) {
    error_reporter->Report("Feature generation failed");
    return;
  }
  previous_time = current_time;
  // If no new audio samples have been received since last time, don't bother
  // running the network model.
  if (how_many_new_slices == 0) {
    return;
  }

  // Run the model on the spectrogram input and make sure it succeeds.
  TfLiteStatus invoke_status = interpreter->Invoke();
  if (invoke_status != kTfLiteOk) {
    error_reporter->Report("Invoke failed");
    return;
  }

  // Obtain a pointer to the output tensor
  TfLiteTensor* output = interpreter->output(0);
  // Determine whether a command was recognized based on the output of inference
  const char* found_command = nullptr;
  uint8_t score = 0;
  bool is_new_command = false;
  TfLiteStatus process_status = recognizer->ProcessLatestResults(
      output, current_time, &found_command, &score, &is_new_command);
  if (process_status != kTfLiteOk) {
    error_reporter->Report("RecognizeCommands::ProcessLatestResults() failed");
    return;
  }
  // Do something based on the recognized command. The default implementation
  // just prints to the error console, but you should replace this with your
  // own function for a real application.
  RespondToCommand(error_reporter, current_time, found_command, score,
                   is_new_command);
}

recognize_commands_test.cc

C/C++
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/recognize_commands.h"

#include "tensorflow/lite/experimental/micro/testing/micro_test.h"
#include "tensorflow/lite/experimental/micro/testing/test_utils.h"

TF_LITE_MICRO_TESTS_BEGIN

TF_LITE_MICRO_TEST(PreviousResultsQueueBasic) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  PreviousResultsQueue queue(error_reporter);
  TF_LITE_MICRO_EXPECT_EQ(0, queue.size());

  uint8_t scores_a[4] = {0, 0, 0, 1};
  queue.push_back({0, scores_a});
  TF_LITE_MICRO_EXPECT_EQ(1, queue.size());
  TF_LITE_MICRO_EXPECT_EQ(0, queue.front().time_);
  TF_LITE_MICRO_EXPECT_EQ(0, queue.back().time_);

  uint8_t scores_b[4] = {0, 0, 1, 0};
  queue.push_back({1, scores_b});
  TF_LITE_MICRO_EXPECT_EQ(2, queue.size());
  TF_LITE_MICRO_EXPECT_EQ(0, queue.front().time_);
  TF_LITE_MICRO_EXPECT_EQ(1, queue.back().time_);

  PreviousResultsQueue::Result pop_result = queue.pop_front();
  TF_LITE_MICRO_EXPECT_EQ(0, pop_result.time_);
  TF_LITE_MICRO_EXPECT_EQ(1, queue.size());
  TF_LITE_MICRO_EXPECT_EQ(1, queue.front().time_);
  TF_LITE_MICRO_EXPECT_EQ(1, queue.back().time_);

  uint8_t scores_c[4] = {0, 1, 0, 0};
  queue.push_back({2, scores_c});
  TF_LITE_MICRO_EXPECT_EQ(2, queue.size());
  TF_LITE_MICRO_EXPECT_EQ(1, queue.front().time_);
  TF_LITE_MICRO_EXPECT_EQ(2, queue.back().time_);
}

TF_LITE_MICRO_TEST(PreviousResultsQueuePushPop) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  PreviousResultsQueue queue(error_reporter);
  TF_LITE_MICRO_EXPECT_EQ(0, queue.size());

  for (int i = 0; i < 123; ++i) {
    uint8_t scores[4] = {0, 0, 0, 1};
    queue.push_back({i, scores});
    TF_LITE_MICRO_EXPECT_EQ(1, queue.size());
    TF_LITE_MICRO_EXPECT_EQ(i, queue.front().time_);
    TF_LITE_MICRO_EXPECT_EQ(i, queue.back().time_);

    PreviousResultsQueue::Result pop_result = queue.pop_front();
    TF_LITE_MICRO_EXPECT_EQ(i, pop_result.time_);
    TF_LITE_MICRO_EXPECT_EQ(0, queue.size());
  }
}

TF_LITE_MICRO_TEST(RecognizeCommandsTestBasic) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  RecognizeCommands recognize_commands(error_reporter);

  std::initializer_list<uint8_t> result_data = {255, 0, 0, 0};
  auto result_dims = {2, 1, 4};
  TfLiteTensor results = tflite::testing::CreateQuantizedTensor(
      result_data, tflite::testing::IntArrayFromInitializer(result_dims),
      "input_tensor", 0.0f, 128.0f);

  const char* found_command;
  uint8_t score;
  bool is_new_command;
  TF_LITE_MICRO_EXPECT_EQ(
      kTfLiteOk, recognize_commands.ProcessLatestResults(
                     &results, 0, &found_command, &score, &is_new_command));
}

TF_LITE_MICRO_TEST(RecognizeCommandsTestFindCommands) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  RecognizeCommands recognize_commands(error_reporter, 1000, 51);

  std::initializer_list<uint8_t> yes_data = {0, 0, 255, 0};
  auto yes_dims = {2, 1, 4};
  TfLiteTensor yes_results = tflite::testing::CreateQuantizedTensor(
      yes_data, tflite::testing::IntArrayFromInitializer(yes_dims),
      "input_tensor", 0.0f, 128.0f);

  bool has_found_new_command = false;
  const char* new_command;
  for (int i = 0; i < 10; ++i) {
    const char* found_command;
    uint8_t score;
    bool is_new_command;
    int32_t current_time_ms = 0 + (i * 100);
    TF_LITE_MICRO_EXPECT_EQ(
        kTfLiteOk, recognize_commands.ProcessLatestResults(
                       &yes_results, current_time_ms, &found_command, &score,
                       &is_new_command));
    if (is_new_command) {
      TF_LITE_MICRO_EXPECT(!has_found_new_command);
      has_found_new_command = true;
      new_command = found_command;
    }
  }
  TF_LITE_MICRO_EXPECT(has_found_new_command);
  if (has_found_new_command) {
    TF_LITE_MICRO_EXPECT_EQ(0, tflite::testing::TestStrcmp("yes", new_command));
  }

  std::initializer_list<uint8_t> no_data = {0, 0, 0, 255};
  auto no_dims = {2, 1, 4};
  TfLiteTensor no_results = tflite::testing::CreateQuantizedTensor(
      no_data, tflite::testing::IntArrayFromInitializer(no_dims),
      "input_tensor", 0.0f, 128.0f);
  has_found_new_command = false;
  new_command = "";
  uint8_t score;
  for (int i = 0; i < 10; ++i) {
    const char* found_command;
    bool is_new_command;
    int32_t current_time_ms = 1000 + (i * 100);
    TF_LITE_MICRO_EXPECT_EQ(
        kTfLiteOk, recognize_commands.ProcessLatestResults(
                       &no_results, current_time_ms, &found_command, &score,
                       &is_new_command));
    if (is_new_command) {
      TF_LITE_MICRO_EXPECT(!has_found_new_command);
      has_found_new_command = true;
      new_command = found_command;
    }
  }
  TF_LITE_MICRO_EXPECT(has_found_new_command);
  if (has_found_new_command) {
    TF_LITE_MICRO_EXPECT_EQ(231, score);
    TF_LITE_MICRO_EXPECT_EQ(0, tflite::testing::TestStrcmp("no", new_command));
  }
}

TF_LITE_MICRO_TEST(RecognizeCommandsTestBadInputLength) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  RecognizeCommands recognize_commands(error_reporter, 1000, 51);

  std::initializer_list<uint8_t> bad_data = {0, 0, 255};
  auto bad_dims = {2, 1, 3};
  TfLiteTensor bad_results = tflite::testing::CreateQuantizedTensor(
      bad_data, tflite::testing::IntArrayFromInitializer(bad_dims),
      "input_tensor", 0.0f, 128.0f);

  const char* found_command;
  uint8_t score;
  bool is_new_command;
  TF_LITE_MICRO_EXPECT_NE(
      kTfLiteOk, recognize_commands.ProcessLatestResults(
                     &bad_results, 0, &found_command, &score, &is_new_command));
}

TF_LITE_MICRO_TEST(RecognizeCommandsTestBadInputTimes) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  RecognizeCommands recognize_commands(error_reporter, 1000, 51);

  std::initializer_list<uint8_t> result_data = {0, 0, 255, 0};
  auto result_dims = {2, 1, 4};
  TfLiteTensor results = tflite::testing::CreateQuantizedTensor(
      result_data, tflite::testing::IntArrayFromInitializer(result_dims),
      "input_tensor", 0.0f, 128.0f);

  const char* found_command;
  uint8_t score;
  bool is_new_command;
  TF_LITE_MICRO_EXPECT_EQ(
      kTfLiteOk, recognize_commands.ProcessLatestResults(
                     &results, 100, &found_command, &score, &is_new_command));
  TF_LITE_MICRO_EXPECT_NE(
      kTfLiteOk, recognize_commands.ProcessLatestResults(
                     &results, 0, &found_command, &score, &is_new_command));
}

TF_LITE_MICRO_TEST(RecognizeCommandsTestTooFewInputs) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  RecognizeCommands recognize_commands(error_reporter, 1000, 51);

  std::initializer_list<uint8_t> result_data = {0, 0, 255, 0};
  auto result_dims = {2, 1, 4};
  TfLiteTensor results = tflite::testing::CreateQuantizedTensor(
      result_data, tflite::testing::IntArrayFromInitializer(result_dims),
      "input_tensor", 0.0f, 128.0f);

  const char* found_command;
  uint8_t score;
  bool is_new_command;
  TF_LITE_MICRO_EXPECT_EQ(
      kTfLiteOk, recognize_commands.ProcessLatestResults(
                     &results, 100, &found_command, &score, &is_new_command));
  TF_LITE_MICRO_EXPECT_EQ(0, score);
  TF_LITE_MICRO_EXPECT_EQ(false, is_new_command);
}

TF_LITE_MICRO_TESTS_END

main_functions.h

C Header File
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_MAIN_FUNCTIONS_H_
#define TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_MAIN_FUNCTIONS_H_

// Initializes all data needed for the example. The name is important, and needs
// to be setup() for Arduino compatibility.
void setup();

// Runs one iteration of data gathering and inference. This should be called
// repeatedly from the application code. The name needs to be loop() for Arduino
// compatibility.
void loop();

#endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_MAIN_FUNCTIONS_H_

recognize_commands.h

C Header File
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_RECOGNIZE_COMMANDS_H_
#define TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_RECOGNIZE_COMMANDS_H_

#include <cstdint>

#include "tensorflow/lite/c/c_api_internal.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h"

// Partial implementation of std::dequeue, just providing the functionality
// that's needed to keep a record of previous neural network results over a
// short time period, so they can be averaged together to produce a more
// accurate overall prediction. This doesn't use any dynamic memory allocation
// so it's a better fit for microcontroller applications, but this does mean
// there are hard limits on the number of results it can store.
class PreviousResultsQueue {
 public:
  PreviousResultsQueue(tflite::ErrorReporter* error_reporter)
      : error_reporter_(error_reporter), front_index_(0), size_(0) {}

  // Data structure that holds an inference result, and the time when it
  // was recorded.
  struct Result {
    Result() : time_(0), scores_() {}
    Result(int32_t time, uint8_t* scores) : time_(time) {
      for (int i = 0; i < kCategoryCount; ++i) {
        scores_[i] = scores[i];
      }
    }
    int32_t time_;
    uint8_t scores_[kCategoryCount];
  };

  int size() { return size_; }
  bool empty() { return size_ == 0; }
  Result& front() { return results_[front_index_]; }
  Result& back() {
    int back_index = front_index_ + (size_ - 1);
    if (back_index >= kMaxResults) {
      back_index -= kMaxResults;
    }
    return results_[back_index];
  }

  void push_back(const Result& entry) {
    if (size() >= kMaxResults) {
      error_reporter_->Report(
          "Couldn't push_back latest result, too many already!");
      return;
    }
    size_ += 1;
    back() = entry;
  }

  Result pop_front() {
    if (size() <= 0) {
      error_reporter_->Report("Couldn't pop_front result, none present!");
      return Result();
    }
    Result result = front();
    front_index_ += 1;
    if (front_index_ >= kMaxResults) {
      front_index_ = 0;
    }
    size_ -= 1;
    return result;
  }

  // Most of the functions are duplicates of dequeue containers, but this
  // is a helper that makes it easy to iterate through the contents of the
  // queue.
  Result& from_front(int offset) {
    if ((offset < 0) || (offset >= size_)) {
      error_reporter_->Report("Attempt to read beyond the end of the queue!");
      offset = size_ - 1;
    }
    int index = front_index_ + offset;
    if (index >= kMaxResults) {
      index -= kMaxResults;
    }
    return results_[index];
  }

 private:
  tflite::ErrorReporter* error_reporter_;
  static constexpr int kMaxResults = 50;
  Result results_[kMaxResults];

  int front_index_;
  int size_;
};

// This class is designed to apply a very primitive decoding model on top of the
// instantaneous results from running an audio recognition model on a single
// window of samples. It applies smoothing over time so that noisy individual
// label scores are averaged, increasing the confidence that apparent matches
// are real.
// To use it, you should create a class object with the configuration you
// want, and then feed results from running a TensorFlow model into the
// processing method. The timestamp for each subsequent call should be
// increasing from the previous, since the class is designed to process a stream
// of data over time.
class RecognizeCommands {
 public:
  // labels should be a list of the strings associated with each one-hot score.
  // The window duration controls the smoothing. Longer durations will give a
  // higher confidence that the results are correct, but may miss some commands.
  // The detection threshold has a similar effect, with high values increasing
  // the precision at the cost of recall. The minimum count controls how many
  // results need to be in the averaging window before it's seen as a reliable
  // average. This prevents erroneous results when the averaging window is
  // initially being populated for example. The suppression argument disables
  // further recognitions for a set time after one has been triggered, which can
  // help reduce spurious recognitions.
  explicit RecognizeCommands(tflite::ErrorReporter* error_reporter,
                             int32_t average_window_duration_ms = 1000,
                             uint8_t detection_threshold = 200,
                             int32_t suppression_ms = 1500,
                             int32_t minimum_count = 3);

  // Call this with the results of running a model on sample data.
  TfLiteStatus ProcessLatestResults(const TfLiteTensor* latest_results,
                                    const int32_t current_time_ms,
                                    const char** found_command, uint8_t* score,
                                    bool* is_new_command);

 private:
  // Configuration
  tflite::ErrorReporter* error_reporter_;
  int32_t average_window_duration_ms_;
  uint8_t detection_threshold_;
  int32_t suppression_ms_;
  int32_t minimum_count_;

  // Working variables
  PreviousResultsQueue previous_results_;
  const char* previous_top_label_;
  int32_t previous_top_label_time_;
};

#endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_RECOGNIZE_COMMANDS_H_

yes_30ms_sample_data.cc

C/C++
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// See the header for documentation on the meaning of this data.

#include "tensorflow/lite/experimental/micro/examples/micro_speech/yes_30ms_sample_data.h"

const int g_yes_30ms_sample_data_size = 480;
const int16_t g_yes_30ms_sample_data[480] = {
    -876,   -470,   510,   803,   170,   -787,   -1568,  -1893,  -1598, -1027,
    -992,   -1803,  -2610, -2484, -1905, -2113,  -3113,  -3399,  -2267, -1261,
    -2007,  -3637,  -3909, -2340, -893,  -1158,  -2272,  -2486,  -1639, -915,
    -777,   -596,   -91,   196,   85,    210,    875,    1373,   1247,  1219,
    1958,   2718,   2328,  1196,  1008,  2350,   3677,   3269,   1503,  366,
    922,    2264,   2810,  1996,  608,   -168,   75,     680,    811,   395,
    -56,    -318,   -607,  -966,  -1108, -925,   -613,   -368,   -369,  -919,
    -1926,  -2460,  -1685, -300,  155,   -611,   -1524,  -2204,  -3227, -3859,
    -2037,  1622,   2382,  -2583, -8448, -7544,  -84,    4814,   915,   -6423,
    -7558,  -1746,  2515,  -59,   -4587, -3858,  1260,   3625,   187,   -4148,
    -3500,  1542,   5467,  4780,  1256,  -1127,  -403,   2481,   5332,  6346,
    5014,   2536,   1216,  2467,  5039,  6238,   5070,   3381,   3269,  4173,
    3905,   2248,   1586,  3299,  5240,  4362,   1004,   -1382,  -489,  2113,
    3168,   1620,   -742,  -1824, -1435, -897,   -1058,  -1500,  -1545, -1398,
    -1965,  -3266,  -4136, -3756, -2609, -1804,  -1986,  -3087,  -4599, -5296,
    -4051,  -1731,  -781,  -2228, -4092, -3977,  -2325,  -1353,  -1568, -1490,
    -428,   178,    -672,  -1650, -1058, 749,    2039,   2079,   1540,  897,
    310,    572,    2266,  4265,  4265,  1869,   -231,   559,    3332,  4752,
    3229,   768,    101,   1364,  2463,  1984,   819,    411,    723,   675,
    -162,   -923,   -743,  -32,   185,   -516,   -1653,  -2359,  -2103, -986,
    42,     -205,   -1702, -2870, -2337, -809,   -221,   -982,   -1544, -946,
    -598,   -2117,  -4291, -4100, -857,  1948,   338,    -4799,  -7972, -5403,
    173,    2371,   -1063, -5533, -5578, -1777,  605,    -985,   -3249, -2213,
    1184,   2691,   560,   -2356, -2288, 1233,   5244,   6441,   4004,  370,
    -663,   2555,   7404,  9282,  6573,  2612,   1836,   4662,   7467,  7393,
    5421,   4262,   4741,  5362,  4705,  3163,   2397,   3337,   4887,  4810,
    2254,   -749,   -1316, 772,   2706,  2016,   -573,   -2552,  -2746, -2012,
    -1647,  -1978,  -2579, -3105, -3473, -3911,  -4484,  -4891,  -4795, -4163,
    -3543,  -3538,  -4275, -5356, -5743, -4637,  -2614,  -1301,  -1825, -3341,
    -4011,  -2937,  -751,  1007,  1245,  235,    -639,   -61,    1626,  2864,
    2967,   2734,   3013,  3329,  2914,  2312,   2666,   3839,   4308,  3162,
    1453,   768,    1255,  1887,  2006,  1715,   1031,   -297,   -1660, -1690,
    -277,   813,    -30,   -2137, -3370, -2854,  -1553,  -593,   -413,  -1146,
    -2567,  -3440,  -2369, -205,  379,   -1258,  -2315,  -812,   262,   -3205,
    -8576,  -7894,  738,   7492,  1951,  -11595, -17098, -6934,  7139,  8065,
    -4575,  -14199, -8946, 3606,  7504,  -547,   -8242,  -5113,  4406,  8113,
    2134,   -5040,  -4089, 4157,  10934, 10158,  4167,   -565,   -192,  4428,
    9765,   12201,  9861,  4512,  1225,  3451,   8483,   10133,  6497,  2574,
    3333,   6806,   6986,  2487,  -1214, 623,    5416,   6647,   2204,  -3289,
    -4556,  -1565,  1544,  1525,  -1236, -4293,  -5695,  -5174,  -3995, -3403,
    -3449,  -3750,  -4505, -6014, -7296, -6523,  -3849,  -2096,  -3288, -5722,
    -6004,  -3581,  -1497, -1960, -3330, -2800,  -434,   964,    -111,  -1739,
    -1136,  1736,   4151,  3736,  1274,  -451,   469,    3386,   5833,  5898,
    3646,   1085,   272,   1743,  4061,  5108,   3837,   1490,   246,   967,
    1866,   859,    -1069, -974,  1542,  2835,   47,     -4285,  -5068, -1567,
    1781,   1223,   -1997, -4227, -3747, -1720,  41,     245,    -1228, -2972,
    -2673,  22,     1980,  -930,  -7721, -11271, -5725,  4974,   8484,  -2007,
    -16979, -19255, -4670, 11057, 9690,  -6417,  -17537, -10841, 4262,  9292,
};

yes_30ms_sample_data.h

C Header File
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// This data was created from the PCM data in a WAV file held in v2 of the
// Speech Commands test dataset, at the path:
// speech_commands_test_set_v0.02/yes/f2e59fea_nohash_1.wav
// The data was extracted starting at an offset of 8,000, which corresponds to
// the 26th spectrogram slice. It's designed to be used to test the
// preprocessing pipeline, to ensure that the expected spectrogram slice is
// produced given this input.

#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_YES_30MS_SAMPLE_DATA_H_
#define TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_YES_30MS_SAMPLE_DATA_H_

#include <cstdint>

extern const int g_yes_30ms_sample_data_size;
extern const int16_t g_yes_30ms_sample_data[];

#endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_YES_30MS_SAMPLE_DATA_H_

yes_1000ms_sample_data.cc

C/C++
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// See the header for documentation on the meaning of this data.

#include "tensorflow/lite/experimental/micro/examples/micro_speech/yes_1000ms_sample_data.h"

const int g_yes_1000ms_sample_data_size = 16000;
const int16_t g_yes_1000ms_sample_data[16000] = {
    -7,     -12,    -18,    -20,    -20,    -21,    -21,    -25,    -29,
    -31,    -31,    -30,    -30,    -29,    -30,    -30,    -29,    -28,
    -24,    -22,    -17,    -12,    -8,     -7,     -6,     -1,     2,
    5,      7,      8,      11,     15,     18,     19,     23,     24,
    24,     27,     27,     26,     25,     28,     30,     32,     33,
    31,     29,     27,     28,     30,     28,     26,     26,     24,
    22,     17,     16,     15,     13,     10,     5,      0,      -4,
    -4,     -7,     -9,     -12,    -14,    -14,    -13,    -11,    -10,
    -8,     -6,     -3,     3,      7,      8,      12,     15,     18,
    21,     19,     19,     21,     23,     24,     23,     22,     19,
    17,     11,     5,      -3,     -12,    -22,    -28,    -35,    -45,
    -54,    -62,    -69,    -76,    -84,    -92,    -100,   -109,   -116,
    -117,   -120,   -120,   -120,   -122,   -124,   -126,   -123,   -121,
    -116,   -113,   -107,   -97,    -88,    -75,    -61,    -50,    -41,
    -27,    -12,    4,      21,     37,     58,     76,     93,     108,
    121,    137,    156,    172,    184,    196,    205,    215,    224,
    235,    242,    245,    242,    240,    238,    231,    223,    214,
    205,    195,    178,    158,    135,    112,    90,     69,     46,
    19,     -11,    -45,    -76,    -105,   -133,   -159,   -186,   -211,
    -236,   -260,   -280,   -294,   -308,   -320,   -331,   -336,   -338,
    -335,   -326,   -316,   -301,   -286,   -267,   -246,   -225,   -203,
    -180,   -154,   -124,   -91,    -59,    -34,    -8,     19,     42,
    64,     87,     103,    119,    134,    148,    162,    174,    182,
    188,    190,    189,    187,    184,    180,    177,    171,    162,
    154,    144,    137,    129,    118,    106,    95,     81,     69,
    58,     48,     37,     26,     14,     3,      -7,     -22,    -31,
    -42,    -52,    -62,    -69,    -75,    -79,    -82,    -87,    -88,
    -92,    -94,    -91,    -87,    -85,    -81,    -74,    -70,    -64,
    -55,    -47,    -40,    -33,    -25,    -19,    -12,    -6,     -4,
    -1,     1,      1,      -2,     -9,     -15,    -17,    -18,    -20,
    -22,    -22,    -26,    -31,    -33,    -35,    -31,    -26,    -17,
    -4,     8,      19,     31,     44,     54,     64,     71,     79,
    86,     92,     102,    109,    111,    109,    104,    96,     84,
    70,     60,     51,     38,     27,     13,     4,      -3,     -9,
    -13,    -18,    -26,    -33,    -32,    -27,    -20,    -10,    -4,
    2,      6,      10,     14,     16,     21,     25,     29,     31,
    33,     35,     37,     33,     22,     15,     13,     11,     12,
    9,      5,      2,      1,      -3,     -9,     -17,    -27,    -32,
    -35,    -36,    -36,    -42,    -50,    -56,    -66,    -77,    -85,
    -96,    -100,   -106,   -113,   -118,   -121,   -119,   -117,   -119,
    -122,   -124,   -123,   -112,   -94,    -77,    -64,    -51,    -37,
    -22,    -3,     17,     37,     54,     68,     86,     100,    114,
    134,    154,    167,    174,    178,    182,    189,    189,    187,
    185,    179,    177,    174,    171,    157,    138,    123,    108,
    94,     76,     50,     25,     6,      -8,     -20,    -37,    -59,
    -86,    -110,   -132,   -147,   -159,   -169,   -178,   -191,   -203,
    -213,   -217,   -215,   -208,   -199,   -194,   -195,   -190,   -178,
    -165,   -155,   -144,   -134,   -123,   -103,   -80,    -56,    -35,
    -18,    -4,     11,     23,     36,     50,     65,     78,     93,
    111,    122,    129,    132,    131,    127,    125,    126,    126,
    128,    127,    125,    122,    118,    111,    108,    104,    99,
    93,     89,     90,     87,     82,     78,     75,     68,     65,
    67,     69,     66,     61,     54,     39,     28,     15,     3,
    -7,     -18,    -25,    -29,    -35,    -42,    -52,    -66,    -78,
    -83,    -85,    -86,    -86,    -82,    -83,    -84,    -83,    -81,
    -75,    -62,    -57,    -53,    -49,    -46,    -41,    -34,    -26,
    -16,    -10,    -7,     -2,     2,      6,      12,     15,     19,
    18,     15,     17,     21,     24,     30,     33,     27,     22,
    21,     20,     23,     24,     21,     15,     13,     8,      3,
    1,      -1,     -3,     -4,     -6,     -9,     -11,    -11,    -8,
    -10,    -13,    -15,    -19,    -17,    -11,    -2,     1,      2,
    6,      9,      10,     12,     13,     9,      8,      10,     13,
    20,     18,     13,     10,     4,      1,      -2,     -6,     -11,
    -13,    -16,    -18,    -15,    -18,    -21,    -21,    -22,    -23,
    -25,    -23,    -22,    -20,    -19,    -16,    -12,    -10,    -9,
    -11,    -15,    -19,    -22,    -19,    -14,    -11,    -9,     -11,
    -17,    -20,    -18,    -19,    -15,    -11,    -8,     -2,     8,
    19,     30,     36,     37,     36,     38,     45,     57,     69,
    77,     81,     79,     75,     76,     74,     69,     66,     60,
    53,     45,     36,     28,     22,     17,     10,     0,      -5,
    -11,    -15,    -18,    -26,    -31,    -33,    -34,    -34,    -35,
    -37,    -37,    -35,    -28,    -24,    -29,    -37,    -45,    -46,
    -41,    -36,    -31,    -32,    -33,    -37,    -37,    -36,    -36,
    -34,    -27,    -19,    -14,    -11,    -8,     -1,     6,      14,
    19,     21,     25,     30,     34,     38,     38,     33,     26,
    22,     19,     20,     18,     17,     15,     10,     2,      -3,
    -5,     -10,    -13,    -13,    -13,    -16,    -16,    -16,    -15,
    -13,    -14,    -13,    -16,    -19,    -20,    -18,    -17,    -18,
    -16,    -16,    -24,    -28,    -28,    -28,    -23,    -21,    -21,
    -20,    -24,    -27,    -23,    -18,    -14,    -7,     4,      11,
    15,     19,     21,     25,     33,     39,     41,     45,     47,
    50,     56,     58,     57,     59,     59,     55,     50,     47,
    39,     34,     30,     24,     18,     11,     8,      3,      0,
    -3,     -8,     -14,    -15,    -13,    -13,    -12,    -14,    -17,
    -17,    -12,    -10,    -4,     -7,     -12,    -10,    -14,    -17,
    -17,    -19,    -25,    -28,    -27,    -29,    -30,    -31,    -35,
    -38,    -43,    -47,    -51,    -52,    -50,    -49,    -48,    -47,
    -45,    -39,    -32,    -30,    -31,    -35,    -35,    -31,    -24,
    -17,    -12,    -11,    -14,    -15,    -17,    -16,    -9,     -5,
    -3,     -1,     0,      1,      0,      3,      12,     21,     26,
    33,     35,     38,     45,     50,     53,     53,     54,     58,
    61,     64,     69,     67,     66,     64,     58,     54,     51,
    46,     44,     45,     41,     35,     31,     27,     25,     27,
    25,     20,     13,     12,     16,     17,     17,     12,     7,
    3,      2,      -2,     -4,     -8,     -14,    -19,    -25,    -29,
    -38,    -49,    -60,    -69,    -73,    -71,    -74,    -82,    -89,
    -98,    -103,   -104,   -103,   -99,    -98,    -98,    -98,    -99,
    -97,    -94,    -91,    -85,    -82,    -78,    -74,    -74,    -71,
    -68,    -61,    -54,    -52,    -47,    -41,    -36,    -32,    -21,
    -12,    -3,     11,     26,     36,     44,     48,     55,     64,
    77,     92,     100,    108,    117,    120,    122,    128,    130,
    129,    130,    127,    124,    122,    121,    118,    114,    110,
    102,    92,     85,     80,     77,     68,     55,     46,     39,
    36,     34,     31,     27,     15,     5,      -1,     -5,     -11,
    -20,    -29,    -37,    -43,    -46,    -47,    -54,    -61,    -65,
    -74,    -82,    -84,    -91,    -94,    -96,    -104,   -109,   -111,
    -111,   -112,   -113,   -111,   -112,   -110,   -104,   -99,    -96,
    -93,    -89,    -87,    -81,    -71,    -63,    -54,    -45,    -43,
    -37,    -30,    -24,    -17,    -12,    -8,     -2,     2,      15,
    23,     28,     35,     41,     42,     44,     52,     58,     66,
    74,     78,     80,     82,     85,     88,     90,     92,     92,
    88,     87,     87,     79,     73,     69,     64,     62,     55,
    50,     45,     41,     36,     29,     24,     20,     16,     12,
    8,      5,      2,      1,      1,      0,      1,      -4,     -4,
    -4,     -4,     -1,     1,      2,      1,      -3,     -6,     -1,
    5,      6,      7,      8,      4,      2,      0,      -2,     -3,
    0,      -3,     -4,     -3,     -4,     -5,     -8,     -15,    -20,
    -25,    -28,    -32,    -37,    -38,    -39,    -43,    -48,    -55,
    -62,    -69,    -75,    -75,    -78,    -81,    -83,    -89,    -89,
    -92,    -91,    -91,    -89,    -83,    -81,    -74,    -66,    -63,
    -54,    -45,    -39,    -31,    -23,    -15,    -4,     6,      14,
    23,     29,     35,     41,     45,     49,     55,     61,     69,
    75,     75,     76,     75,     74,     74,     73,     74,     72,
    69,     69,     65,     62,     57,     52,     44,     35,     33,
    29,     24,     14,     7,      3,      -4,     -12,    -17,    -20,
    -22,    -27,    -32,    -34,    -39,    -42,    -43,    -42,    -43,
    -40,    -38,    -36,    -36,    -37,    -36,    -33,    -31,    -27,
    -24,    -23,    -22,    -17,    -11,    -7,     -7,     -7,     -3,
    5,      13,     19,     25,     27,     25,     27,     35,     40,
    40,     41,     45,     47,     50,     54,     52,     50,     45,
    43,     44,     40,     34,     28,     24,     18,     11,     6,
    -2,     -9,     -14,    -21,    -27,    -35,    -39,    -43,    -50,
    -57,    -62,    -66,    -68,    -71,    -72,    -73,    -74,    -76,
    -76,    -77,    -75,    -75,    -74,    -67,    -61,    -55,    -49,
    -45,    -40,    -30,    -21,    -11,    -4,     4,      13,     23,
    34,     44,     52,     59,     65,     70,     77,     84,     87,
    88,     90,     91,     90,     89,     85,     80,     75,     72,
    71,     64,     56,     48,     41,     34,     27,     21,     12,
    1,      -11,    -19,    -28,    -33,    -39,    -46,    -50,    -53,
    -58,    -63,    -66,    -71,    -73,    -76,    -76,    -74,    -73,
    -71,    -67,    -65,    -62,    -60,    -55,    -51,    -45,    -39,
    -35,    -31,    -27,    -20,    -13,    -6,     -3,     1,      8,
    12,     18,     24,     26,     30,     35,     38,     44,     47,
    47,     51,     53,     52,     53,     52,     50,     51,     49,
    50,     51,     50,     48,     48,     45,     43,     42,     37,
    34,     31,     31,     30,     26,     24,     21,     15,     12,
    11,     7,      4,      1,      -3,     -5,     -7,     -9,     -15,
    -21,    -26,    -28,    -31,    -35,    -39,    -46,    -48,    -49,
    -53,    -58,    -63,    -67,    -69,    -71,    -72,    -74,    -75,
    -77,    -77,    -73,    -72,    -69,    -65,    -60,    -55,    -50,
    -47,    -43,    -38,    -30,    -25,    -20,    -12,    -4,     4,
    9,      16,     20,     24,     28,     35,     43,     50,     58,
    61,     65,     72,     74,     74,     76,     79,     78,     76,
    78,     76,     76,     74,     70,     64,     59,     52,     46,
    41,     33,     26,     19,     12,     5,      -2,     -8,     -15,
    -20,    -26,    -31,    -37,    -39,    -41,    -44,    -44,    -47,
    -51,    -52,    -52,    -48,    -45,    -46,    -48,    -45,    -42,
    -40,    -36,    -32,    -27,    -24,    -22,    -18,    -16,    -11,
    -10,    -5,     0,      3,      8,      11,     16,     18,     21,
    23,     25,     26,     27,     28,     30,     31,     31,     30,
    29,     27,     26,     23,     19,     17,     13,     10,     6,
    0,      -2,     -5,     -10,    -12,    -15,    -19,    -23,    -26,
    -29,    -30,    -30,    -32,    -33,    -34,    -35,    -34,    -31,
    -29,    -29,    -28,    -28,    -23,    -19,    -17,    -12,    -12,
    -10,    -5,     -2,     3,      7,      10,     13,     14,     19,
    22,     26,     31,     34,     34,     35,     36,     39,     43,
    45,     47,     47,     48,     49,     51,     48,     47,     50,
    45,     41,     41,     38,     34,     34,     30,     23,     17,
    11,     7,      4,      -4,     -9,     -15,    -23,    -28,    -32,
    -35,    -39,    -45,    -46,    -49,    -53,    -52,    -53,    -55,
    -56,    -56,    -55,    -54,    -53,    -53,    -51,    -47,    -44,
    -42,    -40,    -37,    -33,    -28,    -25,    -23,    -18,    -15,
    -8,     -6,     -2,     3,      8,      15,     18,     23,     26,
    27,     32,     36,     36,     36,     39,     38,     38,     40,
    39,     35,     31,     29,     25,     23,     19,     15,     11,
    7,      5,      3,      1,      -1,     -6,     -8,     -7,     -10,
    -9,     -10,    -11,    -10,    -7,     -6,     -8,     -6,     -5,
    -4,     1,      2,      4,      7,      7,      9,      11,     11,
    9,      9,      10,     11,     13,     17,     15,     15,     15,
    17,     19,     17,     17,     17,     15,     15,     13,     11,
    12,     8,      7,      5,      3,      0,      -4,     -4,     -6,
    -9,     -12,    -14,    -15,    -15,    -16,    -20,    -19,    -20,
    -20,    -20,    -18,    -18,    -21,    -22,    -21,    -21,    -23,
    -20,    -20,    -23,    -24,    -23,    -25,    -25,    -25,    -25,
    -26,    -24,    -23,    -23,    -23,    -23,    -22,    -19,    -18,
    -15,    -14,    -10,    -8,     -4,     -1,     1,      3,      6,
    8,      9,      14,     19,     22,     24,     26,     29,     32,
    31,     34,     39,     42,     42,     46,     49,     50,     50,
    52,     53,     52,     49,     49,     48,     48,     46,     45,
    40,     34,     30,     25,     21,     17,     13,     10,     6,
    2,      -4,     -9,     -12,    -15,    -18,    -21,    -26,    -28,
    -31,    -32,    -33,    -35,    -35,    -38,    -37,    -36,    -34,
    -35,    -35,    -33,    -33,    -34,    -30,    -26,    -27,    -25,
    -23,    -22,    -18,    -15,    -16,    -12,    -9,     -9,     -6,
    -1,     2,      3,      5,      8,      7,      9,      12,     15,
    17,     18,     18,     19,     18,     20,     19,     18,     21,
    20,     19,     18,     16,     15,     15,     15,     14,     12,
    9,      9,      10,     8,      6,      4,      2,      1,      -1,
    -3,     -1,     -3,     -2,     -4,     -5,     -5,     -8,     -8,
    -10,    -10,    -8,     -8,     -8,     -7,     -8,     -8,     -8,
    -9,     -11,    -12,    -11,    -9,     -7,     -8,     -8,     -8,
    -10,    -8,     -7,     -8,     -7,     -6,     -7,     -5,     -3,
    -3,     -3,     -3,     -2,     0,      3,      3,      5,      7,
    10,     11,     10,     10,     12,     13,     16,     16,     16,
    17,     15,     16,     17,     16,     14,     16,     13,     11,
    11,     9,      9,      6,      4,      4,      3,      0,      -2,
    -4,     -7,     -7,     -7,     -13,    -15,    -13,    -14,    -16,
    -15,    -15,    -17,    -16,    -16,    -18,    -19,    -19,    -20,
    -19,    -16,    -15,    -13,    -12,    -10,    -7,     -6,     -4,
    -4,     -2,     0,      2,      6,      8,      10,     12,     14,
    15,     14,     13,     13,     13,     15,     15,     17,     17,
    17,     18,     17,     16,     15,     15,     14,     11,     9,
    8,      8,      9,      8,      5,      5,      3,      -1,     -1,
    -4,     -5,     -7,     -8,     -8,     -8,     -9,     -10,    -8,
    -11,    -12,    -12,    -12,    -12,    -13,    -11,    -11,    -9,
    -8,     -7,     -8,     -7,     -6,     -7,     -6,     -5,     -4,
    -4,     -2,     -2,     -3,     -2,     -2,     -3,     0,      -1,
    -3,     1,      1,      2,      4,      3,      5,      6,      3,
    3,      4,      3,      3,      4,      5,      4,      6,      7,
    7,      7,      6,      3,      3,      5,      3,      3,      6,
    6,      7,      6,      4,      5,      2,      1,      1,      0,
    0,      2,      1,      1,      1,      -1,     -2,     -3,     -5,
    -4,     -5,     -4,     -4,     -6,     -4,     -4,     -4,     -5,
    -6,     -5,     -6,     -5,     -4,     -5,     -4,     -3,     -4,
    0,      2,      2,      2,      2,      2,      2,      3,      3,
    5,      6,      6,      5,      6,      7,      6,      8,      6,
    5,      5,      5,      6,      6,      6,      5,      5,      2,
    2,      1,      2,      0,      -1,     -1,     -1,     -1,     0,
    -1,     -4,     -6,     -8,     -8,     -9,     -8,     -7,     -6,
    -5,     -5,     -6,     -3,     -4,     -5,     -4,     -7,     -6,
    -4,     -2,     -1,     -1,     1,      1,      1,      1,      1,
    2,      2,      1,      3,      4,      4,      6,      6,      6,
    6,      4,      4,      4,      4,      3,      2,      2,      2,
    2,      1,      1,      1,      0,      1,      1,      0,      -2,
    -2,     -3,     -3,     -3,     -3,     -5,     -4,     -3,     -5,
    -5,     -3,     -5,     -4,     -4,     -2,     -2,     -2,     -1,
    -3,     -2,     -2,     -1,     -3,     -2,     -1,     -2,     -2,
    -2,     0,      0,      0,      0,      0,      1,      0,      0,
    1,      2,      3,      3,      3,      4,      5,      4,      3,
    4,      5,      5,      7,      7,      6,      9,      8,      6,
    7,      8,      6,      5,      7,      8,      8,      8,      7,
    6,      5,      4,      4,      4,      5,      4,      2,      1,
    2,      1,      0,      -2,     -3,     -2,     -4,     -6,     -6,
    -7,     -7,     -8,     -9,     -9,     -9,     -9,     -9,     -9,
    -9,     -10,    -10,    -10,    -8,     -7,     -8,     -6,     -5,
    -4,     -3,     -5,     -2,     -2,     -2,     -1,     -1,     0,
    1,      1,      2,      3,      2,      4,      3,      3,      5,
    3,      3,      5,      4,      5,      6,      5,      4,      5,
    3,      2,      2,      3,      4,      4,      4,      4,      4,
    3,      4,      4,      4,      3,      2,      2,      2,      2,
    2,      2,      2,      2,      1,      1,      1,      2,      1,
    1,      2,      1,      1,      2,      1,      1,      1,      -1,
    0,      1,      0,      -1,     1,      -1,     -1,     -1,     -2,
    -1,     -1,     -1,     -1,     -1,     -1,     -1,     -1,     -2,
    -1,     0,      -1,     -1,     1,      1,      2,      0,      -1,
    0,      -1,     -1,     0,      0,      1,      2,      2,      2,
    1,      1,      0,      0,      0,      0,      1,      1,      0,
    0,      0,      0,      0,      -1,     -2,     -1,     -3,     -4,
    -4,     -4,     -4,     -4,     -4,     -4,     -3,     -3,     -5,
    -6,     -4,     -2,     -2,     -1,     -1,     -1,     -2,     1,
    -1,     1,      0,      0,      1,      1,      1,      1,      2,
    1,      2,      2,      3,      3,      3,      3,      4,      5,
    5,      5,      5,      5,      5,      5,      5,      6,      6,
    5,      5,      5,      6,      6,      5,      3,      6,      5,
    4,      5,      3,      2,      2,      2,      2,      1,      1,
    2,      0,      -1,     0,      -1,     -1,     -1,     -1,     -1,
    -1,     -1,     -3,     -3,     -3,     -3,     -4,     -4,     -5,
    -6,     -6,     -6,     -6,     -6,     -6,     -5,     -5,     -6,
    -5,     -4,     -4,     -4,     -4,     -2,     -2,     -2,     -1,
    -2,     0,      1,      0,      1,      3,      4,      4,      4,
    4,      4,      4,      5,      4,      4,      4,      5,      7,
    5,      4,      4,      4,      4,      3,      2,      2,      2,
    2,      2,      0,      1,      1,      0,      1,      1,      -1,
    0,      -1,     -2,     -1,     -3,     -4,     -4,     -3,     -5,
    -5,     -5,     -5,     -5,     -5,     -4,     -3,     -3,     -2,
    -3,     -2,     -2,     -5,     -3,     -3,     -3,     -2,     0,
    1,      1,      1,      1,      1,      1,      1,      1,      3,
    3,      4,      4,      4,      4,      5,      5,      2,      3,
    4,      3,      5,      4,      3,      4,      3,      3,      5,
    5,      3,      4,      2,      1,      1,      3,      4,      3,
    1,      3,      2,      1,      2,      1,      0,      1,      0,
    1,      0,      1,      1,      1,      1,      0,      -1,     0,
    0,      -1,     -1,     -2,     -1,     -1,     -2,     0,      -1,
    -2,     -1,     -1,     -2,     -2,     -1,     -3,     -3,     -3,
    -3,     -3,     -4,     -3,     -5,     -6,     -4,     -4,     -5,
    -4,     -3,     -5,     -6,     -4,     -5,     -6,     -4,     -3,
    -5,     -4,     -3,     -4,     -3,     -2,     -2,     -2,     0,
    0,      1,      1,      0,      0,      0,      1,      1,      3,
    3,      3,      4,      3,      3,      3,      3,      3,      3,
    3,      3,      3,      3,      3,      3,      3,      3,      3,
    1,      1,      1,      1,      1,      1,      1,      0,      0,
    0,      1,      -2,     -1,     1,      0,      -1,     -2,     -2,
    0,      1,      0,      1,      1,      1,      1,      0,      0,
    1,      0,      0,      2,      1,      0,      1,      1,      1,
    1,      3,      3,      3,      4,      3,      3,      4,      2,
    2,      2,      2,      2,      2,      2,      1,      2,      2,
    2,      2,      -1,     -1,     -1,     -1,     -1,     -1,     -1,
    -1,     -1,     -3,     -3,     -3,     -5,     -4,     -5,     -5,
    -5,     -5,     -7,     -7,     -7,     -8,     -7,     -8,     -7,
    -8,     -8,     -7,     -8,     -8,     -8,     -8,     -7,     -6,
    -6,     -6,     -7,     -6,     -6,     -5,     -5,     -3,     -2,
    -2,     -1,     0,      -1,     0,      1,      2,      2,      3,
    3,      3,      6,      7,      7,      7,      8,      9,      8,
    10,     10,     9,      10,     11,     9,      10,     12,     11,
    10,     9,      9,      9,      9,      10,     9,      6,      6,
    5,      5,      6,      3,      1,      1,      0,      1,      0,
    0,      1,      -1,     -2,     -2,     -1,     -3,     -3,     -2,
    -4,     -4,     -3,     -2,     -4,     -4,     -4,     -5,     -3,
    -3,     -5,     -3,     -3,     -5,     -4,     -2,     -2,     -3,
    -3,     -1,     0,      -1,     0,      0,      0,      -2,     -1,
    0,      -1,     -2,     -2,     -2,     -2,     -1,     -3,     -2,
    -3,     -4,     -3,     -3,     -3,     -3,     -3,     -3,     -3,
    -2,     -4,     -6,     -5,     -3,     -2,     -4,     -3,     -2,
    -4,     -4,     -4,     -3,     -4,     -5,     -4,     -5,     -3,
    -2,     -5,     -2,     -4,     -4,     -3,     -2,     -1,     -1,
    -1,     0,      2,      2,      1,      1,      3,      3,      3,
    3,      4,      4,      5,      6,      5,      5,      6,      7,
    7,      7,      8,      8,      7,      9,      9,      9,      9,
    10,     9,      9,      9,      9,      9,      9,      8,      7,
    9,      9,      6,      7,      5,      2,      3,      2,      1,
    1,      0,      -2,     -2,     -2,     -3,     -3,     -2,     -2,
    -4,     -5,     -4,     -4,     -4,     -4,     -5,     -4,     -4,
    -5,     -4,     -5,     -4,     -5,     -6,     -4,     -4,     -5,
    -5,     -5,     -5,     -6,     -4,     -4,     -4,     -3,     -2,
    -3,     -3,     -2,     -2,     -1,     -2,     -3,     -1,     0,
    -1,     0,      0,      0,      0,      1,      0,      0,      0,
    0,      -1,     1,      1,      1,      0,      -2,     -2,     -3,
    -3,     -4,     -4,     -6,     -7,     -5,     -4,     -5,     -5,
    -4,     -6,     -8,     -7,     -6,     -5,     -5,     -5,     -4,
    -4,     -5,     -4,     -3,     -3,     0,      0,      -2,     -1,
    0,      0,      1,      1,      2,      2,      2,      2,      2,
    4,      5,      5,      5,      6,      7,      7,      9,      10,
    10,     10,     12,     12,     13,     14,     14,     14,     15,
    15,     15,     15,     15,     15,     14,     15,     15,     12,
    13,     13,     12,     10,     11,     11,     11,     10,     8,
    6,      5,      7,      6,      6,      4,      3,      4,      5,
    3,      2,      2,      1,      1,      2,      3,      1,      0,
    0,      1,      0,      -2,     -1,     -2,     -3,     -3,     -3,
    -3,     -4,     -6,     -8,     -9,     -9,     -10,    -12,    -14,
    -15,    -18,    -21,    -21,    -21,    -21,    -22,    -24,    -26,
    -26,    -27,    -27,    -28,    -26,    -25,    -26,    -28,    -27,
    -24,    -23,    -23,    -24,    -21,    -17,    -17,    -15,    -12,
    -12,    -12,    -12,    -9,     -7,     -6,     -5,     -3,     -3,
    -2,     0,      0,      1,      3,      7,      6,      4,      6,
    7,      8,      11,     10,     10,     13,     15,     14,     13,
    18,     20,     18,     19,     21,     23,     24,     23,     22,
    24,     26,     26,     26,     27,     25,     23,     25,     27,
    28,     28,     28,     23,     19,     23,     24,     20,     20,
    21,     15,     13,     15,     16,     14,     11,     8,      7,
    8,      11,     11,     6,      4,      8,      7,      6,      7,
    6,      4,      7,      13,     12,     7,      8,      8,      4,
    1,      1,      1,      2,      -4,     -12,    -18,    -24,    -25,
    -25,    -32,    -41,    -55,    -59,    -61,    -75,    -87,    -96,
    -109,   -122,   -133,   -141,   -148,   -157,   -168,   -180,   -191,
    -198,   -202,   -207,   -206,   -207,   -211,   -211,   -208,   -203,
    -189,   -171,   -153,   -132,   -114,   -96,    -75,    -54,    -30,
    -5,     19,     43,     61,     77,     93,     106,    123,    143,
    161,    182,    198,    202,    201,    209,    229,    242,    240,
    235,    239,    249,    258,    255,    242,    233,    245,    268,
    278,    256,    223,    223,    253,    263,    235,    198,    178,
    188,    215,    230,    200,    143,    113,    128,    158,    158,
    128,    99,     90,     82,     70,     56,     32,     7,      14,
    46,     36,     -23,    -71,    -76,    -54,    -36,    -39,    -74,
    -118,   -134,   -122,   -101,   -104,   -129,   -164,   -174,   -129,
    -86,    -109,   -184,   -219,   -191,   -147,   -141,   -183,   -249,
    -290,   -269,   -236,   -266,   -346,   -394,   -366,   -325,   -353,
    -431,   -472,   -406,   -313,   -316,   -398,   -449,   -401,   -287,
    -194,   -164,   -193,   -245,   -212,   -55,    75,     67,     26,
    67,     165,    237,    269,    293,    319,    333,    368,    414,
    432,    463,    488,    448,    404,    391,    377,    361,    365,
    376,    308,    197,    150,    129,    73,     53,     91,     43,
    -107,   -165,   -54,    1,      -148,   -312,   -273,   -125,   -62,
    -128,   -258,   -294,   -141,   70,     57,     -217,   -378,   -145,
    198,    289,    169,    -47,    -219,   -101,   264,    458,    217,
    -163,   -199,   13,     121,    101,    -51,    -293,   -319,   -62,
    24,     -274,   -474,   -296,   -170,   -336,   -422,   -285,   -248,
    -302,   -130,   98,     -11,    -257,   -146,   184,    278,    264,
    331,    192,    -35,    235,    805,    830,    315,    82,     322,
    503,    522,    619,    557,    242,    163,    399,    507,    489,
    618,    602,    156,    -164,   112,    476,    406,    94,     -154,
    -242,   -132,   56,     5,      -325,   -566,   -527,   -478,   -624,
    -692,   -561,   -551,   -744,   -836,   -671,   -520,   -626,   -736,
    -647,   -581,   -639,   -687,   -702,   -739,   -665,   -383,   -236,
    -414,   -513,   -321,   -114,   -43,    32,     65,     -98,    -236,
    34,     608,    924,    680,    218,    56,     329,    847,    1214,
    1006,   341,    11,     340,    667,    553,    353,    355,    415,
    416,    364,    257,    108,    6,      113,    293,    233,    46,
    4,      25,     -10,    -12,    55,     40,     -65,    -56,    -26,
    -101,   -61,    143,    229,    78,     -161,   -210,   103,    424,
    377,    86,     -274,   -491,   -328,   -37,    60,     128,    188,
    -105,   -625,   -823,   -464,   138,    389,    111,    -343,   -526,
    -306,   13,     205,    250,    -35,    -554,   -764,   -498,   -42,
    167,    -210,   -639,   -448,   -101,   -110,   -171,   -74,    -39,
    47,     424,    616,    324,    98,     367,    853,    942,    416,
    -184,   -130,   339,    472,    369,    239,    -165,   -418,   101,
    742,    659,    325,    365,    476,    233,    -14,    270,    785,
    719,    -29,    -533,   -220,   237,    305,    179,    -190,   -644,
    -610,   -380,   -526,   -601,   -237,   48,     -36,    -124,   -49,
    -6,     23,     117,    55,     -199,   -428,   -512,   -338,   -238,
    -424,   -323,   -135,   -464,   -657,   -189,   100,    -379,   -964,
    -893,   -346,   -64,    -322,   -650,   -480,   32,     238,    201,
    386,    616,    611,    400,    195,    357,    842,    1051,   832,
    712,    829,    1070,   1307,   1081,   551,    363,    544,    623,
    239,    -374,   -609,   -230,   375,    486,    -52,    -446,   -270,
    181,    645,    601,    -135,   -654,   -256,   567,    840,    380,
    -54,    18,     334,    386,    21,     -214,   83,     243,    -316,
    -937,   -1074,  -1006,  -896,   -674,   -424,   -331,   -354,   -380,
    -481,   -392,   80,     358,    171,    -170,   -624,   -796,   -130,
    706,    803,    381,    152,    367,    620,    685,    655,    347,
    36,     180,    417,    412,    358,    288,    189,    150,    16,
    -240,   -428,   -428,   -266,   -335,   -819,   -1150,  -946,   -587,
    -437,   -580,   -961,   -1218,  -1065,  -704,   -431,   -350,   -315,
    -214,   -162,   -81,    26,     -8,     -52,    -117,   -226,   -40,
    285,    241,    -2,     -69,    57,     207,    81,     -144,   -69,
    65,     84,     49,     -168,   -248,   126,    502,    472,    192,
    120,    442,    667,    551,    512,    634,    814,    1014,   1098,
    1156,   1112,   974,    1144,   1330,   1099,   825,    847,    877,
    555,    2,      -243,   -102,   -196,   -471,   -377,   -235,   -439,
    -622,   -547,   -470,   -495,   -431,   -197,   -21,    21,     -9,
    -246,   -438,   -238,   -31,    0,      96,     137,    -25,    -211,
    -181,   -149,   -350,   -368,   -33,    21,     -308,   -323,   32,
    379,    605,    531,    85,     -374,   -367,   9,      277,    147,
    -356,   -698,   -494,   -140,   -126,   -354,   -549,   -673,   -642,
    -428,   -269,   -273,   -246,   -216,   -349,   -323,   -16,    32,
    -387,   -742,   -662,   -434,   -223,   41,     140,    -58,    -227,
    -80,    93,     20,     -166,   -360,   -536,   -555,   -305,   -33,
    -23,    -86,    -75,    -9,     82,     -1,     -156,   24,     532,
    916,    956,    835,    901,    1127,   1279,   1417,   1435,   1144,
    822,    862,    1214,   1352,   1001,   611,    539,    532,    369,
    189,    170,    308,    465,    430,    232,    64,     14,     51,
    -37,    -244,   -321,   -276,   -144,   57,     77,     -215,   -467,
    -335,   -186,   -245,   -133,   -81,    -588,   -1130,  -959,   -520,
    -631,   -1122,  -1270,  -971,   -873,   -1118,  -1157,  -1078,  -1296,
    -1365,  -1010,  -873,   -1138,  -1061,  -379,   89,     51,     177,
    372,    185,    -14,    63,     197,    125,    -123,   -60,    243,
    195,    88,     201,    115,    -63,    -12,    -79,    -492,   -751,
    -489,   49,     163,    -293,   -424,   -52,    229,    302,    212,
    217,    315,    70,     -207,   -210,   -173,   129,    619,    556,
    213,    181,    170,    112,    167,    322,    451,    206,    -136,
    58,     426,    526,    524,    394,    387,    568,    481,    297,
    164,    8,      263,    664,    777,    943,    989,    934,    1283,
    1495,   1153,   861,    738,    582,    614,    692,    655,    629,
    432,    127,    -119,   -338,   -313,   -138,   -204,   -561,   -994,
    -1168,  -948,   -700,   -658,   -788,   -1053,  -1027,  -684,   -566,
    -528,   -355,   -335,   -323,   -28,    206,    87,     56,     387,
    585,    296,    24,     261,    492,    248,    -132,   -469,   -674,
    -502,   -235,   -255,   -517,   -847,   -1038,  -965,   -707,   -630,
    -767,   -639,   -298,   -193,   -290,   -310,   -118,   74,     -77,
    -337,   -324,   -120,   187,    323,    -72,    -552,   -454,   -14,
    29,     -427,   -803,   -735,   -586,   -762,   -918,   -783,   -649,
    -723,   -857,   -786,   -626,   -591,   -417,   -83,    167,    262,
    49,     -161,   157,    842,    1298,   1356,   1206,   1041,   1194,
    1461,   1323,   1070,   1221,   1687,   2051,   2002,   1673,   1464,
    1550,   1851,   1907,   1531,   1327,   1399,   1342,   1287,   1264,
    1152,   1030,   878,    716,    601,    454,    264,    264,    352,
    151,    -193,   -296,   -161,   -93,    -215,   -423,   -617,   -668,
    -547,   -416,   -464,   -807,   -1175,  -1174,  -1045,  -1076,  -1023,
    -829,   -710,   -745,   -1069,  -1443,  -1417,  -1099,  -939,   -1165,
    -1307,  -1056,  -843,   -638,   -304,   -190,   -334,   -578,   -770,
    -705,   -675,   -947,   -957,   -565,   -437,   -617,   -843,   -1015,
    -813,   -489,   -584,   -904,   -1054,  -797,   -229,   -26,    -208,
    -66,    398,    710,    644,    390,    413,    726,    992,    1204,
    1337,   1234,   1104,   1038,   1001,   1043,   982,    847,    885,
    1024,   1098,   1138,   1108,   1038,   966,    885,    882,    878,
    929,    1005,   944,    1008,   1284,   1415,   1289,   1007,   760,
    812,    947,    806,    455,    111,    -72,    -290,   -611,   -626,
    -559,   -765,   -1034,  -1375,  -1632,  -1565,  -1588,  -1728,  -1585,
    -1477,  -1547,  -1533,  -1371,  -1103,  -995,   -1090,  -1102,  -947,
    -686,   -403,   -295,   -250,   -107,   -86,    -171,   -150,   12,
    234,    283,    185,    300,    461,    393,    382,    434,    378,
    306,    202,    195,    253,    -8,     -307,   -105,   264,    342,
    212,    34,     -57,    78,     435,    571,    180,    -165,   -51,
    339,    705,    683,    464,    658,    958,    825,    579,    465,
    390,    241,    61,     202,    429,    128,    -122,   241,    406,
    39,     -167,   -60,    15,     -31,    -68,    146,    402,    344,
    227,    208,    87,     -25,    -31,    -66,    -169,   -249,   -87,
    75,     -181,   -438,   -249,   49,     87,     -40,    -16,    53,
    -86,    -74,    98,     78,     110,    169,    -84,    -323,   -251,
    -102,   -172,   -513,   -750,   -675,   -568,   -587,   -583,   -523,
    -450,   -302,   -245,   -356,   -480,   -590,   -495,   -183,   -105,
    -191,   -215,   -308,   -206,   39,     4,      -77,    -21,    74,
    186,    218,    356,    611,    489,    83,     13,     246,    371,
    348,    240,    61,     -66,    -107,   -170,   -205,   -74,    200,
    277,    45,     -11,    180,    263,    100,    -74,    102,    246,
    6,      -154,   -162,   -197,   -128,   -189,   -227,   -49,    -238,
    -490,   -333,   -188,   1,      215,    150,    144,    128,    -33,
    187,    532,    676,    911,    773,    283,    351,    673,    620,
    349,    105,    205,    425,    325,    295,    372,    340,    511,
    628,    394,    224,    187,    91,     -174,   -556,   -482,   -37,
    -9,     -226,   -382,   -568,   -466,   -208,   -241,   -426,   -656,
    -814,   -788,   -902,   -1065,  -946,   -860,   -896,   -831,   -744,
    -672,   -685,   -743,   -723,   -783,   -813,   -570,   -341,   -239,
    -57,    137,    348,    576,    593,    454,    429,    503,    449,
    238,    173,    350,    423,    419,    530,    501,    272,    156,
    207,    295,    404,    568,    676,    419,    30,     113,    463,
    550,    473,    349,    126,    33,     144,    207,    193,    267,
    304,    81,     -252,   -401,   -368,   -347,   -404,   -452,   -408,
    -272,   -40,    234,    281,    48,     -72,    -18,    54,     208,
    309,    285,    245,    164,    38,     -20,    148,    430,    563,
    655,    679,    453,    300,    319,    219,    25,     -15,    54,
    -117,   -444,   -431,   -135,   -147,   -468,   -667,   -722,   -593,
    -301,   -217,   -428,   -642,   -598,   -400,   -422,   -602,   -628,
    -554,   -509,   -501,   -541,   -488,   -250,   -129,   -284,   -441,
    -358,   -161,   -82,    4,      134,    157,    290,    516,    582,
    702,    859,    871,    858,    759,    615,    616,    754,    839,
    725,    464,    259,    187,    127,    150,    280,    238,    92,
    78,     5,      -86,    6,      67,     -14,    -92,    -143,   -211,
    -89,    213,    300,    107,    -91,    -154,   -153,   -238,   -355,
    -314,   -227,   -168,   -92,    -142,   -219,   -156,   -47,    53,
    -15,    -195,   -161,   -186,   -382,   -395,   -297,   -238,   -240,
    -390,   -502,   -336,   -97,    -29,    -116,   -290,   -289,   -67,
    74,     112,    119,    182,    358,    382,    315,    341,    290,
    218,    190,    101,    -51,    -168,   -132,   -41,    -39,    -15,
    104,    186,    151,    68,     89,     154,    67,     10,     143,
    120,    -185,   -382,   -365,   -263,   -145,   -111,   -159,   -190,
    -53,    151,    177,    179,    384,    553,    502,    490,    572,
    600,    573,    442,    119,    -212,   -260,   -166,   -318,   -506,
    -413,   -279,   -285,   -354,   -390,   -278,   -142,   -85,    -18,
    -19,    -121,   -143,   -32,    88,     118,    42,     -96,    -187,
    -167,   -113,   -172,   -270,   -256,   -178,   -192,   -249,   -128,
    103,    132,    -47,    -147,   -104,   -56,    -9,     45,     35,
    109,    315,    381,    326,    336,    457,    667,    786,    675,
    489,    460,    569,    595,    470,    303,    272,    448,    620,
    545,    226,    -92,    -128,   91,     172,    -98,    -385,   -378,
    -264,   -284,   -362,   -314,   -148,   -72,    -198,   -350,   -353,
    -344,   -389,   -353,   -292,   -327,   -413,   -473,   -519,   -588,
    -577,   -546,   -737,   -989,   -1030,  -997,   -1010,  -861,   -683,
    -731,   -690,   -419,   -197,   -47,    112,    167,    74,     41,
    176,    309,    438,    671,    781,    793,    868,    904,    991,
    1099,   987,    812,    816,    869,    766,    605,    633,    728,
    592,    424,    460,    405,    170,    75,     30,     -105,   -58,
    63,     -58,    -242,   -359,   -415,   -255,   -44,    -127,   -266,
    -191,   -187,   -296,   -273,   -260,   -341,   -345,   -324,   -384,
    -467,   -421,   -233,   -125,   -227,   -341,   -256,   -168,   -217,
    -249,   -302,   -447,   -425,   -274,   -289,   -299,   -229,   -275,
    -272,   -103,   -57,    -117,   -106,   -162,   -256,   -184,   -31,
    51,     69,     31,     -19,    72,     256,    318,    331,    254,
    28,     -7,     121,    48,     -64,    58,     183,    152,    161,
    201,    167,    190,    287,    278,    157,    56,     103,    332,
    460,    299,    166,    238,    308,    374,    508,    509,    373,
    275,    270,    298,    229,    185,    192,    23,     -160,   -80,
    67,     31,     -170,   -378,   -384,   -330,   -500,   -648,   -615,
    -686,   -716,   -510,   -510,   -771,   -752,   -475,   -434,   -556,
    -480,   -403,   -515,   -464,   -255,   -177,   -105,   29,     95,
    152,    210,    190,    180,    279,    408,    325,    225,    462,
    607,    537,    759,    1022,   973,    945,    964,    846,    818,
    952,    907,    584,    313,    302,    428,    533,    479,    260,
    178,    262,    185,    18,     -77,    -263,   -370,   -208,   -240,
    -589,   -739,   -572,   -444,   -405,   -357,   -475,   -738,   -771,
    -542,   -441,   -529,   -651,   -803,   -823,   -556,   -285,   -227,
    -233,   -202,   -168,   -110,   -78,    -220,   -302,   -56,    129,
    -60,    -149,   54,     130,    169,    324,    231,    24,     89,
    269,    320,    262,    231,    225,    138,    67,     153,    310,
    399,    269,    -21,    -197,   -183,   -59,    144,    234,    -13,
    -274,   -168,   32,     -37,    -277,   -417,   -441,   -416,   -324,
    -312,   -467,   -540,   -373,   -166,   -161,   -297,   -365,   -341,
    -246,   -69,    81,     99,     -3,     11,     305,    540,    449,
    394,    586,    667,    606,    685,    665,    425,    410,    585,
    509,    360,    424,    538,    583,    482,    250,    159,    310,
    423,    217,    -131,   -280,   -204,   -51,    -12,    -204,   -338,
    -232,   -143,   -201,   -306,   -374,   -336,   -229,   -257,   -453,
    -576,   -497,   -379,   -326,   -302,   -372,   -504,   -453,   -229,
    -133,   -226,   -328,   -326,   -261,   -151,   -6,     97,     143,
    164,    143,    138,    267,    433,    500,    470,    297,    143,
    279,    504,    556,    475,    333,    233,    225,    228,    198,
    128,    24,     -17,    4,      -55,    -187,   -251,   -213,   -119,
    -94,    -214,   -357,   -349,   -246,   -195,   -183,   -261,   -440,
    -533,   -476,   -341,   -213,   -170,   -220,   -299,   -220,   -8,
    51,     -11,    19,     172,    292,    189,    9,      -6,     102,
    238,    384,    477,    448,    353,    304,    354,    473,    543,
    400,    229,    275,    380,    425,    415,    371,    398,    460,
    377,    202,    154,    199,    110,    -123,   -365,   -524,   -524,
    -360,   -134,   -47,    -182,   -348,   -453,   -542,   -503,   -376,
    -398,   -521,   -595,   -621,   -560,   -439,   -284,   -115,   -80,
    -123,   -57,    28,     -15,    -60,    -9,     47,     119,    203,
    288,    435,    571,    635,    706,    750,    627,    436,    345,
    330,    398,    460,    368,    213,    127,    140,    215,    202,
    58,     -99,    -244,   -387,   -470,   -527,   -637,   -754,   -791,
    -768,   -742,   -739,   -735,   -704,   -649,   -552,   -479,   -491,
    -494,   -454,   -433,   -422,   -398,   -315,   -115,   75,     175,
    244,    307,    360,    398,    460,    532,    529,    446,    422,
    497,    541,    504,    541,    702,    803,    744,    645,    621,
    727,    877,    873,    734,    593,    513,    523,    516,    412,
    336,    334,    274,    199,    163,    123,    125,    117,    107,
    140,    72,     -73,    -114,   -68,    -15,    13,     -122,   -338,
    -367,   -325,   -386,   -497,   -608,   -634,   -546,   -477,   -427,
    -377,   -412,   -464,   -436,   -343,   -276,   -327,   -390,   -313,
    -149,   -17,    2,      -93,    -146,   -104,   -76,    -87,    -131,
    -224,   -280,   -194,   -46,    12,     -76,    -189,   -151,   18,
    160,    200,    99,     -81,    -149,   -95,    -31,    -6,     -45,
    -93,    -97,    -71,    0,      73,     34,     -82,    -129,   -102,
    -84,    -96,    -107,   -69,    -5,     6,      18,     48,     35,
    27,     32,     -4,     -71,    -30,    119,    205,    266,    352,
    325,    237,    282,    352,    358,    342,    265,    203,    200,
    159,    120,    159,    195,    185,    133,    37,     20,     152,
    312,    363,    316,    255,    251,    259,    211,    160,    86,
    -4,     -30,    -79,    -154,   -213,   -271,   -243,   -146,   -147,
    -211,   -283,   -319,   -219,   -157,   -207,   -237,   -252,   -245,
    -136,   0,      42,     -22,    -108,   -82,    34,     130,    179,
    152,    98,     105,    110,    116,    180,    175,    66,     -9,
    -9,     36,     82,     75,     12,     -39,    -14,    23,     1,
    12,     31,     -61,    -155,   -184,   -158,   -86,    -60,    -67,
    -63,    -84,    -100,   -81,    -115,   -171,   -157,   -150,   -179,
    -191,   -209,   -245,   -217,   -128,   -54,    -42,    -73,    -100,
    -88,    -10,    104,    199,    249,    227,    201,    204,    151,
    83,     75,     87,     84,     67,     34,     18,     44,     110,
    218,    275,    232,    190,    209,    263,    294,    256,    174,
    108,    37,     -54,    -110,   -129,   -179,   -293,   -360,   -339,
    -282,   -190,   -135,   -188,   -239,   -234,   -227,   -182,   -127,
    -89,    -51,    -73,    -136,   -151,   -85,    0,      72,     129,
    122,    65,     44,     103,    202,    272,    252,    170,    148,
    167,    152,    130,    127,    79,     14,     70,     157,    142,
    109,    70,     -25,    -57,    -6,     46,     98,     135,    135,
    82,     16,     10,     68,     87,     -20,    -120,   -116,   -98,
    -102,   -129,   -204,   -271,   -282,   -252,   -216,   -215,   -221,
    -156,   -70,    -66,    -120,   -156,   -146,   -126,   -84,    -15,
    -21,    -76,    -8,     131,    146,    86,     42,     12,     44,
    110,    169,    171,    91,     68,     173,    262,    248,    160,
    36,     -90,    -109,   -24,    -12,    -57,    -64,    -78,    -89,
    -75,    -87,    -101,   -82,    -72,    -76,    -81,    -63,    -34,
    -4,     61,     87,     46,     23,     -1,     -8,     40,     63,
    46,     45,     39,     14,     -11,    -25,    -16,    36,     78,
    85,     110,    120,    132,    189,    228,    217,    154,    89,
    57,     14,     -14,    -6,     0,      13,     8,      -50,    -68,
    -60,    -107,   -140,   -126,   -122,   -151,   -147,   -118,   -105,
    -85,    -83,    -100,   -139,   -195,   -194,   -168,   -183,   -173,
    -148,   -166,   -168,   -123,   -59,    -11,    20,     64,     98,
    80,     58,     83,     111,    143,    176,    171,    152,    146,
    165,    174,    143,    93,     30,     5,      21,     42,     35,
    -37,    -94,    -61,    -12,    -5,     -27,    -58,    -85,    -81,
    -11,    79,     65,     -14,    -17,    15,     -4,     -2,     39,
    20,     -29,    -19,    3,      -11,    -39,    -62,    -43,    -34,
    -60,    -77,    -119,   -163,   -128,   -5,     87,     73,     51,
    116,    189,    217,    240,    234,    177,    192,    295,    344,
    313,    263,    236,    240,    230,    179,    99,     19,     -25,
    -16,    -9,     -35,    -66,    -53,    -16,    -40,    -70,    -81,
    -102,   -86,    -87,    -156,   -225,   -228,   -145,   -52,    -22,
    -57,    -171,   -255,   -247,   -208,   -165,   -187,   -242,   -275,
    -261,   -168,   -75,    -13,    8,      -62,    -125,   -136,   -133,
    -81,    -11,    -17,    -80,    -115,   -103,   -27,    71,     134,
    137,    44,     -48,    -24,    69,     156,    194,    175,    112,
    55,     54,     101,    148,    157,    142,    100,    44,     27,
    63,     106,    107,    89,     67,     37,     17,     30,     63,
    69,     61,     21,     -37,    -55,    -72,    -53,    -26,    -53,
    -77,    -87,    -109,   -119,   -80,    -36,    -29,    -38,    -48,
    -57,    -65,    -16,    52,     83,     83,     24,     -27,    -14,
    9,      27,     52,     50,     45,     90,     132,    117,    75,
    16,     -1,     60,     95,     55,     25,     26,     20,     61,
    119,    89,     1,      -61,    -68,    -46,    -36,    -40,    -39,
    -49,    -58,    -16,    30,     13,     -12,    18,     35,     6,
    3,      30,     22,     25,     52,     32,     12,     9,      -5,
    -16,    -25,    -33,    -38,    -44,    -76,    -118,   -118,   -96,
    -54,    -3,     9,      -31,    -82,    -84,    -35,    18,     25,
    -26,    -72,    -48,    8,      25,     8,      -20,    -66,    -105,
    -102,   -80,    -73,    -79,    -80,    -70,    -59,    -55,    -82,
    -113,   -85,    -51,    -59,    -57,    -38,    -13,    -7,     -18,
    -6,     20,     51,     55,     18,     -8,     -7,     24,     78,
    119,    137,    135,    139,    153,    144,    155,    179,    166,
    128,    56,     8,      38,     85,     94,     72,     20,     -32,
    -9,     25,     17,     -15,    -84,    -123,   -106,   -82,    -62,
    -60,    -43,    -4,     -12,    -45,    -68,    -108,   -100,   -47,
    -49,    -64,    -50,    -9,     37,     59,     68,     62,     53,
    49,     25,     13,     32,     40,     60,     109,    82,     18,
    10,     -1,     21,     102,    111,    40,     -10,    -9,     20,
    31,     0,      -51,    -108,   -135,   -89,    -21,    1,      -54,
    -125,   -129,   -113,   -144,   -205,   -227,   -167,   -118,   -114,
    -100,   -71,    5,      34,     -51,    -119,   -120,   -72,    10,
    56,     51,     58,     65,     98,     135,    84,     20,     -3,
    -1,     57,     135,    137,    90,     88,     107,    102,    45,
    -4,     9,      48,     95,     99,     65,     42,     44,     78,
    80,     29,     11,     39,     27,     0,      7,      19,     10,
    -45,    -99,    -86,    -77,    -74,    -57,    -74,    -84,    -92,
    -134,   -114,   -65,    -73,    -76,    -96,    -105,   -50,    -31,
    -17,    17,     9,      18,     62,     75,     55,     63,     76,
    61,     61,     80,     103,    107,    110,    131,    134,    120,
    94,     66,     70,     78,     59,     52,     57,     53,     72,
    76,     31,     -18,    -53,    -57,    -35,    -17,    -9,     -27,
    -34,    -7,     -17,    -26,    -13,    -60,    -86,    -53,    -42,
    -36,    -36,    -46,    -13,    19,     -16,    -47,    -15,    11,
    -9,     -18,    -26,    -24,    14,     8,      -53,    -54,    15,
    43,     15,     -9,     -5,     5,      -12,    -40,    -57,    -74,
    -94,    -105,   -91,    -20,    30,     -10,    -50,    -58,    -52,
    -42,    -47,    -54,    -61,    -83,    -64,    -30,    -3,     31,
    9,      -35,    -43,    -31,    6,      50,     54,     55,     67,
    53,     43,     30,     27,     62,     37,     -26,    -52,    -54,
    -29,    3,      -12,    -23,    11,     26,     23,     31,     57,
    66,     46,     32,     35,     83,     124,    111,    124,    157,
    143,    101,    80,     60,     27,     11,     21,     22,     9,
    -4,     -26,    -41,    -35,    -50,    -103,   -138,   -116,   -90,
    -89,    -90,    -79,    -74,    -58,    -18,    -12,    -29,    -36,
    -17,    22,     30,     -1,     -8,     8,      10,     19,     31,
    36,     38,     41,     28,     -7,     -14,    -6,     -20,    -30,
    -11,    -2,     -9,     0,      25,     56,     78,     68,     40,
    34,     47,     50,     40,     37,     26,     28,     53,     61,
    57,     25,     -35,    -75,    -65,    -48,    -65,    -81,    -67,
    -53,    -41,    3,      19,     -3,     -9,     -2,     -1,     -24,
    -36,    -23,    -26,    -29,    -9,     0,      -15,    -17,    -9,
    12,     50,     45,     14,     19,     37,     24,     9,      16,
    13,     -16,    -19,    3,      -3,     -12,    -10,    -23,    -43,
    -47,    -38,    -46,    -44,    -7,     3,      -19,    -13,    -26,
    -52,    -29,    -19,    -32,    0,      11,     -26,    -24,    -20,
    -41,    -30,    -24,    -53,    -67,    -26,    23,     20,     9,
    6,      -8,     3,      16,     7,      3,      -5,     2,      33,
    53,     72,     94,     86,     69,     96,     118,    95,     91,
    78,     32,     26,     48,     48,     37,     21,     7,      -6,
    -8,     8,      1,      -17,    -2,     18,     1,      -28,    -51,
    -84,    -93,    -74,    -46,    -18,    -19,    -31,    -10,    10,
    10,     7,      -5,     -30,    -39,    -28,    -9,     10,     17,
    11,     14,     20,     -1,     2,      18,     7,      15,     40,
    40,     32,     27,     23,     31,     43,     33,     7,      -3,
    18,     51,     53,     31,     21,     14,     16,     14,     4,
    11,     16,     1,      -24,    -38,    -33,    -27,    -50,    -74,
    -70,    -60,    -54,    -44,    -22,    -22,    -43,    -33,    -16,
    -35,    -36,    -18,    -27,    -42,    -46,    -36,    -17,    -15,
    -22,    -21,    -20,    -2,     15,     12,     22,     27,     22,
    41,     57,     60,     63,     54,     56,     65,     62,     68,
    58,     34,     53,     70,     58,     60,     51,     33,     41,
    39,     16,     -3,     -16,    -18,    -15,    -18,    -32,    -76,
    -85,    -62,    -82,    -87,    -68,    -84,    -75,    -40,    -48,
    -55,    -45,    -42,    -24,    -14,    -1,     27,     23,     -1,
    -2,     12,     15,     32,     55,     52,     55,     82,     81,
    58,     62,     59,     37,     24,     20,     17,     18,     19,
    15,     14,     5,      -18,    -27,    -20,    -19,    -34,    -39,
    -29,    -30,    -27,    -27,    -48,    -52,    -54,    -77,    -48,
    -18,    -36,    -34,    -13,    -21,    -38,    -28,    -15,    -7,
    -6,     -20,    -18,    2,      4,      -11,    -5,     7,      1,
    1,      12,     -2,     -17,    7,      15,     2,      15,     34,
    48,     78,     94,     82,     66,     66,     64,     47,     44,
    57,     64,     74,     65,     34,     26,     31,     32,     33,
    18,     5,      -1,     -18,    -22,    -31,    -54,    -37,    -32,
    -74,    -89,    -77,    -73,    -65,    -72,    -75,    -39,    -21,
    -31,    -31,    -24,    -19,    -8,     -4,     7,      26,     22,
    15,     13,     11,     28,     47,     42,     35,     28,     5,
    18,     55,     55,     45,     44,     18,     9,      18,     -2,
    -5,     6,      -15,    -16,    -12,    -20,    -4,     4,      -15,
    -18,    -10,    -5,     -2,     -16,    -24,    -14,    -7,     -14,
    -33,    -33,    -20,    -17,    -17,    -18,    -30,    -37,    -35,
    -34,    -13,    -3,     -28,    -28,    -10,    -21,    -17,    -4,
    -12,    -16,    -20,    -27,    -16,    -8,     -4,     14,     24,
    11,     17,     30,     27,     14,     7,      28,     30,     22,
    45,     47,     23,     31,     23,     -5,     10,     17,     -5,
    2,      15,     9,      20,     29,     11,     -9,     -8,     8,
    10,     -1,     -14,    -30,    -30,    -8,     -9,     -20,    -17,
    -17,    -12,    1,      6,      -7,     -18,    -6,     10,     -6,
    -7,     29,     35,     21,     16,     9,      25,     44,     26,
    21,     34,     28,     40,     41,     9,      -2,     1,      12,
    34,     18,     -12,    -10,    -16,    -29,    -24,    -25,    -20,
    -17,    -35,    -29,    -12,    -29,    -39,    -32,    -30,    -17,
    -12,    -28,    -20,    -5,     -4,     7,      14,     10,     3,
    -3,     0,      19,     27,     4,      -21,    -18,    -7,     -4,
    0,      1,      -6,     -17,    -30,    -24,    -11,    -9,     0,
    -1,     0,      -3,     -12,    1,      15,     -2,     3,      16,
    -3,     -8,     7,      3,      13,     32,     23,     10,     -6,
    -11,    8,      4,      -12,    -9,     3,      12,     -2,     -31,
    -36,    -33,    -37,    -17,    -5,     -20,    -14,    4,      5,
    4,      6,      17,     31,     27,     23,     16,     -1,     -4,
    15,     24,     21,     18,     7,      -7,     -14,    18,     41,
    25,     14,     13,     2,      5,      12,     8,      15,     10,
    2,      13,     10,     3,      5,      -1,     0,      11,     10,
    6,      2,      7,      10,     -4,     -3,     2,      -13,    -4,
    14,     -4,     -17,    -11,    -4,     8,      3,      -8,     -1,
    -7,     -20,    -4,     23,     23,     8,      5,      24,     21,
    -5,     -2,     7,      -9,     -15,    -8,     -6,     6,      2,
    -26,    -19,    1,      -19,    -31,    -27,    -34,    -41,    -47,
    -39,    -12,    -12,    -29,    -32,    -41,    -36,    -26,    -36,
    -35,    -33,    -29,    -1,     5,      -13,    -21,    -21,    -3,
    12,     1,      -7,     -1,     2,      12,     9,      -1,     15,
    21,     18,     25,     4,      -13,    5,      12,     16,     33,
    33,     19,     21,     26,     30,     30,     24,     23,     19,
    22,     34,     39,     28,     15,     14,     24,     24,     18,
    12,     10,     4,      8,      28,     29,     2,      -7,     6,
    8,      10,     2,      -13,    -8,     -2,     0,      12,     13,
    -1,     3,      21,     26,     24,     17,     11,     15,     19,
    19,     19,     11,     1,      3,      3,      0,      -5,     -11,
    -16,    -26,    -18,    3,      -5,     -17,    2,      10,     6,
    6,      -8,     -11,    4,      -3,     -17,    -10,    -17,    -37,
    -31,    -17,    -26,    -37,    -42,    -53,    -49,    -34,    -40,
    -39,    -21,    -17,    -23,    -23,    -25,    -30,    -24,    -13,
    -10,    -10,    1,      1,      -7,     7,      19,     11,     4,
    -3,     -8,     1,      6,      7,      25,     22,     -5,     3,
    20,     7,      -1,     14,     17,     18,     20,     12,     25,
    41,     23,     19,     37,     39,     21,     17,     23,     17,
    6,      9,      15,     4,      -15,    -8,     8,      7,      1,
    -12,    -18,    -14,    -15,    -10,    0,      -3,     3,      13,
    -8,     -21,    -8,     -26,    -29,    -1,     -9,     -24,    -19,
    -22,    -24,    -18,    -25,    -27,    -28,    -34,    -26,    -9,
    -14,    -14,    -8,     -8,     -5,     4,      4,      -10,    -12,
    -7,     -8,     -10,    -15,    -19,    -10,    -5,     -9,     -9,
    -19,    -33,    -27,    -14,    -15,    -14,    -16,    -25,    -10,
    5,      -7,     -11,    2,      3,      7,      17,     28,     33,
    32,     33,     39,     49,     57,     63,     62,     64,     67,
    59,     55,     67,     71,     58,     53,     53,     44,     38,
    44,     51,     51,     45,     35,     34,     46,     55,     48,
    36,     21,     3,      -5,     2,      7,      0,      -17,    -30,
    -34,    -48,    -62,    -64,    -66,    -66,    -62,    -79,    -90,
    -85,    -88,    -88,    -85,    -88,    -103,   -112,   -112,   -102,
    -99,    -102,   -103,   -110,   -100,   -80,    -60,    -57,    -68,
    -59,    -45,    -35,    -6,     9,      -3,     2,      32,     45,
    48,     51,     40,     51,     78,     85,     83,     87,     94,
    101,    104,    105,    100,    86,     82,     96,     102,    96,
    85,     68,     63,     65,     55,     50,     46,     28,     32,
    43,     33,     30,     27,     8,      18,     36,     27,     20,
    13,     -14,    -19,    8,      12,     0,      -1,     -12,    -24,
    -20,    -27,    -39,    -39,    -39,    -44,    -38,    -32,    -42,
    -38,    -33,    -43,    -55,    -57,    -60,    -61,    -56,    -57,
    -55,    -43,    -46,    -58,    -55,    -50,    -50,    -51,    -48,
    -46,    -44,    -36,    -26,    -20,    -13,    -11,    -8,     1,
    5,      0,      8,      21,     31,     42,     39,     43,     56,
    48,     37,     45,     45,     47,     52,     46,     40,     26,
    18,     28,     30,     22,     14,     0,      -3,     8,      0,
    -7,     0,      -10,    -13,    -9,     -13,    -13,    -18,    -33,
    -32,    -26,    -37,    -41,    -32,    -26,    -30,    -34,    -31,
    -38,    -40,    -24,    -25,    -29,    -15,    -18,    -23,    -4,
    2,      -7,     0,      5,      10,     22,     23,     25,     31,
    33,     37,     38,     39,     43,     46,     41,     44,     46,
    37,     35,     46,     63,     67,     52,     38,     30,     35,
    41,     41,     41,     29,     15,     16,     4,      -4,     3,
    -12,    -18,    -13,    -27,    -39,    -47,    -55,    -44,    -43,
    -53,    -45,    -36,    -37,    -37,    -38,    -40,    -49,    -57,
    -41,    -24,    -28,    -31,    -26,    -20,    -15,    -21,    -23,
    -18,    -19,    -14,    -10,    -11,    1,      -6,     -26,    -14,
    -1,     -7,     -10,    -11,    -9,     0,      -4,     -9,     3,
    8,      0,      -2,     1,      16,     20,     7,      9,      10,
    8,      18,     12,     11,     17,     -6,     -19,    0,      0,
    -10,    -6,     -12,    -14,    -11,    -9,     -2,     -10,    -19,
    -9,     -11,    -4,     18,     7,      -3,     9,      17,     23,
    28,     25,     19,     19,     24,     33,     37,     30,     28,
    35,     44,     43,     33,     31,     30,     26,     33,     39,
    35,     31,     27,     19,     23,     24,     19,     13,     0,
    0,      2,      -7,     -9,     -10,    -13,    -6,     -6,     -23,
    -28,    -15,    -9,     -20,    -34,    -30,    -15,    -12,    -11,
    -3,     -4,     -4,     6,      15,     9,      -11,    -20,    3,
    26,     23,     1,      -16,    -3,     12,     2,      -22,    -36,
    -35,    -28,    -20,    -13,    -19,    -38,    -43,    -29,    -11,
    -5,     -15,    -37,    -40,    -9,     12,     -1,     -23,    -30,
    -16,    12,     21,     -1,     -25,    -21,    4,      34,     55,
    34,     -12,    -11,    47,     99,     107,    58,     0,      8,
    78,     148,    151,    56,     -40,    -2,     142,    215,    99,
    -67,    -64,    76,     153,    99,     -21,    -107,   -92,    -1,
    106,    107,    -123,   -395,   -334,   60,     274,    -69,    -597,
    -626,   -126,   238,    18,     -447,   -577,   -312,   -34,    20,
    -89,    -242,   -332,   -222,   74,     262,    64,     -285,   -232,
    259,    563,    294,    -138,   -130,   312,    642,    515,    189,
    57,     187,    415,    538,    467,    277,    109,    134,    334,
    441,    299,    59,     -7,     128,    228,    146,    -20,    -99,
    -34,    60,     24,     -108,   -188,   -147,   -57,    -48,    -142,
    -224,   -210,   -144,   -122,   -175,   -212,   -176,   -150,   -199,
    -256,   -210,   -100,   -79,    -195,   -298,   -248,   -107,   -48,
    -110,   -192,   -224,   -189,   -112,   -40,    -31,    -124,   -238,
    -193,   -3,     87,     -53,    -221,   -165,   48,     132,    -2,
    -150,   -109,   61,     147,    83,     -20,    -60,    -13,    85,
    157,    130,    17,     -68,    -10,    147,    217,    116,    -20,
    -21,    103,    200,    158,    52,     35,     105,    155,    132,
    81,     74,     110,    114,    74,     48,     68,     100,    77,
    27,     30,     48,     19,     -15,    7,      63,     53,     -56,
    -123,   -41,    81,     75,     -61,    -154,   -84,    45,     68,
    -24,    -105,   -76,    22,     53,     -13,    -63,    -21,    54,
    59,     -1,     -34,    16,     80,     81,     48,     37,     61,
    89,     88,     101,    134,    132,    100,    83,     125,    188,
    173,    101,    95,     172,    214,    149,    68,     94,     181,
    177,    103,    83,     132,    165,    122,    83,     140,    191,
    153,    92,     106,    198,    226,    138,    85,     146,    215,
    187,    110,    77,     115,    146,    115,    91,     96,     78,
    27,     -3,     42,     102,    71,     -23,    -46,    30,     95,
    63,     -18,    -25,    77,     174,    138,    13,     -25,    96,
    218,    181,    34,     -70,    -45,    17,     2,      -67,    -174,
    -346,   -516,   -553,   -446,   -455,   -789,   -1213,  -1308,  -1046,
    -878,   -1179,  -1691,  -1839,  -1528,  -1219,  -1292,  -1623,  -1772,
    -1538,  -1147,  -921,   -951,   -1038,  -929,   -549,   -95,    155,
    127,    97,     387,    931,    1339,   1380,   1234,   1276,   1661,
    2102,   2223,   2027,   1848,   1942,   2198,   2295,   2119,   1856,
    1725,   1745,   1752,   1601,   1335,   1102,   993,    952,    830,
    570,    286,    139,    133,    85,     -135,   -436,   -638,   -645,
    -571,   -620,   -835,   -1064,  -1151,  -1069,  -951,   -964,   -1109,
    -1209,  -1162,  -1044,  -961,   -944,   -977,   -1001,  -912,   -687,
    -517,   -623,   -887,   -897,   -469,   10,     -35,    -590,   -934,
    -545,   184,    427,    -53,    -619,   -563,   40,     489,    339,
    -128,   -306,   -6,     403,    497,    232,    -55,    0,      388,
    704,    584,    145,    -76,    260,    816,    942,    485,    2,
    65,     575,    923,    744,    290,    76,     276,    596,    662,
    419,    134,    92,     280,    434,    344,    88,     -66,    8,
    151,    126,    -81,    -239,   -176,   -29,    -74,    -351,   -574,
    -487,   -208,   -132,   -426,   -780,   -797,   -577,   -595,   -978,
    -1169,  -667,   -36,    -548,   -2285,  -3281,  -1756,  927,    1236,
    -1911,  -5006,  -4073,  -66,    2017,   -295,   -3701,  -3797,  -892,
    975,    -165,   -1978,  -1636,  374,    1482,   679,    -567,   -591,
    706,    2337,   3224,   2743,   1269,   287,    1221,   3597,   5083,
    4106,   1858,   972,    2334,   4096,   4167,   2806,   1916,   2383,
    3045,   2508,   1220,   820,    1784,   2669,   1981,   204,    -876,
    -470,   510,    803,    170,    -787,   -1568,  -1893,  -1598,  -1027,
    -992,   -1803,  -2610,  -2484,  -1905,  -2113,  -3113,  -3399,  -2267,
    -1261,  -2007,  -3637,  -3909,  -2340,  -893,   -1158,  -2272,  -2486,
    -1639,  -915,   -777,   -596,   -91,    196,    85,     210,    875,
    1373,   1247,   1219,   1958,   2718,   2328,   1196,   1008,   2350,
    3677,   3269,   1503,   366,    922,    2264,   2810,   1996,   608,
    -168,   75,     680,    811,    395,    -56,    -318,   -607,   -966,
    -1108,  -925,   -613,   -368,   -369,   -919,   -1926,  -2460,  -1685,
    -300,   155,    -611,   -1524,  -2204,  -3227,  -3859,  -2037,  1622,
    2382,   -2583,  -8448,  -7544,  -84,    4814,   915,    -6423,  -7558,
    -1746,  2515,   -59,    -4587,  -3858,  1260,   3625,   187,    -4148,
    -3500,  1542,   5467,   4780,   1256,   -1127,  -403,   2481,   5332,
    6346,   5014,   2536,   1216,   2467,   5039,   6238,   5070,   3381,
    3269,   4173,   3905,   2248,   1586,   3299,   5240,   4362,   1004,
    -1382,  -489,   2113,   3168,   1620,   -742,   -1824,  -1435,  -897,
    -1058,  -1500,  -1545,  -1398,  -1965,  -3266,  -4136,  -3756,  -2609,
    -1804,  -1986,  -3087,  -4599,  -5296,  -4051,  -1731,  -781,   -2228,
    -4092,  -3977,  -2325,  -1353,  -1568,  -1490,  -428,   178,    -672,
    -1650,  -1058,  749,    2039,   2079,   1540,   897,    310,    572,
    2266,   4265,   4265,   1869,   -231,   559,    3332,   4752,   3229,
    768,    101,    1364,   2463,   1984,   819,    411,    723,    675,
    -162,   -923,   -743,   -32,    185,    -516,   -1653,  -2359,  -2103,
    -986,   42,     -205,   -1702,  -2870,  -2337,  -809,   -221,   -982,
    -1544,  -946,   -598,   -2117,  -4291,  -4100,  -857,   1948,   338,
    -4799,  -7972,  -5403,  173,    2371,   -1063,  -5533,  -5578,  -1777,
    605,    -985,   -3249,  -2213,  1184,   2691,   560,    -2356,  -2288,
    1233,   5244,   6441,   4004,   370,    -663,   2555,   7404,   9282,
    6573,   2612,   1836,   4662,   7467,   7393,   5421,   4262,   4741,
    5362,   4705,   3163,   2397,   3337,   4887,   4810,   2254,   -749,
    -1316,  772,    2706,   2016,   -573,   -2552,  -2746,  -2012,  -1647,
    -1978,  -2579,  -3105,  -3473,  -3911,  -4484,  -4891,  -4795,  -4163,
    -3543,  -3538,  -4275,  -5356,  -5743,  -4637,  -2614,  -1301,  -1825,
    -3341,  -4011,  -2937,  -751,   1007,   1245,   235,    -639,   -61,
    1626,   2864,   2967,   2734,   3013,   3329,   2914,   2312,   2666,
    3839,   4308,   3162,   1453,   768,    1255,   1887,   2006,   1715,
    1031,   -297,   -1660,  -1690,  -277,   813,    -30,    -2137,  -3370,
    -2854,  -1553,  -593,   -413,   -1146,  -2567,  -3440,  -2369,  -205,
    379,    -1258,  -2315,  -812,   262,    -3205,  -8576,  -7894,  738,
    7492,   1951,   -11595, -17098, -6934,  7139,   8065,   -4575,  -14199,
    -8946,  3606,   7504,   -547,   -8242,  -5113,  4406,   8113,   2134,
    -5040,  -4089,  4157,   10934,  10158,  4167,   -565,   -192,   4428,
    9765,   12201,  9861,   4512,   1225,   3451,   8483,   10133,  6497,
    2574,   3333,   6806,   6986,   2487,   -1214,  623,    5416,   6647,
    2204,   -3289,  -4556,  -1565,  1544,   1525,   -1236,  -4293,  -5695,
    -5174,  -3995,  -3403,  -3449,  -3750,  -4505,  -6014,  -7296,  -6523,
    -3849,  -2096,  -3288,  -5722,  -6004,  -3581,  -1497,  -1960,  -3330,
    -2800,  -434,   964,    -111,   -1739,  -1136,  1736,   4151,   3736,
    1274,   -451,   469,    3386,   5833,   5898,   3646,   1085,   272,
    1743,   4061,   5108,   3837,   1490,   246,    967,    1866,   859,
    -1069,  -974,   1542,   2835,   47,     -4285,  -5068,  -1567,  1781,
    1223,   -1997,  -4227,  -3747,  -1720,  41,     245,    -1228,  -2972,
    -2673,  22,     1980,   -930,   -7721,  -11271, -5725,  4974,   8484,
    -2007,  -16979, -19255, -4670,  11057,  9690,   -6417,  -17537, -10841,
    4262,   9292,   206,    -9128,  -6224,  4828,   10018,  3699,   -5183,
    -5121,  4702,   14279,  14466,  5778,   -2633,  -2185,  7036,   16118,
    16305,  8081,   390,    499,    6580,   11150,  10036,  5704,   2902,
    3378,   4664,   3786,   863,    -796,   1216,   4609,   4493,   -338,
    -5670,  -6486,  -2751,  884,    571,    -3095,  -6446,  -6997,  -5770,
    -5041,  -5016,  -4216,  -2579,  -2468,  -5088,  -8129,  -7964,  -4228,
    -323,   497,    -1556,  -3653,  -3615,  -1718,  464,    1808,   2386,
    2832,   3085,   2905,   2676,   3473,   5501,   7094,   6442,   3929,
    1663,   1436,   3254,   5807,   7100,   5044,   -34,    -4091,  -2992,
    2149,   5333,   2562,   -3067,  -5877,  -4480,  -2080,  -1793,  -3026,
    -3838,  -3735,  -3663,  -4472,  -5756,  -5753,  -3576,  -640,   -274,
    -3965,  -7787,  -6757,  -717,   4380,   3595,   -1553,  -5936,  -8603,
    -10223, -8952,  -922,   9700,   9355,   -7788,  -25795, -22413, 2268,
    20887,  12133,  -11291, -20129, -5899,  10236,  8585,   -3645,  -6300,
    4667,   14216,  9346,   -3593,  -8558,  715,    15085,  21179,  14887,
    3733,   -2703,  -675,   7170,   15131,  18360,  13959,  4205,   -2825,
    -656,   7594,   11845,  7182,   319,    -439,   3255,   3213,   -3299,
    -8972,  -6318,  2300,   7190,   2254,   -9247,  -17334, -15064, -4452,
    5160,   5127,   -4268,  -14501, -17256, -11145, -1830,  3786,   2984,
    -2498,  -8101,  -9587,  -5703,  622,    4570,   4035,   1442,   729,
    2493,   3534,   2433,   2239,   5944,   11438,  12371,  6496,   -211,
    -156,   7092,   13566,  11979,  3928,   -2545,  -2226,  2713,   6150,
    5117,   1270,   -1851,  -2859,  -2376,  -1909,  -2364,  -3401,  -4183,
    -3897,  -2875,  -3205,  -5503,  -7822,  -7501,  -3934,  -942,   -1572,
    -4262,  -5939,  -4671,  -2353,  -1387,  -1159,  -1270,  -1328,  -606,
    474,    1044,   -2647,  -11603, -17081, -10374, 5922,   14849,  2056,
    -22033, -31238, -14612, 11094,  17910,  1778,   -15538, -15417, -2045,
    6690,   2855,   -2559,  473,    8823,   11423,  3782,   -4649,  -2775,
    9111,   20847,  21610,  11572,  962,    -1465,  5731,   15559,  20008,
    16950,  9230,   2204,   114,    3088,   8130,   10523,  7643,   2045,
    -2107,  -2945,  -2538,  -3593,  -5210,  -4403,  -857,   1328,   -2497,
    -11667, -18881, -16866, -6286,  3400,   2835,   -7811,  -18322, -19279,
    -10025, 1525,   6930,   3766,   -4647,  -11401, -9904,  -322,   10100,
    12428,  5874,   -274,   926,    6762,   9360,   6778,   5904,   10509,
    15077,  12681,  3846,   -1653,  2460,   11036,  14737,  8967,   -1021,
    -6168,  -3899,  2328,   6041,   3404,   -2878,  -7672,  -6869,  -1918,
    801,    -2188,  -7419,  -8083,  -2687,  1898,   -692,   -8121,  -11198,
...

This file has been truncated, please download it to see its full contents.

yes_1000ms_sample_data.h

C Header File
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// This data was created from the PCM data in a WAV file held in v2 of the
// Speech Commands test dataset, at the path:
// speech_commands_test_set_v0.02/yes/f2e59fea_nohash_1.wav
// This should contain all 16,000 samples from the one-second file.

#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_YES_1000MS_SAMPLE_DATA_H_
#define TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_YES_1000MS_SAMPLE_DATA_H_

#include <cstdint>

extern const int g_yes_1000ms_sample_data_size;
extern const int16_t g_yes_1000ms_sample_data[];

#endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_YES_1000MS_SAMPLE_DATA_H_

audio_provider.cc

C/C++
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/audio_provider.h"

#include <limits>

// These are headers from Ambiq's Apollo3 SDK.
#include "am_bsp.h"         // NOLINT
#include "am_mcu_apollo.h"  // NOLINT
#include "am_util.h"        // NOLINT

#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"

namespace {

// These are the raw buffers that are filled by the ADC during DMA
constexpr int kAdcNumSlots = 2;
constexpr int kAdcSamplesPerSlot = 1024;
constexpr int kAdcSampleBufferSize = (kAdcNumSlots * kAdcSamplesPerSlot);
uint32_t g_ui32ADCSampleBuffer0[kAdcSampleBufferSize];
uint32_t g_ui32ADCSampleBuffer1[kAdcSampleBufferSize];
// Controls the double buffering between the two DMA buffers.
int g_dma_destination_index = 0;
// ADC Device Handle.
static void* g_adc_handle;
// ADC DMA error flag.
volatile bool g_adc_dma_error;
// So the interrupt can use the passed-in error handler to report issues.
tflite::ErrorReporter* g_adc_dma_error_reporter = nullptr;

// Holds a longer history of audio samples in a ring buffer.
constexpr int kAudioCaptureBufferSize = 16000;
int16_t g_audio_capture_buffer[kAudioCaptureBufferSize] = {};
int g_audio_capture_buffer_start = 0;
int64_t g_total_samples_captured = 0;
int32_t g_latest_audio_timestamp = 0;

// Copy of audio samples returned to the caller.
int16_t g_audio_output_buffer[kMaxAudioSampleSize];
bool g_is_audio_initialized = false;

// Start the DMA fetch of ADC samples.
void adc_start_dma(tflite::ErrorReporter* error_reporter) {
  am_hal_adc_dma_config_t ADCDMAConfig;

  // Configure the ADC to use DMA for the sample transfer.
  ADCDMAConfig.bDynamicPriority = true;
  ADCDMAConfig.ePriority = AM_HAL_ADC_PRIOR_SERVICE_IMMED;
  ADCDMAConfig.bDMAEnable = true;
  ADCDMAConfig.ui32SampleCount = kAdcSampleBufferSize;
  if (g_dma_destination_index == 0) {
    ADCDMAConfig.ui32TargetAddress = (uint32_t)g_ui32ADCSampleBuffer0;
  } else {
    ADCDMAConfig.ui32TargetAddress = (uint32_t)g_ui32ADCSampleBuffer1;
  }
  if (AM_HAL_STATUS_SUCCESS !=
      am_hal_adc_configure_dma(g_adc_handle, &ADCDMAConfig)) {
    error_reporter->Report("Error - configuring ADC DMA failed.");
  }

  // Reset the ADC DMA flags.
  g_adc_dma_error = false;
  g_adc_dma_error_reporter = error_reporter;
}

// Configure the ADC.
void adc_config0(tflite::ErrorReporter* error_reporter) {
  am_hal_adc_config_t ADCConfig;
  am_hal_adc_slot_config_t ADCSlotConfig;

  // Initialize the ADC and get the handle.
  if (AM_HAL_STATUS_SUCCESS != am_hal_adc_initialize(0, &g_adc_handle)) {
    error_reporter->Report("Error - reservation of the ADC0 instance failed.");
  }

  // Power on the ADC.
  if (AM_HAL_STATUS_SUCCESS !=
      am_hal_adc_power_control(g_adc_handle, AM_HAL_SYSCTRL_WAKE, false)) {
    error_reporter->Report("Error - ADC0 power on failed.");
  }

  // Set up the ADC configuration parameters. These settings are reasonable
  // for accurate measurements at a low sample rate.
  ADCConfig.eClock = AM_HAL_ADC_CLKSEL_HFRC_DIV2;
  ADCConfig.ePolarity = AM_HAL_ADC_TRIGPOL_RISING;
  ADCConfig.eTrigger = AM_HAL_ADC_TRIGSEL_SOFTWARE;
  ADCConfig.eReference =
      AM_HAL_ADC_REFSEL_INT_2P0;  // AM_HAL_ADC_REFSEL_INT_1P5;
  ADCConfig.eClockMode = AM_HAL_ADC_CLKMODE_LOW_LATENCY;
  ADCConfig.ePowerMode = AM_HAL_ADC_LPMODE0;
  ADCConfig.eRepeat = AM_HAL_ADC_REPEATING_SCAN;
  if (AM_HAL_STATUS_SUCCESS != am_hal_adc_configure(g_adc_handle, &ADCConfig)) {
    error_reporter->Report("Error - configuring ADC0 failed.");
  }

  // Set up an ADC slot (2)
  ADCSlotConfig.eMeasToAvg = AM_HAL_ADC_SLOT_AVG_1;
  ADCSlotConfig.ePrecisionMode = AM_HAL_ADC_SLOT_14BIT;
  ADCSlotConfig.eChannel = AM_HAL_ADC_SLOT_CHSEL_SE2;
  ADCSlotConfig.bWindowCompare = false;
  ADCSlotConfig.bEnabled = true;
  if (AM_HAL_STATUS_SUCCESS !=
      am_hal_adc_configure_slot(g_adc_handle, 2, &ADCSlotConfig)) {
    error_reporter->Report("Error - configuring ADC Slot 2 failed.");
  }

  // Set up an ADC slot (1)
  ADCSlotConfig.eMeasToAvg = AM_HAL_ADC_SLOT_AVG_1;
  ADCSlotConfig.ePrecisionMode = AM_HAL_ADC_SLOT_14BIT;
  ADCSlotConfig.eChannel = AM_HAL_ADC_SLOT_CHSEL_SE1;
  ADCSlotConfig.bWindowCompare = false;
  ADCSlotConfig.bEnabled = true;
  if (AM_HAL_STATUS_SUCCESS !=
      am_hal_adc_configure_slot(g_adc_handle, 1, &ADCSlotConfig)) {
    error_reporter->Report("Error - configuring ADC Slot 1 failed.");
  }

  // Configure the ADC to use DMA for the sample transfer.
  adc_start_dma(error_reporter);

  // For this example, the samples will be coming in slowly. This means we
  // can afford to wake up for every conversion.
  am_hal_adc_interrupt_enable(g_adc_handle,
                              AM_HAL_ADC_INT_DERR | AM_HAL_ADC_INT_DCMP);

  // Enable the ADC.
  if (AM_HAL_STATUS_SUCCESS != am_hal_adc_enable(g_adc_handle)) {
    error_reporter->Report("Error - enabling ADC0 failed.");
  }
}

// Initialize the ADC repetitive sample timer A3.
void init_timerA3_for_ADC() {
  // Start a timer to trigger the ADC periodically (1 second).
  am_hal_ctimer_config_single(3, AM_HAL_CTIMER_TIMERA,
                              AM_HAL_CTIMER_HFRC_12MHZ |
                                  AM_HAL_CTIMER_FN_REPEAT |
                                  AM_HAL_CTIMER_INT_ENABLE);

  am_hal_ctimer_int_enable(AM_HAL_CTIMER_INT_TIMERA3);

  // 750 = 12,000,000 (clock rate) / 16,000 (desired sample rate).
  am_hal_ctimer_period_set(3, AM_HAL_CTIMER_TIMERA, 750, 374);

  // Enable the timer A3 to trigger the ADC directly
  am_hal_ctimer_adc_trigger_enable();

  // Start the timer.
  am_hal_ctimer_start(3, AM_HAL_CTIMER_TIMERA);
}

// Make sure the CPU is running as fast as possible.
void enable_burst_mode(tflite::ErrorReporter* error_reporter) {
  am_hal_burst_avail_e eBurstModeAvailable;
  am_hal_burst_mode_e eBurstMode;

  // Check that the Burst Feature is available.
  if (AM_HAL_STATUS_SUCCESS ==
      am_hal_burst_mode_initialize(&eBurstModeAvailable)) {
    if (AM_HAL_BURST_AVAIL == eBurstModeAvailable) {
      error_reporter->Report("Apollo3 Burst Mode is Available\n");
    } else {
      error_reporter->Report("Apollo3 Burst Mode is Not Available\n");
    }
  } else {
    error_reporter->Report("Failed to Initialize for Burst Mode operation\n");
  }

  // Put the MCU into "Burst" mode.
  if (AM_HAL_STATUS_SUCCESS == am_hal_burst_mode_enable(&eBurstMode)) {
    if (AM_HAL_BURST_MODE == eBurstMode) {
      error_reporter->Report("Apollo3 operating in Burst Mode (96MHz)\n");
    }
  } else {
    error_reporter->Report("Failed to Enable Burst Mode operation\n");
  }
}

}  // namespace

// Interrupt handler for the ADC.
extern "C" void am_adc_isr(void) {
  uint32_t ui32IntMask;

  // Read the interrupt status.
  if (AM_HAL_STATUS_SUCCESS !=
      am_hal_adc_interrupt_status(g_adc_handle, &ui32IntMask, false)) {
    g_adc_dma_error_reporter->Report("Error reading ADC0 interrupt status.");
  }

  // Clear the ADC interrupt.
  if (AM_HAL_STATUS_SUCCESS !=
      am_hal_adc_interrupt_clear(g_adc_handle, ui32IntMask)) {
    g_adc_dma_error_reporter->Report("Error clearing ADC0 interrupt status.");
  }

  // If we got a DMA complete, set the flag.
  if (ui32IntMask & AM_HAL_ADC_INT_DCMP) {
    uint32_t* source_buffer;
    if (g_dma_destination_index == 0) {
      source_buffer = g_ui32ADCSampleBuffer0;
      g_dma_destination_index = 1;
    } else {
      source_buffer = g_ui32ADCSampleBuffer1;
      g_dma_destination_index = 0;
    }
    adc_start_dma(g_adc_dma_error_reporter);

    // For slot 1:
    uint32_t slotCount = 0;
    for (uint32_t indi = 0; indi < kAdcSampleBufferSize; indi++) {
      am_hal_adc_sample_t temp;

      temp.ui32Slot = AM_HAL_ADC_FIFO_SLOT(source_buffer[indi]);
      temp.ui32Sample = AM_HAL_ADC_FIFO_SAMPLE(source_buffer[indi]);

      if (temp.ui32Slot == 1) {
        g_audio_capture_buffer[g_audio_capture_buffer_start] = temp.ui32Sample;
        g_audio_capture_buffer_start =
            (g_audio_capture_buffer_start + 1) % kAudioCaptureBufferSize;
        slotCount++;
      }
    }

    g_total_samples_captured += slotCount;
    g_latest_audio_timestamp =
        (g_total_samples_captured / (kAudioSampleFrequency / 1000));
  }

  // If we got a DMA error, set the flag.
  if (ui32IntMask & AM_HAL_ADC_INT_DERR) {
    g_adc_dma_error = true;
  }
}

TfLiteStatus InitAudioRecording(tflite::ErrorReporter* error_reporter) {
  // Set the clock frequency.
  if (AM_HAL_STATUS_SUCCESS !=
      am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_SYSCLK_MAX, 0)) {
    error_reporter->Report("Error - configuring the system clock failed.");
    return kTfLiteError;
  }

  // Set the default cache configuration and enable it.
  if (AM_HAL_STATUS_SUCCESS !=
      am_hal_cachectrl_config(&am_hal_cachectrl_defaults)) {
    error_reporter->Report("Error - configuring the system cache failed.");
    return kTfLiteError;
  }
  if (AM_HAL_STATUS_SUCCESS != am_hal_cachectrl_enable()) {
    error_reporter->Report("Error - enabling the system cache failed.");
    return kTfLiteError;
  }

  // Ensure the CPU is running as fast as possible.
  enable_burst_mode(error_reporter);

  // Start the CTIMER A3 for timer-based ADC measurements.
  init_timerA3_for_ADC();

  // Enable interrupts.
  NVIC_EnableIRQ(ADC_IRQn);
  am_hal_interrupt_master_enable();

  // Edge Board Pin Definitions
  constexpr int kSfEdgePinMic0 = 11;
  const am_hal_gpio_pincfg_t g_sf_edge_pin_mic0 = {
      .uFuncSel = AM_HAL_PIN_11_ADCSE2,
  };
  constexpr int kSfEdgePinMic1 = 29;
  const am_hal_gpio_pincfg_t g_sf_edge_pin_mic1 = {
      .uFuncSel = AM_HAL_PIN_29_ADCSE1,
  };

  // Set pins to act as our ADC input
  am_hal_gpio_pinconfig(kSfEdgePinMic0, g_sf_edge_pin_mic0);
  am_hal_gpio_pinconfig(kSfEdgePinMic1, g_sf_edge_pin_mic1);

  // Configure the ADC
  adc_config0(error_reporter);

  // Trigger the ADC sampling for the first time manually.
  if (AM_HAL_STATUS_SUCCESS != am_hal_adc_sw_trigger(g_adc_handle)) {
    error_reporter->Report("Error - triggering the ADC0 failed.");
    return kTfLiteError;
  }

  // Enable the LED outputs.
  am_hal_gpio_pinconfig(AM_BSP_GPIO_LED_RED, g_AM_HAL_GPIO_OUTPUT_12);
  am_hal_gpio_pinconfig(AM_BSP_GPIO_LED_YELLOW, g_AM_HAL_GPIO_OUTPUT_12);

  am_hal_gpio_output_set(AM_BSP_GPIO_LED_RED);
  am_hal_gpio_output_set(AM_BSP_GPIO_LED_YELLOW);

  return kTfLiteOk;
}

TfLiteStatus GetAudioSamples(tflite::ErrorReporter* error_reporter,
                             int start_ms, int duration_ms,
                             int* audio_samples_size, int16_t** audio_samples) {
  if (!g_is_audio_initialized) {
    TfLiteStatus init_status = InitAudioRecording(error_reporter);
    if (init_status != kTfLiteOk) {
      return init_status;
    }
    g_is_audio_initialized = true;
  }

  // This is the 'zero' level of the microphone when no audio is present, and
  // should be recalibrated if the hardware configuration ever changes. It was
  // generated experimentally by averaging some samples captured on a board.
  const int16_t kAdcSampleDC = 6003;

  // Temporary gain emulation to deal with too-quiet audio on prototype boards.
  const int16_t kAdcSampleGain = 10;

  // This should only be called when the main thread notices that the latest
  // audio sample data timestamp has changed, so that there's new data in the
  // capture ring buffer. The ring buffer will eventually wrap around and
  // overwrite the data, but the assumption is that the main thread is checking
  // often enough and the buffer is large enough that this call will be made
  // before that happens.
  const int start_offset = start_ms * (kAudioSampleFrequency / 1000);
  const int duration_sample_count =
      duration_ms * (kAudioSampleFrequency / 1000);
  for (int i = 0; i < duration_sample_count; ++i) {
    const int capture_index = (start_offset + i) % kAudioCaptureBufferSize;
    const int32_t capture_value = g_audio_capture_buffer[capture_index];
    int32_t output_value = capture_value - kAdcSampleDC;
    output_value *= kAdcSampleGain;
    if (output_value < std::numeric_limits<int16_t>::min()) {
      output_value = std::numeric_limits<int16_t>::min();
    }
    if (output_value > std::numeric_limits<int16_t>::max()) {
      output_value = std::numeric_limits<int16_t>::max();
    }
    g_audio_output_buffer[i] = output_value;
  }

  *audio_samples_size = kMaxAudioSampleSize;
  *audio_samples = g_audio_output_buffer;
  return kTfLiteOk;
}

int32_t LatestAudioTimestamp() { return g_latest_audio_timestamp; }

command_responder.cc

C/C++
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/command_responder.h"

#include "am_bsp.h"  // NOLINT

// This implementation will light up the LEDs on the board in response to
// different commands.
void RespondToCommand(tflite::ErrorReporter* error_reporter,
                      int32_t current_time, const char* found_command,
                      uint8_t score, bool is_new_command) {
  static bool is_initialized = false;
  if (!is_initialized) {
    // Setup LED's as outputs
    am_hal_gpio_pinconfig(AM_BSP_GPIO_LED_RED, g_AM_HAL_GPIO_OUTPUT_12);
    am_hal_gpio_pinconfig(AM_BSP_GPIO_LED_BLUE, g_AM_HAL_GPIO_OUTPUT_12);
    am_hal_gpio_pinconfig(AM_BSP_GPIO_LED_GREEN, g_AM_HAL_GPIO_OUTPUT_12);
    am_hal_gpio_pinconfig(AM_BSP_GPIO_LED_YELLOW, g_AM_HAL_GPIO_OUTPUT_12);
    is_initialized = true;
  }
  static int count = 0;

  // Toggle the blue LED every time an inference is performed.
  ++count;
  if (count & 1) {
    am_hal_gpio_output_set(AM_BSP_GPIO_LED_BLUE);
  } else {
    am_hal_gpio_output_clear(AM_BSP_GPIO_LED_BLUE);
  }

  // Turn on the yellow LED if 'yes' was heard.
  am_hal_gpio_output_clear(AM_BSP_GPIO_LED_RED);
  am_hal_gpio_output_clear(AM_BSP_GPIO_LED_YELLOW);
  am_hal_gpio_output_clear(AM_BSP_GPIO_LED_GREEN);
  if (is_new_command) {
    error_reporter->Report("Heard %s (%d) @%dms", found_command, score,
                           current_time);
    if (found_command[0] == 'y') {
      am_hal_gpio_output_set(AM_BSP_GPIO_LED_YELLOW);
    }
    if (found_command[0] == 'n') {
      am_hal_gpio_output_set(AM_BSP_GPIO_LED_RED);
    }
    if (found_command[0] == 'u') {
      am_hal_gpio_output_set(AM_BSP_GPIO_LED_GREEN);
    }
  }
}

audio_provider_mock_test.cc

C/C++
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/audio_provider.h"

#include <limits>

#include "tensorflow/lite/c/c_api_internal.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h"
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"

#include "tensorflow/lite/experimental/micro/examples/micro_speech/no_1000ms_sample_data.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/yes_1000ms_sample_data.h"

TF_LITE_MICRO_TESTS_BEGIN

TF_LITE_MICRO_TEST(TestAudioProviderMock) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  int audio_samples_size = 0;
  int16_t* audio_samples = nullptr;
  TfLiteStatus get_status =
      GetAudioSamples(error_reporter, 0, kFeatureSliceDurationMs,
                      &audio_samples_size, &audio_samples);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, get_status);
  TF_LITE_MICRO_EXPECT_LE(audio_samples_size, kMaxAudioSampleSize);
  TF_LITE_MICRO_EXPECT_NE(audio_samples, nullptr);
  for (int i = 0; i < audio_samples_size; ++i) {
    TF_LITE_MICRO_EXPECT_EQ(g_yes_1000ms_sample_data[i], audio_samples[i]);
  }

  get_status = GetAudioSamples(error_reporter, 500, kFeatureSliceDurationMs,
                               &audio_samples_size, &audio_samples);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, get_status);
  TF_LITE_MICRO_EXPECT_LE(audio_samples_size, kMaxAudioSampleSize);
  TF_LITE_MICRO_EXPECT_NE(audio_samples, nullptr);
  for (int i = 0; i < audio_samples_size; ++i) {
    TF_LITE_MICRO_EXPECT_EQ(g_yes_1000ms_sample_data[i + 8000],
                            audio_samples[i]);
  }

  get_status = GetAudioSamples(error_reporter, 1500, kFeatureSliceDurationMs,
                               &audio_samples_size, &audio_samples);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, get_status);
  TF_LITE_MICRO_EXPECT_LE(audio_samples_size, kMaxAudioSampleSize);
  TF_LITE_MICRO_EXPECT_NE(audio_samples, nullptr);
  for (int i = 0; i < audio_samples_size; ++i) {
    TF_LITE_MICRO_EXPECT_EQ(0, audio_samples[i]);
  }

  get_status = GetAudioSamples(error_reporter, 12250, kFeatureSliceDurationMs,
                               &audio_samples_size, &audio_samples);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, get_status);
  TF_LITE_MICRO_EXPECT_LE(audio_samples_size, kMaxAudioSampleSize);
  TF_LITE_MICRO_EXPECT_NE(audio_samples, nullptr);
  for (int i = 0; i < audio_samples_size; ++i) {
    TF_LITE_MICRO_EXPECT_EQ(g_no_1000ms_sample_data[i + 4000],
                            audio_samples[i]);
  }
}

TF_LITE_MICRO_TESTS_END

audio_provider_mock.cc

C/C++
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/audio_provider.h"

#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"

#include "tensorflow/lite/experimental/micro/examples/micro_speech/no_1000ms_sample_data.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/yes_1000ms_sample_data.h"

namespace {
int16_t g_dummy_audio_data[kMaxAudioSampleSize];
int32_t g_latest_audio_timestamp = 0;
}  // namespace

TfLiteStatus GetAudioSamples(tflite::ErrorReporter* error_reporter,
                             int start_ms, int duration_ms,
                             int* audio_samples_size, int16_t** audio_samples) {
  const int yes_start = (0 * kAudioSampleFrequency) / 1000;
  const int yes_end = (1000 * kAudioSampleFrequency) / 1000;
  const int no_start = (4000 * kAudioSampleFrequency) / 1000;
  const int no_end = (5000 * kAudioSampleFrequency) / 1000;
  const int wraparound = (8000 * kAudioSampleFrequency) / 1000;
  const int start_sample = (start_ms * kAudioSampleFrequency) / 1000;
  for (int i = 0; i < kMaxAudioSampleSize; ++i) {
    const int sample_index = (start_sample + i) % wraparound;
    int16_t sample;
    if ((sample_index >= yes_start) && (sample_index < yes_end)) {
      sample = g_yes_1000ms_sample_data[sample_index - yes_start];
    } else if ((sample_index >= no_start) && (sample_index < no_end)) {
      sample = g_no_1000ms_sample_data[sample_index - no_start];
    } else {
      sample = 0;
    }
    g_dummy_audio_data[i] = sample;
  }
  *audio_samples_size = kMaxAudioSampleSize;
  *audio_samples = g_dummy_audio_data;
  return kTfLiteOk;
}

int32_t LatestAudioTimestamp() {
  g_latest_audio_timestamp += 100;
  return g_latest_audio_timestamp;
}

audio_provider_test.cc

C/C++
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/audio_provider.h"

#include <limits>

#include "tensorflow/lite/c/c_api_internal.h"
#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h"
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"

TF_LITE_MICRO_TESTS_BEGIN

TF_LITE_MICRO_TEST(TestAudioProvider) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  int audio_samples_size = 0;
  int16_t* audio_samples = nullptr;
  TfLiteStatus get_status =
      GetAudioSamples(error_reporter, 0, kFeatureSliceDurationMs,
                      &audio_samples_size, &audio_samples);
  TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, get_status);
  TF_LITE_MICRO_EXPECT_LE(audio_samples_size, kMaxAudioSampleSize);
  TF_LITE_MICRO_EXPECT_NE(audio_samples, nullptr);

  // Make sure we can read all of the returned memory locations.
  int total = 0;
  for (int i = 0; i < audio_samples_size; ++i) {
    total += audio_samples[i];
  }
}

TF_LITE_MICRO_TEST(TestTimer) {
  // Make sure that the technically-undefined overflow behavior we rely on below
  // works on this platform. It's still not guaranteed, but at least this is a
  // sanity check.  Turn off when running with ASan, as it will complain about
  // the following undefined behavior.
#ifndef ADDRESS_SANITIZER
  int32_t overflow_value = std::numeric_limits<int32_t>::max();
  overflow_value += 1;
  TF_LITE_MICRO_EXPECT_EQ(std::numeric_limits<int32_t>::min(), overflow_value);
#endif

  const int32_t first_time = LatestAudioTimestamp();
  const int32_t second_time = LatestAudioTimestamp();

  // It's possible that the timer may have wrapped around from +BIG_NUM to
  // -BIG_NUM between the first and second calls, since we're storing
  // milliseconds in a 32-bit integer. It's not reasonable that the call itself
  // would have taken more than 2^31 milliseconds though, so look at the
  // difference and rely on integer overflow to ensure it's accurate.
  const int32_t time_delta = (second_time - first_time);
  TF_LITE_MICRO_EXPECT_LE(0, time_delta);
}

TF_LITE_MICRO_TESTS_END

audio_provider.cc

C/C++
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/audio_provider.h"

#include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"

namespace {
int16_t g_dummy_audio_data[kMaxAudioSampleSize];
int32_t g_latest_audio_timestamp = 0;
}  // namespace

TfLiteStatus GetAudioSamples(tflite::ErrorReporter* error_reporter,
                             int start_ms, int duration_ms,
                             int* audio_samples_size, int16_t** audio_samples) {
  for (int i = 0; i < kMaxAudioSampleSize; ++i) {
    g_dummy_audio_data[i] = 0;
  }
  *audio_samples_size = kMaxAudioSampleSize;
  *audio_samples = g_dummy_audio_data;
  return kTfLiteOk;
}

int32_t LatestAudioTimestamp() {
  g_latest_audio_timestamp += 100;
  return g_latest_audio_timestamp;
}

audio_provider.h

C Header File
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_AUDIO_PROVIDER_H_
#define TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_AUDIO_PROVIDER_H_

#include "tensorflow/lite/c/c_api_internal.h"
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h"

// This is an abstraction around an audio source like a microphone, and is
// expected to return 16-bit PCM sample data for a given point in time. The
// sample data itself should be used as quickly as possible by the caller, since
// to allow memory optimizations there are no guarantees that the samples won't
// be overwritten by new data in the future. In practice, implementations should
// ensure that there's a reasonable time allowed for clients to access the data
// before any reuse.
// The reference implementation can have no platform-specific dependencies, so
// it just returns an array filled with zeros. For real applications, you should
// ensure there's a specialized implementation that accesses hardware APIs.
TfLiteStatus GetAudioSamples(tflite::ErrorReporter* error_reporter,
                             int start_ms, int duration_ms,
                             int* audio_samples_size, int16_t** audio_samples);

// Returns the time that audio data was last captured in milliseconds. There's
// no contract about what time zero represents, the accuracy, or the granularity
// of the result. Subsequent calls will generally not return a lower value, but
// even that's not guaranteed if there's an overflow wraparound.
// The reference implementation of this function just returns a constantly
// incrementing value for each call, since it would need a non-portable platform
// call to access time information. For real applications, you'll need to write
// your own platform-specific implementation.
int32_t LatestAudioTimestamp();

#endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_AUDIO_PROVIDER_H_

command_responder_test.cc

C/C++
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/command_responder.h"

#include "tensorflow/lite/experimental/micro/testing/micro_test.h"
#include "tensorflow/lite/experimental/micro/testing/test_utils.h"

TF_LITE_MICRO_TESTS_BEGIN

TF_LITE_MICRO_TEST(TestCallability) {
  tflite::MicroErrorReporter micro_error_reporter;
  tflite::ErrorReporter* error_reporter = &micro_error_reporter;

  // This will have external side-effects (like printing to the debug console
  // or lighting an LED) that are hard to observe, so the most we can do is
  // make sure the call doesn't crash.
  RespondToCommand(error_reporter, 0, "foo", 0, true);
}

TF_LITE_MICRO_TESTS_END

command_responder.cc

C/C++
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/experimental/micro/examples/micro_speech/command_responder.h"

// The default implementation writes out the name of the recognized command
// to the error console. Real applications will want to take some custom
// action instead, and should implement their own versions of this function.
void RespondToCommand(tflite::ErrorReporter* error_reporter,
                      int32_t current_time, const char* found_command,
                      uint8_t score, bool is_new_command) {
  if (is_new_command) {
    error_reporter->Report("Heard %s (%d) @%dms", found_command, score,
                           current_time);
  }
}

Credits

Hongrui Wei

Hongrui Wei

1 project • 0 followers
Siri Manjunath

Siri Manjunath

1 project • 0 followers
Kexi Dang

Kexi Dang

1 project • 0 followers
COMP554
Qiao He

Qiao He

1 project • 0 followers

Comments