2009-08-09 21:16:39 +00:00
|
|
|
/*
|
2011-02-18 23:47:37 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2009-08-09 21:16:39 +00:00
|
|
|
*
|
2011-02-18 23:47:37 +00:00
|
|
|
* Copyright 2009-2011 Jörg Hermann Müller
|
2009-08-09 21:16:39 +00:00
|
|
|
*
|
|
|
|
* This file is part of AudaSpace.
|
|
|
|
*
|
2011-02-18 23:47:37 +00:00
|
|
|
* Audaspace 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
|
2009-08-09 21:16:39 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* AudaSpace 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
|
2011-02-18 23:47:37 +00:00
|
|
|
* GNU General Public License for more details.
|
2009-08-09 21:16:39 +00:00
|
|
|
*
|
2011-02-18 23:47:37 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Audaspace; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-08-09 21:16:39 +00:00
|
|
|
*
|
2011-02-18 23:47:37 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2009-08-09 21:16:39 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-25 10:21:56 +00:00
|
|
|
/** \file audaspace/FX/AUD_ReverseReader.cpp
|
|
|
|
* \ingroup audfx
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-08-09 21:16:39 +00:00
|
|
|
#include "AUD_ReverseReader.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2010-08-03 08:07:21 +00:00
|
|
|
static const char* props_error = "AUD_ReverseReader: The reader has to be "
|
|
|
|
"seekable and a finite length.";
|
|
|
|
|
2012-11-05 14:24:35 +00:00
|
|
|
AUD_ReverseReader::AUD_ReverseReader(boost::shared_ptr<AUD_IReader> reader) :
|
2010-07-28 09:36:03 +00:00
|
|
|
AUD_EffectReader(reader),
|
|
|
|
m_length(reader->getLength()),
|
|
|
|
m_position(0)
|
2009-08-09 21:16:39 +00:00
|
|
|
{
|
2010-07-28 09:36:03 +00:00
|
|
|
if(m_length < 0 || !reader->isSeekable())
|
2010-08-03 08:07:21 +00:00
|
|
|
AUD_THROW(AUD_ERROR_PROPS, props_error);
|
2009-08-09 21:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_ReverseReader::seek(int position)
|
|
|
|
{
|
|
|
|
m_position = position;
|
|
|
|
}
|
|
|
|
|
2010-07-28 09:36:03 +00:00
|
|
|
int AUD_ReverseReader::getLength() const
|
2009-08-09 21:16:39 +00:00
|
|
|
{
|
|
|
|
return m_length;
|
|
|
|
}
|
|
|
|
|
2010-07-28 09:36:03 +00:00
|
|
|
int AUD_ReverseReader::getPosition() const
|
2009-08-09 21:16:39 +00:00
|
|
|
{
|
|
|
|
return m_position;
|
|
|
|
}
|
|
|
|
|
2011-06-21 20:14:53 +00:00
|
|
|
void AUD_ReverseReader::read(int& length, bool& eos, sample_t* buffer)
|
2009-08-09 21:16:39 +00:00
|
|
|
{
|
|
|
|
// first correct the length
|
2010-01-01 05:09:30 +00:00
|
|
|
if(m_position + length > m_length)
|
2010-07-28 09:36:03 +00:00
|
|
|
length = m_length - m_position;
|
2009-08-09 21:16:39 +00:00
|
|
|
|
|
|
|
if(length <= 0)
|
|
|
|
{
|
|
|
|
length = 0;
|
2011-06-21 20:14:53 +00:00
|
|
|
eos = true;
|
2009-08-09 21:16:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-14 12:13:19 +00:00
|
|
|
const AUD_Specs specs = getSpecs();
|
|
|
|
const int samplesize = AUD_SAMPLE_SIZE(specs);
|
2009-08-09 21:16:39 +00:00
|
|
|
|
2011-06-21 20:25:48 +00:00
|
|
|
sample_t temp[AUD_CHANNEL_MAX];
|
2009-08-09 21:16:39 +00:00
|
|
|
|
|
|
|
int len = length;
|
|
|
|
|
|
|
|
// read from reader
|
2010-01-01 05:09:30 +00:00
|
|
|
m_reader->seek(m_length - m_position - len);
|
2011-06-21 20:14:53 +00:00
|
|
|
m_reader->read(len, eos, buffer);
|
2009-08-09 21:16:39 +00:00
|
|
|
|
|
|
|
// set null if reader didn't give enough data
|
|
|
|
if(len < length)
|
2010-01-01 05:09:30 +00:00
|
|
|
memset(buffer, 0, (length - len) * samplesize);
|
2009-08-09 21:16:39 +00:00
|
|
|
|
|
|
|
// copy the samples reverted
|
2011-06-14 12:13:19 +00:00
|
|
|
for(int i = 0; i < length / 2; i++)
|
|
|
|
{
|
|
|
|
memcpy(temp,
|
|
|
|
buffer + (len - 1 - i) * specs.channels,
|
|
|
|
samplesize);
|
|
|
|
memcpy(buffer + (len - 1 - i) * specs.channels,
|
|
|
|
buffer + i * specs.channels,
|
|
|
|
samplesize);
|
2010-01-01 05:09:30 +00:00
|
|
|
memcpy(buffer + i * specs.channels,
|
2011-06-14 12:13:19 +00:00
|
|
|
temp,
|
2009-08-09 21:16:39 +00:00
|
|
|
samplesize);
|
2011-06-14 12:13:19 +00:00
|
|
|
}
|
2009-08-09 21:16:39 +00:00
|
|
|
|
|
|
|
m_position += length;
|
2011-06-21 20:14:53 +00:00
|
|
|
eos = false;
|
2009-08-09 21:16:39 +00:00
|
|
|
}
|