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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
/* akodePlayObject
Copyright (C) 2003-2005 Allan Sandfeld Jensen <kde@carewolf.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
//#define AKODEARTS_SINGLETHREADED
//#define AKODEARTS_SRCRESAMPLING
#define AKODEARTS_FRAMEBUFFER 32
// The instream-buffer must be smaller than 64kbytes (1<<16)
#define AKODEARTS_INSTREAMBUFFER (1<<14)
#include <math.h>
#include <soundserver.h>
#include <audiosubsys.h>
#include <debug.h>
#include "config.h"
#include <akode/audioframe.h>
#include <akode/localfile.h>
#include <akode/mmapfile.h>
#include <akode/decoder.h>
#include <akode/resampler.h>
#include <akode/fast_resampler.h>
#include <akode/wav_decoder.h>
#include "arts_inputstream.h"
#ifndef AKODEARTS_SINGLETHREADED
#include <akode/buffered_decoder.h>
#endif
#ifdef AKODEARTS_SRCRESAMPLING
#define AKODEARTS_RESAMPLER "src"
#else
#define AKODEARTS_RESAMPLER "fast"
#endif
#include "akodePlayObject_impl.h"
using namespace Arts;
using namespace aKode;
akodePlayObject_impl::akodePlayObject_impl(const string &plugin)
: source(0)
, frameDecoder(0)
, decoder(0)
, bufferedDecoder(0)
, resampler(0)
, buffer(0)
, inBuffer(0)
, buf_pos(0)
, mState(posIdle)
, mSpeed(1.0)
, m_packetQueue(0)
, m_bytebuffer(0)
, m_fading(false)
, decoderPlugin(plugin)
, resamplerPlugin(AKODEARTS_RESAMPLER)
{
m_packetQueue = new queue<DataPacket<mcopbyte>*>;
if(!resamplerPlugin.isLoaded())
resamplerPlugin.load("fast");
}
akodePlayObject_impl::~akodePlayObject_impl()
{
delete m_packetQueue;
unload();
}
bool akodePlayObject_impl::loadPlugin(const string &plugin)
{
return decoderPlugin.load(plugin);
}
bool akodePlayObject_impl::streamMedia(Arts::InputStream inputstream)
{
arts_debug("akode: opening input-stream");
m_bytebuffer = new aKode::ByteBuffer(AKODEARTS_INSTREAMBUFFER);
instream = inputstream;
Arts::StreamPlayObject self = Arts::StreamPlayObject::_from_base(_copy());
connect(instream, "outdata", self, "indata");
source = new Arts_InputStream(instream, m_bytebuffer);
return loadSource();
}
bool akodePlayObject_impl::loadMedia(const string &filename)
{
arts_debug("akode: opening %s", filename.c_str());
source = new MMapFile(filename.c_str());
if (!source->openRO()) {
delete source;
source = new LocalFile(filename.c_str());
if (!source->openRO()) {
delete source;
source = 0;
return false;
}
}
source->close();
return loadSource();
}
bool akodePlayObject_impl::loadSource()
{
if (!decoderPlugin.isLoaded()) {
return false;
}
frameDecoder = decoderPlugin.openDecoder(source);
if (!frameDecoder) {
delete source;
source = 0;
arts_warning("akode: Could not open frame-decoder");
return false;
}
#ifndef AKODEARTS_SINGLETHREADED
bufferedDecoder = new BufferedDecoder();
bufferedDecoder->setBufferSize(AKODEARTS_FRAMEBUFFER);
bufferedDecoder->openDecoder(frameDecoder);
decoder = bufferedDecoder;
#else
decoder = frameDecoder;
#endif
return true;
}
string akodePlayObject_impl::description()
{
return "akodePlayObject";
}
Arts::InputStream akodePlayObject_impl::inputStream() {
return instream;
}
string akodePlayObject_impl::mediaName()
{
if (source)
return source->filename;
else
return string();
}
poCapabilities akodePlayObject_impl::capabilities()
{
return (poCapabilities)(capPause | capSeek);
}
poState akodePlayObject_impl::state()
{
return mState;
}
void akodePlayObject_impl::play()
{
arts_debug("akode: play");
if (!decoder) {
arts_warning("akode: No media loaded");
return;
}
if (mState == posIdle) {
mState = posPlaying;
if (!inBuffer) inBuffer = new AudioFrame;
if (!buffer) buffer = inBuffer;
buf_pos = 0;
} else
mState = posPlaying;
}
void akodePlayObject_impl::pause()
{
arts_debug("akode: pause");
mState = posPaused;
}
void akodePlayObject_impl::halt()
{
arts_debug("akode: halt");
if (mState == posIdle) return;
mState = posIdle;
unload();
}
void akodePlayObject_impl::unload()
{
arts_debug("akode: unload");
if (m_bytebuffer) m_bytebuffer->release();
#ifndef AKODEARTS_SINGLETHREADED
if (bufferedDecoder) {
bufferedDecoder->stop();
bufferedDecoder->closeDecoder();
delete bufferedDecoder;
bufferedDecoder = 0;
}
#endif
delete frameDecoder;
frameDecoder = 0;
decoder = 0;
if (buffer != inBuffer)
delete inBuffer;
delete buffer;
inBuffer = buffer = 0;
buf_pos = 0;
delete resampler;
resampler = 0;
delete source;
source = 0;
#ifndef AKODEARTS_SINGLETHREADED
delete m_bytebuffer;
m_bytebuffer = 0;
#endif
}
poTime akodePlayObject_impl::currentTime()
{
poTime time;
long pos;
if (decoder) {
pos = decoder->position(); // decoder time
if (pos < 0 ) pos = 0;
else
if (samplingRate > 0 && buffer)
{
float lpos = (float)(buf_pos-buffer->length) / (float)samplingRate; // local time
pos += (long)(lpos*1000.0);
}
}
else
pos = 0;
time.seconds = pos / 1000 ;
time.ms = pos % 1000;
return time;
}
poTime akodePlayObject_impl::overallTime()
{
poTime time;
long len;
if (decoder) {
len = decoder->length();
if (len < 0 ) len = 0;
}
else
len = 0;
time.seconds = len / 1000;
time.ms = len % 1000;
return time;
}
void akodePlayObject_impl::seek(const poTime &time)
{
arts_debug("akode: seek");
if (!decoder) {
arts_warning("akode: No media loaded");
return;
}
long akode_pos = time.seconds*1000+time.ms;
if (decoder->seek(akode_pos) && buffer) {
buffer->length = 0; // force re-read
buf_pos = 0;
}
}
bool akodePlayObject_impl::readFrame()
{
arts_debug("akode: readFrame");
if (!inBuffer || !decoder) return false;
if (m_bytebuffer) processQueue();
if(!decoder->readFrame(inBuffer)) {
if (decoder->eof()) {
arts_debug("akode: eof");
halt();
} else
if (decoder->error()) {
arts_debug("akode: error");
halt();
} else
buffer->length=0;
return false;
}
// Invalid frame from broken plugin
if (inBuffer->sample_rate == 0) return false;
if (samplingRate != inBuffer->sample_rate || mSpeed != 1.0) {
//arts_debug("akode: resampling to %d/%d", inBuffer->sample_rate, samplingRate);
if ( !buffer || buffer==inBuffer ) buffer = new AudioFrame;
if ( !resampler)
resampler = resamplerPlugin.openResampler();
resampler->setSampleRate(samplingRate);
resampler->setSpeed(mSpeed);
resampler->doFrame(inBuffer, buffer);
} else {
if ( buffer !=inBuffer) delete buffer;
buffer = inBuffer;
}
buf_pos = 0;
return true;
}
bool akodePlayObject_impl::eof()
{
if (!decoder || !buffer) return true;
else
return buf_pos>=buffer->length && decoder->eof();
}
// GCC's lack of good template support means this is easyist done using DEFINE
#define SEND_BLOCK(T) \
T* data = (T*)buffer->data[0]; \
j = i; bp = buf_pos; \
while (bp < buffer->length && j<count) { \
left[j] = data[bp]*scale; \
j++; bp++; \
} \
if (buffer->channels > 1) \
data = (T*)buffer->data[1]; \
j = i; bp = buf_pos; \
while (bp < buffer->length && j<count) { \
right[j] = data[bp]*scale; \
j++; bp++; \
}
void akodePlayObject_impl::calculateBlock(unsigned long cnt)
{
long count = (long)cnt;
// arts_debug("akode: calculateBlock");
long i = 0, j, bp;
if (!decoder) {
arts_warning("akode: No media loaded");
goto fill_out;
}
if (!buffer)
// Not playing yet
goto fill_out;
while ((mState == posPlaying || m_fading) && i<count) {
if (buf_pos >= buffer->length) {
buf_pos = 0;
if (!readFrame()) {
break;
}
}
if (buffer->channels > 2 || buffer->sample_width > 24 || buffer->sample_width == 0) {
arts_warning("akode: Incompatible media");
halt();
break;
}
signed char width = buffer->sample_width;
float scale = (float)(1<<(width-1));
if (width >= 0) {
scale = 1/scale;
if (width <= 8) {
SEND_BLOCK(int8_t);
i=j;
buf_pos=bp;
}
else if (width <= 16) {
SEND_BLOCK(int16_t);
i=j;
buf_pos=bp;
}
else {
SEND_BLOCK(int32_t);
i=j;
buf_pos=bp;
}
}
else {
scale = 1.0;
SEND_BLOCK(float);
i=j;
buf_pos=bp;
}
}
// fill-out the rest with silence
fill_out:
for (; i < count; i++) {
left[i] = right[i] = 0;
}
}
void akodePlayObject_impl::streamInit()
{
arts_debug("akode: streamInit");
}
void akodePlayObject_impl::streamStart()
{
arts_debug("akode: streamStart");
#ifndef AKODEARTS_SINGLETHREADED
bufferedDecoder->start();
#endif
}
void akodePlayObject_impl::streamEnd()
{
arts_debug("akode: streamEnd");
mState = posIdle;
if (decoder) unload();
}
void akodePlayObject_impl::speed(float newValue)
{
mSpeed = newValue;
}
float akodePlayObject_impl::speed()
{
return mSpeed;
}
void akodePlayObject_impl::process_indata(DataPacket<mcopbyte> *inpacket) {
arts_debug("akode: process_indata");
m_packetQueue->push(inpacket);
if (!m_bytebuffer) return;
processQueue();
}
void akodePlayObject_impl::processQueue()
{
while (!m_packetQueue->empty()) {
long freespace = m_bytebuffer->space();
DataPacket<mcopbyte> *inpacket = m_packetQueue->front();
if (!inpacket) return;
if (freespace >= inpacket->size) {
if (m_bytebuffer->write((char*)inpacket->contents, inpacket->size, false)) {
m_packetQueue->pop();
inpacket->processed();
}
} else
return;
}
if (instream.eof()) m_bytebuffer->close();
}
REGISTER_IMPLEMENTATION(akodePlayObject_impl);
|