Blender V2.61 - r43446

AUD_DelayReader.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * Copyright 2009-2011 Jörg Hermann Müller
00005  *
00006  * This file is part of AudaSpace.
00007  *
00008  * Audaspace is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * AudaSpace is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Audaspace; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00030 #include "AUD_DelayReader.h"
00031 
00032 #include <cstring>
00033 
00034 AUD_DelayReader::AUD_DelayReader(AUD_Reference<AUD_IReader> reader, float delay) :
00035         AUD_EffectReader(reader),
00036         m_delay(int(delay * reader->getSpecs().rate)),
00037         m_remdelay(int(delay * reader->getSpecs().rate))
00038 {
00039 }
00040 
00041 void AUD_DelayReader::seek(int position)
00042 {
00043     if(position < m_delay)
00044     {
00045         m_remdelay = m_delay - position;
00046         m_reader->seek(0);
00047     }
00048     else
00049     {
00050         m_remdelay = 0;
00051         m_reader->seek(position - m_delay);
00052     }
00053 }
00054 
00055 int AUD_DelayReader::getLength() const
00056 {
00057     int len = m_reader->getLength();
00058     if(len < 0)
00059         return len;
00060     return len + m_delay;
00061 }
00062 
00063 int AUD_DelayReader::getPosition() const
00064 {
00065     if(m_remdelay > 0)
00066         return m_delay - m_remdelay;
00067     return m_reader->getPosition() + m_delay;
00068 }
00069 
00070 void AUD_DelayReader::read(int& length, bool& eos, sample_t* buffer)
00071 {
00072     if(m_remdelay > 0)
00073     {
00074         AUD_Specs specs = m_reader->getSpecs();
00075         int samplesize = AUD_SAMPLE_SIZE(specs);
00076 
00077         if(length > m_remdelay)
00078         {
00079             memset(buffer, 0, m_remdelay * samplesize);
00080 
00081             int len = length - m_remdelay;
00082             m_reader->read(len, eos, buffer + m_remdelay * specs.channels);
00083 
00084             length = m_remdelay + len;
00085 
00086             m_remdelay = 0;
00087         }
00088         else
00089         {
00090             memset(buffer, 0, length * samplesize);
00091             m_remdelay -= length;
00092         }
00093     }
00094     else
00095         m_reader->read(length, eos, buffer);
00096 }