Skip to content

Commit 797258e

Browse files
committed
EncodedAudioInfo channel change
1 parent 8818535 commit 797258e

File tree

3 files changed

+55
-32
lines changed

3 files changed

+55
-32
lines changed

src/AudioTools/AudioCodecs/AudioEncoded.h

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ class EncodedAudioOutput : public ModifyingOutput {
9494
}
9595
}
9696

97-
void setOutput(Print &outputStream) override { setOutput(&outputStream); }
98-
9997
/// Defines the output
10098
void setOutput(Print *outputStream) {
10199
ptr_out = outputStream;
@@ -107,6 +105,20 @@ class EncodedAudioOutput : public ModifyingOutput {
107105
}
108106
}
109107

108+
void setOutput(AudioStream* out) {
109+
setOutput((Print*)out);
110+
to_notify = out;
111+
}
112+
113+
void setOutput(AudioOutput*out){
114+
setOutput((Print*)out);
115+
to_notify = out;
116+
}
117+
118+
void setOutput(Print &outputStream) override { setOutput(&outputStream); }
119+
void setOutput(AudioOutput &outputStream) { setOutput(&outputStream); }
120+
void setOutput(AudioStream &outputStream) { setOutput(&outputStream); }
121+
110122
void setEncoder(AudioEncoder *encoder) {
111123
if (encoder == nullptr) {
112124
encoder = CodecNOP::instance();
@@ -138,8 +150,12 @@ class EncodedAudioOutput : public ModifyingOutput {
138150
TRACED();
139151
if (!active) {
140152
TRACED();
141-
const CodecNOP *nop = CodecNOP::instance();
142-
if (decoder_ptr != nop || encoder_ptr != nop) {
153+
// Setup notification
154+
if (to_notify != nullptr) {
155+
decoder_ptr->removeNotifyAudioChange(*to_notify);
156+
decoder_ptr->addNotifyAudioChange(*to_notify);
157+
}
158+
if (decoder_ptr != undefined || encoder_ptr != undefined) {
143159
active = true;
144160
if (!decoder_ptr->begin(cfg)) active = false;
145161
if (!encoder_ptr->begin(cfg)) active = false;
@@ -220,12 +236,22 @@ class EncodedAudioOutput : public ModifyingOutput {
220236
return *this;
221237
}
222238

239+
/// Provide audio info from decoder if relevant
240+
AudioInfo audioInfo() override {
241+
if (decoder_ptr != undefined){
242+
return decoder_ptr->audioInfo();
243+
}
244+
return ModifyingOutput::audioInfo();
245+
}
246+
223247
protected:
224248
// AudioInfo info;
225-
AudioDecoder *decoder_ptr = CodecNOP::instance(); // decoder
226-
AudioEncoder *encoder_ptr = CodecNOP::instance(); // decoder
249+
CodecNOP* undefined = CodecNOP::instance();
250+
AudioDecoder *decoder_ptr = undefined; // decoder
251+
AudioEncoder *encoder_ptr = undefined; // decoder
227252
AudioWriter *writer_ptr = nullptr;
228253
Print *ptr_out = nullptr;
254+
AudioInfoSupport *to_notify = nullptr;
229255
bool active = false;
230256
bool check_available_for_write = false;
231257
int frame_size = DEFAULT_BUFFER_SIZE;
@@ -371,6 +397,8 @@ class EncodedAudioStream : public ReformatBaseStream {
371397
return *this;
372398
};
373399

400+
AudioInfo audioInfo() override { return enc_out.audioInfo(); }
401+
374402
protected:
375403
EncodedAudioOutput enc_out;
376404
float byte_factor = 2.0f;

src/AudioTools/CoreAudio/AudioStreamsConverter.h

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,8 @@ class ChannelFormatConverterStream : public ReformatBaseStream {
130130

131131
void setAudioInfo(AudioInfo cfg) override {
132132
TRACED();
133-
from_channels = cfg.channels;
134-
LOGI("--> ChannelFormatConverterStream");
135-
AudioStream::setAudioInfo(cfg);
136-
switch (bits_per_sample) {
137-
case 8:
138-
getConverter<int8_t>()->setAudioInfo(cfg);
139-
break;
140-
case 16:
141-
getConverter<int16_t>()->setAudioInfo(cfg);
142-
break;
143-
case 24:
144-
getConverter<int24_t>()->setAudioInfo(cfg);
145-
break;
146-
case 32:
147-
getConverter<int32_t>()->setAudioInfo(cfg);
148-
break;
149-
}
133+
end();
134+
begin(cfg, to_channels);
150135
}
151136

152137
/// Returns the AudioInfo with the to_channels
@@ -169,24 +154,31 @@ class ChannelFormatConverterStream : public ReformatBaseStream {
169154
}
170155

171156
bool begin(AudioInfo cfg, int toChannels) {
172-
assert(toChannels != 0);
157+
// assert(toChannels != 0);
158+
if (toChannels == 0) {
159+
LOGE("toChannels is 0");
160+
return false;
161+
}
173162
// is_output_notify = false;
174163
to_channels = toChannels;
175164
from_channels = cfg.channels;
176165
bits_per_sample = cfg.bits_per_sample;
177166
LOGI("--> ChannelFormatConverterStream");
178167
AudioStream::setAudioInfo(cfg);
179168
LOGI("begin %d -> %d channels", cfg.channels, toChannels);
180-
bool result = setupConverter(cfg.channels, toChannels);
181-
if (!result) {
169+
is_active = setupConverter(cfg.channels, toChannels);
170+
if (!is_active) {
182171
TRACEE()
183172
}
184-
return result;
173+
return is_active;
185174
}
186175

187176
bool begin() override { return begin(audioInfo(), to_channels); }
188177

189-
void end() override { cleanupConverter(); }
178+
void end() override {
179+
cleanupConverter();
180+
is_active = false;
181+
}
190182

191183
void setToChannels(uint16_t channels) { to_channels = channels; }
192184

@@ -261,8 +253,9 @@ class ChannelFormatConverterStream : public ReformatBaseStream {
261253
protected:
262254
void *converter = nullptr;
263255
int bits_per_sample = 0;
264-
int to_channels;
265-
int from_channels;
256+
int to_channels = 0;
257+
int from_channels = 0;
258+
bool is_active = false;
266259

267260
template <typename T>
268261
ChannelFormatConverterStreamT<T> *getConverter() {
@@ -782,6 +775,7 @@ class FormatConverterStream : public ReformatBaseStream {
782775
/// Set the input audio information
783776
void setAudioInfo(AudioInfo info) override {
784777
TRACEI();
778+
info.logInfo("FormatConverterStream");
785779
ReformatBaseStream::setAudioInfo(info);
786780
// ChannelFormatConverter -> NumberFormatConverter -> SampleRateCoverter
787781
channelFormatConverter.setAudioInfo(info);

src/AudioTools/CoreAudio/BaseConverter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,7 @@ class ChannelEnhancer {
12941294
}
12951295

12961296
size_t convert(uint8_t *target, uint8_t *src, size_t size) {
1297+
if (from_channels == 0) return size;
12971298
int frame_count = size / (sizeof(T) * from_channels);
12981299
size_t result_size = 0;
12991300
T *result = (T *)target;
@@ -1321,8 +1322,8 @@ class ChannelEnhancer {
13211322
}
13221323

13231324
protected:
1324-
int from_channels;
1325-
int to_channels;
1325+
int from_channels = 0;
1326+
int to_channels = 0;
13261327
};
13271328

13281329
/**

0 commit comments

Comments
 (0)