[f57d9f]: libkwave / modules / ChannelMixer.cpp Maximize Restore History

Download this file

ChannelMixer.cpp    260 lines (223 with data), 8.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*************************************************************************
ChannelMixer.cpp - matrix based mixer for multiple channels
-------------------
begin : Sun Oct 10 2010
copyright : (C) 2010 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <QLatin1String>
#include <QMetaObject>
#include <QMutexLocker>
#include <QObject>
#include <QtGlobal>
#include <QVarLengthArray>
#include "libkwave/MixerMatrix.h"
#include "libkwave/Sample.h"
#include "libkwave/modules/ChannelMixer.h"
#include "libkwave/modules/Indexer.h"
#include "libkwave/modules/StreamObject.h"
#ifndef unlikely
#define unlikely(x) (__builtin_expect((x),0))
#endif
#ifndef likely
#define likely(x) (__builtin_expect((x),1))
#endif
//***************************************************************************
Kwave::ChannelMixer::ChannelMixer(unsigned int inputs, unsigned int outputs)
:Kwave::SampleSource(),
m_matrix(0),
m_inputs(inputs),
m_outputs(outputs),
m_indexer(),
m_input_queue(),
m_output_buffer(),
m_lock()
{
}
//***************************************************************************
bool Kwave::ChannelMixer::init()
{
if (!m_inputs || !m_outputs) return false;
// create queues for the input data
m_input_queue.resize(m_inputs);
Q_ASSERT(m_input_queue.count() == static_cast<int>(m_inputs));
if (m_input_queue.count() != static_cast<int>(m_inputs)) return false;
// create the buffers for the output data
for (unsigned int index = 0; index < m_outputs; index++) {
// create a buffer for the input
Kwave::SampleBuffer *out_buffer = new Kwave::SampleBuffer();
Q_ASSERT(out_buffer);
if (!out_buffer) return false;
m_output_buffer.append(out_buffer);
}
// create indexing proxies and connect their output to this mixer
for (unsigned int index = 0; index < m_inputs; index++) {
Kwave::StreamObject *indexer = new Kwave::Indexer(index);
Q_ASSERT(indexer);
if (!indexer) return false;
m_indexer.append(indexer);
bool ok = Kwave::connect(
*indexer, SIGNAL(output(unsigned int, Kwave::SampleArray)),
*this, SLOT(idxInput(unsigned int, Kwave::SampleArray)));
Q_ASSERT(ok);
if (!ok) return false;
}
// create the mixer matrix
// create a translation matrix for mixing up/down to the desired
// number of output channels
m_matrix = new Kwave::MixerMatrix(m_inputs, m_outputs);
Q_ASSERT(m_matrix);
if (!m_matrix) return false;
// everything succeeded
return true;
}
//***************************************************************************
Kwave::ChannelMixer::~ChannelMixer()
{
QMutexLocker _lock(&m_lock);
while (!m_indexer.isEmpty()) {
Kwave::StreamObject *indexer = m_indexer[0];
if (indexer) delete indexer;
m_indexer.remove(0);
}
m_input_queue.clear();
while (!m_output_buffer.isEmpty()) {
Kwave::SampleBuffer *buffer = m_output_buffer[0];
if (buffer) delete buffer;
m_output_buffer.remove(0);
}
}
//***************************************************************************
static QByteArray _sig(const QString &sig)
{
return QMetaObject::normalizedSignature(sig.toLatin1());
}
//***************************************************************************
unsigned int Kwave::ChannelMixer::tracksOfPort(const QString &port) const
{
QMutexLocker _lock(const_cast<QMutex *>(&m_lock));
if (_sig(port) == _sig(SLOT(input(Kwave::SampleArray)))) {
// input ports
return m_inputs; // init is done
} else if (_sig(port) == _sig(SIGNAL(output(Kwave::SampleArray)))) {
// output ports
return m_outputs;
} else if (_sig(port) ==
_sig(SLOT(idxInput(unsigned int, Kwave::SampleArray)))) {
return 1;
}
qFatal("unknown port");
return 0;
}
//***************************************************************************
Kwave::StreamObject *Kwave::ChannelMixer::port(const QString &port,
unsigned int track)
{
QMutexLocker _lock(&m_lock);
if (_sig(port) == _sig(SLOT(input(Kwave::SampleArray)))) {
// input proxy
Q_ASSERT(static_cast<int>(track) < m_indexer.count());
if (static_cast<int>(track) >= m_indexer.count()) return 0;
return m_indexer.at(track);
} else if (_sig(port) == _sig(SIGNAL(output(Kwave::SampleArray)))) {
// output proxy
Q_ASSERT(static_cast<int>(track) < m_output_buffer.count());
if (static_cast<int>(track) >= m_output_buffer.count()) return 0;
return m_output_buffer[track];
} else if (_sig(port) ==
_sig(SLOT(idxInput(unsigned int, Kwave::SampleArray)))) {
return this;
}
qFatal("unknown port");
return 0;
}
//***************************************************************************
void Kwave::ChannelMixer::idxInput(unsigned int index, Kwave::SampleArray data)
{
QMutexLocker _lock(&m_lock);
// put the data into the corresponding input queue
Q_ASSERT(index < m_inputs);
Q_ASSERT(static_cast<int>(index) < m_input_queue.count());
if (static_cast<int>(index) < m_input_queue.count())
m_input_queue[index].enqueue(data);
// check: if there is one empty queue we are not yet ready for mixing
bool ready = true;
foreach (const QQueue<Kwave::SampleArray> &queue, m_input_queue) {
if (queue.isEmpty()) {
ready = false;
break;
}
}
// mix if we are ready
if (ready && m_matrix) mix();
}
//***************************************************************************
void Kwave::ChannelMixer::mix()
{
Q_ASSERT(m_matrix);
// all inputs should contain a buffer, dequeue them into a vector
// and form an array of pointers to the raw data, for speeding up
QVector<Kwave::SampleArray> v_input(m_inputs);
QVarLengthArray<const sample_t *> input(m_inputs);
unsigned int min_len = UINT_MAX;
for (unsigned int track = 0; track < m_inputs; track++) {
// dequeue the buffer with input data
QQueue<Kwave::SampleArray> &queue = m_input_queue[track];
Q_ASSERT(!queue.isEmpty());
Kwave::SampleArray buffer = queue.dequeue();
v_input[track] = buffer;
// get a pointer for quick access
const sample_t *raw_data = v_input[track].data();
input[track] = raw_data;
// detect minimum input length
min_len = qMin(min_len, buffer.size());
}
Q_ASSERT(min_len);
if (!min_len) return; // zero length buffer in the queue, data underrun?
// make sure all output buffers are large enough
// and build an array of pointers to the raw data, for speeding up
QVarLengthArray<sample_t *> output(m_outputs);
for (unsigned int track = 0; track < m_outputs; track++) {
Kwave::SampleBuffer *buffer = m_output_buffer[track];
Q_ASSERT(buffer);
if (!buffer) return;
if (buffer->data().size() < min_len)
buffer->data().resize(min_len);
if (buffer->data().size() < min_len) {
Q_ASSERT(buffer->data().size() >= min_len);
qWarning("ChannelMixer: failed to increase buffer size to %u",
min_len);
return; // OOM ?
}
output[track] = buffer->data().data();
}
// mix all channels together, using the mixer matrix
for (unsigned int y = 0; y < m_outputs; y++) {
sample_t *out = output[y];
for (unsigned int pos = 0; pos < min_len; pos++) {
double sum = 0.0;
for (unsigned int x = 0; x < m_inputs; x++) {
const double f = (*m_matrix)[x][y];
const double i = static_cast<double>(input[x][pos]);
sum += (f * i);
}
out[pos] = static_cast<sample_t>(sum);
}
// emit the output
Kwave::SampleBuffer *out_buf = m_output_buffer[y];
if (unlikely(out_buf->data().size() > min_len))
out_buf->data().resize(min_len);
out_buf->done();
}
}
//***************************************************************************
//***************************************************************************