arduino-audio-tools
Loading...
Searching...
No Matches
I2SDriverRP2040-MBED.h
Go to the documentation of this file.
1#pragma once
2
5#if defined(RP2040_MBED)
6#include "RP2040-I2S.h"
7
8#define IS_I2S_IMPLEMENTED
9
10namespace audio_tools {
11
12//#if !defined(ARDUINO_ARCH_MBED_RP2040)
13//static ::I2S I2S;
14//#endif
15
22class I2SDriverRP2040MBED : public I2SDriverBase {
23 friend class I2SStream;
24
25 public:
27 I2SConfigStd defaultConfig(RxTxMode mode) {
28 I2SConfigStd c(mode);
29 return c;
30 }
31
33 bool setAudioInfo(AudioInfo) { return false; }
34
36 bool begin(RxTxMode mode = TX_MODE) {
37 TRACED();
38 return begin(defaultConfig(mode));
39 }
40
42 bool begin(I2SConfigStd cfg) {
43 TRACEI();
44 this->cfg = cfg;
45 cfg.logInfo();
46 if (cfg.rx_tx_mode != TX_MODE) {
47 LOGE("Unsupported mode: only TX_MODE is supported");
48 return false;
49 }
50 if (!I2S.setBCLK(cfg.pin_bck)) {
51 LOGE("Could not set bck pin: %d", cfg.pin_bck);
52 return false;
53 }
54 if (!I2S.setDATA(cfg.pin_data)) {
55 LOGE("Could not set data pin: %d", cfg.pin_data);
56 return false;
57 }
58 if (cfg.bits_per_sample != 16) {
59 LOGE("Unsupported bits_per_sample: %d", (int)cfg.bits_per_sample);
60 return false;
61 }
62
63 if (cfg.channels < 1 || cfg.channels > 2) {
64 LOGE("Unsupported channels: '%d' - only 2 is supported", (int)cfg.channels);
65 return false;
66 }
67
68 int rate = cfg.sample_rate;
69 if (cfg.channels == 1) {
70 rate = rate / 2;
71 }
72
73 if (!I2S.begin(rate)) {
74 LOGE("Could not start I2S");
75 return false;
76 }
77 return true;
78 }
79
81 void end() { I2S.end(); }
82
84 I2SConfigStd config() { return cfg; }
85
87 size_t writeBytes(const void *src, size_t size_bytes) {
88 TRACED();
89 size_t result = 0;
90 int16_t *p16 = (int16_t *)src;
91
92 if (cfg.channels == 1) {
93 int samples = size_bytes / 2;
94 // multiply 1 channel into 2
95 int16_t buffer[samples * 2]; // from 1 byte to 2 bytes
96 for (int j = 0; j < samples; j++) {
97 buffer[j * 2] = p16[j];
98 buffer[j * 2 + 1] = p16[j];
99 }
100 result = I2S.write((const uint8_t *)buffer, size_bytes * 2) * 2;
101 } else if (cfg.channels == 2) {
102 result = I2S.write((const uint8_t *)src, size_bytes) * 4;
103 }
104 return result;
105 }
106
107 size_t readBytes(void *dest, size_t size_bytes) {
108 TRACEE();
109 size_t result = 0;
110 return result;
111 }
112
113 int availableForWrite() {
114 int result = 0;
115 if (cfg.channels == 1) {
116 // it should be a multiple of 2
117 result = I2S.availableForWrite() / 2 * 2;
118 // return half of it because we double when writing
119 result = result / 2;
120 } else {
121 // it should be a multiple of 4
122 result = I2S.availableForWrite() / 4 * 4;
123 }
124 if (result < 4) {
125 result = 0;
126 }
127 return result;
128 }
129
130 int available() { return 0; }
131
132 void flush() { return I2S.flush(); }
133
134 protected:
135 I2SConfigStd cfg;
136
137 // blocking write
138 void writeSample(int16_t sample) {
139 int written = I2S.write(sample);
140 while (!written) {
141 delay(5);
142 LOGW("written: %d ", written);
143 written = I2S.write(sample);
144 }
145 }
146
147 int writeSamples(int samples, int16_t *values) {
148 int result = 0;
149 for (int j = 0; j < samples; j++) {
150 int16_t sample = values[j];
151 writeSample(sample);
152 writeSample(sample);
153 result++;
154 }
155 return result;
156 }
157};
158
160
161} // namespace audio_tools
162
163#endif
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define TRACEE()
Definition AudioLoggerIDF.h:34
#define LOGE(...)
Definition AudioLoggerIDF.h:30
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:26
@ TX_MODE
Definition AudioTypes.h:26
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void delay(uint32_t ms)
Definition Arduino.h:255
I2SDriverESP32 I2SDriver
Definition I2SDriverESP32.h:403
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508
constexpr const _Ep * end(initializer_list< _Ep > __il) noexcept
Definition InitializerList.h:63
constexpr const _Ep * begin(initializer_list< _Ep > __il) noexcept
Definition InitializerList.h:55