|
| 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 | +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. |
| 17 | +
|
| 18 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 19 | +you may not use this file except in compliance with the License. |
| 20 | +You may obtain a copy of the License at |
| 21 | +
|
| 22 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | +
|
| 24 | +Unless required by applicable law or agreed to in writing, software |
| 25 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 26 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 | +See the License for the specific language governing permissions and |
| 28 | +limitations under the License. |
| 29 | +==============================================================================*/ |
| 30 | + |
| 31 | +#if defined(ARDUINO) && !defined(ARDUINO_ARDUINO_NANO33BLE) |
| 32 | +#define ARDUINO_EXCLUDE_CODE |
| 33 | +#endif // defined(ARDUINO) && !defined(ARDUINO_ARDUINO_NANO33BLE) |
| 34 | + |
| 35 | +#ifndef ARDUINO_EXCLUDE_CODE |
| 36 | + |
| 37 | +#include "audio_provider.h" |
| 38 | + |
| 39 | +#include "PDM.h" |
| 40 | +#include "micro_features_micro_model_settings.h" |
| 41 | + |
| 42 | +namespace { |
| 43 | +bool g_is_audio_initialized = false; |
| 44 | +// An internal buffer able to fit 16x our sample size |
| 45 | +constexpr int kAudioCaptureBufferSize = DEFAULT_PDM_BUFFER_SIZE * 16; |
| 46 | +int16_t g_audio_capture_buffer[kAudioCaptureBufferSize]; |
| 47 | +// A buffer that holds our output |
| 48 | +int16_t g_audio_output_buffer[kMaxAudioSampleSize]; |
| 49 | +// Mark as volatile so we can check in a while loop to see if |
| 50 | +// any samples have arrived yet. |
| 51 | +volatile int32_t g_latest_audio_timestamp = 0; |
| 52 | +} // namespace |
| 53 | + |
| 54 | +void CaptureSamples() { |
| 55 | + // This is how many bytes of new data we have each time this is called |
| 56 | + const int number_of_samples = DEFAULT_PDM_BUFFER_SIZE/2; |
| 57 | + // Calculate what timestamp the last audio sample represents |
| 58 | + const int32_t time_in_ms = |
| 59 | + g_latest_audio_timestamp + |
| 60 | + (number_of_samples / (kAudioSampleFrequency / 1000)); |
| 61 | + // Determine the index, in the history of all samples, of the last sample |
| 62 | + const int32_t start_sample_offset = |
| 63 | + g_latest_audio_timestamp * (kAudioSampleFrequency / 1000); |
| 64 | + // Determine the index of this sample in our ring buffer |
| 65 | + const int capture_index = start_sample_offset % kAudioCaptureBufferSize; |
| 66 | + // Read the data to the correct place in our buffer |
| 67 | + PDM.read(g_audio_capture_buffer + capture_index, DEFAULT_PDM_BUFFER_SIZE); |
| 68 | + // This is how we let the outside world know that new audio data has arrived. |
| 69 | + g_latest_audio_timestamp = time_in_ms; |
| 70 | +} |
| 71 | + |
| 72 | +TfLiteStatus InitAudioRecording(tflite::ErrorReporter* error_reporter) { |
| 73 | + // Hook up the callback that will be called with each sample |
| 74 | + PDM.onReceive(CaptureSamples); |
| 75 | + // Start listening for audio: MONO @ 16KHz with gain at 20 |
| 76 | + PDM.begin(1, kAudioSampleFrequency); |
| 77 | + PDM.setGain(20); |
| 78 | + // Block until we have our first audio sample |
| 79 | + while (!g_latest_audio_timestamp) { |
| 80 | + } |
| 81 | + |
| 82 | + return kTfLiteOk; |
| 83 | +} |
| 84 | + |
| 85 | +TfLiteStatus GetAudioSamples(tflite::ErrorReporter* error_reporter, |
| 86 | + int start_ms, int duration_ms, |
| 87 | + int* audio_samples_size, int16_t** audio_samples) { |
| 88 | + // Set everything up to start receiving audio |
| 89 | + if (!g_is_audio_initialized) { |
| 90 | + TfLiteStatus init_status = InitAudioRecording(error_reporter); |
| 91 | + if (init_status != kTfLiteOk) { |
| 92 | + return init_status; |
| 93 | + } |
| 94 | + g_is_audio_initialized = true; |
| 95 | + } |
| 96 | + // This next part should only be called when the main thread notices that the |
| 97 | + // latest audio sample data timestamp has changed, so that there's new data |
| 98 | + // in the capture ring buffer. The ring buffer will eventually wrap around and |
| 99 | + // overwrite the data, but the assumption is that the main thread is checking |
| 100 | + // often enough and the buffer is large enough that this call will be made |
| 101 | + // before that happens. |
| 102 | + |
| 103 | + // Determine the index, in the history of all samples, of the first |
| 104 | + // sample we want |
| 105 | + const int start_offset = start_ms * (kAudioSampleFrequency / 1000); |
| 106 | + // Determine how many samples we want in total |
| 107 | + const int duration_sample_count = |
| 108 | + duration_ms * (kAudioSampleFrequency / 1000); |
| 109 | + for (int i = 0; i < duration_sample_count; ++i) { |
| 110 | + // For each sample, transform its index in the history of all samples into |
| 111 | + // its index in g_audio_capture_buffer |
| 112 | + const int capture_index = (start_offset + i) % kAudioCaptureBufferSize; |
| 113 | + // Write the sample to the output buffer |
| 114 | + g_audio_output_buffer[i] = g_audio_capture_buffer[capture_index]; |
| 115 | + } |
| 116 | + |
| 117 | + // Set pointers to provide access to the audio |
| 118 | + *audio_samples_size = kMaxAudioSampleSize; |
| 119 | + *audio_samples = g_audio_output_buffer; |
| 120 | + |
| 121 | + return kTfLiteOk; |
| 122 | +} |
| 123 | + |
| 124 | +int32_t LatestAudioTimestamp() { return g_latest_audio_timestamp; } |
| 125 | + |
| 126 | +#endif // ARDUINO_EXCLUDE_CODE |
0 commit comments