Skip to content

Commit 82c4463

Browse files
committed
I2SCodecStream setConfig
1 parent 0dce519 commit 82c4463

File tree

1 file changed

+70
-80
lines changed

1 file changed

+70
-80
lines changed

src/AudioLibs/I2SCodecStream.h

Lines changed: 70 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#pragma once
2-
#include "AudioBoard.h" // install audio-driver library
2+
#include "AudioBoard.h" // install audio-driver library
33
#include "AudioConfig.h"
44
#include "AudioI2S/I2SStream.h"
55

66
#pragma GCC diagnostic ignored "-Wclass-memaccess"
77

8-
98
// Added to be compatible with the AudioKitStream.h
109
#ifndef PIN_AUDIO_KIT_SD_CARD_CS
1110
#define PIN_AUDIO_KIT_SD_CARD_CS 13
@@ -21,23 +20,19 @@ namespace audio_tools {
2120
* @ingroup io
2221
* @author Phil Schatzmann
2322
* @copyright GPLv3
24-
*/
23+
*/
2524
struct I2SCodecConfig : public I2SConfig {
2625
input_device_t input_device = ADC_INPUT_LINE1;
2726
output_device_t output_device = DAC_OUTPUT_ALL;
2827
// to be compatible with the AudioKitStream -> do not activate SD spi if false
2928
bool sd_active = true;
3029

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;
3533
}
3634

37-
bool operator!=(I2SCodecConfig alt){
38-
return !(*this == alt);
39-
}
40-
35+
bool operator!=(I2SCodecConfig alt) { return !(*this == alt); }
4136
};
4237

4338
/**
@@ -47,7 +42,7 @@ struct I2SCodecConfig : public I2SConfig {
4742
* @copyright GPLv3
4843
*/
4944
class I2SCodecStream : public AudioStream {
50-
public:
45+
public:
5146
/// Default Constructor (w/o codec)
5247
I2SCodecStream() = default;
5348
/**
@@ -82,7 +77,7 @@ class I2SCodecStream : public AudioStream {
8277
is_active = i2s.begin(cfg);
8378

8479
// if setvolume was called before begin
85-
if (is_active && volume>0.0f) {
80+
if (is_active && volume > 0.0f) {
8681
setVolume(volume);
8782
}
8883
return is_active;
@@ -98,8 +93,7 @@ class I2SCodecStream : public AudioStream {
9893
/// Stops the I2S interface
9994
void end() {
10095
TRACED();
101-
if (p_board)
102-
p_board->end();
96+
if (p_board) p_board->end();
10397
i2s.end();
10498
is_active = false;
10599
}
@@ -109,13 +103,26 @@ class I2SCodecStream : public AudioStream {
109103
TRACEI();
110104
// save volume if possible
111105
AudioStream::setAudioInfo(info);
112-
beginCodec(info);
113106
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;
118110
}
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);
119126
}
120127

121128
/// Writes the audio data to I2S
@@ -138,29 +145,25 @@ class I2SCodecStream : public AudioStream {
138145
/// sets the volume (range 0.0 - 1.0)
139146
bool setVolume(float vol) {
140147
volume = vol;
141-
if (!is_active || p_board == nullptr)
142-
return false;
148+
if (!is_active || p_board == nullptr) return false;
143149
return p_board->setVolume(vol * 100.0);
144150
}
145151

146152
/// Provides the actual volume (0.0 - 1.0)
147153
int getVolume() {
148-
if (p_board == nullptr)
149-
return -1;
154+
if (p_board == nullptr) return -1;
150155
return p_board->getVolume();
151156
}
152-
157+
153158
/// Mute / unmote
154159
bool setMute(bool mute) {
155-
if (p_board == nullptr)
156-
return false;
160+
if (p_board == nullptr) return false;
157161
return p_board->setMute(mute);
158162
}
159163

160164
/// Sets the output of the PA Power Pin
161165
bool setPAPower(bool active) {
162-
if (p_board == nullptr)
163-
return false;
166+
if (p_board == nullptr) return false;
164167
return p_board->setPAPower(active);
165168
}
166169

@@ -173,37 +176,31 @@ class I2SCodecStream : public AudioStream {
173176
/// checks if a board has been defined
174177
bool hasBoard() { return p_board != nullptr; }
175178

176-
/// Provides the gpio for the indicated function
179+
/// Provides the gpio for the indicated function
177180
GpioPin getPinID(PinFunction function) {
178-
if (p_board == nullptr)
179-
return -1;
181+
if (p_board == nullptr) return -1;
180182
return p_board->getPins().getPinID(function);
181183
}
182184

183-
/// Provides the gpio for the indicated function
185+
/// Provides the gpio for the indicated function
184186
GpioPin getPinID(PinFunction function, int pos) {
185-
if (p_board == nullptr)
186-
return -1;
187+
if (p_board == nullptr) return -1;
187188
return p_board->getPins().getPinID(function, pos);
188189
}
189190

190191
/// 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); }
194193

195194
/// Provides access to the pin information
196195
DriverPins &getPins() { return p_board->getPins(); }
197196

198197
/// Provides the i2s driver
199-
I2SDriver* driver() {
200-
return i2s.driver();
201-
}
198+
I2SDriver *driver() { return i2s.driver(); }
202199

203-
protected:
200+
protected:
204201
I2SStream i2s;
205202
I2SCodecConfig cfg;
206-
I2SCodecConfig cfg_driver; // last driver state
203+
CodecConfig codec_cfg;
207204
AudioBoard *p_board = nullptr;
208205
bool is_active = false;
209206
float volume = -1.0f;
@@ -242,30 +239,23 @@ class I2SCodecStream : public AudioStream {
242239
codec_cfg.i2s.fmt = toFormat(info.i2s_format);
243240
// use reverse logic for codec setting
244241
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;
247243

248244
// 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);
256246
}
257247

258248
sample_bits_t toCodecBits(int bits) {
259249
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;
269259
}
270260
LOGE("Unsupported bits: %d", bits);
271261
return BIT_LENGTH_16BITS;
@@ -301,26 +291,26 @@ class I2SCodecStream : public AudioStream {
301291

302292
i2s_format_t toFormat(I2SFormat fmt) {
303293
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;
322312
}
323313
}
324314
};
325315

326-
} // namespace audio_tools
316+
} // namespace audio_tools

0 commit comments

Comments
 (0)