linuxsampler 2.3.1
AudioChannel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 - 2012 Christian Schoenebeck *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 ***************************************************************************/
23
24#include "AudioChannel.h"
25
26#include "../../common/global_private.h"
27#include "../../common/Thread.h" // needed for allocAlignedMem() and freeAlignedMem()
28
29#if defined(__APPLE__)
30# include <stdlib.h>
31#else
32# include <malloc.h>
33#endif
34
35
36namespace LinuxSampler {
37
45 this->ChannelNr = ChannelNr;
46 this->pBuffer = (float *) Thread::allocAlignedMem(16,BufferSize*sizeof(float));
47 this->uiBufferSize = BufferSize;
48 this->pMixChannel = NULL;
49 this->UsesExternalBuffer = false;
50
51 Parameters["NAME"] = new ParameterName("Channel " + ToString(ChannelNr));
52 Parameters["IS_MIX_CHANNEL"] = new ParameterIsMixChannel(false);
53
54 Clear();
55 }
56
64 AudioChannel::AudioChannel(uint ChannelNr, float* pBuffer, uint BufferSize) {
65 this->ChannelNr = ChannelNr;
66 this->pBuffer = pBuffer;
67 this->uiBufferSize = BufferSize;
68 this->pMixChannel = NULL;
69 this->UsesExternalBuffer = true;
70
71 Parameters["NAME"] = new ParameterName("Channel " + ToString(ChannelNr));
72 Parameters["IS_MIX_CHANNEL"] = new ParameterIsMixChannel(false);
73
74 Clear();
75 }
76
85 this->ChannelNr = ChannelNr;
86 this->pBuffer = pMixChannelDestination->Buffer();
87 this->uiBufferSize = pMixChannelDestination->uiBufferSize;
88 this->pMixChannel = pMixChannelDestination;
89 this->UsesExternalBuffer = true;
90
91 Parameters["NAME"] = new ParameterName("Channel " + ToString(ChannelNr));
92 Parameters["IS_MIX_CHANNEL"] = new ParameterIsMixChannel(true);
93 //TODO: Parameters["MIX_CHANNEL_DESTINATION"] = new ParameterMixChannelDestination(dest_chan);
94
95 Clear();
96 }
97
102 std::map<String,DeviceRuntimeParameter*>::iterator iter = Parameters.begin();
103 while (iter != Parameters.end()) { delete iter->second; iter++; }
104 if (!UsesExternalBuffer) Thread::freeAlignedMem(pBuffer);
105 }
106
118 memcpy((float* __restrict)pDst->Buffer(), (const float* __restrict)Buffer(), Samples * sizeof(float));
119 }
120
133 void AudioChannel::CopyTo(AudioChannel* pDst, const uint Samples, const float fLevel) {
134 if (fLevel == 1.0f) CopyTo(pDst, Samples);
135 else {
136 const float* __restrict pSrcBuf = Buffer();
137 float* __restrict pDstBuf = pDst->Buffer();
138 #if HAVE_GCC_VECTOR_EXTENSIONS
139 if ((size_t)pSrcBuf % 16 == 0 && (size_t)pDstBuf % 16 == 0) {
140 const v4sf vcoeff = { fLevel, fLevel, fLevel, fLevel };
141 const v4sf* __restrict src =
142 static_cast<const v4sf* __restrict>(
143 (const void* __restrict)pSrcBuf
144 );
146 static_cast<v4sf* __restrict>(
147 (void* __restrict)pDstBuf
148 );
149 const int cells = Samples / 4;
150 for (int i = 0; i < cells; ++i)
151 dst[i] = src[i] * vcoeff;
152 } else {
153 #endif
154 for (int i = 0; i < Samples; i++)
155 pDstBuf[i] = pSrcBuf[i] * fLevel;
156 #if HAVE_GCC_VECTOR_EXTENSIONS
157 }
158 #endif
159 }
160 }
161
170 const float* __restrict pSrcBuf = Buffer();
171 float* __restrict pDstBuf = pDst->Buffer();
172 #if HAVE_GCC_VECTOR_EXTENSIONS
173 if ((size_t)pSrcBuf % 16 == 0 && (size_t)pDstBuf % 16 == 0) {
174 const v4sf* __restrict src =
175 static_cast<const v4sf* __restrict>(
176 (const void* __restrict)pSrcBuf
177 );
179 static_cast<v4sf* __restrict>(
180 (void* __restrict)pDstBuf
181 );
182 const int cells = Samples / 4;
183 for (int i = 0; i < cells; ++i)
184 dst[i] += src[i];
185 } else {
186 #endif
187 for (int i = 0; i < Samples; i++)
188 pDstBuf[i] += pSrcBuf[i];
189 #if HAVE_GCC_VECTOR_EXTENSIONS
190 }
191 #endif
192 }
193
203 void AudioChannel::MixTo(AudioChannel* pDst, const uint Samples, const float fLevel) {
204 if (fLevel == 1.0f) MixTo(pDst, Samples);
205 else {
206 const float* __restrict pSrcBuf = Buffer();
207 float* __restrict pDstBuf = pDst->Buffer();
208 #if HAVE_GCC_VECTOR_EXTENSIONS
209 if ((size_t)pSrcBuf % 16 == 0 && (size_t)pDstBuf % 16 == 0) {
210 const v4sf vcoeff = { fLevel, fLevel, fLevel, fLevel };
211 const v4sf* __restrict src =
212 static_cast<const v4sf* __restrict>(
213 (const void* __restrict)pSrcBuf
214 );
216 static_cast<v4sf* __restrict>(
217 (void* __restrict)pDstBuf
218 );
219 const int cells = Samples / 4;
220 for (int i = 0; i < cells; ++i)
221 dst[i] += src[i] * vcoeff;
222 } else {
223 #endif
224 for (int i = 0; i < Samples; i++)
225 pDstBuf[i] += pSrcBuf[i] * fLevel;
226 #if HAVE_GCC_VECTOR_EXTENSIONS
227 }
228 #endif
229 }
230 }
231
232 std::map<String,DeviceRuntimeParameter*> AudioChannel::ChannelParameters() {
233 return Parameters;
234 }
235}
Audio Channel (always mono)
virtual ~AudioChannel()
Destructor.
AudioChannel(uint ChannelNr, uint BufferSize)
Create real channel.
float * Buffer()
Audio signal buffer.
std::map< String, DeviceRuntimeParameter * > Parameters
void CopyTo(AudioChannel *pDst, const uint Samples)
Copies audio data (unmodified) from this AudioChannel to the given destination AudioChannel.
std::map< String, DeviceRuntimeParameter * > ChannelParameters()
void Clear()
Reset audio buffer with silence.
void MixTo(AudioChannel *pDst, const uint Samples)
Copies audio data (unmodified) from this AudioChannel and mixes it to the given destination AudioChan...
Wraps as a kind of pointer class some data object shared with other threads, to protect / synchronize...
static void * allocAlignedMem(size_t boundary, size_t size)
Allocates an aligned block of memory.
Definition Thread.h:83
static void freeAlignedMem(void *ptr)
Frees an aligned block of memory allocated with allocAlignedMem()
Definition Thread.h:95