1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2007 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 |
|
28 |
#if defined(__APPLE__) |
29 |
# include <stdlib.h> |
30 |
#else |
31 |
# include <malloc.h> |
32 |
#endif |
33 |
|
34 |
|
35 |
namespace LinuxSampler { |
36 |
|
37 |
/** |
38 |
* Create real channel. |
39 |
* |
40 |
* @param ChannelNr - channel number of this new channel |
41 |
* @param BufferSize - desired audio data buffer size |
42 |
*/ |
43 |
AudioChannel::AudioChannel(uint ChannelNr, uint BufferSize) { |
44 |
this->ChannelNr = ChannelNr; |
45 |
#if defined(__APPLE__) |
46 |
this->pBuffer = (float *) malloc(BufferSize*sizeof(float)); |
47 |
#else |
48 |
this->pBuffer = (float *) memalign(16,BufferSize*sizeof(float)); |
49 |
#endif |
50 |
this->uiBufferSize = BufferSize; |
51 |
this->pMixChannel = NULL; |
52 |
this->UsesExternalBuffer = false; |
53 |
|
54 |
Parameters["NAME"] = new ParameterName("Channel " + ToString(ChannelNr)); |
55 |
Parameters["IS_MIX_CHANNEL"] = new ParameterIsMixChannel(false); |
56 |
|
57 |
Clear(); |
58 |
} |
59 |
|
60 |
/** |
61 |
* Create channel with external (already existing) audio buffer. |
62 |
* |
63 |
* @param ChannelNr - channel number of this new channel |
64 |
* @param pBuffer - external audio buffer |
65 |
* @param BufferSize - size of the external buffer |
66 |
*/ |
67 |
AudioChannel::AudioChannel(uint ChannelNr, float* pBuffer, uint BufferSize) { |
68 |
this->ChannelNr = ChannelNr; |
69 |
this->pBuffer = pBuffer; |
70 |
this->uiBufferSize = BufferSize; |
71 |
this->pMixChannel = NULL; |
72 |
this->UsesExternalBuffer = true; |
73 |
|
74 |
Parameters["NAME"] = new ParameterName("Channel " + ToString(ChannelNr)); |
75 |
Parameters["IS_MIX_CHANNEL"] = new ParameterIsMixChannel(false); |
76 |
|
77 |
Clear(); |
78 |
} |
79 |
|
80 |
/** |
81 |
* Create mix channel. |
82 |
* |
83 |
* @param ChannelNr - channel number of this new channel |
84 |
* @param pMixChannelDestination - a real audio channel this new mix |
85 |
* channel refers to |
86 |
*/ |
87 |
AudioChannel::AudioChannel(uint ChannelNr, AudioChannel* pMixChannelDestination) { |
88 |
this->ChannelNr = ChannelNr; |
89 |
this->pBuffer = pMixChannelDestination->Buffer(); |
90 |
this->uiBufferSize = pMixChannelDestination->uiBufferSize; |
91 |
this->pMixChannel = pMixChannelDestination; |
92 |
this->UsesExternalBuffer = true; |
93 |
|
94 |
Parameters["NAME"] = new ParameterName("Channel " + ToString(ChannelNr)); |
95 |
Parameters["IS_MIX_CHANNEL"] = new ParameterIsMixChannel(true); |
96 |
//TODO: Parameters["MIX_CHANNEL_DESTINATION"] = new ParameterMixChannelDestination(dest_chan); |
97 |
|
98 |
Clear(); |
99 |
} |
100 |
|
101 |
/** |
102 |
* Destructor |
103 |
*/ |
104 |
AudioChannel::~AudioChannel() { |
105 |
std::map<String,DeviceRuntimeParameter*>::iterator iter = Parameters.begin(); |
106 |
while (iter != Parameters.end()) { delete iter->second; iter++; } |
107 |
if (!UsesExternalBuffer) free(pBuffer); |
108 |
} |
109 |
|
110 |
/** |
111 |
* Copies audio data (unmodified) from this AudioChannel to the given |
112 |
* destination AudioChannel. |
113 |
* |
114 |
* @e Caution: This method will overwrite the content in the destination |
115 |
* channel buffer. |
116 |
* |
117 |
* @param pDst - destination channel |
118 |
* @param Samples - amount of sample points to be copied |
119 |
*/ |
120 |
void AudioChannel::CopyTo(AudioChannel* pDst, const uint Samples) { |
121 |
memcpy(pDst->Buffer(), Buffer(), Samples * sizeof(float)); |
122 |
} |
123 |
|
124 |
/** |
125 |
* Copies audio data from this AudioChannel to the given destination |
126 |
* AudioChannel and applies the given volume coefficient to the |
127 |
* destination audio signal. |
128 |
* |
129 |
* @e Caution: This method will overwrite the content in the destination |
130 |
* channel buffer. |
131 |
* |
132 |
* @param pDst - destination channel |
133 |
* @param Samples - amount of sample points to be copied |
134 |
* @param fLevel - volume coefficient to be applied |
135 |
*/ |
136 |
void AudioChannel::CopyTo(AudioChannel* pDst, const uint Samples, const float fLevel) { |
137 |
if (fLevel == 1.0f) CopyTo(pDst, Samples); |
138 |
else { |
139 |
float* pSrcBuf = Buffer(); |
140 |
float* pDstBuf = pDst->Buffer(); |
141 |
for (int i = 0; i < Samples; i++) |
142 |
pDstBuf[i] = pSrcBuf[i] * fLevel; |
143 |
} |
144 |
} |
145 |
|
146 |
/** |
147 |
* Copies audio data (unmodified) from this AudioChannel and mixes it to the |
148 |
* given destination AudioChannel. |
149 |
* |
150 |
* @param pDst - destination channel |
151 |
* @param Samples - amount of sample points to be mixed over |
152 |
*/ |
153 |
void AudioChannel::MixTo(AudioChannel* pDst, const uint Samples) { |
154 |
//TODO: there's probably a more efficient way to do this ... |
155 |
float* pSrcBuf = Buffer(); |
156 |
float* pDstBuf = pDst->Buffer(); |
157 |
for (int i = 0; i < Samples; i++) |
158 |
pDstBuf[i] += pSrcBuf[i]; |
159 |
} |
160 |
|
161 |
/** |
162 |
* Copies audio data from this AudioChannel, applies the given volume |
163 |
* coefficient to the audio signal and mixes it to the given destination |
164 |
* channel. |
165 |
* |
166 |
* @param pDst - destination channel |
167 |
* @param Samples - amount of sample points to be mixed over |
168 |
* @param fLevel - volume coefficient to be applied |
169 |
*/ |
170 |
void AudioChannel::MixTo(AudioChannel* pDst, const uint Samples, const float fLevel) { |
171 |
if (fLevel == 1.0f) MixTo(pDst, Samples); |
172 |
else { |
173 |
float* pSrcBuf = Buffer(); |
174 |
float* pDstBuf = pDst->Buffer(); |
175 |
for (int i = 0; i < Samples; i++) |
176 |
pDstBuf[i] += pSrcBuf[i] * fLevel; |
177 |
} |
178 |
} |
179 |
|
180 |
std::map<String,DeviceRuntimeParameter*> AudioChannel::ChannelParameters() { |
181 |
return Parameters; |
182 |
} |
183 |
} |