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