Blender V2.61 - r43446

AUD_Mixer.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_Mixer.h"
00031 #include "AUD_IReader.h"
00032 
00033 #include <cstring>
00034 
00035 AUD_Mixer::AUD_Mixer(AUD_DeviceSpecs specs) :
00036     m_specs(specs)
00037 {
00038     switch(m_specs.format)
00039     {
00040     case AUD_FORMAT_U8:
00041         m_convert = AUD_convert_float_u8;
00042         break;
00043     case AUD_FORMAT_S16:
00044         m_convert = AUD_convert_float_s16;
00045         break;
00046     case AUD_FORMAT_S24:
00047 
00048 #ifdef __BIG_ENDIAN__
00049         m_convert = AUD_convert_float_s24_be;
00050 #else
00051         m_convert = AUD_convert_float_s24_le;
00052 #endif
00053         break;
00054     case AUD_FORMAT_S32:
00055         m_convert = AUD_convert_float_s32;
00056         break;
00057     case AUD_FORMAT_FLOAT32:
00058         m_convert = AUD_convert_copy<float>;
00059         break;
00060     case AUD_FORMAT_FLOAT64:
00061         m_convert = AUD_convert_float_double;
00062         break;
00063     default:
00064         break;
00065     }
00066 }
00067 
00068 AUD_DeviceSpecs AUD_Mixer::getSpecs() const
00069 {
00070     return m_specs;
00071 }
00072 
00073 void AUD_Mixer::setSpecs(AUD_Specs specs)
00074 {
00075     m_specs.specs = specs;
00076 }
00077 
00078 void AUD_Mixer::clear(int length)
00079 {
00080     m_buffer.assureSize(length * m_specs.channels * AUD_SAMPLE_SIZE(m_specs));
00081 
00082     m_length = length;
00083 
00084     memset(m_buffer.getBuffer(), 0, length * m_specs.channels * AUD_SAMPLE_SIZE(m_specs));
00085 }
00086 
00087 void AUD_Mixer::mix(sample_t* buffer, int start, int length, float volume)
00088 {
00089     sample_t* out = m_buffer.getBuffer();
00090 
00091     length = (AUD_MIN(m_length, length + start) - start) * m_specs.channels;
00092     start *= m_specs.channels;
00093 
00094     for(int i = 0; i < length; i++)
00095         out[i + start] += buffer[i] * volume;
00096 }
00097 
00098 void AUD_Mixer::read(data_t* buffer, float volume)
00099 {
00100     sample_t* out = m_buffer.getBuffer();
00101 
00102     for(int i = 0; i < m_length * m_specs.channels; i++)
00103         out[i] *= volume;
00104 
00105     m_convert(buffer, (data_t*) out, m_length * m_specs.channels);
00106 }