/[svn]/linuxsampler/trunk/src/engines/gig/Engine.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/gig/Engine.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2043 - (show annotations) (download)
Sat Jan 9 09:37:01 2010 UTC (10 years, 3 months ago) by persson
File size: 16562 byte(s)
* gig engine: implemented the "round robin keyboard" dimension
* gig engine: fixed round robin and random dimensions for cases when
  number of dimension zones is not a power of two
* gig engine: made round robin use a counter for each region instead
  of each key
* fixed building with libgig installed in a non-standard directory

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005-2009 Christian Schoenebeck *
7 * Copyright (C) 2009 Grigor Iliev *
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
22 * MA 02111-1307 USA *
23 ***************************************************************************/
24
25 #include "Engine.h"
26 #include "EngineChannel.h"
27
28 namespace LinuxSampler { namespace gig {
29 Engine::Format Engine::GetEngineFormat() { return GIG; }
30
31 /**
32 * Reacts on supported control change commands (e.g. pitch bend wheel,
33 * modulation wheel, aftertouch).
34 *
35 * @param pEngineChannel - engine channel on which this event occured on
36 * @param itControlChangeEvent - controller, value and time stamp of the event
37 */
38 void Engine::ProcessControlChange (
39 LinuxSampler::EngineChannel* pEngineChannel,
40 Pool<Event>::Iterator& itControlChangeEvent
41 ) {
42 dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value));
43
44 EngineChannel* pChannel = dynamic_cast<EngineChannel*>(pEngineChannel);
45 // handle the "control triggered" MIDI rule: a control change
46 // event can trigger a new note on or note off event
47 if (pChannel->pInstrument) {
48
49 ::gig::MidiRule* rule;
50 for (int i = 0 ; (rule = pChannel->pInstrument->GetMidiRule(i)) ; i++) {
51
52 if (::gig::MidiRuleCtrlTrigger* ctrlTrigger =
53 dynamic_cast< ::gig::MidiRuleCtrlTrigger*>(rule)) {
54 if (itControlChangeEvent->Param.CC.Controller ==
55 ctrlTrigger->ControllerNumber) {
56
57 uint8_t oldCCValue = pChannel->ControllerTable[
58 itControlChangeEvent->Param.CC.Controller];
59 uint8_t newCCValue = itControlChangeEvent->Param.CC.Value;
60
61 for (int i = 0 ; i < ctrlTrigger->Triggers ; i++) {
62 ::gig::MidiRuleCtrlTrigger::trigger_t* pTrigger =
63 &ctrlTrigger->pTriggers[i];
64
65 // check if the controller has passed the
66 // trigger point in the right direction
67 if ((pTrigger->Descending &&
68 oldCCValue > pTrigger->TriggerPoint &&
69 newCCValue <= pTrigger->TriggerPoint) ||
70 (!pTrigger->Descending &&
71 oldCCValue < pTrigger->TriggerPoint &&
72 newCCValue >= pTrigger->TriggerPoint)) {
73
74 RTList<Event>::Iterator itNewEvent = pGlobalEvents->allocAppend();
75 if (itNewEvent) {
76 *itNewEvent = *itControlChangeEvent;
77 itNewEvent->Param.Note.Key = pTrigger->Key;
78
79 if (pTrigger->NoteOff || pTrigger->Velocity == 0) {
80 itNewEvent->Type = Event::type_note_off;
81 itNewEvent->Param.Note.Velocity = 100;
82
83 ProcessNoteOff(pEngineChannel, itNewEvent);
84 } else {
85 itNewEvent->Type = Event::type_note_on;
86 //TODO: if Velocity is 255, the triggered velocity should
87 // depend on how fast the controller is moving
88 itNewEvent->Param.Note.Velocity =
89 pTrigger->Velocity == 255 ? 100 :
90 pTrigger->Velocity;
91
92 ProcessNoteOn(pEngineChannel, itNewEvent);
93 }
94 }
95 else dmsg(1,("Event pool emtpy!\n"));
96 }
97 }
98 }
99 }
100 }
101 }
102
103 // update controller value in the engine channel's controller table
104 pChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;
105
106 ProcessHardcodedControllers(pEngineChannel, itControlChangeEvent);
107
108 // handle FX send controllers
109 ProcessFxSendControllers(pChannel, itControlChangeEvent);
110 }
111
112 DiskThread* Engine::CreateDiskThread() {
113 return new DiskThread (
114 iMaxDiskStreams,
115 ((pAudioOutputDevice->MaxSamplesPerCycle() << CONFIG_MAX_PITCH) << 1) + 6, //FIXME: assuming stereo
116 &instruments
117 );
118 }
119
120 void Engine::TriggerNewVoices (
121 LinuxSampler::EngineChannel* pEngineChannel,
122 RTList<Event>::Iterator& itNoteOnEvent,
123 bool HandleKeyGroupConflicts
124 ) {
125 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
126 // first, get total amount of required voices (dependant on amount of layers)
127 ::gig::Region* pRegion = pChannel->pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key);
128 if (pRegion && !RegionSuspended(pRegion)) {
129 int voicesRequired = pRegion->Layers;
130 // now launch the required amount of voices
131 for (int i = 0; i < voicesRequired; i++)
132 LaunchVoice(pChannel, itNoteOnEvent, i, false, true, HandleKeyGroupConflicts);
133 }
134 }
135
136 void Engine::TriggerReleaseVoices (
137 LinuxSampler::EngineChannel* pEngineChannel,
138 RTList<Event>::Iterator& itNoteOffEvent
139 ) {
140 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
141 MidiKey* pKey = &pChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];
142 // first, get total amount of required voices (dependant on amount of layers)
143 ::gig::Region* pRegion = pChannel->pInstrument->GetRegion(itNoteOffEvent->Param.Note.Key);
144 if (pRegion) {
145 int voicesRequired = pRegion->Layers;
146
147 // MIDI note-on velocity is used instead of note-off velocity
148 itNoteOffEvent->Param.Note.Velocity = pKey->Velocity;
149
150 // now launch the required amount of voices
151 for (int i = 0; i < voicesRequired; i++)
152 LaunchVoice(pChannel, itNoteOffEvent, i, true, false, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples
153 }
154 }
155
156 Pool<Voice>::Iterator Engine::LaunchVoice (
157 LinuxSampler::EngineChannel* pEngineChannel,
158 Pool<Event>::Iterator& itNoteOnEvent,
159 int iLayer,
160 bool ReleaseTriggerVoice,
161 bool VoiceStealing,
162 bool HandleKeyGroupConflicts
163 ) {
164 EngineChannel* pChannel = static_cast<EngineChannel*>(pEngineChannel);
165 int MIDIKey = itNoteOnEvent->Param.Note.Key;
166 EngineChannel::MidiKey* pKey = &pChannel->pMIDIKeyInfo[MIDIKey];
167 ::gig::Region* pRegion = pChannel->pInstrument->GetRegion(MIDIKey);
168
169 // if nothing defined for this key
170 if (!pRegion) return Pool<Voice>::Iterator(); // nothing to do
171
172 // only mark the first voice of a layered voice (group) to be in a
173 // key group, so the layered voices won't kill each other
174 int iKeyGroup = (iLayer == 0 && !ReleaseTriggerVoice) ? pRegion->KeyGroup : 0;
175
176 if (HandleKeyGroupConflicts) pChannel->HandleKeyGroupConflicts(iKeyGroup, itNoteOnEvent);
177
178 Voice::type_t VoiceType = Voice::type_normal;
179
180 // get current dimension values to select the right dimension region
181 //TODO: for stolen voices this dimension region selection block is processed twice, this should be changed
182 //FIXME: controller values for selecting the dimension region here are currently not sample accurate
183 uint DimValues[8] = { 0 };
184 for (int i = pRegion->Dimensions - 1; i >= 0; i--) {
185 switch (pRegion->pDimensionDefinitions[i].dimension) {
186 case ::gig::dimension_samplechannel:
187 DimValues[i] = 0; //TODO: we currently ignore this dimension
188 break;
189 case ::gig::dimension_layer:
190 DimValues[i] = iLayer;
191 break;
192 case ::gig::dimension_velocity:
193 DimValues[i] = itNoteOnEvent->Param.Note.Velocity;
194 break;
195 case ::gig::dimension_channelaftertouch:
196 DimValues[i] = pChannel->ControllerTable[128];
197 break;
198 case ::gig::dimension_releasetrigger:
199 VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal;
200 DimValues[i] = (uint) ReleaseTriggerVoice;
201 break;
202 case ::gig::dimension_keyboard:
203 DimValues[i] = (uint) (pChannel->CurrentKeyDimension * pRegion->pDimensionDefinitions[i].zones);
204 break;
205 case ::gig::dimension_roundrobin:
206 DimValues[i] = uint(*pChannel->pMIDIKeyInfo[MIDIKey].pRoundRobinIndex % pRegion->pDimensionDefinitions[i].zones); // RoundRobinIndex is incremented for each note on in this Region
207 break;
208 case ::gig::dimension_roundrobinkeyboard:
209 DimValues[i] = uint(pChannel->RoundRobinIndex % pRegion->pDimensionDefinitions[i].zones); // RoundRobinIndex is incremented for each note on
210 break;
211 case ::gig::dimension_random:
212 RandomSeed = RandomSeed * 1103515245 + 12345; // classic pseudo random number generator
213 DimValues[i] = uint(RandomSeed / 4294967296.0f * pRegion->pDimensionDefinitions[i].zones);
214 break;
215 case ::gig::dimension_modwheel:
216 DimValues[i] = pChannel->ControllerTable[1];
217 break;
218 case ::gig::dimension_breath:
219 DimValues[i] = pChannel->ControllerTable[2];
220 break;
221 case ::gig::dimension_foot:
222 DimValues[i] = pChannel->ControllerTable[4];
223 break;
224 case ::gig::dimension_portamentotime:
225 DimValues[i] = pChannel->ControllerTable[5];
226 break;
227 case ::gig::dimension_effect1:
228 DimValues[i] = pChannel->ControllerTable[12];
229 break;
230 case ::gig::dimension_effect2:
231 DimValues[i] = pChannel->ControllerTable[13];
232 break;
233 case ::gig::dimension_genpurpose1:
234 DimValues[i] = pChannel->ControllerTable[16];
235 break;
236 case ::gig::dimension_genpurpose2:
237 DimValues[i] = pChannel->ControllerTable[17];
238 break;
239 case ::gig::dimension_genpurpose3:
240 DimValues[i] = pChannel->ControllerTable[18];
241 break;
242 case ::gig::dimension_genpurpose4:
243 DimValues[i] = pChannel->ControllerTable[19];
244 break;
245 case ::gig::dimension_sustainpedal:
246 DimValues[i] = pChannel->ControllerTable[64];
247 break;
248 case ::gig::dimension_portamento:
249 DimValues[i] = pChannel->ControllerTable[65];
250 break;
251 case ::gig::dimension_sostenutopedal:
252 DimValues[i] = pChannel->ControllerTable[66];
253 break;
254 case ::gig::dimension_softpedal:
255 DimValues[i] = pChannel->ControllerTable[67];
256 break;
257 case ::gig::dimension_genpurpose5:
258 DimValues[i] = pChannel->ControllerTable[80];
259 break;
260 case ::gig::dimension_genpurpose6:
261 DimValues[i] = pChannel->ControllerTable[81];
262 break;
263 case ::gig::dimension_genpurpose7:
264 DimValues[i] = pChannel->ControllerTable[82];
265 break;
266 case ::gig::dimension_genpurpose8:
267 DimValues[i] = pChannel->ControllerTable[83];
268 break;
269 case ::gig::dimension_effect1depth:
270 DimValues[i] = pChannel->ControllerTable[91];
271 break;
272 case ::gig::dimension_effect2depth:
273 DimValues[i] = pChannel->ControllerTable[92];
274 break;
275 case ::gig::dimension_effect3depth:
276 DimValues[i] = pChannel->ControllerTable[93];
277 break;
278 case ::gig::dimension_effect4depth:
279 DimValues[i] = pChannel->ControllerTable[94];
280 break;
281 case ::gig::dimension_effect5depth:
282 DimValues[i] = pChannel->ControllerTable[95];
283 break;
284 case ::gig::dimension_none:
285 std::cerr << "gig::Engine::LaunchVoice() Error: dimension=none\n" << std::flush;
286 break;
287 default:
288 std::cerr << "gig::Engine::LaunchVoice() Error: Unknown dimension\n" << std::flush;
289 }
290 }
291
292 // return if this is a release triggered voice and there is no
293 // releasetrigger dimension (could happen if an instrument
294 // change has occured between note on and off)
295 if (ReleaseTriggerVoice && VoiceType != Voice::type_release_trigger) return Pool<Voice>::Iterator();
296
297 ::gig::DimensionRegion* pDimRgn = pRegion->GetDimensionRegionByValue(DimValues);
298
299 // no need to continue if sample is silent
300 if (!pDimRgn->pSample || !pDimRgn->pSample->SamplesTotal) return Pool<Voice>::Iterator();
301
302 // allocate a new voice for the key
303 Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
304
305 int res = InitNewVoice (
306 pChannel, pDimRgn, itNoteOnEvent, VoiceType, iLayer,
307 iKeyGroup, ReleaseTriggerVoice, VoiceStealing, itNewVoice
308 );
309 if (!res) return itNewVoice;
310
311 return Pool<Voice>::Iterator(); // no free voice or error
312 }
313
314 bool Engine::DiskStreamSupported() {
315 return true;
316 }
317
318 String Engine::Description() {
319 return "GigaSampler Format Engine";
320 }
321
322 String Engine::Version() {
323 String s = "$Revision: 1.107 $";
324 return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
325 }
326
327 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC