Skip to content

Commit 0dce519

Browse files
committed
I2SCodecStream setVolume
1 parent 188ade4 commit 0dce519

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/AudioLibs/I2SCodecStream.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ class I2SCodecStream : public AudioStream {
8080
return false;
8181
}
8282
is_active = i2s.begin(cfg);
83+
84+
// if setvolume was called before begin
85+
if (is_active && volume>0.0f) {
86+
setVolume(volume);
87+
}
8388
return is_active;
8489
}
8590

@@ -102,14 +107,15 @@ class I2SCodecStream : public AudioStream {
102107
/// updates the sample rate dynamically
103108
virtual void setAudioInfo(AudioInfo info) {
104109
TRACEI();
105-
AudioStream::setAudioInfo(info);
106-
float vol = -1.0f;
107110
// save volume if possible
108-
if (is_active) vol = getVolume();
111+
AudioStream::setAudioInfo(info);
109112
beginCodec(info);
110113
i2s.setAudioInfo(info);
111-
// restore volume
112-
if (vol>0.0f) setVolume(vol);
114+
// restore volume
115+
if (volume > 0.0f) {
116+
LOGI("restoring volume: %f", volume);
117+
setVolume(volume);
118+
}
113119
}
114120

115121
/// Writes the audio data to I2S
@@ -131,7 +137,8 @@ class I2SCodecStream : public AudioStream {
131137

132138
/// sets the volume (range 0.0 - 1.0)
133139
bool setVolume(float vol) {
134-
if (p_board == nullptr)
140+
volume = vol;
141+
if (!is_active || p_board == nullptr)
135142
return false;
136143
return p_board->setVolume(vol * 100.0);
137144
}
@@ -199,6 +206,7 @@ class I2SCodecStream : public AudioStream {
199206
I2SCodecConfig cfg_driver; // last driver state
200207
AudioBoard *p_board = nullptr;
201208
bool is_active = false;
209+
float volume = -1.0f;
202210

203211
/// We use the board pins if they are available
204212
void setupI2SPins() {

0 commit comments

Comments
 (0)