Blender  V2.59
AUD_ChannelMapperReader.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: AUD_ChannelMapperReader.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_ChannelMapperReader.h"
00033 
00034 AUD_ChannelMapperReader::AUD_ChannelMapperReader(AUD_IReader* reader,
00035                                                                                                  float **mapping) :
00036                 AUD_EffectReader(reader)
00037 {
00038         m_specs = reader->getSpecs();
00039 
00040         int channels = -1;
00041         m_rch = m_specs.channels;
00042         while(mapping[++channels] != 0);
00043 
00044         m_mapping = new float*[channels];
00045         m_specs.channels = (AUD_Channels)channels;
00046 
00047         float sum;
00048         int i;
00049 
00050         while(channels--)
00051         {
00052                 m_mapping[channels] = new float[m_rch];
00053                 sum = 0.0f;
00054                 for(i=0; i < m_rch; i++)
00055                         sum += mapping[channels][i];
00056                 for(i=0; i < m_rch; i++)
00057                         m_mapping[channels][i] = sum > 0.0f ?
00058                                                                          mapping[channels][i]/sum : 0.0f;
00059         }
00060 }
00061 
00062 AUD_ChannelMapperReader::~AUD_ChannelMapperReader()
00063 {
00064         int channels = m_specs.channels;
00065 
00066         while(channels--)
00067         {
00068                 delete[] m_mapping[channels];
00069         }
00070 
00071         delete[] m_mapping;
00072 }
00073 
00074 AUD_Specs AUD_ChannelMapperReader::getSpecs() const
00075 {
00076         return m_specs;
00077 }
00078 
00079 void AUD_ChannelMapperReader::read(int & length, sample_t* & buffer)
00080 {
00081         sample_t* in = buffer;
00082 
00083         m_reader->read(length, in);
00084 
00085         if(m_buffer.getSize() < length * AUD_SAMPLE_SIZE(m_specs))
00086                 m_buffer.resize(length * AUD_SAMPLE_SIZE(m_specs));
00087 
00088         buffer = m_buffer.getBuffer();
00089         sample_t sum;
00090 
00091         for(int i = 0; i < length; i++)
00092         {
00093                 for(int j = 0; j < m_specs.channels; j++)
00094                 {
00095                         sum = 0;
00096                         for(int k = 0; k < m_rch; k++)
00097                                 sum += m_mapping[j][k] * in[i * m_rch + k];
00098                         buffer[i * m_specs.channels + j] = sum;
00099                 }
00100         }
00101 }