Blender  V2.59
AUD_FaderReader.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: AUD_FaderReader.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_FaderReader.h"
00033 
00034 #include <cstring>
00035 
00036 AUD_FaderReader::AUD_FaderReader(AUD_IReader* reader, AUD_FadeType type,
00037                                                                  float start,float length) :
00038                 AUD_EffectReader(reader),
00039                 m_type(type),
00040                 m_start(start),
00041                 m_length(length),
00042                 m_empty(true)
00043 {
00044 }
00045 
00046 void AUD_FaderReader::read(int & length, sample_t* & buffer)
00047 {
00048         int position = m_reader->getPosition();
00049         AUD_Specs specs = m_reader->getSpecs();
00050         int samplesize = AUD_SAMPLE_SIZE(specs);
00051 
00052         m_reader->read(length, buffer);
00053 
00054         if((position + length) / (float)specs.rate <= m_start)
00055         {
00056                 if(m_type != AUD_FADE_OUT)
00057                 {
00058                         if(m_buffer.getSize() < length * samplesize)
00059                         {
00060                                 m_buffer.resize(length * samplesize);
00061                                 m_empty = false;
00062                         }
00063 
00064                         buffer = m_buffer.getBuffer();
00065 
00066                         if(!m_empty)
00067                         {
00068                                 memset(buffer, 0, length * samplesize);
00069                                 m_empty = true;
00070                         }
00071                 }
00072         }
00073         else if(position / (float)specs.rate >= m_start+m_length)
00074         {
00075                 if(m_type == AUD_FADE_OUT)
00076                 {
00077                         if(m_buffer.getSize() < length * samplesize)
00078                         {
00079                                 m_buffer.resize(length * samplesize);
00080                                 m_empty = false;
00081                         }
00082 
00083                         buffer = m_buffer.getBuffer();
00084 
00085                         if(!m_empty)
00086                         {
00087                                 memset(buffer, 0, length * samplesize);
00088                                 m_empty = true;
00089                         }
00090                 }
00091         }
00092         else
00093         {
00094                 if(m_buffer.getSize() < length * samplesize)
00095                         m_buffer.resize(length * samplesize);
00096 
00097                 sample_t* buf = m_buffer.getBuffer();
00098                 float volume = 1.0f;
00099 
00100                 for(int i = 0; i < length * specs.channels; i++)
00101                 {
00102                         if(i % specs.channels == 0)
00103                         {
00104                                 volume = (((position+i)/(float)specs.rate)-m_start) / m_length;
00105                                 if(volume > 1.0f)
00106                                         volume = 1.0f;
00107                                 else if(volume < 0.0f)
00108                                         volume = 0.0f;
00109 
00110                                 if(m_type == AUD_FADE_OUT)
00111                                         volume = 1.0f - volume;
00112                         }
00113 
00114                         buf[i] = buffer[i] * volume;
00115                 }
00116 
00117                 buffer = buf;
00118                 m_empty = false;
00119         }
00120 }