1
1
#pragma once
2
- #include " AudioBoard.h" // install audio-driver library
2
+ #include " AudioBoard.h" // install audio-driver library
3
3
#include " AudioConfig.h"
4
4
#include " AudioI2S/I2SStream.h"
5
5
6
6
#pragma GCC diagnostic ignored "-Wclass-memaccess"
7
7
8
-
9
8
// Added to be compatible with the AudioKitStream.h
10
9
#ifndef PIN_AUDIO_KIT_SD_CARD_CS
11
10
#define PIN_AUDIO_KIT_SD_CARD_CS 13
@@ -21,23 +20,19 @@ namespace audio_tools {
21
20
* @ingroup io
22
21
* @author Phil Schatzmann
23
22
* @copyright GPLv3
24
- */
23
+ */
25
24
struct I2SCodecConfig : public I2SConfig {
26
25
input_device_t input_device = ADC_INPUT_LINE1;
27
26
output_device_t output_device = DAC_OUTPUT_ALL;
28
27
// to be compatible with the AudioKitStream -> do not activate SD spi if false
29
28
bool sd_active = true ;
30
29
31
- bool operator ==(I2SCodecConfig alt){
32
- return input_device == alt.input_device
33
- && output_device == alt.output_device
34
- && *((AudioInfo*)this ) == alt;
30
+ bool operator ==(I2SCodecConfig alt) {
31
+ return input_device == alt.input_device &&
32
+ output_device == alt.output_device && *((AudioInfo *)this ) == alt;
35
33
}
36
34
37
- bool operator !=(I2SCodecConfig alt){
38
- return !(*this == alt);
39
- }
40
-
35
+ bool operator !=(I2SCodecConfig alt) { return !(*this == alt); }
41
36
};
42
37
43
38
/* *
@@ -47,7 +42,7 @@ struct I2SCodecConfig : public I2SConfig {
47
42
* @copyright GPLv3
48
43
*/
49
44
class I2SCodecStream : public AudioStream {
50
- public:
45
+ public:
51
46
// / Default Constructor (w/o codec)
52
47
I2SCodecStream () = default ;
53
48
/* *
@@ -82,7 +77,7 @@ class I2SCodecStream : public AudioStream {
82
77
is_active = i2s.begin (cfg);
83
78
84
79
// if setvolume was called before begin
85
- if (is_active && volume> 0 .0f ) {
80
+ if (is_active && volume > 0 .0f ) {
86
81
setVolume (volume);
87
82
}
88
83
return is_active;
@@ -98,8 +93,7 @@ class I2SCodecStream : public AudioStream {
98
93
// / Stops the I2S interface
99
94
void end () {
100
95
TRACED ();
101
- if (p_board)
102
- p_board->end ();
96
+ if (p_board) p_board->end ();
103
97
i2s.end ();
104
98
is_active = false ;
105
99
}
@@ -109,13 +103,26 @@ class I2SCodecStream : public AudioStream {
109
103
TRACEI ();
110
104
// save volume if possible
111
105
AudioStream::setAudioInfo (info);
112
- beginCodec (info);
113
106
i2s.setAudioInfo (info);
114
- // restore volume
115
- if (volume > 0 .0f ) {
116
- LOGI (" restoring volume: %f" , volume);
117
- setVolume (volume);
107
+
108
+ if (!is_active || p_board == nullptr ) {
109
+ return ;
118
110
}
111
+
112
+ if (cfg.sample_rate == info.sample_rate &&
113
+ cfg.bits_per_sample == info.bits_per_sample &&
114
+ cfg.channels == info.channels ) {
115
+ return ;
116
+ }
117
+
118
+ cfg.sample_rate = info.sample_rate ;
119
+ cfg.bits_per_sample = info.bits_per_sample ;
120
+ cfg.channels = info.channels ;
121
+
122
+ // update values in codec
123
+ codec_cfg.i2s .bits = toCodecBits (cfg.bits_per_sample );
124
+ codec_cfg.i2s .rate = toRate (cfg.sample_rate );
125
+ p_board->setConfig (codec_cfg);
119
126
}
120
127
121
128
// / Writes the audio data to I2S
@@ -138,29 +145,25 @@ class I2SCodecStream : public AudioStream {
138
145
// / sets the volume (range 0.0 - 1.0)
139
146
bool setVolume (float vol) {
140
147
volume = vol;
141
- if (!is_active || p_board == nullptr )
142
- return false ;
148
+ if (!is_active || p_board == nullptr ) return false ;
143
149
return p_board->setVolume (vol * 100.0 );
144
150
}
145
151
146
152
// / Provides the actual volume (0.0 - 1.0)
147
153
int getVolume () {
148
- if (p_board == nullptr )
149
- return -1 ;
154
+ if (p_board == nullptr ) return -1 ;
150
155
return p_board->getVolume ();
151
156
}
152
-
157
+
153
158
// / Mute / unmote
154
159
bool setMute (bool mute) {
155
- if (p_board == nullptr )
156
- return false ;
160
+ if (p_board == nullptr ) return false ;
157
161
return p_board->setMute (mute);
158
162
}
159
163
160
164
// / Sets the output of the PA Power Pin
161
165
bool setPAPower (bool active) {
162
- if (p_board == nullptr )
163
- return false ;
166
+ if (p_board == nullptr ) return false ;
164
167
return p_board->setPAPower (active);
165
168
}
166
169
@@ -173,37 +176,31 @@ class I2SCodecStream : public AudioStream {
173
176
// / checks if a board has been defined
174
177
bool hasBoard () { return p_board != nullptr ; }
175
178
176
- // / Provides the gpio for the indicated function
179
+ // / Provides the gpio for the indicated function
177
180
GpioPin getPinID (PinFunction function) {
178
- if (p_board == nullptr )
179
- return -1 ;
181
+ if (p_board == nullptr ) return -1 ;
180
182
return p_board->getPins ().getPinID (function);
181
183
}
182
184
183
- // / Provides the gpio for the indicated function
185
+ // / Provides the gpio for the indicated function
184
186
GpioPin getPinID (PinFunction function, int pos) {
185
- if (p_board == nullptr )
186
- return -1 ;
187
+ if (p_board == nullptr ) return -1 ;
187
188
return p_board->getPins ().getPinID (function, pos);
188
189
}
189
190
190
191
// / Provides the gpio for the indicated key pos
191
- GpioPin getKey (int pos){
192
- return getPinID (PinFunction::KEY, pos);
193
- }
192
+ GpioPin getKey (int pos) { return getPinID (PinFunction::KEY, pos); }
194
193
195
194
// / Provides access to the pin information
196
195
DriverPins &getPins () { return p_board->getPins (); }
197
196
198
197
// / Provides the i2s driver
199
- I2SDriver* driver () {
200
- return i2s.driver ();
201
- }
198
+ I2SDriver *driver () { return i2s.driver (); }
202
199
203
- protected:
200
+ protected:
204
201
I2SStream i2s;
205
202
I2SCodecConfig cfg;
206
- I2SCodecConfig cfg_driver; // last driver state
203
+ CodecConfig codec_cfg;
207
204
AudioBoard *p_board = nullptr ;
208
205
bool is_active = false ;
209
206
float volume = -1 .0f ;
@@ -242,30 +239,23 @@ class I2SCodecStream : public AudioStream {
242
239
codec_cfg.i2s .fmt = toFormat (info.i2s_format );
243
240
// use reverse logic for codec setting
244
241
codec_cfg.i2s .mode = info.is_master ? MODE_SLAVE : MODE_MASTER;
245
- if (p_board == nullptr )
246
- return false ;
242
+ if (p_board == nullptr ) return false ;
247
243
248
244
// setup driver only on changes
249
- bool result = true ;
250
- if (!is_active || (cfg_driver != info)){
251
- result = p_board->begin (codec_cfg);
252
- cfg_driver = info;
253
- }
254
- return result;
255
-
245
+ return p_board->begin (codec_cfg);
256
246
}
257
247
258
248
sample_bits_t toCodecBits (int bits) {
259
249
switch (bits) {
260
- case 16 :
261
- LOGD (" BIT_LENGTH_16BITS" );
262
- return BIT_LENGTH_16BITS;
263
- case 24 :
264
- LOGD (" BIT_LENGTH_24BITS" );
265
- return BIT_LENGTH_24BITS;
266
- case 32 :
267
- LOGD (" BIT_LENGTH_32BITS" );
268
- return BIT_LENGTH_32BITS;
250
+ case 16 :
251
+ LOGD (" BIT_LENGTH_16BITS" );
252
+ return BIT_LENGTH_16BITS;
253
+ case 24 :
254
+ LOGD (" BIT_LENGTH_24BITS" );
255
+ return BIT_LENGTH_24BITS;
256
+ case 32 :
257
+ LOGD (" BIT_LENGTH_32BITS" );
258
+ return BIT_LENGTH_32BITS;
269
259
}
270
260
LOGE (" Unsupported bits: %d" , bits);
271
261
return BIT_LENGTH_16BITS;
@@ -301,26 +291,26 @@ class I2SCodecStream : public AudioStream {
301
291
302
292
i2s_format_t toFormat (I2SFormat fmt) {
303
293
switch (fmt) {
304
- case I2S_PHILIPS_FORMAT:
305
- case I2S_STD_FORMAT:
306
- LOGD (" I2S_NORMAL" );
307
- return I2S_NORMAL;
308
- case I2S_LEFT_JUSTIFIED_FORMAT:
309
- case I2S_MSB_FORMAT:
310
- LOGD (" I2S_LEFT" );
311
- return I2S_LEFT;
312
- case I2S_RIGHT_JUSTIFIED_FORMAT:
313
- case I2S_LSB_FORMAT:
314
- LOGD (" I2S_RIGHT" );
315
- return I2S_RIGHT;
316
- case I2S_PCM:
317
- LOGD (" I2S_DSP" );
318
- return I2S_DSP;
319
- default :
320
- LOGE (" unsupported mode" );
321
- return I2S_NORMAL;
294
+ case I2S_PHILIPS_FORMAT:
295
+ case I2S_STD_FORMAT:
296
+ LOGD (" I2S_NORMAL" );
297
+ return I2S_NORMAL;
298
+ case I2S_LEFT_JUSTIFIED_FORMAT:
299
+ case I2S_MSB_FORMAT:
300
+ LOGD (" I2S_LEFT" );
301
+ return I2S_LEFT;
302
+ case I2S_RIGHT_JUSTIFIED_FORMAT:
303
+ case I2S_LSB_FORMAT:
304
+ LOGD (" I2S_RIGHT" );
305
+ return I2S_RIGHT;
306
+ case I2S_PCM:
307
+ LOGD (" I2S_DSP" );
308
+ return I2S_DSP;
309
+ default :
310
+ LOGE (" unsupported mode" );
311
+ return I2S_NORMAL;
322
312
}
323
313
}
324
314
};
325
315
326
- } // namespace audio_tools
316
+ } // namespace audio_tools
0 commit comments