[3e4acc]: libkwave / KwaveMimeData.cpp Maximize Restore History

Download this file

KwaveMimeData.cpp    317 lines (260 with data), 9.8 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/***************************************************************************
KwaveMimeData.cpp - mime data container for Kwave's audio data
-------------------
begin : Oct 04 2008
copyright : (C) 2008 by Thomas Eschenbacher
email : Thomas Eschenbacher <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 <QApplication>
#include <QBuffer>
#include <QMutableListIterator>
#include <QVariant>
#include <QWidget>
#include "libkwave/CodecManager.h"
#include "libkwave/CompressionType.h"
#include "libkwave/Decoder.h"
#include "libkwave/Encoder.h"
#include "libkwave/KwaveConnect.h"
#include "libkwave/KwaveMimeData.h"
#include "libkwave/LabelList.h"
#include "libkwave/MultiStreamWriter.h"
#include "libkwave/MultiTrackWriter.h"
#include "libkwave/Sample.h"
#include "libkwave/SampleReader.h"
#include "libkwave/Writer.h"
#include "libkwave/Signal.h"
#include "libkwave/SignalManager.h"
#include "libkwave/MultiTrackReader.h"
#include "libkwave/MultiTrackWriter.h"
#include "libkwave/modules/ChannelMixer.h"
#include "libkwave/modules/RateConverter.h"
// RFC 2361:
#define WAVE_FORMAT_PCM "audio/vnd.wave" // ; codec=001"
//***************************************************************************
Kwave::MimeData::MimeData()
:QMimeData(), m_data()
{
}
//***************************************************************************
Kwave::MimeData::~MimeData()
{
}
//***************************************************************************
bool Kwave::MimeData::encode(QWidget *widget,
MultiTrackReader &src,
const Kwave::MetaDataList &meta_data)
{
// use our default encoder
Encoder *encoder = CodecManager::encoder(WAVE_FORMAT_PCM);
Q_ASSERT(encoder);
if (!encoder) return false;
Q_ASSERT(src.tracks());
if (!src.tracks()) return false;
// set hourglass cursor
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
sample_index_t first = src.first();
sample_index_t last = src.last();
Kwave::MetaDataList new_meta_data = meta_data.selectByRange(first, last);
// create a buffer for the wav data
m_data.resize(0);
QBuffer dst(&m_data);
// move all meta data left, to start at the beginning of the selection
new_meta_data.shiftLeft(first, first, QList<unsigned int>());
// fix the length information in the new file info
// and change to uncompressed mode
FileInfo info(meta_data);
info.set(INF_COMPRESSION, QVariant(AF_COMPRESSION_NONE));
info.setLength(last - first + 1);
info.setTracks(src.tracks());
new_meta_data.replace(info);
// encode into the buffer
encoder->encode(widget, src, dst, new_meta_data);
delete encoder;
// set the mime data into this mime data container
setData(WAVE_FORMAT_PCM, m_data);
// remove hourglass
QApplication::restoreOverrideCursor();
return true;
}
//***************************************************************************
unsigned int Kwave::MimeData::decode(QWidget *widget, const QMimeData *e,
SignalManager &sig, sample_index_t pos)
{
// decode, use the first format that matches
unsigned int decoded_length = 0;
unsigned int decoded_tracks = 0;
// try to find a suitable decoder
foreach (QString format, e->formats()) {
// skip all non-supported formats
if (!CodecManager::canDecode(format)) continue;
Decoder *decoder = CodecManager::decoder(format);
Q_ASSERT(decoder);
if (!decoder) return 0;
QByteArray raw_data = e->data(format);
QBuffer src(&raw_data);
// open the mime source and get header information
bool ok = decoder->open(widget, src);
if (!ok) {
delete decoder;
continue;
}
decoded_length = FileInfo(decoder->metaData()).length();
decoded_tracks = FileInfo(decoder->metaData()).tracks();
Q_ASSERT(decoded_length);
Q_ASSERT(decoded_tracks);
if (!decoded_length || !decoded_tracks) {
delete decoder;
continue;
}
// get sample rates of source and destination
double src_rate = FileInfo(decoder->metaData()).rate();
double dst_rate = sig.rate();
// if the sample rate has to be converted, adjust the length
// right border
if ((src_rate != dst_rate) && (dst_rate > 1) && sig.tracks())
decoded_length *= (dst_rate / src_rate);
sample_index_t left = pos;
sample_index_t right = left + decoded_length - 1;
QList<unsigned int> tracks = sig.selectedTracks();
if (tracks.isEmpty()) tracks = sig.allTracks();
// special case: destination is currently empty
if (!sig.tracks()) {
// encode into an empty window -> create tracks
qDebug("Kwave::MimeData::decode(...) -> new signal");
dst_rate = src_rate;
sig.newSignal(0,
src_rate,
FileInfo(decoder->metaData()).bits(),
decoded_tracks);
ok = (sig.tracks() == decoded_tracks);
if (!ok) {
delete decoder;
continue;
}
}
const unsigned int dst_tracks = sig.selectedTracks().count();
// create the final sink
MultiTrackWriter dst(sig, sig.selectedTracks(), Insert, left, right);
// if the track count does not match, then we need a channel mixer
Q_ASSERT(ok);
Kwave::ChannelMixer *mixer = 0;
if (ok && (decoded_tracks != dst_tracks)) {
qDebug("Kwave::MimeData::decode(...) -> mixing channels: %u -> %u",
decoded_tracks, dst_tracks);
mixer = new Kwave::ChannelMixer(decoded_tracks, dst_tracks);
Q_ASSERT(mixer);
ok &= (mixer) && mixer->init();
Q_ASSERT(ok);
}
Q_ASSERT(ok);
// if the sample rates do not match, then we need a rate converter
Kwave::StreamObject *rate_converter = 0;
if (ok && (src_rate != dst_rate)) {
#ifdef HAVE_SAMPLERATE_SUPPORT
// create a sample rate converter
qDebug("Kwave::MimeData::decode(...) -> rate conversion: "\
"%0.1f -> %0.1f", src_rate, dst_rate);
rate_converter =
new Kwave::MultiTrackSource<Kwave::RateConverter, true>(
dst_tracks, widget);
Q_ASSERT(rate_converter);
if (rate_converter)
rate_converter->setAttribute(SLOT(setRatio(const QVariant)),
QVariant(dst_rate / src_rate));
else
ok = false;
#else
ok = false;
#warning sample rate conversion is disabled
#endif
}
Q_ASSERT(ok);
// set hourglass cursor
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
if (ok && ((rate_converter != 0) || (mixer != 0))) {
// pass all data through a filter chain
Kwave::MultiStreamWriter adapter(decoded_tracks);
// pass the data through a sample rate converter
// decoder -> adapter -> [mixer] -> [converter] -> dst
Kwave::StreamObject *last_output = &adapter;
if (ok && mixer) {
// connect the channel mixer
ok = Kwave::connect(
*last_output, SIGNAL(output(Kwave::SampleArray)),
*mixer, SLOT(input(Kwave::SampleArray))
);
last_output = mixer;
}
if (ok && rate_converter) {
// connect the rate converter
ok = Kwave::connect(
*last_output, SIGNAL(output(Kwave::SampleArray)),
*rate_converter, SLOT(input(Kwave::SampleArray))
);
last_output = rate_converter;
}
// connect the sink
if (ok) {
ok = Kwave::connect(
*last_output, SIGNAL(output(Kwave::SampleArray)),
dst, SLOT(input(Kwave::SampleArray))
);
}
// this also starts the conversion automatically
if (ok)
ok = decoder->decode(widget, adapter);
// flush all samples that are still in the adapter
adapter.flush();
} else if (ok) {
// decode directly without any filter
ok = decoder->decode(widget, dst);
}
dst.flush();
// clean up the filter chain
if (mixer) delete mixer;
if (rate_converter) delete rate_converter;
// remove hourglass
QApplication::restoreOverrideCursor();
// failed :-(
Q_ASSERT(ok);
if (!ok) {
delete decoder;
decoded_length = 0;
continue;
}
// take care of the meta data, shift all it by "left" and
// add it to the signal
Kwave::MetaDataList meta_data = decoder->metaData();
// adjust meta data position in case of different sample rate
if (src_rate != dst_rate)
meta_data.scalePositions(dst_rate / src_rate, tracks);
meta_data.shiftRight(0, left, tracks);
// remove the file info, this must not be handled here, otherwise
// this would overwrite the file info of the destination
meta_data.remove(meta_data.selectByType(FileInfo::metaDataType()));
// add the remaining meta data (e.g. labels etc)
sig.metaData().add(meta_data);
delete decoder;
break;
}
// qDebug("Kwave::MimeData::decode -> decoded_length=%u", decoded_length);
return decoded_length;
}
//***************************************************************************
void Kwave::MimeData::clear()
{
m_data.clear();
}
//***************************************************************************
#include "KwaveMimeData.moc"
//***************************************************************************
//***************************************************************************