2006-07-04 07:10:29 +02:00
|
|
|
// Copyright (c) 2006, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// AEGISUB
|
|
|
|
//
|
|
|
|
// Website: http://aegisub.cellosoft.com
|
|
|
|
// Contact: mailto:zeratul@cellosoft.com
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Headers
|
|
|
|
#include "audio_provider_stream.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
|
2006-07-04 10:25:54 +02:00
|
|
|
#define BUFSIZE 65536
|
2006-07-04 09:00:58 +02:00
|
|
|
|
|
|
|
|
2006-07-04 07:10:29 +02:00
|
|
|
///////////////
|
|
|
|
// Constructor
|
|
|
|
StreamAudioProvider::StreamAudioProvider() {
|
|
|
|
bufLen = 8192;
|
|
|
|
startPos = 0;
|
2006-07-04 09:00:58 +02:00
|
|
|
endPos = BUFSIZE;
|
|
|
|
buffered = 0;
|
|
|
|
hasBuf = false;
|
2006-07-08 00:47:55 +02:00
|
|
|
num_samples = ~0ULL;
|
2006-07-04 07:10:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////
|
|
|
|
// Destructor
|
|
|
|
StreamAudioProvider::~StreamAudioProvider() {
|
|
|
|
for (std::list<BufferChunk*>::iterator cur=buffer.begin();cur!=buffer.end();cur++) {
|
|
|
|
delete *cur;
|
|
|
|
}
|
|
|
|
buffer.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
// Get audio
|
|
|
|
void StreamAudioProvider::GetAudio(void *buf, __int64 start, __int64 count) {
|
|
|
|
// Write
|
|
|
|
__int64 left = count;
|
2006-07-04 10:25:54 +02:00
|
|
|
int written = 0;
|
2006-07-04 07:10:29 +02:00
|
|
|
int toWrite;
|
2006-07-04 10:25:54 +02:00
|
|
|
short *dst = (short*) buf;
|
2006-07-04 09:00:58 +02:00
|
|
|
while (hasBuf && left > 0) {
|
2006-07-04 07:10:29 +02:00
|
|
|
// Discard done
|
2006-07-04 09:00:58 +02:00
|
|
|
if (startPos == BUFSIZE) {
|
2006-07-04 10:25:54 +02:00
|
|
|
delete buffer.front();
|
2006-07-04 07:10:29 +02:00
|
|
|
buffer.pop_front();
|
|
|
|
startPos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is last?
|
|
|
|
bool isLast = buffer.size() == 1;
|
2006-07-04 09:00:58 +02:00
|
|
|
int size = BUFSIZE;
|
2006-07-04 07:10:29 +02:00
|
|
|
if (isLast) size = endPos;
|
|
|
|
|
|
|
|
// Write
|
2006-07-04 10:25:54 +02:00
|
|
|
toWrite = MIN(size-startPos,int(left));
|
|
|
|
memcpy(dst+written,&(buffer.front()->buf[startPos]),toWrite*2);
|
2006-07-04 07:10:29 +02:00
|
|
|
startPos += toWrite;
|
|
|
|
written += toWrite;
|
|
|
|
left -= toWrite;
|
2006-07-04 09:00:58 +02:00
|
|
|
buffered -= toWrite;
|
2006-07-04 07:10:29 +02:00
|
|
|
|
|
|
|
// Last
|
|
|
|
if (isLast) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Still left, fill with zero
|
|
|
|
if (left > 0) {
|
2006-07-04 09:00:58 +02:00
|
|
|
hasBuf = false;
|
2006-07-04 07:10:29 +02:00
|
|
|
for (__int64 i=written;i<count;i++) {
|
|
|
|
dst[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////
|
|
|
|
// Append audio to stream
|
2006-07-04 10:25:54 +02:00
|
|
|
void StreamAudioProvider::Append(void *voidptr, __int64 count) {
|
2006-07-04 07:10:29 +02:00
|
|
|
// Read
|
|
|
|
__int64 left = count;
|
2006-07-04 10:25:54 +02:00
|
|
|
int read = 0;
|
2006-07-04 07:10:29 +02:00
|
|
|
int toRead;
|
2006-07-04 10:25:54 +02:00
|
|
|
short *src = (short*) voidptr;
|
2006-07-04 07:10:29 +02:00
|
|
|
while (left > 0) {
|
|
|
|
// Check space
|
2006-07-04 09:00:58 +02:00
|
|
|
if (endPos == BUFSIZE) {
|
2006-07-04 07:10:29 +02:00
|
|
|
buffer.push_back(new BufferChunk);
|
|
|
|
endPos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read
|
2006-07-04 09:00:58 +02:00
|
|
|
toRead = MIN(int(BUFSIZE-endPos),int(left));
|
2006-07-04 10:25:54 +02:00
|
|
|
memcpy(&(buffer.back()->buf[endPos]),src+read,toRead*2);
|
2006-07-04 07:10:29 +02:00
|
|
|
endPos += toRead;
|
|
|
|
read += toRead;
|
2006-07-04 09:00:58 +02:00
|
|
|
buffered += toRead;
|
2006-07-04 07:10:29 +02:00
|
|
|
left -= toRead;
|
|
|
|
}
|
2006-07-04 09:00:58 +02:00
|
|
|
|
|
|
|
// Set buffered status
|
|
|
|
if (buffered > bufLen) hasBuf = true;
|
2006-07-04 07:10:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-04 08:13:54 +02:00
|
|
|
//////////////////
|
|
|
|
// Set parameters
|
|
|
|
void StreamAudioProvider::SetParams(int chan,int rate,int bps) {
|
|
|
|
channels = chan;
|
|
|
|
sample_rate = rate;
|
|
|
|
bytes_per_sample = bps;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-04 07:10:29 +02:00
|
|
|
////////////////////////////
|
|
|
|
// Buffer chunk constructor
|
|
|
|
StreamAudioProvider::BufferChunk::BufferChunk() {
|
2006-07-04 09:00:58 +02:00
|
|
|
buf.resize(BUFSIZE);
|
2006-07-04 07:10:29 +02:00
|
|
|
isFree = true;
|
|
|
|
}
|