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-2008 Christian Schoenebeck * |
* Copyright (C) 2005-2008 Christian Schoenebeck * |
7 |
* Copyright (C) 2009-2010 Christian Schoenebeck and Grigor Iliev * |
* Copyright (C) 2009-2012 Christian Schoenebeck and Grigor Iliev * |
8 |
|
* Copyright (C) 2013-2014 Christian Schoenebeck and Andreas Persson * |
9 |
* * |
* * |
10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
11 |
* 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 * |
27 |
#include "AbstractEngineChannel.h" |
#include "AbstractEngineChannel.h" |
28 |
#include "EngineFactory.h" |
#include "EngineFactory.h" |
29 |
#include "../common/global_private.h" |
#include "../common/global_private.h" |
30 |
|
#include "../effects/EffectFactory.h" |
31 |
|
|
32 |
namespace LinuxSampler { |
namespace LinuxSampler { |
33 |
|
|
61 |
} else { // create a new engine (and disk thread) instance for the given audio output device |
} else { // create a new engine (and disk thread) instance for the given audio output device |
62 |
dmsg(4,("Creating new Engine.\n")); |
dmsg(4,("Creating new Engine.\n")); |
63 |
pEngine = (AbstractEngine*) EngineFactory::Create(pChannel->EngineName()); |
pEngine = (AbstractEngine*) EngineFactory::Create(pChannel->EngineName()); |
64 |
|
pEngine->CreateInstrumentScriptVM(); |
65 |
pEngine->Connect(pDevice); |
pEngine->Connect(pDevice); |
66 |
engines[pChannel->GetEngineFormat()][pDevice] = pEngine; |
engines[pChannel->GetEngineFormat()][pDevice] = pEngine; |
67 |
} |
} |
79 |
pSysexBuffer = new RingBuffer<uint8_t,false>(CONFIG_SYSEX_BUFFER_SIZE, 0); |
pSysexBuffer = new RingBuffer<uint8_t,false>(CONFIG_SYSEX_BUFFER_SIZE, 0); |
80 |
pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
81 |
pEventPool = new Pool<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT); |
pEventPool = new Pool<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT); |
82 |
|
pEventPool->setPoolElementIDsReservedBits(INSTR_SCRIPT_EVENT_ID_RESERVED_BITS); |
83 |
pGlobalEvents = new RTList<Event>(pEventPool); |
pGlobalEvents = new RTList<Event>(pEventPool); |
84 |
FrameTime = 0; |
FrameTime = 0; |
85 |
RandomSeed = 0; |
RandomSeed = 0; |
86 |
pDedicatedVoiceChannelLeft = pDedicatedVoiceChannelRight = NULL; |
pDedicatedVoiceChannelLeft = pDedicatedVoiceChannelRight = NULL; |
87 |
|
pScriptVM = NULL; |
88 |
} |
} |
89 |
|
|
90 |
AbstractEngine::~AbstractEngine() { |
AbstractEngine::~AbstractEngine() { |
95 |
if (pSysexBuffer) delete pSysexBuffer; |
if (pSysexBuffer) delete pSysexBuffer; |
96 |
if (pDedicatedVoiceChannelLeft) delete pDedicatedVoiceChannelLeft; |
if (pDedicatedVoiceChannelLeft) delete pDedicatedVoiceChannelLeft; |
97 |
if (pDedicatedVoiceChannelRight) delete pDedicatedVoiceChannelRight; |
if (pDedicatedVoiceChannelRight) delete pDedicatedVoiceChannelRight; |
98 |
|
if (pScriptVM) delete pScriptVM; |
99 |
Unregister(); |
Unregister(); |
100 |
} |
} |
101 |
|
|
102 |
|
void AbstractEngine::CreateInstrumentScriptVM() { |
103 |
|
dmsg(2,("Created sampler format independent instrument script VM.\n")); |
104 |
|
if (pScriptVM) return; |
105 |
|
pScriptVM = new InstrumentScriptVM; // format independent script runner |
106 |
|
} |
107 |
|
|
108 |
/** |
/** |
109 |
* Once an engine channel is disconnected from an audio output device, |
* Once an engine channel is disconnected from an audio output device, |
110 |
* it will immediately call this method to unregister itself from the |
* it will immediately call this method to unregister itself from the |
176 |
*/ |
*/ |
177 |
void AbstractEngine::ResetScaleTuning() { |
void AbstractEngine::ResetScaleTuning() { |
178 |
memset(&ScaleTuning[0], 0x00, 12); |
memset(&ScaleTuning[0], 0x00, 12); |
179 |
|
ScaleTuningChanged.raise(); |
180 |
} |
} |
181 |
|
|
182 |
/** |
/** |
358 |
*/ |
*/ |
359 |
uint8_t AbstractEngine::GSCheckSum(const RingBuffer<uint8_t,false>::NonVolatileReader AddrReader, uint DataSize) { |
uint8_t AbstractEngine::GSCheckSum(const RingBuffer<uint8_t,false>::NonVolatileReader AddrReader, uint DataSize) { |
360 |
RingBuffer<uint8_t,false>::NonVolatileReader reader = AddrReader; |
RingBuffer<uint8_t,false>::NonVolatileReader reader = AddrReader; |
361 |
uint bytes = 3 /*addr*/ + DataSize; |
uint bytes = 3 /*addr*/ + DataSize; |
|
uint8_t addr_and_data[bytes]; |
|
|
reader.read(&addr_and_data[0], bytes); |
|
362 |
uint8_t sum = 0; |
uint8_t sum = 0; |
363 |
for (uint i = 0; i < bytes; i++) sum += addr_and_data[i]; |
uint8_t c; |
364 |
|
for (uint i = 0; i < bytes; ++i) { |
365 |
|
if (!reader.pop(&c)) break; |
366 |
|
sum += c; |
367 |
|
} |
368 |
return 128 - sum % 128; |
return 128 - sum % 128; |
369 |
} |
} |
370 |
|
|
373 |
* |
* |
374 |
* @param ScaleTunes - detuning of all twelve semitones (in cents) |
* @param ScaleTunes - detuning of all twelve semitones (in cents) |
375 |
*/ |
*/ |
376 |
void AbstractEngine::AdjustScale(int8_t ScaleTunes[12]) { |
void AbstractEngine::AdjustScaleTuning(const int8_t ScaleTunes[12]) { |
377 |
memcpy(&this->ScaleTuning[0], &ScaleTunes[0], 12); //TODO: currently not sample accurate |
memcpy(&this->ScaleTuning[0], &ScaleTunes[0], 12); |
378 |
|
ScaleTuningChanged.raise(); |
379 |
|
} |
380 |
|
|
381 |
|
void AbstractEngine::GetScaleTuning(int8_t* pScaleTunes) { |
382 |
|
memcpy(pScaleTunes, &this->ScaleTuning[0], 12); |
383 |
} |
} |
384 |
|
|
385 |
uint AbstractEngine::VoiceCount() { |
uint AbstractEngine::VoiceCount() { |
395 |
} |
} |
396 |
|
|
397 |
/** |
/** |
398 |
* Moves pitchbend event from the general (input) event list to the engine |
* Stores the latest pitchbend event as current pitchbend scalar value. |
|
* channel's event list. It will actually processed later by the |
|
|
* respective voice. |
|
399 |
* |
* |
400 |
* @param pEngineChannel - engine channel on which this event occured on |
* @param pEngineChannel - engine channel on which this event occured on |
401 |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
524 |
for (int i = 0; i < engineChannels.size(); ++i) { |
for (int i = 0; i < engineChannels.size(); ++i) { |
525 |
AbstractEngineChannel* pEngineChannel |
AbstractEngineChannel* pEngineChannel |
526 |
= static_cast<AbstractEngineChannel*>(engineChannels[i]); |
= static_cast<AbstractEngineChannel*>(engineChannels[i]); |
527 |
if (pEngineChannel->GetMidiInputPort() == itSysexEvent->pMidiInputPort) { |
Sync< ArrayList<MidiInputPort*> > midiInputs = pEngineChannel->midiInputs.front(); |
528 |
KillAllVoices(pEngineChannel, itSysexEvent); |
for (int k = 0; k < midiInputs->size(); ++k) { |
529 |
pEngineChannel->ResetControllers(); |
if ((*midiInputs)[k] == itSysexEvent->pMidiInputPort) { |
530 |
|
KillAllVoices(pEngineChannel, itSysexEvent); |
531 |
|
pEngineChannel->ResetControllers(); |
532 |
|
break; |
533 |
|
} |
534 |
} |
} |
535 |
} |
} |
536 |
} |
} |
551 |
if (GSCheckSum(checksum_reader, 12)) goto free_sysex_data; |
if (GSCheckSum(checksum_reader, 12)) goto free_sysex_data; |
552 |
#endif // CONFIG_ASSERT_GS_SYSEX_CHECKSUM |
#endif // CONFIG_ASSERT_GS_SYSEX_CHECKSUM |
553 |
for (int i = 0; i < 12; i++) scale_tunes[i] -= 64; |
for (int i = 0; i < 12; i++) scale_tunes[i] -= 64; |
554 |
AdjustScale((int8_t*) scale_tunes); |
AdjustScaleTuning((int8_t*) scale_tunes); |
555 |
dmsg(3,("\t\t\tNew scale applied.\n")); |
dmsg(3,("\t\t\tNew scale applied.\n")); |
556 |
break; |
break; |
557 |
} |
} |
563 |
for (int i = 0; i < engineChannels.size(); ++i) { |
for (int i = 0; i < engineChannels.size(); ++i) { |
564 |
AbstractEngineChannel* pEngineChannel |
AbstractEngineChannel* pEngineChannel |
565 |
= static_cast<AbstractEngineChannel*>(engineChannels[i]); |
= static_cast<AbstractEngineChannel*>(engineChannels[i]); |
566 |
if ( |
if (pEngineChannel->midiChannel == part || |
567 |
(pEngineChannel->midiChannel == part || |
pEngineChannel->midiChannel == midi_chan_all) |
568 |
pEngineChannel->midiChannel == midi_chan_all) && |
{ |
569 |
pEngineChannel->GetMidiInputPort() == itSysexEvent->pMidiInputPort |
Sync< ArrayList<MidiInputPort*> > midiInputs = pEngineChannel->midiInputs.front(); |
570 |
) { |
for (int k = 0; k < midiInputs->size(); ++k) { |
571 |
try { |
if ((*midiInputs)[k] == itSysexEvent->pMidiInputPort) { |
572 |
pEngineChannel->SetMidiInstrumentMap(map); |
try { |
573 |
} catch (Exception e) { |
pEngineChannel->SetMidiInstrumentMap(map); |
574 |
dmsg(2,("\t\t\tCould not apply MIDI instrument map %d to part %d: %s\n", map, part, e.Message().c_str())); |
} catch (Exception e) { |
575 |
goto free_sysex_data; |
dmsg(2,("\t\t\tCould not apply MIDI instrument map %d to part %d: %s\n", map, part, e.Message().c_str())); |
576 |
} catch (...) { |
goto free_sysex_data; |
577 |
dmsg(2,("\t\t\tCould not apply MIDI instrument map %d to part %d (unknown exception)\n", map, part)); |
} catch (...) { |
578 |
goto free_sysex_data; |
dmsg(2,("\t\t\tCould not apply MIDI instrument map %d to part %d (unknown exception)\n", map, part)); |
579 |
|
goto free_sysex_data; |
580 |
|
} |
581 |
|
break; |
582 |
|
} |
583 |
} |
} |
584 |
} |
} |
585 |
} |
} |