Skip to Content
タイニーエムエル
book

タイニーエムエル

by Pete Warden, Daniel Situnayake
March 2025
Intermediate to advanced
504 pages
7h 53m
Japanese
O'Reilly Media, Inc.
Content preview from タイニーエムエル

付録B. Arduinoでオーディオをキャプチャする

以下の文章では、第7章のwake-wordアプリケーションの音声キャプチャ・コードを説明する。機械学習とは直接関係ないので、付録として提供する。

Arduino Nano 33 BLE Senseには、オンボードのマイクが搭載されている。マイクからオーディオ・データを受信するには、新しいオーディオ・データのチャンクが準備できたときに呼び出されるコールバック関数を登録すればいい。

そのたびに、新しいデータの塊を、データの予備をストアするバッファに書き込む。オーディオデータは多くのメモリを消費するため、バッファには一定量のデータしか保存できない。バッファがいっぱいになると、このデータは上書きされる。

プログラミングが推論を実行する準備ができたらいつでも、このバッファから最後の1秒分のデータを読み取ることができる。われわれがデータにアクセスするよりも早く新しいデータが入り続ける限り、バッファには常に、前処理をしてモデルに送り込むのに十分な新しいデータがあることになる。

前処理と推論の各サイクルは複雑で、完了するまでに時間がかかる。このため、Arduinoでは推論を1秒間に数回しか実行できない。つまり、バッファがいっぱいになりやすいのだ。

第7章で見たように、audio_provider.hはこれら2つの関数を実装している:

  • GetAudioSamples() 生オーディオデータのチャンクへのポインタを提供する。

  • LatestAudioTimestamp() 直近にキャプチャされたオーディオのタイムスタンプを返す。

Arduino用にこれらを実装したコードはarduino/audio_provider.ccにある。

最初の部分では、いくつかの依存関係を取り込む。PDM.hライブラリーは、マイクからデータを取得するために使用するAPIを定義している。micro_model_settings.hファイルには、モデルのデータ要件に関連する定数が含まれており、正しい形式で音声を提供するのに役立つ:

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

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

次のコードでは、いくつかの重要な変数をセットアップする:

namespace {
bool g_is_audio_initialized = false;
// An internal buffer able to fit 16x our sample size
constexpr int kAudioCaptureBufferSize = DEFAULT_PDM_BUFFER_SIZE * 16;
int16_t g_audio_capture_buffer[kAudioCaptureBufferSize];
// A buffer that holds our output
int16_t g_audio_output_buffer[kMaxAudioSampleSize];
// Mark as volatile so we can check in a while loop to see if
// any samples have arrived ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

認定倫理的ハッカー(CEH)勉強ガイド

認定倫理的ハッカー(CEH)勉強ガイド

Matt Walker
実践者向けの説明可能なAI

実践者向けの説明可能なAI

Michael Munn, David Pitman
ソフトウェア工学の基礎

ソフトウェア工学の基礎

Nathaniel Schutta, Dan Vega
エッジのAI

エッジのAI

Daniel Situnayake, Jenny Plunkett

Publisher Resources

ISBN: 9798341627000