LoginSignup
10
5

More than 5 years have passed since last update.

ESP32でWAVファイルをFlashメモリから再生する

Posted at

ESP32でWAVファイルをFlashメモリから再生する

ESP32にはDACが(8bitですが)搭載されています。
ので、そこからWAVファイルの音源をなんとかして再生したい、と思った際のメモです。

ざっくりした手順

  1. WAVファイルを Audacity などで、unsigned 8-bit の RAW (header-less) にする
  2. RAWファイルのバイナリデータをテキストでヘッダーファイルにし、Flashに配置する
  3. サンプリングレートを考慮したタイミングでDACから再生する

以下、詳細手順メモです。

1. WAVファイルを unsigned 8-bit の RAW (header-less) にする

AudacityでWAVファイルを開く

Export as WAV

  • Other uncompressed files
  • RAW (header-less)
  • Unsigned 8-bit PCM

2. RAWファイルをテキストでヘッダーファイルにし、Flashに配置する

HxDなどのバイナリエディタで.rawファイルを開く

配列にできるように体裁を整える


サウンドの配列をつくる

const PROGMEM を追加することでFlashに配置される。
また、配列のサイズをRAWファイルのサイズに合わせること。

raw_audio.h
 const PROGMEM unsigned char raw_audio[/* size of raw file */] =
 {
     // copy & paste raw data here
 };

3. サンプリングレートを考慮したタイミングでDACから再生する

play_wav_raw.ino
 const uint32_t SAMPLE_DELAY_US = (uint32_t)(1000000. / 44100.);

 void play(const unsigned char* ptr, uint32_t size)
 {
     uint64_t count = 0;
     while (count < size)
     {
         dacWrite(25, ptr[count]);
         ets_delay_us(SAMPLE_DELAY_US); // 44.1kHzになるよう待つ
         ++count;
     }
 }

 void setup()
 {
 }

 void loop()
 {
     play(audio_raw_array, sizeof(audio_raw_array));
     delay(2000);
 }

参考にさせていただいた記事

http://kghr.blog.fc2.com/blog-entry-126.html
https://github.com/kghrlabo/esp32_simple_wav_player

10
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
5