Blender V2.61 - r43446

AUD_FaderReader.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_FaderReader.h"
00031 
00032 #include <cstring>
00033 
00034 AUD_FaderReader::AUD_FaderReader(AUD_Reference<AUD_IReader> reader, AUD_FadeType type,
00035                                  float start,float length) :
00036         AUD_EffectReader(reader),
00037         m_type(type),
00038         m_start(start),
00039         m_length(length)
00040 {
00041 }
00042 
00043 void AUD_FaderReader::read(int& length, bool& eos, sample_t* buffer)
00044 {
00045     int position = m_reader->getPosition();
00046     AUD_Specs specs = m_reader->getSpecs();
00047     int samplesize = AUD_SAMPLE_SIZE(specs);
00048 
00049     m_reader->read(length, eos, buffer);
00050 
00051     if((position + length) / (float)specs.rate <= m_start)
00052     {
00053         if(m_type != AUD_FADE_OUT)
00054         {
00055             memset(buffer, 0, length * samplesize);
00056         }
00057     }
00058     else if(position / (float)specs.rate >= m_start+m_length)
00059     {
00060         if(m_type == AUD_FADE_OUT)
00061         {
00062             memset(buffer, 0, length * samplesize);
00063         }
00064     }
00065     else
00066     {
00067         float volume = 1.0f;
00068 
00069         for(int i = 0; i < length * specs.channels; i++)
00070         {
00071             if(i % specs.channels == 0)
00072             {
00073                 volume = (((position+i)/(float)specs.rate)-m_start) / m_length;
00074                 if(volume > 1.0f)
00075                     volume = 1.0f;
00076                 else if(volume < 0.0f)
00077                     volume = 0.0f;
00078 
00079                 if(m_type == AUD_FADE_OUT)
00080                     volume = 1.0f - volume;
00081             }
00082 
00083             buffer[i] = buffer[i] * volume;
00084         }
00085     }
00086 }