Blender  V2.59
AUD_Mixer.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: AUD_Mixer.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_Mixer.h"
00033 #include "AUD_IReader.h"
00034 
00035 #include <cstring>
00036 
00037 AUD_Mixer::AUD_Mixer(AUD_DeviceSpecs specs) :
00038         m_specs(specs)
00039 {
00040         int bigendian = 1;
00041         bigendian = (((char*)&bigendian)[0]) ? 0: 1; // 1 if Big Endian
00042 
00043         switch(m_specs.format)
00044         {
00045         case AUD_FORMAT_U8:
00046                 m_convert = AUD_convert_float_u8;
00047                 break;
00048         case AUD_FORMAT_S16:
00049                 m_convert = AUD_convert_float_s16;
00050                 break;
00051         case AUD_FORMAT_S24:
00052                 if(bigendian)
00053                         m_convert = AUD_convert_float_s24_be;
00054                 else
00055                         m_convert = AUD_convert_float_s24_le;
00056                 break;
00057         case AUD_FORMAT_S32:
00058                 m_convert = AUD_convert_float_s32;
00059                 break;
00060         case AUD_FORMAT_FLOAT32:
00061                 m_convert = AUD_convert_copy<float>;
00062                 break;
00063         case AUD_FORMAT_FLOAT64:
00064                 m_convert = AUD_convert_float_double;
00065                 break;
00066         default:
00067                 break;
00068         }
00069 }
00070 
00071 AUD_DeviceSpecs AUD_Mixer::getSpecs() const
00072 {
00073         return m_specs;
00074 }
00075 
00076 void AUD_Mixer::add(sample_t* buffer, int start, int length, float volume)
00077 {
00078         AUD_MixerBuffer buf;
00079         buf.buffer = buffer;
00080         buf.start = start;
00081         buf.length = length;
00082         buf.volume = volume;
00083         m_buffers.push_back(buf);
00084 }
00085 
00086 void AUD_Mixer::superpose(data_t* buffer, int length, float volume)
00087 {
00088         AUD_MixerBuffer buf;
00089 
00090         int channels = m_specs.channels;
00091 
00092         if(m_buffer.getSize() < length * channels * 4)
00093                 m_buffer.resize(length * channels * 4);
00094 
00095         sample_t* out = m_buffer.getBuffer();
00096         sample_t* in;
00097 
00098         memset(out, 0, length * channels * 4);
00099 
00100         int end;
00101 
00102         while(!m_buffers.empty())
00103         {
00104                 buf = m_buffers.front();
00105                 m_buffers.pop_front();
00106 
00107                 end = buf.length * channels;
00108                 in = buf.buffer;
00109 
00110                 for(int i = 0; i < end; i++)
00111                         out[i + buf.start * channels] += in[i] * buf.volume * volume;
00112         }
00113 
00114         m_convert(buffer, (data_t*) out, length * channels);
00115 }