2006-02-25 09:26:29 +01:00
|
|
|
// Copyright (c) 2005-2006, Rodrigo Braz Monteiro, Fredrik Mellbin
|
|
|
|
// 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.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2006-02-25 09:26:29 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file audio_provider_ram.cpp
|
|
|
|
/// @brief Caching audio provider using heap memory for backing
|
|
|
|
/// @ingroup audio_input
|
|
|
|
///
|
2006-02-25 09:26:29 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-25 09:26:29 +01:00
|
|
|
#include "audio_provider_ram.h"
|
2011-09-28 21:49:37 +02:00
|
|
|
|
2012-01-08 02:33:39 +01:00
|
|
|
#include "audio_controller.h"
|
2011-09-28 21:49:37 +02:00
|
|
|
#include "compat.h"
|
2008-10-29 01:18:13 +01:00
|
|
|
#include "main.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "utils.h"
|
2006-02-25 09:26:29 +01:00
|
|
|
|
2012-03-25 06:05:38 +02:00
|
|
|
#include <libaegisub/background_runner.h>
|
|
|
|
#include <libaegisub/scoped_ptr.h>
|
|
|
|
|
2006-02-25 09:26:29 +01:00
|
|
|
#define CacheBits ((22))
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2006-02-25 09:26:29 +01:00
|
|
|
#define CacheBlockSize ((1 << CacheBits))
|
|
|
|
|
2011-09-28 21:49:37 +02:00
|
|
|
RAMAudioProvider::RAMAudioProvider(AudioProvider *src, agi::BackgroundRunner *br) {
|
2012-03-25 06:05:38 +02:00
|
|
|
agi::scoped_ptr<AudioProvider> source(src);
|
2011-09-28 21:47:40 +02:00
|
|
|
|
2009-06-05 01:02:29 +02:00
|
|
|
samples_native_endian = source->AreSamplesNativeEndian();
|
2006-02-25 09:26:29 +01:00
|
|
|
|
|
|
|
// Allocate cache
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t ssize = source->GetNumSamples() * source->GetBytesPerSample();
|
2006-02-25 09:26:29 +01:00
|
|
|
blockcount = (ssize + CacheBlockSize - 1) >> CacheBits;
|
|
|
|
blockcache = new char*[blockcount];
|
2011-09-28 21:49:37 +02:00
|
|
|
memset(blockcache, 0, blockcount * sizeof(char*));
|
2006-02-25 09:26:29 +01:00
|
|
|
|
|
|
|
// Allocate cache blocks
|
|
|
|
try {
|
|
|
|
for (int i = 0; i < blockcount; i++) {
|
2011-09-28 21:47:40 +02:00
|
|
|
blockcache[i] = new char[std::min<size_t>(CacheBlockSize, ssize - i * CacheBlockSize)];
|
2006-02-25 09:26:29 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-28 21:47:40 +02:00
|
|
|
catch (std::bad_alloc const&) {
|
2006-02-25 09:26:29 +01:00
|
|
|
Clear();
|
2012-01-08 02:33:39 +01:00
|
|
|
throw agi::AudioCacheOpenError("Couldn't open audio, not enough ram available.", 0);
|
2006-02-25 09:26:29 +01:00
|
|
|
}
|
|
|
|
|
2006-02-25 09:39:15 +01:00
|
|
|
// Copy parameters
|
2006-02-25 09:26:29 +01:00
|
|
|
bytes_per_sample = source->GetBytesPerSample();
|
|
|
|
num_samples = source->GetNumSamples();
|
|
|
|
channels = source->GetChannels();
|
|
|
|
sample_rate = source->GetSampleRate();
|
|
|
|
filename = source->GetFilename();
|
|
|
|
|
2011-09-28 21:49:37 +02:00
|
|
|
br->Run(std::tr1::bind(&RAMAudioProvider::FillCache, this, src, std::tr1::placeholders::_1));
|
2006-02-25 09:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RAMAudioProvider::~RAMAudioProvider() {
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
void RAMAudioProvider::FillCache(AudioProvider *source, agi::ProgressSink *ps) {
|
2011-09-28 21:49:37 +02:00
|
|
|
ps->SetMessage(STD_STR(_("Reading into RAM")));
|
|
|
|
|
2011-09-28 21:47:40 +02:00
|
|
|
int64_t readsize = CacheBlockSize / source->GetBytesPerSample();
|
|
|
|
for (int i = 0; i < blockcount; i++) {
|
|
|
|
source->GetAudio((char*)blockcache[i], i * readsize, std::min(readsize, num_samples - i * readsize));
|
|
|
|
|
|
|
|
ps->SetProgress(i, (blockcount - 1));
|
|
|
|
if (ps->IsCancelled()) {
|
|
|
|
Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-25 09:26:29 +01:00
|
|
|
void RAMAudioProvider::Clear() {
|
|
|
|
// Free ram cache
|
|
|
|
if (blockcache) {
|
|
|
|
for (int i = 0; i < blockcount; i++) {
|
|
|
|
delete [] blockcache[i];
|
|
|
|
}
|
|
|
|
delete [] blockcache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
void RAMAudioProvider::GetAudio(void *buf, int64_t start, int64_t count) const {
|
2006-02-25 09:26:29 +01:00
|
|
|
// Requested beyond the length of audio
|
|
|
|
if (start+count > num_samples) {
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t oldcount = count;
|
2006-02-25 09:26:29 +01:00
|
|
|
count = num_samples-start;
|
|
|
|
if (count < 0) count = 0;
|
|
|
|
|
|
|
|
// Fill beyond with zero
|
|
|
|
if (bytes_per_sample == 1) {
|
|
|
|
char *temp = (char *) buf;
|
|
|
|
for (int i=count;i<oldcount;i++) {
|
|
|
|
temp[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bytes_per_sample == 2) {
|
|
|
|
short *temp = (short *) buf;
|
|
|
|
for (int i=count;i<oldcount;i++) {
|
|
|
|
temp[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
// Prepare copy
|
|
|
|
char *charbuf = (char *)buf;
|
|
|
|
int i = (start*bytes_per_sample) >> CacheBits;
|
|
|
|
int start_offset = (start*bytes_per_sample) & (CacheBlockSize-1);
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t bytesremaining = count*bytes_per_sample;
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2006-02-25 09:26:29 +01:00
|
|
|
// Copy
|
|
|
|
while (bytesremaining) {
|
2012-03-25 06:05:06 +02:00
|
|
|
int readsize = std::min<int>(bytesremaining, CacheBlockSize - start_offset);
|
2006-02-25 09:26:29 +01:00
|
|
|
|
|
|
|
memcpy(charbuf,(char *)(blockcache[i++]+start_offset),readsize);
|
|
|
|
|
|
|
|
charbuf+=readsize;
|
|
|
|
|
|
|
|
start_offset=0;
|
|
|
|
bytesremaining-=readsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|