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