Skip to content

Commit 47de4d9

Browse files
committed
InputMerge
1 parent 35df311 commit 47de4d9

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/AudioTools/CoreAudio/AudioStreams.h

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,22 +1295,21 @@ class InputMerge : public AudioStream {
12951295

12961296
virtual bool begin() {
12971297
// make sure that we use the correct channel count
1298-
info.channels = size();
1298+
info.channels = channelCount();
12991299
return AudioStream::begin();
13001300
}
13011301

13021302
/// Provides the data from all streams mixed together
13031303
size_t readBytes(uint8_t *data, size_t len) override {
13041304
LOGD("readBytes: %d", (int)len);
13051305
T *p_data = (T *)data;
1306-
int result_len = MIN(available(), len / size());
1307-
int sample_count = result_len / sizeof(T);
1308-
int stream_count = streams.size();
1306+
int result_len = MIN(available(), len);
1307+
int frames = result_len / (sizeof(T) * total_channel_count);
13091308
int result_idx = 0;
1310-
for (int j = 0; j < sample_count; j++) {
1311-
for (int i = 0; i < stream_count; i++) {
1312-
for (int ch = 0; ch < channels[i]; ch++) {
1313-
p_data[result_idx++] = weights[i] * readSample<T>(streams[i]);
1309+
for (int j = 0; j < frames; j++) {
1310+
for (int i = 0; i < records.size(); i++) {
1311+
for (int ch = 0; ch < records[i].channels; ch++) {
1312+
p_data[result_idx++] = records[i].weight * readSample<T>(records[i].stream);
13141313
}
13151314
}
13161315
}
@@ -1319,36 +1318,34 @@ class InputMerge : public AudioStream {
13191318

13201319
/// Adds a new input stream with 1 channel
13211320
void add(Stream &in, int channelCount, float weight = 1.0) {
1322-
streams.push_back(&in);
1323-
weights.push_back(weight);
1324-
channels.push_back(channelCount);
1321+
MergeRecord rec(in, channelCount, weight);;
1322+
records.push_back(rec);
13251323
total_channel_count += channelCount;
13261324
}
13271325

13281326
/// Defines a new weight for the indicated channel: If you set it to 0 it is
13291327
/// muted.
13301328
void setWeight(int channel, float weight) {
1331-
if (channel < size()) {
1332-
weights[channel] = weight;
1329+
if (channel < channelCount()) {
1330+
records[channel].weight = weight;
13331331
} else {
1334-
LOGE("Invalid channel %d - max is %d", channel, size() - 1);
1332+
LOGE("Invalid channel %d - max is %d", channel, channelCount() - 1);
13351333
}
13361334
}
13371335

13381336
/// Remove all input streams
13391337
void end() override {
1340-
streams.clear();
1341-
weights.clear();
1338+
records.clear();
13421339
}
13431340

13441341
/// Number of channels to which are mixed together = number of result channels
1345-
int size() { return total_channel_count; }
1342+
int channelCount() { return total_channel_count; }
13461343

13471344
/// Provides the min available data from all streams
13481345
int available() override {
1349-
int result = streams[0]->available();
1350-
for (int j = 1; j < size(); j++) {
1351-
int tmp = streams[j]->available();
1346+
int result = records[0].stream->available();
1347+
for (int j = 1; j < channelCount(); j++) {
1348+
int tmp = records[j].stream->available();
13521349
if (tmp < result) {
13531350
result = tmp;
13541351
}
@@ -1357,10 +1354,18 @@ class InputMerge : public AudioStream {
13571354
}
13581355

13591356
protected:
1357+
struct MergeRecord {
1358+
Stream *stream=nullptr;
1359+
int channels = 0;
1360+
float weight=1.0;
1361+
MergeRecord(Stream* str, int ch, float w){
1362+
stream = str;
1363+
channels = ch;
1364+
weight = w;
1365+
}
1366+
};
1367+
Vector<MergeRecord> records;
13601368
int total_channel_count = 0;
1361-
Vector<Stream *> streams{0};
1362-
Vector<float> weights{0};
1363-
Vector<int> channels{0};
13641369
};
13651370

13661371
/**

0 commit comments

Comments
 (0)