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 Christian Schoenebeck * |
* Copyright (C) 2005 - 2007 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 * |
23 |
|
|
24 |
#include "EngineChannel.h" |
#include "EngineChannel.h" |
25 |
|
|
26 |
|
#include "../../common/global_private.h" |
27 |
|
|
28 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
29 |
|
|
30 |
EngineChannel::EngineChannel() { |
EngineChannel::EngineChannel() { |
32 |
pEngine = NULL; |
pEngine = NULL; |
33 |
pInstrument = NULL; |
pInstrument = NULL; |
34 |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
35 |
pCCEvents = NULL; // we allocate when we retrieve the right Engine object |
pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
|
pEventQueue = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
|
36 |
pActiveKeys = new Pool<uint>(128); |
pActiveKeys = new Pool<uint>(128); |
37 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
38 |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
43 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
44 |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
45 |
} |
} |
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i] = NULL; // we allocate when we retrieve the right Engine object |
|
|
} |
|
46 |
InstrumentIdx = -1; |
InstrumentIdx = -1; |
47 |
InstrumentStat = -1; |
InstrumentStat = -1; |
48 |
|
pChannelLeft = NULL; |
49 |
|
pChannelRight = NULL; |
50 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
51 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
52 |
|
pMidiInputPort = NULL; |
53 |
|
midiChannel = midi_chan_all; |
54 |
|
ResetControllers(); |
55 |
|
SoloMode = false; |
56 |
|
PortamentoMode = false; |
57 |
|
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
58 |
} |
} |
59 |
|
|
60 |
EngineChannel::~EngineChannel() { |
EngineChannel::~EngineChannel() { |
63 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
64 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
65 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
66 |
|
RemoveAllFxSends(); |
67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
|
* Implementation of virtual method from abstract EngineChannel interface. |
71 |
|
* This method will periodically be polled (e.g. by the LSCP server) to |
72 |
|
* check if some engine channel parameter has changed since the last |
73 |
|
* StatusChanged() call. |
74 |
|
* |
75 |
|
* This method can also be used to mark the engine channel as changed |
76 |
|
* from outside, e.g. by a MIDI input device. The optional argument |
77 |
|
* \a nNewStatus can be used for this. |
78 |
|
* |
79 |
|
* TODO: This "poll method" is just a lazy solution and might be |
80 |
|
* replaced in future. |
81 |
|
* @param bNewStatus - (optional, default: false) sets the new status flag |
82 |
|
* @returns true if engine channel status has changed since last |
83 |
|
* StatusChanged() call |
84 |
|
*/ |
85 |
|
bool EngineChannel::StatusChanged(bool bNewStatus) { |
86 |
|
bool b = bStatusChanged; |
87 |
|
bStatusChanged = bNewStatus; |
88 |
|
return b; |
89 |
|
} |
90 |
|
|
91 |
|
void EngineChannel::Reset() { |
92 |
|
if (pEngine) pEngine->DisableAndLock(); |
93 |
|
ResetInternal(); |
94 |
|
ResetControllers(); |
95 |
|
if (pEngine) { |
96 |
|
pEngine->Enable(); |
97 |
|
pEngine->Reset(); |
98 |
|
} |
99 |
} |
} |
100 |
|
|
101 |
/** |
/** |
102 |
* This method is not thread safe! |
* This method is not thread safe! |
103 |
*/ |
*/ |
104 |
void EngineChannel::ResetInternal() { |
void EngineChannel::ResetInternal() { |
|
Pitch = 0; |
|
|
SustainPedal = false; |
|
|
GlobalVolume = 1.0; |
|
|
GlobalPanLeft = 1.0f; |
|
|
GlobalPanRight = 1.0f; |
|
105 |
CurrentKeyDimension = 0; |
CurrentKeyDimension = 0; |
106 |
|
|
|
ResetControllers(); |
|
|
|
|
107 |
// reset key info |
// reset key info |
108 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
109 |
if (pMIDIKeyInfo[i].pActiveVoices) |
if (pMIDIKeyInfo[i].pActiveVoices) |
116 |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
117 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
118 |
} |
} |
119 |
|
SoloKey = -1; // no solo key active yet |
120 |
|
PortamentoPos = -1.0f; // no portamento active yet |
121 |
|
|
122 |
// reset all key groups |
// reset all key groups |
123 |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
130 |
pEventQueue->init(); |
pEventQueue->init(); |
131 |
|
|
132 |
if (pEngine) pEngine->ResetInternal(); |
if (pEngine) pEngine->ResetInternal(); |
133 |
|
|
134 |
|
// status of engine channel has changed, so set notify flag |
135 |
|
bStatusChanged = true; |
136 |
} |
} |
137 |
|
|
138 |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
161 |
* This method will then actually start to load the instrument and block |
* This method will then actually start to load the instrument and block |
162 |
* the calling thread until loading was completed. |
* the calling thread until loading was completed. |
163 |
* |
* |
|
* @returns detailed description of the method call result |
|
164 |
* @see PrepareLoadInstrument() |
* @see PrepareLoadInstrument() |
165 |
*/ |
*/ |
166 |
void EngineChannel::LoadInstrument() { |
void EngineChannel::LoadInstrument() { |
167 |
|
::gig::Instrument* oldInstrument = pInstrument; |
|
if (pEngine) pEngine->DisableAndLock(); |
|
|
|
|
|
ResetInternal(); |
|
168 |
|
|
169 |
// free old instrument |
// free old instrument |
170 |
if (pInstrument) { |
if (oldInstrument) { |
171 |
// give old instrument back to instrument manager |
if (pEngine) { |
172 |
Engine::instruments.HandBack(pInstrument, this); |
// make sure we don't trigger any new notes with the |
173 |
|
// old instrument |
174 |
|
::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0); |
175 |
|
|
176 |
|
// give old instrument back to instrument manager, but |
177 |
|
// keep the dimension regions and samples that are in |
178 |
|
// use |
179 |
|
Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse); |
180 |
|
} else { |
181 |
|
Engine::instruments.HandBack(oldInstrument, this); |
182 |
|
} |
183 |
} |
} |
184 |
|
|
185 |
// delete all key groups |
// delete all key groups |
186 |
ActiveKeyGroups.clear(); |
ActiveKeyGroups.clear(); |
187 |
|
|
188 |
// request gig instrument from instrument manager |
// request gig instrument from instrument manager |
189 |
|
::gig::Instrument* newInstrument; |
190 |
try { |
try { |
191 |
instrument_id_t instrid; |
InstrumentManager::instrument_id_t instrid; |
192 |
instrid.FileName = InstrumentFile; |
instrid.FileName = InstrumentFile; |
193 |
instrid.iInstrument = InstrumentIdx; |
instrid.Index = InstrumentIdx; |
194 |
pInstrument = Engine::instruments.Borrow(instrid, this); |
newInstrument = Engine::instruments.Borrow(instrid, this); |
195 |
if (!pInstrument) { |
if (!newInstrument) { |
196 |
InstrumentStat = -1; |
throw InstrumentManagerException("resource was not created"); |
|
dmsg(1,("no instrument loaded!!!\n")); |
|
|
exit(EXIT_FAILURE); |
|
197 |
} |
} |
198 |
} |
} |
199 |
catch (RIFF::Exception e) { |
catch (RIFF::Exception e) { |
200 |
InstrumentStat = -2; |
InstrumentStat = -2; |
201 |
|
StatusChanged(true); |
202 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
203 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
204 |
} |
} |
205 |
catch (InstrumentResourceManagerException e) { |
catch (InstrumentManagerException e) { |
206 |
InstrumentStat = -3; |
InstrumentStat = -3; |
207 |
|
StatusChanged(true); |
208 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
209 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
210 |
} |
} |
211 |
catch (...) { |
catch (...) { |
212 |
InstrumentStat = -4; |
InstrumentStat = -4; |
213 |
throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
StatusChanged(true); |
214 |
|
throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
215 |
} |
} |
216 |
|
|
217 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
// rebuild ActiveKeyGroups map with key groups of current instrument |
218 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion()) |
219 |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
220 |
|
|
221 |
InstrumentIdxName = pInstrument->pInfo->Name; |
InstrumentIdxName = newInstrument->pInfo->Name; |
222 |
InstrumentStat = 100; |
InstrumentStat = 100; |
223 |
|
|
224 |
// inform audio driver for the need of two channels |
if (pEngine) pEngine->ChangeInstrument(this, newInstrument); |
225 |
try { |
else pInstrument = newInstrument; |
226 |
if (pEngine && pEngine->pAudioOutputDevice) |
|
227 |
pEngine->pAudioOutputDevice->AcquireChannels(2); // gig Engine only stereo |
StatusChanged(true); |
|
} |
|
|
catch (AudioOutputException e) { |
|
|
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
|
|
throw LinuxSamplerException(msg); |
|
|
} |
|
|
|
|
|
if (pEngine) pEngine->Enable(); |
|
228 |
} |
} |
229 |
|
|
230 |
/** |
/** |
246 |
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
247 |
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
248 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
249 |
|
bStatusChanged = true; // status of engine has changed, so set notify flag |
250 |
} |
} |
251 |
|
|
252 |
/** |
/** |
258 |
void EngineChannel::OnResourceProgress(float fProgress) { |
void EngineChannel::OnResourceProgress(float fProgress) { |
259 |
this->InstrumentStat = int(fProgress * 100.0f); |
this->InstrumentStat = int(fProgress * 100.0f); |
260 |
dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat)); |
dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat)); |
261 |
|
bStatusChanged = true; // status of engine has changed, so set notify flag |
262 |
} |
} |
263 |
|
|
264 |
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
268 |
} |
} |
269 |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
270 |
ResetInternal(); |
ResetInternal(); |
271 |
pEvents = new RTList<Event>(pEngine->pEventPool); |
pEvents = new RTList<Event>(pEngine->pEventPool); |
|
pCCEvents = new RTList<Event>(pEngine->pEventPool); |
|
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i] = new RTList<Event>(pEngine->pEventPool); |
|
|
} |
|
272 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
273 |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
274 |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
275 |
} |
} |
276 |
AudioDeviceChannelLeft = 0; |
AudioDeviceChannelLeft = 0; |
277 |
AudioDeviceChannelRight = 1; |
AudioDeviceChannelRight = 1; |
278 |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
if (fxSends.empty()) { // render directly into the AudioDevice's output buffers |
279 |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
pChannelLeft = pAudioOut->Channel(AudioDeviceChannelLeft); |
280 |
|
pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight); |
281 |
|
} else { // use local buffers for rendering and copy later |
282 |
|
// ensure the local buffers have the correct size |
283 |
|
if (pChannelLeft) delete pChannelLeft; |
284 |
|
if (pChannelRight) delete pChannelRight; |
285 |
|
pChannelLeft = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle()); |
286 |
|
pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle()); |
287 |
|
} |
288 |
|
if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable(); |
289 |
|
MidiInputPort::AddSysexListener(pEngine); |
290 |
} |
} |
291 |
|
|
292 |
void EngineChannel::DisconnectAudioOutputDevice() { |
void EngineChannel::DisconnectAudioOutputDevice() { |
296 |
delete pEvents; |
delete pEvents; |
297 |
pEvents = NULL; |
pEvents = NULL; |
298 |
} |
} |
|
if (pCCEvents) { |
|
|
delete pCCEvents; |
|
|
pCCEvents = NULL; |
|
|
} |
|
299 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
300 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
if (pMIDIKeyInfo[i].pActiveVoices) { |
301 |
delete pMIDIKeyInfo[i].pActiveVoices; |
delete pMIDIKeyInfo[i].pActiveVoices; |
306 |
pMIDIKeyInfo[i].pEvents = NULL; |
pMIDIKeyInfo[i].pEvents = NULL; |
307 |
} |
} |
308 |
} |
} |
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
if (pSynthesisEvents[i]) { |
|
|
delete pSynthesisEvents[i]; |
|
|
pSynthesisEvents[i] = NULL; |
|
|
} |
|
|
} |
|
309 |
Engine* oldEngine = pEngine; |
Engine* oldEngine = pEngine; |
310 |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
311 |
pEngine = NULL; |
pEngine = NULL; |
312 |
Engine::FreeEngine(this, oldAudioDevice); |
Engine::FreeEngine(this, oldAudioDevice); |
313 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
314 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
315 |
|
if (!fxSends.empty()) { // free the local rendering buffers |
316 |
|
if (pChannelLeft) delete pChannelLeft; |
317 |
|
if (pChannelRight) delete pChannelRight; |
318 |
|
} |
319 |
|
pChannelLeft = NULL; |
320 |
|
pChannelRight = NULL; |
321 |
} |
} |
322 |
} |
} |
323 |
|
|
324 |
|
AudioOutputDevice* EngineChannel::GetAudioOutputDevice() { |
325 |
|
return (pEngine) ? pEngine->pAudioOutputDevice : NULL; |
326 |
|
} |
327 |
|
|
328 |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
329 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
330 |
|
|
332 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
333 |
switch (EngineAudioChannel) { |
switch (EngineAudioChannel) { |
334 |
case 0: // left output channel |
case 0: // left output channel |
335 |
pOutputLeft = pChannel->Buffer(); |
if (fxSends.empty()) pChannelLeft = pChannel; |
336 |
AudioDeviceChannelLeft = AudioDeviceChannel; |
AudioDeviceChannelLeft = AudioDeviceChannel; |
337 |
break; |
break; |
338 |
case 1: // right output channel |
case 1: // right output channel |
339 |
pOutputRight = pChannel->Buffer(); |
if (fxSends.empty()) pChannelRight = pChannel; |
340 |
AudioDeviceChannelRight = AudioDeviceChannel; |
AudioDeviceChannelRight = AudioDeviceChannel; |
341 |
break; |
break; |
342 |
default: |
default: |
343 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
344 |
} |
} |
345 |
|
|
346 |
|
bStatusChanged = true; |
347 |
} |
} |
348 |
|
|
349 |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
357 |
} |
} |
358 |
} |
} |
359 |
|
|
360 |
|
void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) { |
361 |
|
if (!pMidiPort || pMidiPort == this->pMidiInputPort) return; |
362 |
|
DisconnectMidiInputPort(); |
363 |
|
this->pMidiInputPort = pMidiPort; |
364 |
|
this->midiChannel = MidiChannel; |
365 |
|
pMidiPort->Connect(this, MidiChannel); |
366 |
|
} |
367 |
|
|
368 |
|
void EngineChannel::DisconnectMidiInputPort() { |
369 |
|
MidiInputPort* pOldPort = this->pMidiInputPort; |
370 |
|
this->pMidiInputPort = NULL; |
371 |
|
if (pOldPort) pOldPort->Disconnect(this); |
372 |
|
} |
373 |
|
|
374 |
|
MidiInputPort* EngineChannel::GetMidiInputPort() { |
375 |
|
return pMidiInputPort; |
376 |
|
} |
377 |
|
|
378 |
|
midi_chan_t EngineChannel::MidiChannel() { |
379 |
|
return midiChannel; |
380 |
|
} |
381 |
|
|
382 |
|
FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) { |
383 |
|
if (pEngine) pEngine->DisableAndLock(); |
384 |
|
FxSend* pFxSend = new FxSend(this, MidiCtrl, Name); |
385 |
|
if (fxSends.empty()) { |
386 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
387 |
|
AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice; |
388 |
|
// create local render buffers |
389 |
|
pChannelLeft = new AudioChannel(0, pDevice->MaxSamplesPerCycle()); |
390 |
|
pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle()); |
391 |
|
} else { |
392 |
|
// postpone local render buffer creation until audio device is assigned |
393 |
|
pChannelLeft = NULL; |
394 |
|
pChannelRight = NULL; |
395 |
|
} |
396 |
|
} |
397 |
|
fxSends.push_back(pFxSend); |
398 |
|
if (pEngine) pEngine->Enable(); |
399 |
|
fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount()); |
400 |
|
|
401 |
|
return pFxSend; |
402 |
|
} |
403 |
|
|
404 |
|
FxSend* EngineChannel::GetFxSend(uint FxSendIndex) { |
405 |
|
return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL; |
406 |
|
} |
407 |
|
|
408 |
|
uint EngineChannel::GetFxSendCount() { |
409 |
|
return fxSends.size(); |
410 |
|
} |
411 |
|
|
412 |
|
void EngineChannel::RemoveFxSend(FxSend* pFxSend) { |
413 |
|
if (pEngine) pEngine->DisableAndLock(); |
414 |
|
for ( |
415 |
|
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
416 |
|
iter != fxSends.end(); iter++ |
417 |
|
) { |
418 |
|
if (*iter == pFxSend) { |
419 |
|
delete pFxSend; |
420 |
|
fxSends.erase(iter); |
421 |
|
if (fxSends.empty()) { |
422 |
|
// destroy local render buffers |
423 |
|
if (pChannelLeft) delete pChannelLeft; |
424 |
|
if (pChannelRight) delete pChannelRight; |
425 |
|
// fallback to render directly into AudioOutputDevice's buffers |
426 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
427 |
|
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
428 |
|
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
429 |
|
} else { // we update the pointers later |
430 |
|
pChannelLeft = NULL; |
431 |
|
pChannelRight = NULL; |
432 |
|
} |
433 |
|
} |
434 |
|
break; |
435 |
|
} |
436 |
|
} |
437 |
|
if (pEngine) pEngine->Enable(); |
438 |
|
fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount()); |
439 |
|
} |
440 |
|
|
441 |
/** |
/** |
442 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
443 |
* voice for the given key. |
* voice for the given key. This method is meant for real time rendering, |
444 |
|
* that is an event will immediately be created with the current system |
445 |
|
* time as time stamp. |
446 |
* |
* |
447 |
* @param Key - MIDI key number of the triggered key |
* @param Key - MIDI key number of the triggered key |
448 |
* @param Velocity - MIDI velocity value of the triggered key |
* @param Velocity - MIDI velocity value of the triggered key |
460 |
} |
} |
461 |
|
|
462 |
/** |
/** |
463 |
|
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
464 |
|
* voice for the given key. This method is meant for offline rendering |
465 |
|
* and / or for cases where the exact position of the event in the current |
466 |
|
* audio fragment is already known. |
467 |
|
* |
468 |
|
* @param Key - MIDI key number of the triggered key |
469 |
|
* @param Velocity - MIDI velocity value of the triggered key |
470 |
|
* @param FragmentPos - sample point position in the current audio |
471 |
|
* fragment to which this event belongs to |
472 |
|
*/ |
473 |
|
void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
474 |
|
if (FragmentPos < 0) { |
475 |
|
dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!")); |
476 |
|
} |
477 |
|
else if (pEngine) { |
478 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
479 |
|
event.Type = Event::type_note_on; |
480 |
|
event.Param.Note.Key = Key; |
481 |
|
event.Param.Note.Velocity = Velocity; |
482 |
|
event.pEngineChannel = this; |
483 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
484 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
485 |
|
} |
486 |
|
} |
487 |
|
|
488 |
|
/** |
489 |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
490 |
* voice(s) on the given key. |
* voice(s) on the given key. This method is meant for real time rendering, |
491 |
|
* that is an event will immediately be created with the current system |
492 |
|
* time as time stamp. |
493 |
* |
* |
494 |
* @param Key - MIDI key number of the released key |
* @param Key - MIDI key number of the released key |
495 |
* @param Velocity - MIDI release velocity value of the released key |
* @param Velocity - MIDI release velocity value of the released key |
507 |
} |
} |
508 |
|
|
509 |
/** |
/** |
510 |
|
* Will be called by the MIDIIn Thread to signal the audio thread to release |
511 |
|
* voice(s) on the given key. This method is meant for offline rendering |
512 |
|
* and / or for cases where the exact position of the event in the current |
513 |
|
* audio fragment is already known. |
514 |
|
* |
515 |
|
* @param Key - MIDI key number of the released key |
516 |
|
* @param Velocity - MIDI release velocity value of the released key |
517 |
|
* @param FragmentPos - sample point position in the current audio |
518 |
|
* fragment to which this event belongs to |
519 |
|
*/ |
520 |
|
void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
521 |
|
if (FragmentPos < 0) { |
522 |
|
dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!")); |
523 |
|
} |
524 |
|
else if (pEngine) { |
525 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
526 |
|
event.Type = Event::type_note_off; |
527 |
|
event.Param.Note.Key = Key; |
528 |
|
event.Param.Note.Velocity = Velocity; |
529 |
|
event.pEngineChannel = this; |
530 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
531 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
532 |
|
} |
533 |
|
} |
534 |
|
|
535 |
|
/** |
536 |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
537 |
* the pitch value for all voices. |
* the pitch value for all voices. This method is meant for real time |
538 |
|
* rendering, that is an event will immediately be created with the |
539 |
|
* current system time as time stamp. |
540 |
* |
* |
541 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
542 |
*/ |
*/ |
552 |
} |
} |
553 |
|
|
554 |
/** |
/** |
555 |
|
* Will be called by the MIDIIn Thread to signal the audio thread to change |
556 |
|
* the pitch value for all voices. This method is meant for offline |
557 |
|
* rendering and / or for cases where the exact position of the event in |
558 |
|
* the current audio fragment is already known. |
559 |
|
* |
560 |
|
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
561 |
|
* @param FragmentPos - sample point position in the current audio |
562 |
|
* fragment to which this event belongs to |
563 |
|
*/ |
564 |
|
void EngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) { |
565 |
|
if (FragmentPos < 0) { |
566 |
|
dmsg(1,("EngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!")); |
567 |
|
} |
568 |
|
else if (pEngine) { |
569 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
570 |
|
event.Type = Event::type_pitchbend; |
571 |
|
event.Param.Pitch.Pitch = Pitch; |
572 |
|
event.pEngineChannel = this; |
573 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
574 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
575 |
|
} |
576 |
|
} |
577 |
|
|
578 |
|
/** |
579 |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
580 |
* continuous controller value has changed. |
* continuous controller value has changed. This method is meant for real |
581 |
|
* time rendering, that is an event will immediately be created with the |
582 |
|
* current system time as time stamp. |
583 |
* |
* |
584 |
* @param Controller - MIDI controller number of the occured control change |
* @param Controller - MIDI controller number of the occured control change |
585 |
* @param Value - value of the control change |
* @param Value - value of the control change |
596 |
} |
} |
597 |
} |
} |
598 |
|
|
599 |
|
/** |
600 |
|
* Will be called by the MIDIIn Thread to signal the audio thread that a |
601 |
|
* continuous controller value has changed. This method is meant for |
602 |
|
* offline rendering and / or for cases where the exact position of the |
603 |
|
* event in the current audio fragment is already known. |
604 |
|
* |
605 |
|
* @param Controller - MIDI controller number of the occured control change |
606 |
|
* @param Value - value of the control change |
607 |
|
* @param FragmentPos - sample point position in the current audio |
608 |
|
* fragment to which this event belongs to |
609 |
|
*/ |
610 |
|
void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) { |
611 |
|
if (FragmentPos < 0) { |
612 |
|
dmsg(1,("EngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!")); |
613 |
|
} |
614 |
|
else if (pEngine) { |
615 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
616 |
|
event.Type = Event::type_control_change; |
617 |
|
event.Param.CC.Controller = Controller; |
618 |
|
event.Param.CC.Value = Value; |
619 |
|
event.pEngineChannel = this; |
620 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
621 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
622 |
|
} |
623 |
|
} |
624 |
|
|
625 |
void EngineChannel::ClearEventLists() { |
void EngineChannel::ClearEventLists() { |
626 |
pEvents->clear(); |
pEvents->clear(); |
|
pCCEvents->clear(); |
|
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i]->clear(); |
|
|
} |
|
627 |
// empty MIDI key specific event lists |
// empty MIDI key specific event lists |
628 |
{ |
{ |
629 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
635 |
} |
} |
636 |
|
|
637 |
void EngineChannel::ResetControllers() { |
void EngineChannel::ResetControllers() { |
638 |
|
Pitch = 0; |
639 |
|
SustainPedal = false; |
640 |
|
SostenutoPedal = false; |
641 |
|
GlobalVolume = 1.0f; |
642 |
|
MidiVolume = 1.0; |
643 |
|
GlobalPanLeft = 1.0f; |
644 |
|
GlobalPanRight = 1.0f; |
645 |
|
GlobalTranspose = 0; |
646 |
// set all MIDI controller values to zero |
// set all MIDI controller values to zero |
647 |
memset(ControllerTable, 0x00, 128); |
memset(ControllerTable, 0x00, 129); |
648 |
|
// reset all FX Send levels |
649 |
|
for ( |
650 |
|
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
651 |
|
iter != fxSends.end(); iter++ |
652 |
|
) { |
653 |
|
(*iter)->Reset(); |
654 |
|
} |
655 |
} |
} |
656 |
|
|
657 |
/** |
/** |
668 |
* current audio cycle |
* current audio cycle |
669 |
*/ |
*/ |
670 |
void EngineChannel::ImportEvents(uint Samples) { |
void EngineChannel::ImportEvents(uint Samples) { |
671 |
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
672 |
Event* pEvent; |
Event* pEvent; |
673 |
while (true) { |
while (true) { |
674 |
// get next event from input event queue |
// get next event from input event queue |
690 |
eventQueueReader.free(); // free all copied events from input queue |
eventQueueReader.free(); // free all copied events from input queue |
691 |
} |
} |
692 |
|
|
693 |
|
void EngineChannel::RemoveAllFxSends() { |
694 |
|
if (pEngine) pEngine->DisableAndLock(); |
695 |
|
if (!fxSends.empty()) { // free local render buffers |
696 |
|
if (pChannelLeft) { |
697 |
|
delete pChannelLeft; |
698 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
699 |
|
// fallback to render directly to the AudioOutputDevice's buffer |
700 |
|
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
701 |
|
} else pChannelLeft = NULL; |
702 |
|
} |
703 |
|
if (pChannelRight) { |
704 |
|
delete pChannelRight; |
705 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
706 |
|
// fallback to render directly to the AudioOutputDevice's buffer |
707 |
|
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
708 |
|
} else pChannelRight = NULL; |
709 |
|
} |
710 |
|
} |
711 |
|
for (int i = 0; i < fxSends.size(); i++) delete fxSends[i]; |
712 |
|
fxSends.clear(); |
713 |
|
if (pEngine) pEngine->Enable(); |
714 |
|
} |
715 |
|
|
716 |
float EngineChannel::Volume() { |
float EngineChannel::Volume() { |
717 |
return GlobalVolume; |
return GlobalVolume; |
718 |
} |
} |
719 |
|
|
720 |
void EngineChannel::Volume(float f) { |
void EngineChannel::Volume(float f) { |
721 |
GlobalVolume = f; |
GlobalVolume = f; |
722 |
|
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
723 |
} |
} |
724 |
|
|
725 |
uint EngineChannel::Channels() { |
uint EngineChannel::Channels() { |
745 |
String EngineChannel::EngineName() { |
String EngineChannel::EngineName() { |
746 |
return LS_GIG_ENGINE_NAME; |
return LS_GIG_ENGINE_NAME; |
747 |
} |
} |
748 |
|
|
749 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |