19 #include <ucommon/ucommon.h>
20 #include <ucommon/export.h>
24 using namespace UCOMMON_NAMESPACE;
26 AudioStream::AudioStream() : AudioFile()
33 encSize = decSize = 0;
37 AudioStream::AudioStream(
const char *fname, mode_t m, timeout_t framing) :
45 open(fname, m, framing);
48 AudioStream::AudioStream(
const char *fname, info_t& info, timeout_t framing) :
56 create(fname, info, framing);
59 AudioStream::~AudioStream()
65 ssize_t AudioStream::getBuffer(encoded_t data,
size_t request)
68 return getPacket(data);
70 return AudioFile::getBuffer(data, request);
73 ssize_t AudioStream::getPacket(encoded_t data)
79 return AudioFile::getBuffer(data, 0);
83 count = codec->getEstimated();
85 status = AudioFile::getBuffer(framebuf, count);
86 if(count && (
size_t)status != count)
89 status = codec->getPacket(data, framebuf, status);
90 if(status == Audio::ndata)
100 bool AudioStream::is_streamable(
void)
111 void AudioStream::flush(
void)
119 for(fpos = bufferPosition; fpos < getCount() * bufferChannels; ++fpos)
120 bufferFrame[fpos] = 0;
121 if(bufferChannels == 1)
122 putMono(bufferFrame, 1);
124 putStereo(bufferFrame, 1);
127 delete[] bufferFrame;
132 void AudioStream::close(
void)
137 AudioCodec::release(codec);
148 encSize = decSize = 0;
149 encBuffer = decBuffer = NULL;
155 void AudioStream::create(
const char *fname, info_t& info, timeout_t framing)
161 AudioFile::create(fname, info, framing);
167 if(is_linear(AudioFile::info.encoding))
170 codec = AudioCodec::get(AudioFile::info);
175 framebuf =
new unsigned char[maxFramesize(AudioFile::info)];
178 void AudioStream::open(
const char *fname, mode_t m, timeout_t framing)
184 AudioFile::open(fname, m, framing);
190 if(is_linear(info.encoding))
193 codec = AudioCodec::get(info);
197 framebuf =
new unsigned char[maxFramesize(info)];
200 unsigned AudioStream::getCount(
void)
205 return info.framecount;
208 unsigned AudioStream::getMono(linear_t buffer,
unsigned frames)
210 unsigned char *iobuf = (
unsigned char *)buffer;
211 unsigned count, offset, copied = 0;
213 linear_t dbuf = NULL;
221 count = frames * getCount();
223 if(is_stereo(info.encoding))
224 dbuf =
new sample_t[count * 2];
228 iobuf = (
unsigned char *)dbuf;
231 len = AudioFile::getBuffer(iobuf);
232 if(len < (ssize_t)info.framesize)
236 codec->decode(buffer, iobuf, info.framecount);
241 swapEndian(info, dbuf, info.framecount);
243 swapEndian(info, buffer, info.framecount);
247 buffer += info.framecount;
251 for(offset = 0; offset < info.framecount; ++offset)
253 dbuf[offset * 2] / 2 + dbuf[offset * 2 + 1] / 2;
255 buffer += info.framecount;
264 unsigned AudioStream::getStereo(linear_t buffer,
unsigned frames)
266 unsigned char *iobuf = (
unsigned char *)buffer;
267 unsigned offset, copied = 0;
280 len = AudioFile::getBuffer(iobuf);
281 if(len < (ssize_t)info.framesize)
286 codec->decode(buffer, iobuf, info.framecount);
289 swapEndian(info, buffer, info.framecount);
292 if(is_stereo(info.encoding)) {
293 buffer += (info.framecount * 2);
296 offset = info.framecount;
298 buffer[offset * 2] = buffer[offset];
299 buffer[offset * 2 + 1] = buffer[offset];
301 buffer += (info.framecount * 2);
306 unsigned AudioStream::putMono(linear_t buffer,
unsigned frames)
308 linear_t iobuf = buffer, dbuf = NULL;
309 unsigned offset, copied = 0;
318 if(is_stereo(info.encoding)) {
319 dbuf =
new sample_t[info.framecount * 2];
325 for(offset = 0; offset < info.framecount; ++offset)
326 dbuf[offset * 2] = dbuf[offset * 2 + 1] = buffer[offset];
330 codec->encode(iobuf, framebuf, info.framecount);
331 len = putBuffer(framebuf);
332 if(len < (ssize_t)info.framesize)
335 buffer += info.framecount;
338 swapEndian(info, iobuf, info.framecount);
339 len = putBuffer((encoded_t)iobuf);
340 if(len < (ssize_t)info.framesize)
343 buffer += info.framecount;
351 unsigned AudioStream::putStereo(linear_t buffer,
unsigned frames)
353 linear_t iobuf = buffer, mbuf = NULL;
354 unsigned offset, copied = 0;
363 if(!is_stereo(info.encoding)) {
364 mbuf =
new sample_t[info.framecount];
370 for(offset = 0; offset < info.framecount; ++offset)
371 mbuf[offset] = buffer[offset * 2] / 2 + buffer[offset * 2 + 1] / 2;
375 codec->encode(iobuf, framebuf, info.framecount);
376 len = putBuffer(framebuf);
377 if(len < (ssize_t)info.framesize)
380 buffer += info.framecount;
383 swapEndian(info, iobuf, info.framecount);
384 len = putBuffer((encoded_t)iobuf);
385 if(len < (ssize_t)info.framesize)
395 unsigned AudioStream::bufMono(linear_t samples,
unsigned count)
397 unsigned size = getCount();
399 if(bufferChannels != 1)
403 bufferFrame =
new sample_t[size];
407 return bufAudio(samples, count, size);
410 unsigned AudioStream::bufStereo(linear_t samples,
unsigned count)
412 unsigned size = getCount() * 2;
414 if(bufferChannels != 2)
418 bufferFrame =
new sample_t[size];
422 return bufAudio(samples, count * 2, size);
425 unsigned AudioStream::bufAudio(linear_t samples,
unsigned count,
unsigned size)
428 unsigned frames = 0, copy, result;
431 fill = size - bufferPosition;
432 else if(count < size)
439 memcpy(&bufferFrame[bufferPosition], samples, fill * 2);
440 bufferPosition += fill;
445 if(bufferPosition == size) {
446 if(bufferChannels == 1)
447 frames = putMono(bufferFrame, 1);
449 frames = putStereo(bufferFrame, 1);
459 copy = (count / size);
460 if(bufferChannels == 1)
461 result = putMono(samples, copy);
463 result = putStereo(samples, copy);
466 return frames + result;
468 samples += copy * size;
469 count -= copy * size;
473 memcpy(bufferFrame, samples, count * 2);
474 bufferPosition = count;
479 unsigned AudioStream::getEncoded(encoded_t addr,
unsigned frames)
481 unsigned count = 0, len;
483 if(is_linear(info.encoding))
484 return getMono((linear_t)addr, frames);
487 len = AudioFile::getBuffer(addr);
488 if(len < info.framesize)
490 addr += info.framesize;
496 unsigned AudioStream::putEncoded(encoded_t addr,
unsigned frames)
501 if(is_linear(info.encoding))
502 return putMono((linear_t)addr, frames);
505 len = putBuffer(addr);
506 if(len < (ssize_t)info.framesize)
508 addr += info.framesize;
514 unsigned AudioStream::getEncoded(
AudioCodec *codec, encoded_t addr,
unsigned frames)
518 unsigned bufsize = 0;
523 return getEncoded(addr, frames);
525 ci = codec->getInfo();
527 if(ci.encoding == info.encoding && ci.framecount == info.framecount)
528 return getEncoded(addr, frames);
533 while(bufsize < ci.framesize)
534 bufsize += info.framesize;
536 if(encSize != bufsize) {
540 encBuffer =
new sample_t[bufsize];
544 while(count < frames && !eof) {
545 while(used < ci.framesize) {
546 if(getMono(encBuffer + used, 1) < 1) {
550 used += info.framesize;
552 codec->encode(encBuffer, addr, ci.framesize);
553 if(ci.framesize < used)
554 memcpy(encBuffer, encBuffer + ci.framesize, used - ci.framesize);
555 used -= ci.framesize;
560 unsigned AudioStream::putEncoded(
AudioCodec *codec, encoded_t addr,
unsigned frames)
566 return putEncoded(addr, frames);
568 ci = codec->getInfo();
569 if(ci.encoding == info.encoding && ci.framecount == info.framecount)
570 return putEncoded(addr, frames);
575 if(ci.framecount != decSize) {
578 decBuffer =
new sample_t[ci.framecount];
579 decSize = ci.framecount;
582 while(count < frames) {
583 codec->decode(decBuffer, addr, ci.framecount);
584 if(bufMono(decBuffer, ci.framecount) < ci.framecount)
587 addr += ci.framesize;
#define BAYONNE_NAMESPACE
GNU Bayonne library namespace.