46 |
ResetControllers(); |
ResetControllers(); |
47 |
PortamentoMode = false; |
PortamentoMode = false; |
48 |
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
49 |
|
pScriptEvents = NULL; |
50 |
} |
} |
51 |
|
|
52 |
AbstractEngineChannel::~AbstractEngineChannel() { |
AbstractEngineChannel::~AbstractEngineChannel() { |
53 |
|
unloadCurrentInstrumentScript(); |
54 |
|
if (pScriptEvents) delete pScriptEvents; |
55 |
delete pEventQueue; |
delete pEventQueue; |
56 |
DeleteGroupEventLists(); |
DeleteGroupEventLists(); |
57 |
RemoveAllFxSends(); |
RemoveAllFxSends(); |
145 |
} |
} |
146 |
|
|
147 |
/** |
/** |
148 |
|
* Loads the real-time instrument script given by @a text on this engine |
149 |
|
* channel. A resource manager is used to allocate and share equivalent |
150 |
|
* scripts on multiple engine channels. |
151 |
|
* |
152 |
|
* @param text - source code of script |
153 |
|
*/ |
154 |
|
void AbstractEngineChannel::loadInstrumentScript(const String& text) { |
155 |
|
dmsg(1,("Loading real-time instrument script ... ")); |
156 |
|
|
157 |
|
// hand back old script reference and VM execution contexts |
158 |
|
// (if not done already) |
159 |
|
unloadCurrentInstrumentScript(); |
160 |
|
|
161 |
|
// get new script reference |
162 |
|
script.parserContext = pEngine->scripts.Borrow(text, this); |
163 |
|
if (!script.parserContext->errors().empty()) { |
164 |
|
std::vector<ParserIssue> errors = script.parserContext->errors(); |
165 |
|
std::cerr << "[ScriptVM] Could not load instrument script, there were " |
166 |
|
<< errors.size() << " parser errors:\n"; |
167 |
|
for (int i = 0; i < errors.size(); ++i) |
168 |
|
errors[i].dump(); |
169 |
|
return; // stop here if there were any parser errors |
170 |
|
} |
171 |
|
|
172 |
|
script.handlerInit = script.parserContext->eventHandlerByName("init"); |
173 |
|
script.handlerNote = script.parserContext->eventHandlerByName("note"); |
174 |
|
script.handlerRelease = script.parserContext->eventHandlerByName("release"); |
175 |
|
script.handlerController = script.parserContext->eventHandlerByName("controller"); |
176 |
|
script.bHasValidScript = |
177 |
|
script.handlerInit || script.handlerNote || script.handlerRelease || |
178 |
|
script.handlerController; |
179 |
|
|
180 |
|
// amount of script handlers each script event has to execute |
181 |
|
int handlerExecCount = 0; |
182 |
|
if (script.handlerInit) handlerExecCount++; // "init" handler is always executed before the actual event handler |
183 |
|
if (script.handlerNote || script.handlerRelease || script.handlerController) // only one of these are executed after "init" handler |
184 |
|
handlerExecCount++; |
185 |
|
|
186 |
|
// create script event pool (if it doesn't exist already) |
187 |
|
if (!pScriptEvents) |
188 |
|
pScriptEvents = new Pool<ScriptEvent>(CONFIG_MAX_EVENTS_PER_FRAGMENT); |
189 |
|
|
190 |
|
// create new VM execution contexts for new script |
191 |
|
while (!pScriptEvents->poolIsEmpty()) { |
192 |
|
RTList<ScriptEvent>::Iterator it = pScriptEvents->allocAppend(); |
193 |
|
it->execCtx = pEngine->pScriptVM->createExecContext( |
194 |
|
script.parserContext |
195 |
|
); |
196 |
|
it->handlers = new VMEventHandler*[handlerExecCount+1]; |
197 |
|
} |
198 |
|
pScriptEvents->clear(); |
199 |
|
|
200 |
|
dmsg(1,("Done\n")); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
|
* Unloads the currently used real-time instrument script on this sampler |
205 |
|
* channel. A resource manager is used to share equivalent scripts among |
206 |
|
* multiple sampler channels, and to deallocate the parsed script once not |
207 |
|
* used on any engine channel anymore. |
208 |
|
*/ |
209 |
|
void AbstractEngineChannel::unloadCurrentInstrumentScript() { |
210 |
|
if (script.parserContext) |
211 |
|
dmsg(1,("Unloading current instrument script.")); |
212 |
|
|
213 |
|
// free allocated VM execution contexts |
214 |
|
if (pScriptEvents) { |
215 |
|
pScriptEvents->clear(); |
216 |
|
while (!pScriptEvents->poolIsEmpty()) { |
217 |
|
RTList<ScriptEvent>::Iterator it = pScriptEvents->allocAppend(); |
218 |
|
if (it->execCtx) { |
219 |
|
// free VM execution context object |
220 |
|
delete it->execCtx; |
221 |
|
it->execCtx = NULL; |
222 |
|
// free C array of handler pointers |
223 |
|
delete [] it->handlers; |
224 |
|
} |
225 |
|
} |
226 |
|
pScriptEvents->clear(); |
227 |
|
} |
228 |
|
// hand back VM representation of script |
229 |
|
if (script.parserContext) { |
230 |
|
pEngine->scripts.HandBack(script.parserContext, this); |
231 |
|
script.parserContext = NULL; |
232 |
|
script.handlerInit = NULL; |
233 |
|
script.handlerNote = NULL; |
234 |
|
script.handlerRelease = NULL; |
235 |
|
script.handlerController = NULL; |
236 |
|
} |
237 |
|
script.bHasValidScript = false; |
238 |
|
} |
239 |
|
|
240 |
|
/** |
241 |
* Implementation of virtual method from abstract EngineChannel interface. |
* Implementation of virtual method from abstract EngineChannel interface. |
242 |
* This method will periodically be polled (e.g. by the LSCP server) to |
* This method will periodically be polled (e.g. by the LSCP server) to |
243 |
* check if some engine channel parameter has changed since the last |
* check if some engine channel parameter has changed since the last |
715 |
} |
} |
716 |
} |
} |
717 |
|
|
718 |
|
void AbstractEngineChannel::SendChannelPressure(uint8_t Value, uint8_t MidiChannel) { |
719 |
|
if (pEngine) { |
720 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
721 |
|
LockGuard g; |
722 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
723 |
|
|
724 |
|
Event event = pEngine->pEventGenerator->CreateEvent(); |
725 |
|
event.Type = Event::type_channel_pressure; |
726 |
|
event.Param.ChannelPressure.Value = Value; |
727 |
|
event.Param.ChannelPressure.Channel = MidiChannel; |
728 |
|
event.pEngineChannel = this; |
729 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
730 |
|
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
731 |
|
} |
732 |
|
} |
733 |
|
|
734 |
|
void AbstractEngineChannel::SendChannelPressure(uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) { |
735 |
|
if (pEngine) { |
736 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
737 |
|
LockGuard g; |
738 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
739 |
|
|
740 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
741 |
|
event.Type = Event::type_channel_pressure; |
742 |
|
event.Param.ChannelPressure.Value = Value; |
743 |
|
event.Param.ChannelPressure.Channel = MidiChannel; |
744 |
|
event.pEngineChannel = this; |
745 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
746 |
|
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
747 |
|
} |
748 |
|
} |
749 |
|
|
750 |
|
void AbstractEngineChannel::SendPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint8_t MidiChannel) { |
751 |
|
if (pEngine) { |
752 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
753 |
|
LockGuard g; |
754 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
755 |
|
|
756 |
|
Event event = pEngine->pEventGenerator->CreateEvent(); |
757 |
|
event.Type = Event::type_note_pressure; |
758 |
|
event.Param.NotePressure.Key = Key; |
759 |
|
event.Param.NotePressure.Value = Value; |
760 |
|
event.Param.NotePressure.Channel = MidiChannel; |
761 |
|
event.pEngineChannel = this; |
762 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
763 |
|
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
764 |
|
} |
765 |
|
} |
766 |
|
|
767 |
|
void AbstractEngineChannel::SendPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) { |
768 |
|
if (pEngine) { |
769 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
770 |
|
LockGuard g; |
771 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
772 |
|
|
773 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
774 |
|
event.Type = Event::type_note_pressure; |
775 |
|
event.Param.NotePressure.Key = Key; |
776 |
|
event.Param.NotePressure.Value = Value; |
777 |
|
event.Param.NotePressure.Channel = MidiChannel; |
778 |
|
event.pEngineChannel = this; |
779 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
780 |
|
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
781 |
|
} |
782 |
|
} |
783 |
|
|
784 |
/** |
/** |
785 |
* Copy all events from the engine channel's input event queue buffer to |
* Copy all events from the engine channel's input event queue buffer to |
786 |
* the internal event list. This will be done at the beginning of each |
* the internal event list. This will be done at the beginning of each |
825 |
event.Param.Note.Channel = channel; |
event.Param.Note.Channel = channel; |
826 |
break; |
break; |
827 |
case VirtualMidiDevice::EVENT_TYPE_CC: |
case VirtualMidiDevice::EVENT_TYPE_CC: |
828 |
event.Type = Event::type_control_change; |
switch (devEvent.Arg1) { |
829 |
event.Param.CC.Controller = devEvent.Arg1; |
case 0: // bank select MSB ... |
830 |
event.Param.CC.Value = devEvent.Arg2; |
SetMidiBankMsb(devEvent.Arg2); |
831 |
event.Param.CC.Channel = channel; |
continue; // don't push this event into FIFO |
832 |
|
case 32: // bank select LSB ... |
833 |
|
SetMidiBankLsb(devEvent.Arg2); |
834 |
|
continue; // don't push this event into FIFO |
835 |
|
default: // regular MIDI CC ... |
836 |
|
event.Type = Event::type_control_change; |
837 |
|
event.Param.CC.Controller = devEvent.Arg1; |
838 |
|
event.Param.CC.Value = devEvent.Arg2; |
839 |
|
event.Param.CC.Channel = channel; |
840 |
|
} |
841 |
break; |
break; |
842 |
|
case VirtualMidiDevice::EVENT_TYPE_PITCHBEND: |
843 |
|
event.Type = Event::type_pitchbend; |
844 |
|
event.Param.Pitch.Pitch = int(devEvent.Arg2 << 7 | devEvent.Arg1) - 8192; |
845 |
|
event.Param.Pitch.Channel = channel; |
846 |
|
break; |
847 |
|
case VirtualMidiDevice::EVENT_TYPE_PROGRAM: |
848 |
|
SendProgramChange(devEvent.Arg1); |
849 |
|
continue; // don't push this event into FIFO |
850 |
default: |
default: |
851 |
std::cerr << "AbstractEngineChannel::ImportEvents() ERROR: unknown event type (" |
std::cerr << "AbstractEngineChannel::ImportEvents() ERROR: unknown event type (" |
852 |
<< devEvent.Type << "). This is a bug!"; |
<< devEvent.Type << "). This is a bug!"; |
888 |
eventQueueReader.free(); // free all copied events from input queue |
eventQueueReader.free(); // free all copied events from input queue |
889 |
} |
} |
890 |
|
|
891 |
|
/** |
892 |
|
* Called by real-time instrument script functions to schedule a new event |
893 |
|
* somewhere in future. |
894 |
|
* |
895 |
|
* @returns unique event ID of scheduled new event |
896 |
|
*/ |
897 |
|
int AbstractEngineChannel::ScheduleEvent(const Event* pEvent, int delay) { //TODO: delay not implemented yet |
898 |
|
// since delay is not implemented yet, we simply add the new event |
899 |
|
// to the event list of the current audio fragmet cycle for now |
900 |
|
RTList<Event>::Iterator itEvent = pEvents->allocAppend(); |
901 |
|
if (itEvent) *itEvent = *pEvent; // copy event |
902 |
|
return pEvents->getID(itEvent); |
903 |
|
} |
904 |
|
|
905 |
|
/** |
906 |
|
* Called by real-time instrument script functions to ignore the event |
907 |
|
* reflected by given event ID. The event will be freed immediately to its |
908 |
|
* pool and cannot be dereferenced by its old ID anymore. Even if its |
909 |
|
* allocated back from the Pool later on, it will have a different ID. |
910 |
|
*/ |
911 |
|
void AbstractEngineChannel::IgnoreEvent(int id) { |
912 |
|
RTList<Event>::Iterator it = pEvents->fromID(id); |
913 |
|
if (it) pEvents->free(it); |
914 |
|
} |
915 |
|
|
916 |
FxSend* AbstractEngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) { |
FxSend* AbstractEngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) { |
917 |
if (pEngine) pEngine->DisableAndLock(); |
if (pEngine) pEngine->DisableAndLock(); |
918 |
FxSend* pFxSend = new FxSend(this, MidiCtrl, Name); |
FxSend* pFxSend = new FxSend(this, MidiCtrl, Name); |