1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 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 "EngineChannel.h" |
25 |
|
26 |
namespace LinuxSampler { namespace gig { |
27 |
|
28 |
EngineChannel::EngineChannel() { |
29 |
pMIDIKeyInfo = new midi_key_info_t[128]; |
30 |
pEngine = NULL; |
31 |
pInstrument = NULL; |
32 |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
33 |
pCCEvents = NULL; // we allocate when we retrieve the right Engine object |
34 |
pEventQueue = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
35 |
pActiveKeys = new Pool<uint>(128); |
36 |
for (uint i = 0; i < 128; i++) { |
37 |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
38 |
pMIDIKeyInfo[i].KeyPressed = false; |
39 |
pMIDIKeyInfo[i].Active = false; |
40 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
41 |
pMIDIKeyInfo[i].pEvents = NULL; // we allocate when we retrieve the right Engine object |
42 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
43 |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
44 |
} |
45 |
for (uint i = 0; i < Event::destination_count; i++) { |
46 |
pSynthesisEvents[i] = NULL; // we allocate when we retrieve the right Engine object |
47 |
} |
48 |
InstrumentIdx = -1; |
49 |
InstrumentStat = -1; |
50 |
AudioDeviceChannelLeft = -1; |
51 |
AudioDeviceChannelRight = -1; |
52 |
} |
53 |
|
54 |
EngineChannel::~EngineChannel() { |
55 |
DisconnectAudioOutputDevice(); |
56 |
if (pInstrument) Engine::instruments.HandBack(pInstrument, this); |
57 |
if (pEventQueue) delete pEventQueue; |
58 |
if (pActiveKeys) delete pActiveKeys; |
59 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
60 |
} |
61 |
|
62 |
/** |
63 |
* Implementation of virtual method from abstract EngineChannel interface. |
64 |
* This method will periodically be polled (e.g. by the LSCP server) to |
65 |
* check if some engine channel parameter has changed since the last |
66 |
* StatusChanged() call. |
67 |
* |
68 |
* TODO: This "poll method" is just a lazy solution and might be |
69 |
* replaced in future. |
70 |
* |
71 |
* @returns true if engine channel status has changed since last |
72 |
* StatusChanged() call |
73 |
*/ |
74 |
bool EngineChannel::StatusChanged() { |
75 |
bool b = bStatusChanged; |
76 |
bStatusChanged = false; |
77 |
return b; |
78 |
} |
79 |
|
80 |
/** |
81 |
* This method is not thread safe! |
82 |
*/ |
83 |
void EngineChannel::ResetInternal() { |
84 |
Pitch = 0; |
85 |
SustainPedal = false; |
86 |
GlobalVolume = 1.0; |
87 |
GlobalPanLeft = 1.0f; |
88 |
GlobalPanRight = 1.0f; |
89 |
CurrentKeyDimension = 0; |
90 |
|
91 |
ResetControllers(); |
92 |
|
93 |
// reset key info |
94 |
for (uint i = 0; i < 128; i++) { |
95 |
if (pMIDIKeyInfo[i].pActiveVoices) |
96 |
pMIDIKeyInfo[i].pActiveVoices->clear(); |
97 |
if (pMIDIKeyInfo[i].pEvents) |
98 |
pMIDIKeyInfo[i].pEvents->clear(); |
99 |
pMIDIKeyInfo[i].KeyPressed = false; |
100 |
pMIDIKeyInfo[i].Active = false; |
101 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
102 |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
103 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
104 |
} |
105 |
|
106 |
// reset all key groups |
107 |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
108 |
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
109 |
|
110 |
// free all active keys |
111 |
pActiveKeys->clear(); |
112 |
|
113 |
// delete all input events |
114 |
pEventQueue->init(); |
115 |
|
116 |
if (pEngine) pEngine->ResetInternal(); |
117 |
|
118 |
// status of engine channel has changed, so set notify flag |
119 |
bStatusChanged = true; |
120 |
} |
121 |
|
122 |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
123 |
return pEngine; |
124 |
} |
125 |
|
126 |
/** |
127 |
* More or less a workaround to set the instrument name, index and load |
128 |
* status variable to zero percent immediately, that is without blocking |
129 |
* the calling thread. It might be used in future for other preparations |
130 |
* as well though. |
131 |
* |
132 |
* @param FileName - file name of the Gigasampler instrument file |
133 |
* @param Instrument - index of the instrument in the .gig file |
134 |
* @see LoadInstrument() |
135 |
*/ |
136 |
void EngineChannel::PrepareLoadInstrument(const char* FileName, uint Instrument) { |
137 |
InstrumentFile = FileName; |
138 |
InstrumentIdx = Instrument; |
139 |
InstrumentStat = 0; |
140 |
} |
141 |
|
142 |
/** |
143 |
* Load an instrument from a .gig file. PrepareLoadInstrument() has to |
144 |
* be called first to provide the information which instrument to load. |
145 |
* This method will then actually start to load the instrument and block |
146 |
* the calling thread until loading was completed. |
147 |
* |
148 |
* @returns detailed description of the method call result |
149 |
* @see PrepareLoadInstrument() |
150 |
*/ |
151 |
void EngineChannel::LoadInstrument() { |
152 |
|
153 |
if (pEngine) pEngine->DisableAndLock(); |
154 |
|
155 |
ResetInternal(); |
156 |
|
157 |
// free old instrument |
158 |
if (pInstrument) { |
159 |
// give old instrument back to instrument manager |
160 |
Engine::instruments.HandBack(pInstrument, this); |
161 |
} |
162 |
|
163 |
// delete all key groups |
164 |
ActiveKeyGroups.clear(); |
165 |
|
166 |
// request gig instrument from instrument manager |
167 |
try { |
168 |
instrument_id_t instrid; |
169 |
instrid.FileName = InstrumentFile; |
170 |
instrid.iInstrument = InstrumentIdx; |
171 |
pInstrument = Engine::instruments.Borrow(instrid, this); |
172 |
if (!pInstrument) { |
173 |
InstrumentStat = -1; |
174 |
dmsg(1,("no instrument loaded!!!\n")); |
175 |
exit(EXIT_FAILURE); |
176 |
} |
177 |
} |
178 |
catch (RIFF::Exception e) { |
179 |
InstrumentStat = -2; |
180 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
181 |
throw LinuxSamplerException(msg); |
182 |
} |
183 |
catch (InstrumentResourceManagerException e) { |
184 |
InstrumentStat = -3; |
185 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
186 |
throw LinuxSamplerException(msg); |
187 |
} |
188 |
catch (...) { |
189 |
InstrumentStat = -4; |
190 |
throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
191 |
} |
192 |
|
193 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
194 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
195 |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
196 |
|
197 |
InstrumentIdxName = pInstrument->pInfo->Name; |
198 |
InstrumentStat = 100; |
199 |
|
200 |
// inform audio driver for the need of two channels |
201 |
try { |
202 |
if (pEngine && pEngine->pAudioOutputDevice) |
203 |
pEngine->pAudioOutputDevice->AcquireChannels(2); // gig Engine only stereo |
204 |
} |
205 |
catch (AudioOutputException e) { |
206 |
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
207 |
throw LinuxSamplerException(msg); |
208 |
} |
209 |
|
210 |
if (pEngine) pEngine->Enable(); |
211 |
} |
212 |
|
213 |
/** |
214 |
* Will be called by the InstrumentResourceManager when the instrument |
215 |
* we are currently using on this EngineChannel is going to be updated, |
216 |
* so we can stop playback before that happens. |
217 |
*/ |
218 |
void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
219 |
dmsg(3,("gig::Engine: Received instrument update message.\n")); |
220 |
if (pEngine) pEngine->DisableAndLock(); |
221 |
ResetInternal(); |
222 |
this->pInstrument = NULL; |
223 |
} |
224 |
|
225 |
/** |
226 |
* Will be called by the InstrumentResourceManager when the instrument |
227 |
* update process was completed, so we can continue with playback. |
228 |
*/ |
229 |
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
230 |
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
231 |
if (pEngine) pEngine->Enable(); |
232 |
bStatusChanged = true; // status of engine has changed, so set notify flag |
233 |
} |
234 |
|
235 |
/** |
236 |
* Will be called by the InstrumentResourceManager on progress changes |
237 |
* while loading or realoading an instrument for this EngineChannel. |
238 |
* |
239 |
* @param fProgress - current progress as value between 0.0 and 1.0 |
240 |
*/ |
241 |
void EngineChannel::OnResourceProgress(float fProgress) { |
242 |
this->InstrumentStat = int(fProgress * 100.0f); |
243 |
dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat)); |
244 |
bStatusChanged = true; // status of engine has changed, so set notify flag |
245 |
} |
246 |
|
247 |
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
248 |
if (pEngine) { |
249 |
if (pEngine->pAudioOutputDevice == pAudioOut) return; |
250 |
DisconnectAudioOutputDevice(); |
251 |
} |
252 |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
253 |
ResetInternal(); |
254 |
pEvents = new RTList<Event>(pEngine->pEventPool); |
255 |
pCCEvents = new RTList<Event>(pEngine->pEventPool); |
256 |
for (uint i = 0; i < Event::destination_count; i++) { |
257 |
pSynthesisEvents[i] = new RTList<Event>(pEngine->pEventPool); |
258 |
} |
259 |
for (uint i = 0; i < 128; i++) { |
260 |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
261 |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
262 |
} |
263 |
AudioDeviceChannelLeft = 0; |
264 |
AudioDeviceChannelRight = 1; |
265 |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
266 |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
267 |
} |
268 |
|
269 |
void EngineChannel::DisconnectAudioOutputDevice() { |
270 |
if (pEngine) { // if clause to prevent disconnect loops |
271 |
ResetInternal(); |
272 |
if (pEvents) { |
273 |
delete pEvents; |
274 |
pEvents = NULL; |
275 |
} |
276 |
if (pCCEvents) { |
277 |
delete pCCEvents; |
278 |
pCCEvents = NULL; |
279 |
} |
280 |
for (uint i = 0; i < 128; i++) { |
281 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
282 |
delete pMIDIKeyInfo[i].pActiveVoices; |
283 |
pMIDIKeyInfo[i].pActiveVoices = NULL; |
284 |
} |
285 |
if (pMIDIKeyInfo[i].pEvents) { |
286 |
delete pMIDIKeyInfo[i].pEvents; |
287 |
pMIDIKeyInfo[i].pEvents = NULL; |
288 |
} |
289 |
} |
290 |
for (uint i = 0; i < Event::destination_count; i++) { |
291 |
if (pSynthesisEvents[i]) { |
292 |
delete pSynthesisEvents[i]; |
293 |
pSynthesisEvents[i] = NULL; |
294 |
} |
295 |
} |
296 |
Engine* oldEngine = pEngine; |
297 |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
298 |
pEngine = NULL; |
299 |
Engine::FreeEngine(this, oldAudioDevice); |
300 |
AudioDeviceChannelLeft = -1; |
301 |
AudioDeviceChannelRight = -1; |
302 |
} |
303 |
} |
304 |
|
305 |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
306 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
307 |
|
308 |
AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel); |
309 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
310 |
switch (EngineAudioChannel) { |
311 |
case 0: // left output channel |
312 |
pOutputLeft = pChannel->Buffer(); |
313 |
AudioDeviceChannelLeft = AudioDeviceChannel; |
314 |
break; |
315 |
case 1: // right output channel |
316 |
pOutputRight = pChannel->Buffer(); |
317 |
AudioDeviceChannelRight = AudioDeviceChannel; |
318 |
break; |
319 |
default: |
320 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
321 |
} |
322 |
} |
323 |
|
324 |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
325 |
switch (EngineAudioChannel) { |
326 |
case 0: // left channel |
327 |
return AudioDeviceChannelLeft; |
328 |
case 1: // right channel |
329 |
return AudioDeviceChannelRight; |
330 |
default: |
331 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
332 |
} |
333 |
} |
334 |
|
335 |
/** |
336 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
337 |
* voice for the given key. |
338 |
* |
339 |
* @param Key - MIDI key number of the triggered key |
340 |
* @param Velocity - MIDI velocity value of the triggered key |
341 |
*/ |
342 |
void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) { |
343 |
if (pEngine) { |
344 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
345 |
event.Type = Event::type_note_on; |
346 |
event.Param.Note.Key = Key; |
347 |
event.Param.Note.Velocity = Velocity; |
348 |
event.pEngineChannel = this; |
349 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
350 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
351 |
} |
352 |
} |
353 |
|
354 |
/** |
355 |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
356 |
* voice(s) on the given key. |
357 |
* |
358 |
* @param Key - MIDI key number of the released key |
359 |
* @param Velocity - MIDI release velocity value of the released key |
360 |
*/ |
361 |
void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) { |
362 |
if (pEngine) { |
363 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
364 |
event.Type = Event::type_note_off; |
365 |
event.Param.Note.Key = Key; |
366 |
event.Param.Note.Velocity = Velocity; |
367 |
event.pEngineChannel = this; |
368 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
369 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
370 |
} |
371 |
} |
372 |
|
373 |
/** |
374 |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
375 |
* the pitch value for all voices. |
376 |
* |
377 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
378 |
*/ |
379 |
void EngineChannel::SendPitchbend(int Pitch) { |
380 |
if (pEngine) { |
381 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
382 |
event.Type = Event::type_pitchbend; |
383 |
event.Param.Pitch.Pitch = Pitch; |
384 |
event.pEngineChannel = this; |
385 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
386 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
387 |
} |
388 |
} |
389 |
|
390 |
/** |
391 |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
392 |
* continuous controller value has changed. |
393 |
* |
394 |
* @param Controller - MIDI controller number of the occured control change |
395 |
* @param Value - value of the control change |
396 |
*/ |
397 |
void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) { |
398 |
if (pEngine) { |
399 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
400 |
event.Type = Event::type_control_change; |
401 |
event.Param.CC.Controller = Controller; |
402 |
event.Param.CC.Value = Value; |
403 |
event.pEngineChannel = this; |
404 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
405 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
406 |
} |
407 |
} |
408 |
|
409 |
void EngineChannel::ClearEventLists() { |
410 |
pEvents->clear(); |
411 |
pCCEvents->clear(); |
412 |
for (uint i = 0; i < Event::destination_count; i++) { |
413 |
pSynthesisEvents[i]->clear(); |
414 |
} |
415 |
// empty MIDI key specific event lists |
416 |
{ |
417 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
418 |
RTList<uint>::Iterator end = pActiveKeys->end(); |
419 |
for(; iuiKey != end; ++iuiKey) { |
420 |
pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
421 |
} |
422 |
} |
423 |
} |
424 |
|
425 |
void EngineChannel::ResetControllers() { |
426 |
// set all MIDI controller values to zero |
427 |
memset(ControllerTable, 0x00, 128); |
428 |
} |
429 |
|
430 |
/** |
431 |
* Copy all events from the engine channel's input event queue buffer to |
432 |
* the internal event list. This will be done at the beginning of each |
433 |
* audio cycle (that is each RenderAudio() call) to distinguish all |
434 |
* events which have to be processed in the current audio cycle. Each |
435 |
* EngineChannel has it's own input event queue for the common channel |
436 |
* specific events (like NoteOn, NoteOff and ControlChange events). |
437 |
* Beside that, the engine also has a input event queue for global |
438 |
* events (usually SysEx messages). |
439 |
* |
440 |
* @param Samples - number of sample points to be processed in the |
441 |
* current audio cycle |
442 |
*/ |
443 |
void EngineChannel::ImportEvents(uint Samples) { |
444 |
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
445 |
Event* pEvent; |
446 |
while (true) { |
447 |
// get next event from input event queue |
448 |
if (!(pEvent = eventQueueReader.pop())) break; |
449 |
// if younger event reached, ignore that and all subsequent ones for now |
450 |
if (pEvent->FragmentPos() >= Samples) { |
451 |
eventQueueReader--; |
452 |
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
453 |
pEvent->ResetFragmentPos(); |
454 |
break; |
455 |
} |
456 |
// copy event to internal event list |
457 |
if (pEvents->poolIsEmpty()) { |
458 |
dmsg(1,("Event pool emtpy!\n")); |
459 |
break; |
460 |
} |
461 |
*pEvents->allocAppend() = *pEvent; |
462 |
} |
463 |
eventQueueReader.free(); // free all copied events from input queue |
464 |
} |
465 |
|
466 |
float EngineChannel::Volume() { |
467 |
return GlobalVolume; |
468 |
} |
469 |
|
470 |
void EngineChannel::Volume(float f) { |
471 |
GlobalVolume = f; |
472 |
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
473 |
} |
474 |
|
475 |
uint EngineChannel::Channels() { |
476 |
return 2; |
477 |
} |
478 |
|
479 |
String EngineChannel::InstrumentFileName() { |
480 |
return InstrumentFile; |
481 |
} |
482 |
|
483 |
String EngineChannel::InstrumentName() { |
484 |
return InstrumentIdxName; |
485 |
} |
486 |
|
487 |
int EngineChannel::InstrumentIndex() { |
488 |
return InstrumentIdx; |
489 |
} |
490 |
|
491 |
int EngineChannel::InstrumentStatus() { |
492 |
return InstrumentStat; |
493 |
} |
494 |
|
495 |
String EngineChannel::EngineName() { |
496 |
return LS_GIG_ENGINE_NAME; |
497 |
} |
498 |
|
499 |
}} // namespace LinuxSampler::gig |