Raspberry Pi Pico W で Bluetooth Audio Receiver (2/2)

前回はSample Sourceをビルドしただけであっさり音が出たのですが、PWMでの音なのでもう少しDACにお金をかけようと、I2Sフォーマットを出力して、DACに渡すようにソフトウェアを書き換えました

参考にするのはArudino-IDEという開発環境の Arduino-Pico というものでSample Source(スケッチ)の
スケッチ とは、Arduino(アルデュイーノ) の仕組みの用語です。小学生のプログラミングなどでも使われていて、仕組みを理解すると、比較的簡単にプログラミングできます
前回の BluetoothAudio > A2DPSink というソースに、I2S > SimpleTone の処理を追加/編集して PWM に出力していたものをI2Sモジュールに出力するように変更します

DACはGY-PCM5201というDAC基板で、中華製で安いのを買ったらどうも模倣品らしくて基板の裏のシルクで LRCK のはずのところが RCK になっていました (汗)
ICのシルクは正しいそうですがノイズが多いです
Appendix
1. 回路図
2. 写真 (ミニステレオでヘッドホンで聞ける)
3. スマホで Bluetooth Audio の接続 名前を PicoW Rx にしてみました
4. 変更した差分

回路図
diff --git a/A2DPSink.ino b/A2DPSink_00a.ino
index 32dc30f..3ab3582 100755
--- a/A2DPSink.ino
+++ b/A2DPSink_00a.ino
@@ -3,10 +3,22 @@
 // Hook up a phono plug to GP0 and GP1 (and GND of course...the 1st 3 pins on the PCB)
 // Connect wired earbuds up and connect over BT from your phone and play some music.
 
+
 #include <BluetoothAudio.h>
-#include <PWMAudio.h>
+#include <I2S.h>
+#include <Time.h>
+
+I2S i2s(OUTPUT); // audio Consumer
+// GPIO pin numbers 26-28 (31,32,34pin)
+#define pBCLK 26
+#define pWS (pBCLK+1)
+#define pDOUT 28
+#define pMCLK 19  // optional MCLK pin
+const int sampleRate = 48000; // minimum for UDA1334A
+
+#define MCLK_MUL  256     // depends on audio hardware. Suits common hardware.
+#define PIN_XSMT (16)
 
-PWMAudio pwm;
 A2DPSink a2dp;
 
 volatile A2DPSink::PlaybackStatus status = A2DPSink::STOPPED;
@@ -31,13 +43,26 @@ void playbackCB(void *param, A2DPSink::PlaybackStatus state) {
 }
 
 void setup() {
+  pinMode(LED_BUILTIN, OUTPUT);
+  digitalWrite(LED_BUILTIN, 1);
+  pinMode(PIN_XSMT, OUTPUT);
+//  digitalWrite(PIN_XSMT, HIGH); 
+
   Serial.begin(115200);
   delay(3000);
   Serial.printf("Starting, connect to the PicoW and start playing music\n");
   Serial.printf("Use BOOTSEL to pause/resume playback\n");
-  a2dp.setName("PicoW Boom 00:00:00:00:00:00");
-  a2dp.setConsumer(new BluetoothAudioConsumerPWM(pwm));
-  a2dp.onVolume(volumeCB);
+  a2dp.setName("PicoW RX 00:00:00:00:00:00");
+  a2dp.setConsumer(new BluetoothAudioConsumerI2S(i2s));
+  i2s.setBCLK(pBCLK);
+  i2s.setDATA(pDOUT);
+  i2s.setBitsPerSample(16); // 8,16,24,32
+
+  if (!i2s.begin(sampleRate)) {
+    Serial.println("Failed to initialize I2S!");
+    while (1); // do nothing
+  }
+
   a2dp.onConnect(connectCB);
   a2dp.onPlaybackStatus(playbackCB);
   a2dp.begin();