1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/lite/experimental/micro/examples/micro_speech/audio_provider.h"
17 
18 #include "tensorflow/lite/experimental/micro/examples/micro_speech/micro_features/micro_model_settings.h"
19 
20 #include "tensorflow/lite/experimental/micro/examples/micro_speech/no_1000ms_sample_data.h"
21 #include "tensorflow/lite/experimental/micro/examples/micro_speech/yes_1000ms_sample_data.h"
22 
23 namespace {
24 int16_t g_dummy_audio_data[kMaxAudioSampleSize];
25 int32_t g_latest_audio_timestamp = 0;
26 }  // namespace
27 
GetAudioSamples(tflite::ErrorReporter * error_reporter,int start_ms,int duration_ms,int * audio_samples_size,int16_t ** audio_samples)28 TfLiteStatus GetAudioSamples(tflite::ErrorReporter* error_reporter,
29                              int start_ms, int duration_ms,
30                              int* audio_samples_size, int16_t** audio_samples) {
31   const int yes_start = (0 * kAudioSampleFrequency) / 1000;
32   const int yes_end = (1000 * kAudioSampleFrequency) / 1000;
33   const int no_start = (4000 * kAudioSampleFrequency) / 1000;
34   const int no_end = (5000 * kAudioSampleFrequency) / 1000;
35   const int wraparound = (8000 * kAudioSampleFrequency) / 1000;
36   const int start_sample = (start_ms * kAudioSampleFrequency) / 1000;
37   for (int i = 0; i < kMaxAudioSampleSize; ++i) {
38     const int sample_index = (start_sample + i) % wraparound;
39     int16_t sample;
40     if ((sample_index >= yes_start) && (sample_index < yes_end)) {
41       sample = g_yes_1000ms_sample_data[sample_index - yes_start];
42     } else if ((sample_index >= no_start) && (sample_index < no_end)) {
43       sample = g_no_1000ms_sample_data[sample_index - no_start];
44     } else {
45       sample = 0;
46     }
47     g_dummy_audio_data[i] = sample;
48   }
49   *audio_samples_size = kMaxAudioSampleSize;
50   *audio_samples = g_dummy_audio_data;
51   return kTfLiteOk;
52 }
53 
LatestAudioTimestamp()54 int32_t LatestAudioTimestamp() {
55   g_latest_audio_timestamp += 100;
56   return g_latest_audio_timestamp;
57 }
58