1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005-2008 Christian Schoenebeck * |
7 |
* Copyright (C) 2009-2010 Christian Schoenebeck and 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 "AbstractEngine.h" |
26 |
#include "AbstractEngineChannel.h" |
27 |
#include "EngineFactory.h" |
28 |
#include "../common/global_private.h" |
29 |
#include "../effects/EffectFactory.h" |
30 |
|
31 |
namespace LinuxSampler { |
32 |
|
33 |
//InstrumentResourceManager Engine::instruments; |
34 |
|
35 |
std::map<AbstractEngine::Format, std::map<AudioOutputDevice*,AbstractEngine*> > AbstractEngine::engines; |
36 |
|
37 |
/** |
38 |
* Get an AbstractEngine object for the given AbstractEngineChannel and the |
39 |
* given AudioOutputDevice. All engine channels which are connected to |
40 |
* the same audio output device will use the same engine instance. This |
41 |
* method will be called by an EngineChannel whenever it's |
42 |
* connecting to an audio output device. |
43 |
* |
44 |
* @param pChannel - engine channel which acquires an engine object |
45 |
* @param pDevice - the audio output device \a pChannel is connected to |
46 |
*/ |
47 |
AbstractEngine* AbstractEngine::AcquireEngine(AbstractEngineChannel* pChannel, AudioOutputDevice* pDevice) { |
48 |
AbstractEngine* pEngine = NULL; |
49 |
// check if there's already an engine for the given audio output device |
50 |
std::map<AbstractEngine::Format, std::map<AudioOutputDevice*,AbstractEngine*> >::iterator it; |
51 |
it = engines.find(pChannel->GetEngineFormat()); |
52 |
if (it != engines.end() && (*it).second.count(pDevice)) { |
53 |
dmsg(4,("Using existing Engine.\n")); |
54 |
pEngine = (*it).second[pDevice]; |
55 |
|
56 |
// Disable the engine while the new engine channel is |
57 |
// added and initialized. The engine will be enabled again |
58 |
// in EngineChannel::Connect. |
59 |
pEngine->DisableAndLock(); |
60 |
} else { // create a new engine (and disk thread) instance for the given audio output device |
61 |
dmsg(4,("Creating new Engine.\n")); |
62 |
pEngine = (AbstractEngine*) EngineFactory::Create(pChannel->EngineName()); |
63 |
pEngine->Connect(pDevice); |
64 |
engines[pChannel->GetEngineFormat()][pDevice] = pEngine; |
65 |
} |
66 |
// register engine channel to the engine instance |
67 |
pEngine->engineChannels.add(pChannel); |
68 |
// remember index in the ArrayList |
69 |
pChannel->iEngineIndexSelf = pEngine->engineChannels.size() - 1; |
70 |
dmsg(4,("This Engine has now %d EngineChannels.\n",pEngine->engineChannels.size())); |
71 |
return pEngine; |
72 |
} |
73 |
|
74 |
AbstractEngine::AbstractEngine() { |
75 |
pAudioOutputDevice = NULL; |
76 |
pEventGenerator = NULL; |
77 |
pSysexBuffer = new RingBuffer<uint8_t,false>(CONFIG_SYSEX_BUFFER_SIZE, 0); |
78 |
pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
79 |
pEventPool = new Pool<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT); |
80 |
pGlobalEvents = new RTList<Event>(pEventPool); |
81 |
FrameTime = 0; |
82 |
RandomSeed = 0; |
83 |
pDedicatedVoiceChannelLeft = pDedicatedVoiceChannelRight = NULL; |
84 |
} |
85 |
|
86 |
AbstractEngine::~AbstractEngine() { |
87 |
if (pEventQueue) delete pEventQueue; |
88 |
if (pEventPool) delete pEventPool; |
89 |
if (pEventGenerator) delete pEventGenerator; |
90 |
if (pGlobalEvents) delete pGlobalEvents; |
91 |
if (pSysexBuffer) delete pSysexBuffer; |
92 |
if (pDedicatedVoiceChannelLeft) delete pDedicatedVoiceChannelLeft; |
93 |
if (pDedicatedVoiceChannelRight) delete pDedicatedVoiceChannelRight; |
94 |
Unregister(); |
95 |
} |
96 |
|
97 |
/** |
98 |
* Once an engine channel is disconnected from an audio output device, |
99 |
* it will immediately call this method to unregister itself from the |
100 |
* engine instance and if that engine instance is not used by any other |
101 |
* engine channel anymore, then that engine instance will be destroyed. |
102 |
* |
103 |
* @param pChannel - engine channel which wants to disconnect from it's |
104 |
* engine instance |
105 |
* @param pDevice - audio output device \a pChannel was connected to |
106 |
*/ |
107 |
void AbstractEngine::FreeEngine(AbstractEngineChannel* pChannel, AudioOutputDevice* pDevice) { |
108 |
dmsg(4,("Disconnecting EngineChannel from Engine.\n")); |
109 |
AbstractEngine* pEngine = engines[pChannel->GetEngineFormat()][pDevice]; |
110 |
// unregister EngineChannel from the Engine instance |
111 |
pEngine->engineChannels.remove(pChannel); |
112 |
// if the used Engine instance is not used anymore, then destroy it |
113 |
if (pEngine->engineChannels.empty()) { |
114 |
pDevice->Disconnect(pEngine); |
115 |
engines[pChannel->GetEngineFormat()].erase(pDevice); |
116 |
delete pEngine; |
117 |
dmsg(4,("Destroying Engine.\n")); |
118 |
} |
119 |
else dmsg(4,("This Engine has now %d EngineChannels.\n",pEngine->engineChannels.size())); |
120 |
} |
121 |
|
122 |
void AbstractEngine::Enable() { |
123 |
dmsg(3,("AbstractEngine: enabling\n")); |
124 |
EngineDisabled.PushAndUnlock(false, 2, 0, true); // set condition object 'EngineDisabled' to false (wait max. 2s) |
125 |
dmsg(3,("AbstractEngine: enabled (val=%d)\n", EngineDisabled.GetUnsafe())); |
126 |
} |
127 |
|
128 |
/** |
129 |
* Temporarily stop the engine to not do anything. The engine will just be |
130 |
* frozen during that time, that means after enabling it again it will |
131 |
* continue where it was, with all its voices and playback state it had at |
132 |
* the point of disabling. Notice that the engine's (audio) thread will |
133 |
* continue to run, it just remains in an inactive loop during that time. |
134 |
* |
135 |
* If you need to be sure that all voices and disk streams are killed as |
136 |
* well, use @c SuspendAll() instead. |
137 |
* |
138 |
* @see Enable(), SuspendAll() |
139 |
*/ |
140 |
void AbstractEngine::Disable() { |
141 |
dmsg(3,("AbstractEngine: disabling\n")); |
142 |
bool* pWasDisabled = EngineDisabled.PushAndUnlock(true, 2); // wait max. 2s |
143 |
if (!pWasDisabled) dmsg(3,("AbstractEngine warning: Timeout waiting to disable engine.\n")); |
144 |
} |
145 |
|
146 |
void AbstractEngine::DisableAndLock() { |
147 |
dmsg(3,("AbstractEngine: disabling\n")); |
148 |
bool* pWasDisabled = EngineDisabled.Push(true, 2); // wait max. 2s |
149 |
if (!pWasDisabled) dmsg(3,("AbstractEngine warning: Timeout waiting to disable engine.\n")); |
150 |
} |
151 |
|
152 |
/** |
153 |
* Reset all voices and disk thread and clear input event queue and all |
154 |
* control and status variables. |
155 |
*/ |
156 |
void AbstractEngine::Reset() { |
157 |
DisableAndLock(); |
158 |
ResetInternal(); |
159 |
ResetScaleTuning(); |
160 |
Enable(); |
161 |
} |
162 |
|
163 |
/** |
164 |
* Reset to normal, chromatic scale (means equal tempered). |
165 |
*/ |
166 |
void AbstractEngine::ResetScaleTuning() { |
167 |
memset(&ScaleTuning[0], 0x00, 12); |
168 |
} |
169 |
|
170 |
/** |
171 |
* Copy all events from the engine's global input queue buffer to the |
172 |
* engine's internal event list. This will be done at the beginning of |
173 |
* each audio cycle (that is each RenderAudio() call) to distinguish |
174 |
* all global events which have to be processed in the current audio |
175 |
* cycle. These events are usually just SysEx messages. Every |
176 |
* EngineChannel has it's own input event queue buffer and event list |
177 |
* to handle common events like NoteOn, NoteOff and ControlChange |
178 |
* events. |
179 |
* |
180 |
* @param Samples - number of sample points to be processed in the |
181 |
* current audio cycle |
182 |
*/ |
183 |
void AbstractEngine::ImportEvents(uint Samples) { |
184 |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
185 |
Event* pEvent; |
186 |
while (true) { |
187 |
// get next event from input event queue |
188 |
if (!(pEvent = eventQueueReader.pop())) break; |
189 |
// if younger event reached, ignore that and all subsequent ones for now |
190 |
if (pEvent->FragmentPos() >= Samples) { |
191 |
eventQueueReader--; |
192 |
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
193 |
pEvent->ResetFragmentPos(); |
194 |
break; |
195 |
} |
196 |
// copy event to internal event list |
197 |
if (pGlobalEvents->poolIsEmpty()) { |
198 |
dmsg(1,("Event pool emtpy!\n")); |
199 |
break; |
200 |
} |
201 |
*pGlobalEvents->allocAppend() = *pEvent; |
202 |
} |
203 |
eventQueueReader.free(); // free all copied events from input queue |
204 |
} |
205 |
|
206 |
/** |
207 |
* Clear all engine global event lists. |
208 |
*/ |
209 |
void AbstractEngine::ClearEventLists() { |
210 |
pGlobalEvents->clear(); |
211 |
} |
212 |
|
213 |
/** |
214 |
* Will be called in case the respective engine channel sports FX send |
215 |
* channels. In this particular case, engine channel local buffers are |
216 |
* used to render and mix all voices to. This method is responsible for |
217 |
* copying the audio data from those local buffers to the master audio |
218 |
* output channels as well as to the FX send audio output channels with |
219 |
* their respective FX send levels. |
220 |
* |
221 |
* @param pEngineChannel - engine channel from which audio should be |
222 |
* routed |
223 |
* @param Samples - amount of sample points to be routed in |
224 |
* this audio fragment cycle |
225 |
*/ |
226 |
void AbstractEngine::RouteAudio(EngineChannel* pEngineChannel, uint Samples) { |
227 |
AbstractEngineChannel* pChannel = static_cast<AbstractEngineChannel*>(pEngineChannel); |
228 |
AudioChannel* ppSource[2] = { |
229 |
pChannel->pChannelLeft, |
230 |
pChannel->pChannelRight |
231 |
}; |
232 |
// route dry signal |
233 |
{ |
234 |
AudioChannel* pDstL = pAudioOutputDevice->Channel(pChannel->AudioDeviceChannelLeft); |
235 |
AudioChannel* pDstR = pAudioOutputDevice->Channel(pChannel->AudioDeviceChannelRight); |
236 |
ppSource[0]->MixTo(pDstL, Samples); |
237 |
ppSource[1]->MixTo(pDstR, Samples); |
238 |
} |
239 |
// route FX send signal (wet) |
240 |
{ |
241 |
for (int iFxSend = 0; iFxSend < pChannel->GetFxSendCount(); iFxSend++) { |
242 |
FxSend* pFxSend = pChannel->GetFxSend(iFxSend); |
243 |
const bool success = RouteFxSend(pFxSend, ppSource, pFxSend->Level(), Samples); |
244 |
if (!success) goto channel_cleanup; |
245 |
} |
246 |
} |
247 |
channel_cleanup: |
248 |
// reset buffers with silence (zero out) for the next audio cycle |
249 |
ppSource[0]->Clear(); |
250 |
ppSource[1]->Clear(); |
251 |
} |
252 |
|
253 |
/** |
254 |
* Similar to RouteAudio(), but this method is even more special. It is |
255 |
* only called by voices which have dedicated effect send(s) level(s). So |
256 |
* such voices have to be routed separately apart from the other voices |
257 |
* which can just be mixed together and routed afterwards in one turn. |
258 |
*/ |
259 |
void AbstractEngine::RouteDedicatedVoiceChannels(EngineChannel* pEngineChannel, optional<float> FxSendLevels[2], uint Samples) { |
260 |
AbstractEngineChannel* pChannel = static_cast<AbstractEngineChannel*>(pEngineChannel); |
261 |
AudioChannel* ppSource[2] = { |
262 |
pDedicatedVoiceChannelLeft, |
263 |
pDedicatedVoiceChannelRight |
264 |
}; |
265 |
// route dry signal |
266 |
{ |
267 |
AudioChannel* pDstL = pAudioOutputDevice->Channel(pChannel->AudioDeviceChannelLeft); |
268 |
AudioChannel* pDstR = pAudioOutputDevice->Channel(pChannel->AudioDeviceChannelRight); |
269 |
ppSource[0]->MixTo(pDstL, Samples); |
270 |
ppSource[1]->MixTo(pDstR, Samples); |
271 |
} |
272 |
// route FX send signals (wet) |
273 |
// (we simply hard code the voices 'reverb send' to the 1st effect |
274 |
// send bus, and the voioces 'chorus send' to the 2nd effect send bus) |
275 |
{ |
276 |
for (int iFxSend = 0; iFxSend < 2 && iFxSend < pChannel->GetFxSendCount(); iFxSend++) { |
277 |
// no voice specific FX send level defined for this effect? |
278 |
if (!FxSendLevels[iFxSend]) continue; // ignore this effect then |
279 |
|
280 |
FxSend* pFxSend = pChannel->GetFxSend(iFxSend); |
281 |
const bool success = RouteFxSend(pFxSend, ppSource, *FxSendLevels[iFxSend], Samples); |
282 |
if (!success) goto channel_cleanup; |
283 |
} |
284 |
} |
285 |
channel_cleanup: |
286 |
// reset buffers with silence (zero out) for the next dedicated voice rendering/routing process |
287 |
ppSource[0]->Clear(); |
288 |
ppSource[1]->Clear(); |
289 |
} |
290 |
|
291 |
/** |
292 |
* Route the audio signal given by @a ppSource to the effect send bus |
293 |
* defined by @a pFxSend (wet signal only). |
294 |
* |
295 |
* @param pFxSend - definition of effect send bus |
296 |
* @param ppSource - the 2 channels of the audio signal to be routed |
297 |
* @param FxSendLevel - the effect send level to by applied |
298 |
* @param Samples - amount of sample points to be processed |
299 |
* @returns true if signal was routed successfully, false on error |
300 |
*/ |
301 |
bool AbstractEngine::RouteFxSend(FxSend* pFxSend, AudioChannel* ppSource[2], float FxSendLevel, uint Samples) { |
302 |
for (int iChan = 0; iChan < 2; ++iChan) { |
303 |
const int iDstChan = pFxSend->DestinationChannel(iChan); |
304 |
if (iDstChan < 0) { |
305 |
dmsg(1,("Engine::RouteAudio() Error: invalid FX send (%s) destination channel (%d->%d)", ((iChan) ? "R" : "L"), iChan, iDstChan)); |
306 |
return false; // error |
307 |
} |
308 |
AudioChannel* pDstChan = NULL; |
309 |
if (pFxSend->DestinationEffectChain() >= 0) { // fx send routed to an internal send effect |
310 |
EffectChain* pEffectChain = |
311 |
pAudioOutputDevice->SendEffectChainByID( |
312 |
pFxSend->DestinationEffectChain() |
313 |
); |
314 |
if (!pEffectChain) { |
315 |
dmsg(1,("Engine::RouteAudio() Error: invalid FX send (%s) destination effect chain %d", ((iChan) ? "R" : "L"), pFxSend->DestinationEffectChain())); |
316 |
return false; // error |
317 |
} |
318 |
Effect* pEffect = |
319 |
pEffectChain->GetEffect( |
320 |
pFxSend->DestinationEffectChainPosition() |
321 |
); |
322 |
if (!pEffect) { |
323 |
dmsg(1,("Engine::RouteAudio() Error: invalid FX send (%s) destination effect %d of effect chain %d", ((iChan) ? "R" : "L"), pFxSend->DestinationEffectChainPosition(), pFxSend->DestinationEffectChain())); |
324 |
return false; // error |
325 |
} |
326 |
pDstChan = pEffect->InputChannel(iDstChan); |
327 |
} else { // FX send routed directly to an audio output channel |
328 |
pDstChan = pAudioOutputDevice->Channel(iDstChan); |
329 |
} |
330 |
if (!pDstChan) { |
331 |
dmsg(1,("Engine::RouteAudio() Error: invalid FX send (%s) destination channel (%d->%d)", ((iChan) ? "R" : "L"), iChan, iDstChan)); |
332 |
return false; // error |
333 |
} |
334 |
ppSource[iChan]->MixTo(pDstChan, Samples, FxSendLevel); |
335 |
} |
336 |
return true; // success |
337 |
} |
338 |
|
339 |
/** |
340 |
* Calculates the Roland GS sysex check sum. |
341 |
* |
342 |
* @param AddrReader - reader which currently points to the first GS |
343 |
* command address byte of the GS sysex message in |
344 |
* question |
345 |
* @param DataSize - size of the GS message data (in bytes) |
346 |
*/ |
347 |
uint8_t AbstractEngine::GSCheckSum(const RingBuffer<uint8_t,false>::NonVolatileReader AddrReader, uint DataSize) { |
348 |
RingBuffer<uint8_t,false>::NonVolatileReader reader = AddrReader; |
349 |
uint bytes = 3 /*addr*/ + DataSize; |
350 |
uint8_t addr_and_data[bytes]; |
351 |
reader.read(&addr_and_data[0], bytes); |
352 |
uint8_t sum = 0; |
353 |
for (uint i = 0; i < bytes; i++) sum += addr_and_data[i]; |
354 |
return 128 - sum % 128; |
355 |
} |
356 |
|
357 |
/** |
358 |
* Allows to tune each of the twelve semitones of an octave. |
359 |
* |
360 |
* @param ScaleTunes - detuning of all twelve semitones (in cents) |
361 |
*/ |
362 |
void AbstractEngine::AdjustScale(int8_t ScaleTunes[12]) { |
363 |
memcpy(&this->ScaleTuning[0], &ScaleTunes[0], 12); //TODO: currently not sample accurate |
364 |
} |
365 |
|
366 |
uint AbstractEngine::VoiceCount() { |
367 |
return atomic_read(&ActiveVoiceCount); |
368 |
} |
369 |
|
370 |
void AbstractEngine::SetVoiceCount(uint Count) { |
371 |
atomic_set(&ActiveVoiceCount, Count); |
372 |
} |
373 |
|
374 |
uint AbstractEngine::VoiceCountMax() { |
375 |
return ActiveVoiceCountMax; |
376 |
} |
377 |
|
378 |
/** |
379 |
* Moves pitchbend event from the general (input) event list to the engine |
380 |
* channel's event list. It will actually processed later by the |
381 |
* respective voice. |
382 |
* |
383 |
* @param pEngineChannel - engine channel on which this event occured on |
384 |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
385 |
*/ |
386 |
void AbstractEngine::ProcessPitchbend(AbstractEngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) { |
387 |
pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
388 |
} |
389 |
|
390 |
void AbstractEngine::ProcessFxSendControllers ( |
391 |
AbstractEngineChannel* pEngineChannel, |
392 |
Pool<Event>::Iterator& itControlChangeEvent |
393 |
) { |
394 |
if (!pEngineChannel->fxSends.empty()) { |
395 |
for (int iFxSend = 0; iFxSend < pEngineChannel->GetFxSendCount(); iFxSend++) { |
396 |
FxSend* pFxSend = pEngineChannel->GetFxSend(iFxSend); |
397 |
if (pFxSend->MidiController() == itControlChangeEvent->Param.CC.Controller) { |
398 |
pFxSend->SetLevel(itControlChangeEvent->Param.CC.Value); |
399 |
pFxSend->SetInfoChanged(true); |
400 |
} |
401 |
} |
402 |
} |
403 |
} |
404 |
|
405 |
/** |
406 |
* Will be called by the MIDI input device whenever a MIDI system |
407 |
* exclusive message has arrived. |
408 |
* |
409 |
* @param pData - pointer to sysex data |
410 |
* @param Size - lenght of sysex data (in bytes) |
411 |
* @param pSender - the MIDI input port on which the SysEx message was |
412 |
* received |
413 |
*/ |
414 |
void AbstractEngine::SendSysex(void* pData, uint Size, MidiInputPort* pSender) { |
415 |
Event event = pEventGenerator->CreateEvent(); |
416 |
event.Type = Event::type_sysex; |
417 |
event.Param.Sysex.Size = Size; |
418 |
event.pEngineChannel = NULL; // as Engine global event |
419 |
event.pMidiInputPort = pSender; |
420 |
if (pEventQueue->write_space() > 0) { |
421 |
if (pSysexBuffer->write_space() >= Size) { |
422 |
// copy sysex data to input buffer |
423 |
uint toWrite = Size; |
424 |
uint8_t* pPos = (uint8_t*) pData; |
425 |
while (toWrite) { |
426 |
const uint writeNow = RTMath::Min(toWrite, pSysexBuffer->write_space_to_end()); |
427 |
pSysexBuffer->write(pPos, writeNow); |
428 |
toWrite -= writeNow; |
429 |
pPos += writeNow; |
430 |
|
431 |
} |
432 |
// finally place sysex event into input event queue |
433 |
pEventQueue->push(&event); |
434 |
} |
435 |
else dmsg(1,("Engine: Sysex message too large (%d byte) for input buffer (%d byte)!",Size,CONFIG_SYSEX_BUFFER_SIZE)); |
436 |
} |
437 |
else dmsg(1,("Engine: Input event queue full!")); |
438 |
} |
439 |
|
440 |
/** |
441 |
* Reacts on MIDI system exclusive messages. |
442 |
* |
443 |
* @param itSysexEvent - sysex data size and time stamp of the sysex event |
444 |
*/ |
445 |
void AbstractEngine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) { |
446 |
RingBuffer<uint8_t,false>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
447 |
|
448 |
uint8_t exclusive_status, id; |
449 |
if (!reader.pop(&exclusive_status)) goto free_sysex_data; |
450 |
if (!reader.pop(&id)) goto free_sysex_data; |
451 |
if (exclusive_status != 0xF0) goto free_sysex_data; |
452 |
|
453 |
switch (id) { |
454 |
case 0x7f: { // (Realtime) Universal Sysex (GM Standard) |
455 |
uint8_t sysex_channel, sub_id1, sub_id2, val_msb, val_lsb;; |
456 |
if (!reader.pop(&sysex_channel)) goto free_sysex_data; |
457 |
if (!reader.pop(&sub_id1)) goto free_sysex_data; |
458 |
if (!reader.pop(&sub_id2)) goto free_sysex_data; |
459 |
if (!reader.pop(&val_lsb)) goto free_sysex_data; |
460 |
if (!reader.pop(&val_msb)) goto free_sysex_data; |
461 |
//TODO: for now we simply ignore the sysex channel, seldom used anyway |
462 |
switch (sub_id1) { |
463 |
case 0x04: // Device Control |
464 |
switch (sub_id2) { |
465 |
case 0x01: { // Master Volume |
466 |
const double volume = |
467 |
double((uint(val_msb)<<7) | uint(val_lsb)) / 16383.0; |
468 |
#if CONFIG_MASTER_VOLUME_SYSEX_BY_PORT |
469 |
// apply volume to all sampler channels that |
470 |
// are connected to the same MIDI input port |
471 |
// this sysex message arrived on |
472 |
for (int i = 0; i < engineChannels.size(); ++i) { |
473 |
EngineChannel* pEngineChannel = engineChannels[i]; |
474 |
if (pEngineChannel->GetMidiInputPort() == |
475 |
itSysexEvent->pMidiInputPort) |
476 |
{ |
477 |
pEngineChannel->Volume(volume); |
478 |
} |
479 |
} |
480 |
#else |
481 |
// apply volume globally to the whole sampler |
482 |
GLOBAL_VOLUME = volume; |
483 |
#endif // CONFIG_MASTER_VOLUME_SYSEX_BY_PORT |
484 |
break; |
485 |
} |
486 |
} |
487 |
break; |
488 |
} |
489 |
break; |
490 |
} |
491 |
case 0x41: { // Roland |
492 |
dmsg(3,("Roland Sysex\n")); |
493 |
uint8_t device_id, model_id, cmd_id; |
494 |
if (!reader.pop(&device_id)) goto free_sysex_data; |
495 |
if (!reader.pop(&model_id)) goto free_sysex_data; |
496 |
if (!reader.pop(&cmd_id)) goto free_sysex_data; |
497 |
if (model_id != 0x42 /*GS*/) goto free_sysex_data; |
498 |
if (cmd_id != 0x12 /*DT1*/) goto free_sysex_data; |
499 |
|
500 |
// command address |
501 |
uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB) |
502 |
const RingBuffer<uint8_t,false>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later |
503 |
if (reader.read(&addr[0], 3) != 3) goto free_sysex_data; |
504 |
if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters |
505 |
dmsg(3,("\tSystem Parameter\n")); |
506 |
if (addr[2] == 0x7f) { // GS reset |
507 |
for (int i = 0; i < engineChannels.size(); ++i) { |
508 |
AbstractEngineChannel* pEngineChannel |
509 |
= static_cast<AbstractEngineChannel*>(engineChannels[i]); |
510 |
if (pEngineChannel->GetMidiInputPort() == itSysexEvent->pMidiInputPort) { |
511 |
KillAllVoices(pEngineChannel, itSysexEvent); |
512 |
pEngineChannel->ResetControllers(); |
513 |
} |
514 |
} |
515 |
} |
516 |
} |
517 |
else if (addr[0] == 0x40 && addr[1] == 0x01) { // Common Parameters |
518 |
dmsg(3,("\tCommon Parameter\n")); |
519 |
} |
520 |
else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x10) { // Part Parameters (1) |
521 |
dmsg(3,("\tPart Parameter\n")); |
522 |
switch (addr[2]) { |
523 |
case 0x40: { // scale tuning |
524 |
dmsg(3,("\t\tScale Tuning\n")); |
525 |
uint8_t scale_tunes[12]; // detuning of all 12 semitones of an octave |
526 |
if (reader.read(&scale_tunes[0], 12) != 12) goto free_sysex_data; |
527 |
uint8_t checksum; |
528 |
if (!reader.pop(&checksum)) goto free_sysex_data; |
529 |
#if CONFIG_ASSERT_GS_SYSEX_CHECKSUM |
530 |
if (GSCheckSum(checksum_reader, 12)) goto free_sysex_data; |
531 |
#endif // CONFIG_ASSERT_GS_SYSEX_CHECKSUM |
532 |
for (int i = 0; i < 12; i++) scale_tunes[i] -= 64; |
533 |
AdjustScale((int8_t*) scale_tunes); |
534 |
dmsg(3,("\t\t\tNew scale applied.\n")); |
535 |
break; |
536 |
} |
537 |
case 0x15: { // chromatic / drumkit mode |
538 |
dmsg(3,("\t\tMIDI Instrument Map Switch\n")); |
539 |
uint8_t part = addr[1] & 0x0f; |
540 |
uint8_t map; |
541 |
if (!reader.pop(&map)) goto free_sysex_data; |
542 |
for (int i = 0; i < engineChannels.size(); ++i) { |
543 |
AbstractEngineChannel* pEngineChannel |
544 |
= static_cast<AbstractEngineChannel*>(engineChannels[i]); |
545 |
if ( |
546 |
(pEngineChannel->midiChannel == part || |
547 |
pEngineChannel->midiChannel == midi_chan_all) && |
548 |
pEngineChannel->GetMidiInputPort() == itSysexEvent->pMidiInputPort |
549 |
) { |
550 |
try { |
551 |
pEngineChannel->SetMidiInstrumentMap(map); |
552 |
} catch (Exception e) { |
553 |
dmsg(2,("\t\t\tCould not apply MIDI instrument map %d to part %d: %s\n", map, part, e.Message().c_str())); |
554 |
goto free_sysex_data; |
555 |
} catch (...) { |
556 |
dmsg(2,("\t\t\tCould not apply MIDI instrument map %d to part %d (unknown exception)\n", map, part)); |
557 |
goto free_sysex_data; |
558 |
} |
559 |
} |
560 |
} |
561 |
dmsg(3,("\t\t\tApplied MIDI instrument map %d to part %d.\n", map, part)); |
562 |
break; |
563 |
} |
564 |
} |
565 |
} |
566 |
else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x20) { // Part Parameters (2) |
567 |
} |
568 |
else if (addr[0] == 0x41) { // Drum Setup Parameters |
569 |
} |
570 |
break; |
571 |
} |
572 |
} |
573 |
|
574 |
free_sysex_data: // finally free sysex data |
575 |
pSysexBuffer->increment_read_ptr(itSysexEvent->Param.Sysex.Size); |
576 |
} |
577 |
|
578 |
String AbstractEngine::GetFormatString(Format f) { |
579 |
switch(f) { |
580 |
case GIG: return "GIG"; |
581 |
case SF2: return "SF2"; |
582 |
case SFZ: return "SFZ"; |
583 |
default: return "UNKNOWN"; |
584 |
} |
585 |
} |
586 |
|
587 |
String AbstractEngine::EngineName() { |
588 |
return GetFormatString(GetEngineFormat()); |
589 |
} |
590 |
|
591 |
// static constant initializers |
592 |
const AbstractEngine::FloatTable AbstractEngine::VolumeCurve(InitVolumeCurve()); |
593 |
const AbstractEngine::FloatTable AbstractEngine::PanCurve(InitPanCurve()); |
594 |
const AbstractEngine::FloatTable AbstractEngine::CrossfadeCurve(InitCrossfadeCurve()); |
595 |
|
596 |
float* AbstractEngine::InitVolumeCurve() { |
597 |
// line-segment approximation |
598 |
const float segments[] = { |
599 |
0, 0, 2, 0.0046, 16, 0.016, 31, 0.051, 45, 0.115, 54.5, 0.2, |
600 |
64.5, 0.39, 74, 0.74, 92, 1.03, 114, 1.94, 119.2, 2.2, 127, 2.2 |
601 |
}; |
602 |
return InitCurve(segments); |
603 |
} |
604 |
|
605 |
float* AbstractEngine::InitPanCurve() { |
606 |
// line-segment approximation |
607 |
const float segments[] = { |
608 |
0, 0, 1, 0, |
609 |
2, 0.05, 31.5, 0.7, 51, 0.851, 74.5, 1.12, |
610 |
127, 1.41, 128, 1.41 |
611 |
}; |
612 |
return InitCurve(segments, 129); |
613 |
} |
614 |
|
615 |
float* AbstractEngine::InitCrossfadeCurve() { |
616 |
// line-segment approximation |
617 |
const float segments[] = { |
618 |
0, 0, 1, 0.03, 10, 0.1, 51, 0.58, 127, 1 |
619 |
}; |
620 |
return InitCurve(segments); |
621 |
} |
622 |
|
623 |
float* AbstractEngine::InitCurve(const float* segments, int size) { |
624 |
float* y = new float[size]; |
625 |
for (int x = 0 ; x < size ; x++) { |
626 |
if (x > segments[2]) segments += 2; |
627 |
y[x] = segments[1] + (x - segments[0]) * |
628 |
(segments[3] - segments[1]) / (segments[2] - segments[0]); |
629 |
} |
630 |
return y; |
631 |
} |
632 |
|
633 |
} // namespace LinuxSampler |