|
Blender
V2.59
|
00001 /* 00002 * $Id: AUD_LinearResampleReader.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_LinearResampleReader.h" 00033 00034 #include <cmath> 00035 #include <cstring> 00036 00037 #define CC channels + channel 00038 00039 AUD_LinearResampleReader::AUD_LinearResampleReader(AUD_IReader* reader, 00040 AUD_Specs specs) : 00041 AUD_EffectReader(reader), 00042 m_sspecs(reader->getSpecs()), 00043 m_factor(float(specs.rate) / float(m_sspecs.rate)), 00044 m_tspecs(specs), 00045 m_position(0), 00046 m_sposition(0) 00047 { 00048 m_tspecs.channels = m_sspecs.channels; 00049 m_cache.resize(2 * AUD_SAMPLE_SIZE(m_tspecs)); 00050 } 00051 00052 void AUD_LinearResampleReader::seek(int position) 00053 { 00054 m_position = position; 00055 m_sposition = floor(position / m_factor); 00056 m_reader->seek(m_sposition); 00057 } 00058 00059 int AUD_LinearResampleReader::getLength() const 00060 { 00061 return m_reader->getLength() * m_factor; 00062 } 00063 00064 int AUD_LinearResampleReader::getPosition() const 00065 { 00066 return m_position; 00067 } 00068 00069 AUD_Specs AUD_LinearResampleReader::getSpecs() const 00070 { 00071 return m_tspecs; 00072 } 00073 00074 void AUD_LinearResampleReader::read(int & length, sample_t* & buffer) 00075 { 00076 int samplesize = AUD_SAMPLE_SIZE(m_tspecs); 00077 int size = length * samplesize; 00078 00079 if(m_buffer.getSize() < size) 00080 m_buffer.resize(size); 00081 00082 int need = ceil((m_position + length) / m_factor) + 1 - m_sposition; 00083 int len = need; 00084 sample_t* buf; 00085 buffer = m_buffer.getBuffer(); 00086 00087 m_reader->read(len, buf); 00088 00089 if(len < need) 00090 length = floor((m_sposition + len - 1) * m_factor) - m_position; 00091 00092 float spos; 00093 sample_t low, high; 00094 int channels = m_sspecs.channels; 00095 00096 for(int channel = 0; channel < channels; channel++) 00097 { 00098 for(int i = 0; i < length; i++) 00099 { 00100 spos = (m_position + i) / m_factor - m_sposition; 00101 00102 if(floor(spos) < 0) 00103 { 00104 low = m_cache.getBuffer()[(int)(floor(spos) + 2) * CC]; 00105 if(ceil(spos) < 0) 00106 high = m_cache.getBuffer()[(int)(ceil(spos) + 2) * CC]; 00107 else 00108 high = buf[(int)ceil(spos) * CC]; 00109 } 00110 else 00111 { 00112 low = buf[(int)floor(spos) * CC]; 00113 high = buf[(int)ceil(spos) * CC]; 00114 } 00115 buffer[i * CC] = low + (spos - floor(spos)) * (high - low); 00116 } 00117 } 00118 00119 if(len > 1) 00120 memcpy(m_cache.getBuffer(), 00121 buf + (len - 2) * channels, 00122 2 * samplesize); 00123 else if(len == 1) 00124 memcpy(m_cache.getBuffer() + 1 * channels, buf, samplesize); 00125 00126 m_sposition += len; 00127 m_position += length; 00128 }