少し前にSteamのセールで買った『American Truck Simulator』(アメリカントラックシミュレーター)を時間が出来た時に起動して遊んでいます。ゲームの紹介ではないので詳細は省きますが、アメリカ大陸をでっかいトラックで荷物を運んで報酬を得るというはまらない人には全くピンとこないであろう内容です(笑)。
このゲーム、トラックを運転している時にBGMとしてWEBラジオを流したままにすることが出来るのですが、その時いつも流すのが** Houston Blues Radio **(ヒューストンブルースラジオ)というブルース専門チャンネルです。
ゲーム以外のときでも聞きたいなと思ったので、買ったままあまり触らず置いてあったM5Stack BASICを使ってWEBラジオを鳴らしてみることにしました。(スマホとかパソコンとかでも聞けるじゃない?と言われればそうなんですが・・・)
M5Stack内蔵のスピーカーで鳴らしてみる
今回は取り敢えず鳴ればいいということでM5Stack内蔵スピーカーで鳴らしてみることにします。
ライブラリの追加
まずはライブラリの追加、ESP8266Audioというライブラリーです。
ArduinoIDEで開発していくので上のgithubからダウンロードしたライブラリを「スケッチ」→「ライブラリをインクルード」→「.zip形式のライブラリをインストール」から追加します。
サンプルスケッチの変更
ライブラリが追加されるとサンプルスケッチがインストールされますので今回はこれを変更していきます。
ArduinoIDEから「ファイル」→「スケッチ例」→「ESP8266 Audio」→「StreamMP3FromHTTP」を選択すると新しいウィンドウが開いて以下のコードが読み込まれます。
#include <Arduino.h> #if defined(ARDUINO_ARCH_RP2040) void setup() {} void loop() {} #else #if defined(ESP32) #include <WiFi.h> #else #include <ESP8266WiFi.h> #endif #include "AudioFileSourceICYStream.h" #include "AudioFileSourceBuffer.h" #include "AudioGeneratorMP3.h" #include "AudioOutputI2SNoDAC.h" // To run, set your ESP8266 build to 160MHz, update the SSID info, and upload. // Enter your WiFi setup here: #ifndef STASSID #define STASSID "your-ssid" #define STAPSK "your-password" #endif const char* ssid = STASSID; const char* password = STAPSK; // Randomly picked URL const char *URL="http://kvbstreams.dyndns.org:8000/wkvi-am"; AudioGeneratorMP3 *mp3; AudioFileSourceICYStream *file; AudioFileSourceBuffer *buff; AudioOutputI2SNoDAC *out; // Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc. void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string) { const char *ptr = reinterpret_cast<const char *>(cbData); (void) isUnicode; // Punt this ball for now // Note that the type and string may be in PROGMEM, so copy them to RAM for printf char s1[32], s2[64]; strncpy_P(s1, type, sizeof(s1)); s1[sizeof(s1)-1]=0; strncpy_P(s2, string, sizeof(s2)); s2[sizeof(s2)-1]=0; Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, s1, s2); Serial.flush(); } // Called when there's a warning or error (like a buffer underflow or decode hiccup) void StatusCallback(void *cbData, int code, const char *string) { const char *ptr = reinterpret_cast<const char *>(cbData); // Note that the string may be in PROGMEM, so copy it to RAM for printf char s1[64]; strncpy_P(s1, string, sizeof(s1)); s1[sizeof(s1)-1]=0; Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1); Serial.flush(); } void setup() { Serial.begin(115200); delay(1000); Serial.println("Connecting to WiFi"); WiFi.disconnect(); WiFi.softAPdisconnect(true); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); // Try forever while (WiFi.status() != WL_CONNECTED) { Serial.println("...Connecting to WiFi"); delay(1000); } Serial.println("Connected"); audioLogger = &Serial; file = new AudioFileSourceICYStream(URL); file->RegisterMetadataCB(MDCallback, (void*)"ICY"); buff = new AudioFileSourceBuffer(file, 2048); buff->RegisterStatusCB(StatusCallback, (void*)"buffer"); out = new AudioOutputI2SNoDAC(); mp3 = new AudioGeneratorMP3(); mp3->RegisterStatusCB(StatusCallback, (void*)"mp3"); mp3->begin(buff, out); } void loop() { static int lastms = 0; if (mp3->isRunning()) { if (millis()-lastms > 1000) { lastms = millis(); Serial.printf("Running for %d ms...\n", lastms); Serial.flush(); } if (!mp3->loop()) mp3->stop(); } else { Serial.printf("MP3 done\n"); delay(1000); } } #endif
取り敢えず最小限の変更で鳴るところまでいければということで以下の内容を書き換え
- 1行目:#include <Arduino.h> → #include <M5Stack.h>
- 26行目:#define STASSID "your-ssid" 自分のSSIDに書き換え
- 27行目:#define STAPSK "your-password" 自分のPASSWORDに書き換え
- 30行目:const char *URL="http://kvbstreams.dyndns.org:8000/wkvi-am"; 聞きたい局のURLに書き換え ※https://ais-edge89-dal02.cdnstream.com/b76353_128mp3
これだけで一旦書き込み、鳴ると思ったんですがノイズ音が出るだけで鳴らず・・・
追加で以下の内容を変更して鳴るようになりました
- 16行目:#include "AudioOutputI2SNoDAC.h" → #include "AudioOutputI2S.h"
- 35行目:AudioOutputI2SNoDAC *out; → AudioOutputI2S *out;
- 88行目:out = new AudioOutputI2SNoDAC(); → out = new AudioOutputI2S(0, true);
AudioOutputI2SNoDACはAudioOutputI2Sのサブクラスになっているのでインスタンスを変えるだけで行けるようです。
取り敢えず鳴るにはなりましたが内蔵スピーカーでは推して知るべし、今後は外部デコーダを使うことを検討するとします。
*1:※Houston Blues Radioはブラウザではポップアップウィンドウ内にプレイヤーが表示される様になっていたためポップアップウィンドウ内のソースを表示してURLを取得