3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005 - 2009 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
#include "AudioChannel.h" |
#include "AudioChannel.h" |
25 |
#include <malloc.h> |
|
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 |
|
|
36 |
namespace LinuxSampler { |
namespace LinuxSampler { |
37 |
|
|
43 |
*/ |
*/ |
44 |
AudioChannel::AudioChannel(uint ChannelNr, uint BufferSize) { |
AudioChannel::AudioChannel(uint ChannelNr, uint BufferSize) { |
45 |
this->ChannelNr = ChannelNr; |
this->ChannelNr = ChannelNr; |
46 |
this->pBuffer = (float *) memalign(16,BufferSize*sizeof(float)); |
this->pBuffer = (float *) Thread::allocAlignedMem(16,BufferSize*sizeof(float)); |
47 |
this->uiBufferSize = BufferSize; |
this->uiBufferSize = BufferSize; |
48 |
this->pMixChannel = NULL; |
this->pMixChannel = NULL; |
49 |
this->UsesExternalBuffer = false; |
this->UsesExternalBuffer = false; |
59 |
* |
* |
60 |
* @param ChannelNr - channel number of this new channel |
* @param ChannelNr - channel number of this new channel |
61 |
* @param pBuffer - external audio buffer |
* @param pBuffer - external audio buffer |
62 |
* @param BufferSIze - size of the external buffer |
* @param BufferSize - size of the external buffer |
63 |
*/ |
*/ |
64 |
AudioChannel::AudioChannel(uint ChannelNr, float* pBuffer, uint BufferSize) { |
AudioChannel::AudioChannel(uint ChannelNr, float* pBuffer, uint BufferSize) { |
65 |
this->ChannelNr = ChannelNr; |
this->ChannelNr = ChannelNr; |
101 |
AudioChannel::~AudioChannel() { |
AudioChannel::~AudioChannel() { |
102 |
std::map<String,DeviceRuntimeParameter*>::iterator iter = Parameters.begin(); |
std::map<String,DeviceRuntimeParameter*>::iterator iter = Parameters.begin(); |
103 |
while (iter != Parameters.end()) { delete iter->second; iter++; } |
while (iter != Parameters.end()) { delete iter->second; iter++; } |
104 |
if (!UsesExternalBuffer) free(pBuffer); |
if (!UsesExternalBuffer) Thread::freeAlignedMem(pBuffer); |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
|
* Copies audio data (unmodified) from this AudioChannel to the given |
109 |
|
* destination AudioChannel. |
110 |
|
* |
111 |
|
* @e Caution: This method will overwrite the content in the destination |
112 |
|
* channel buffer. |
113 |
|
* |
114 |
|
* @param pDst - destination channel |
115 |
|
* @param Samples - amount of sample points to be copied |
116 |
|
*/ |
117 |
|
void AudioChannel::CopyTo(AudioChannel* pDst, const uint Samples) { |
118 |
|
memcpy(pDst->Buffer(), Buffer(), Samples * sizeof(float)); |
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
|
* Copies audio data from this AudioChannel to the given destination |
123 |
|
* AudioChannel and applies the given volume coefficient to the |
124 |
|
* destination audio signal. |
125 |
|
* |
126 |
|
* @e Caution: This method will overwrite the content in the destination |
127 |
|
* channel buffer. |
128 |
|
* |
129 |
|
* @param pDst - destination channel |
130 |
|
* @param Samples - amount of sample points to be copied |
131 |
|
* @param fLevel - volume coefficient to be applied |
132 |
|
*/ |
133 |
|
void AudioChannel::CopyTo(AudioChannel* pDst, const uint Samples, const float fLevel) { |
134 |
|
if (fLevel == 1.0f) CopyTo(pDst, Samples); |
135 |
|
else { |
136 |
|
float* pSrcBuf = Buffer(); |
137 |
|
float* 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* src = static_cast<v4sf*>((void*)Buffer()); |
142 |
|
v4sf* dst = static_cast<v4sf*>((void*)pDst->Buffer()); |
143 |
|
const int cells = Samples / 4; |
144 |
|
for (int i = 0; i < cells; ++i) |
145 |
|
dst[i] = src[i] * vcoeff; |
146 |
|
} else { |
147 |
|
#endif |
148 |
|
for (int i = 0; i < Samples; i++) |
149 |
|
pDstBuf[i] = pSrcBuf[i] * fLevel; |
150 |
|
#if HAVE_GCC_VECTOR_EXTENSIONS |
151 |
|
} |
152 |
|
#endif |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
|
* Copies audio data (unmodified) from this AudioChannel and mixes it to the |
158 |
|
* given destination AudioChannel. |
159 |
|
* |
160 |
|
* @param pDst - destination channel |
161 |
|
* @param Samples - amount of sample points to be mixed over |
162 |
|
*/ |
163 |
|
void AudioChannel::MixTo(AudioChannel* pDst, const uint Samples) { |
164 |
|
float* pSrcBuf = Buffer(); |
165 |
|
float* pDstBuf = pDst->Buffer(); |
166 |
|
#if HAVE_GCC_VECTOR_EXTENSIONS |
167 |
|
if ((size_t)pSrcBuf % 16 == 0 && (size_t)pDstBuf % 16 == 0) { |
168 |
|
const v4sf* src = static_cast<v4sf*>((void*)Buffer()); |
169 |
|
v4sf* dst = static_cast<v4sf*>((void*)pDst->Buffer()); |
170 |
|
const int cells = Samples / 4; |
171 |
|
for (int i = 0; i < cells; ++i) |
172 |
|
dst[i] += src[i]; |
173 |
|
} else { |
174 |
|
#endif |
175 |
|
for (int i = 0; i < Samples; i++) |
176 |
|
pDstBuf[i] += pSrcBuf[i]; |
177 |
|
#if HAVE_GCC_VECTOR_EXTENSIONS |
178 |
|
} |
179 |
|
#endif |
180 |
|
} |
181 |
|
|
182 |
|
/** |
183 |
|
* Copies audio data from this AudioChannel, applies the given volume |
184 |
|
* coefficient to the audio signal and mixes it to the given destination |
185 |
|
* channel. |
186 |
|
* |
187 |
|
* @param pDst - destination channel |
188 |
|
* @param Samples - amount of sample points to be mixed over |
189 |
|
* @param fLevel - volume coefficient to be applied |
190 |
|
*/ |
191 |
|
void AudioChannel::MixTo(AudioChannel* pDst, const uint Samples, const float fLevel) { |
192 |
|
if (fLevel == 1.0f) MixTo(pDst, Samples); |
193 |
|
else { |
194 |
|
float* pSrcBuf = Buffer(); |
195 |
|
float* pDstBuf = pDst->Buffer(); |
196 |
|
#if HAVE_GCC_VECTOR_EXTENSIONS |
197 |
|
if ((size_t)pSrcBuf % 16 == 0 && (size_t)pDstBuf % 16 == 0) { |
198 |
|
const v4sf vcoeff = { fLevel, fLevel, fLevel, fLevel }; |
199 |
|
const v4sf* src = static_cast<v4sf*>((void*)Buffer()); |
200 |
|
v4sf* dst = static_cast<v4sf*>((void*)pDst->Buffer()); |
201 |
|
const int cells = Samples / 4; |
202 |
|
for (int i = 0; i < cells; ++i) |
203 |
|
dst[i] += src[i] * vcoeff; |
204 |
|
} else { |
205 |
|
#endif |
206 |
|
for (int i = 0; i < Samples; i++) |
207 |
|
pDstBuf[i] += pSrcBuf[i] * fLevel; |
208 |
|
#if HAVE_GCC_VECTOR_EXTENSIONS |
209 |
|
} |
210 |
|
#endif |
211 |
|
} |
212 |
} |
} |
213 |
|
|
214 |
std::map<String,DeviceRuntimeParameter*> AudioChannel::ChannelParameters() { |
std::map<String,DeviceRuntimeParameter*> AudioChannel::ChannelParameters() { |