@@ -1295,22 +1295,21 @@ class InputMerge : public AudioStream {
1295
1295
1296
1296
virtual bool begin () {
1297
1297
// make sure that we use the correct channel count
1298
- info.channels = size ();
1298
+ info.channels = channelCount ();
1299
1299
return AudioStream::begin ();
1300
1300
}
1301
1301
1302
1302
// / Provides the data from all streams mixed together
1303
1303
size_t readBytes (uint8_t *data, size_t len) override {
1304
1304
LOGD (" readBytes: %d" , (int )len);
1305
1305
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);
1309
1308
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 );
1314
1313
}
1315
1314
}
1316
1315
}
@@ -1319,36 +1318,34 @@ class InputMerge : public AudioStream {
1319
1318
1320
1319
// / Adds a new input stream with 1 channel
1321
1320
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);
1325
1323
total_channel_count += channelCount;
1326
1324
}
1327
1325
1328
1326
// / Defines a new weight for the indicated channel: If you set it to 0 it is
1329
1327
// / muted.
1330
1328
void setWeight (int channel, float weight) {
1331
- if (channel < size ()) {
1332
- weights [channel] = weight;
1329
+ if (channel < channelCount ()) {
1330
+ records [channel]. weight = weight;
1333
1331
} else {
1334
- LOGE (" Invalid channel %d - max is %d" , channel, size () - 1 );
1332
+ LOGE (" Invalid channel %d - max is %d" , channel, channelCount () - 1 );
1335
1333
}
1336
1334
}
1337
1335
1338
1336
// / Remove all input streams
1339
1337
void end () override {
1340
- streams.clear ();
1341
- weights.clear ();
1338
+ records.clear ();
1342
1339
}
1343
1340
1344
1341
// / 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; }
1346
1343
1347
1344
// / Provides the min available data from all streams
1348
1345
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 ();
1352
1349
if (tmp < result) {
1353
1350
result = tmp;
1354
1351
}
@@ -1357,10 +1354,18 @@ class InputMerge : public AudioStream {
1357
1354
}
1358
1355
1359
1356
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;
1360
1368
int total_channel_count = 0 ;
1361
- Vector<Stream *> streams{0 };
1362
- Vector<float > weights{0 };
1363
- Vector<int > channels{0 };
1364
1369
};
1365
1370
1366
1371
/* *
0 commit comments