--- linuxsampler/trunk/src/engines/common/InstrumentScriptVMFunctions.cpp 2014/06/12 18:25:11 2629 +++ linuxsampler/trunk/src/engines/common/InstrumentScriptVMFunctions.cpp 2016/04/19 14:07:53 2879 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Christian Schoenebeck + * Copyright (c) 2014-2016 Christian Schoenebeck * * http://www.linuxsampler.org * @@ -22,46 +22,61 @@ int note = args->arg(0)->asInt()->evalInt(); int velocity = (args->argsCount() >= 2) ? args->arg(1)->asInt()->evalInt() : 127; int sampleoffset = (args->argsCount() >= 3) ? args->arg(2)->asInt()->evalInt() : 0; - int duration = (args->argsCount() >= 4) ? args->arg(3)->asInt()->evalInt() : 0; //TODO: once -1 is implemented, it might be a better default value instead of 0 + int duration = (args->argsCount() >= 4) ? args->arg(3)->asInt()->evalInt() : 0; //TODO: -1 might be a better default value instead of 0 if (note < 0 || note > 127) { errMsg("play_note(): argument 1 is an invalid note number"); - return errorResult(-1); + return errorResult(0); } if (velocity < 0 || velocity > 127) { errMsg("play_note(): argument 2 is an invalid velocity value"); - return errorResult(-1); + return errorResult(0); } if (sampleoffset < 0) { errMsg("play_note(): argument 3 may not be a negative sample offset"); - return errorResult(-1); + return errorResult(0); } else if (sampleoffset != 0) { wrnMsg("play_note(): argument 3 does not support a sample offset other than 0 yet"); } if (duration < -1) { errMsg("play_note(): argument 4 must be a duration value of at least -1 or higher"); - return errorResult(-1); - } else if (duration == -1) { - wrnMsg("play_note(): argument 4 does not support special value -1 as duration yet"); - } else if (duration != 0) { - wrnMsg("play_note(): argument 4 does not support any other value as 0 as duration yet"); + return errorResult(0); } AbstractEngineChannel* pEngineChannel = static_cast(m_vm->m_event->cause.pEngineChannel); - Event e = m_vm->m_event->cause; + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs e.Type = Event::type_note_on; e.Param.Note.Key = note; e.Param.Note.Velocity = velocity; - memset(&e.Format, 0, sizeof(e.Format)); // init format speific stuff with zero + // make this new note dependent to the life time of the original note + if (duration == -1) { + if (m_vm->currentVMEventHandler()->eventHandlerType() != VM_EVENT_HANDLER_NOTE) { + errMsg("play_note(): -1 for argument 4 may only be used for note event handlers"); + return errorResult(0); + } + e.Param.Note.ParentNoteID = m_vm->m_event->cause.Param.Note.ID; + } + + const note_id_t id = pEngineChannel->ScheduleNoteMicroSec(&e, 0); - int id = pEngineChannel->ScheduleEvent(&e, duration); + // if a duration is supplied (and note-on event was scheduled + // successfully above), then schedule a subsequent note-off event + if (id && duration > 0) { + e.Type = Event::type_note_off; + e.Param.Note.Velocity = 127; + pEngineChannel->ScheduleEventMicroSec(&e, duration); + } - return successResult(id); + // even if id is null, don't return an errorResult() here, because that + // would abort the script, and under heavy load it may be considerable + // that ScheduleNoteMicroSec() fails above, so simply ignore that + return successResult( ScriptID::fromNoteID(id) ); } InstrumentScriptVMFunction_set_controller::InstrumentScriptVMFunction_set_controller(InstrumentScriptVM* parent) @@ -76,8 +91,8 @@ AbstractEngineChannel* pEngineChannel = static_cast(m_vm->m_event->cause.pEngineChannel); - Event e = m_vm->m_event->cause; - memset(&e.Format, 0, sizeof(e.Format)); // init format speific stuff with zero + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs if (controller == CTRL_TABLE_IDX_AFTERTOUCH) { e.Type = Event::type_channel_pressure; e.Param.ChannelPressure.Value = value & 127; @@ -93,9 +108,12 @@ return errorResult(); } - int id = pEngineChannel->ScheduleEvent(&e, 0); + const event_id_t id = pEngineChannel->ScheduleEventMicroSec(&e, 0); - return successResult(id); + // even if id is null, don't return an errorResult() here, because that + // would abort the script, and under heavy load it may be considerable + // that ScheduleEventMicroSec() fails above, so simply ignore that + return successResult( ScriptID::fromEventID(id) ); } InstrumentScriptVMFunction_ignore_event::InstrumentScriptVMFunction_ignore_event(InstrumentScriptVM* parent) @@ -103,17 +121,29 @@ { } - VMFnResult* InstrumentScriptVMFunction_ignore_event::exec(VMFnArgs* args) { - int id = args->arg(0)->asInt()->evalInt(); - if (id < 0) { - wrnMsg("ignore_event(): argument may not be a negative event ID"); - return successResult(); - } + bool InstrumentScriptVMFunction_ignore_event::acceptsArgType(int iArg, ExprType_t type) const { + return type == INT_EXPR || type == INT_ARR_EXPR; + } + VMFnResult* InstrumentScriptVMFunction_ignore_event::exec(VMFnArgs* args) { AbstractEngineChannel* pEngineChannel = - static_cast(m_vm->m_event->cause.pEngineChannel); + static_cast(m_vm->m_event->cause.pEngineChannel); - pEngineChannel->IgnoreEvent(id); + if (args->arg(0)->exprType() == INT_EXPR) { + const ScriptID id = args->arg(0)->asInt()->evalInt(); + if (!id) { + wrnMsg("ignore_event(): event ID argument may not be zero"); + // not errorResult(), because that would abort the script, not intentional in this case + return successResult(); + } + pEngineChannel->IgnoreEventByScriptID(id); + } else if (args->arg(0)->exprType() == INT_ARR_EXPR) { + VMIntArrayExpr* ids = args->arg(0)->asIntArray(); + for (int i = 0; i < ids->arraySize(); ++i) { + const ScriptID id = ids->evalIntElement(i); + pEngineChannel->IgnoreEventByScriptID(id); + } + } return successResult(); } @@ -124,16 +154,16 @@ } VMFnResult* InstrumentScriptVMFunction_ignore_controller::exec(VMFnArgs* args) { - int id = (args->argsCount() >= 1) ? args->arg(0)->asInt()->evalInt() : m_vm->m_event->id; - if (id < 0) { - wrnMsg("ignore_controller(): argument may not be a negative event ID"); + const ScriptID id = (args->argsCount() >= 1) ? args->arg(0)->asInt()->evalInt() : m_vm->m_event->id; + if (!id && args->argsCount() >= 1) { + wrnMsg("ignore_controller(): event ID argument may not be zero"); return successResult(); } AbstractEngineChannel* pEngineChannel = static_cast(m_vm->m_event->cause.pEngineChannel); - pEngineChannel->IgnoreEvent(id); + pEngineChannel->IgnoreEventByScriptID(id); return successResult(); } @@ -143,34 +173,158 @@ { } + bool InstrumentScriptVMFunction_note_off::acceptsArgType(int iArg, ExprType_t type) const { + return type == INT_EXPR || type == INT_ARR_EXPR; + } + VMFnResult* InstrumentScriptVMFunction_note_off::exec(VMFnArgs* args) { - int id = args->arg(0)->asInt()->evalInt(); + AbstractEngineChannel* pEngineChannel = + static_cast(m_vm->m_event->cause.pEngineChannel); + int velocity = (args->argsCount() >= 2) ? args->arg(1)->asInt()->evalInt() : 127; + if (velocity < 0 || velocity > 127) { + errMsg("note_off(): argument 2 is an invalid velocity value"); + return errorResult(); + } - if (id < 0) { - wrnMsg("note_off(): argument 1 may not be a negative event ID"); - return successResult(); + if (args->arg(0)->exprType() == INT_EXPR) { + const ScriptID id = args->arg(0)->asInt()->evalInt(); + if (!id) { + wrnMsg("note_off(): note ID for argument 1 may not be zero"); + return successResult(); + } + if (!id.isNoteID()) { + wrnMsg("note_off(): argument 1 is not a note ID"); + return successResult(); + } + + NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); + if (!pNote) return successResult(); + + Event e = pNote->cause; + e.Init(); // clear IDs + e.CopyTimeFrom(m_vm->m_event->cause); // set fragment time for "now" + e.Type = Event::type_note_off; + e.Param.Note.Velocity = velocity; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } else if (args->arg(0)->exprType() == INT_ARR_EXPR) { + VMIntArrayExpr* ids = args->arg(0)->asIntArray(); + for (int i = 0; i < ids->arraySize(); ++i) { + const ScriptID id = ids->evalIntElement(i); + if (!id || !id.isNoteID()) continue; + + NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); + if (!pNote) continue; + + Event e = pNote->cause; + e.Init(); // clear IDs + e.CopyTimeFrom(m_vm->m_event->cause); // set fragment time for "now" + e.Type = Event::type_note_off; + e.Param.Note.Velocity = velocity; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } } - if (velocity < 0 || velocity > 127) { - errMsg("note_off(): argument 2 is an invalid velocity value"); + return successResult(); + } + + InstrumentScriptVMFunction_set_event_mark::InstrumentScriptVMFunction_set_event_mark(InstrumentScriptVM* parent) + : m_vm(parent) + { + } + + VMFnResult* InstrumentScriptVMFunction_set_event_mark::exec(VMFnArgs* args) { + const ScriptID id = args->arg(0)->asInt()->evalInt(); + const int groupID = args->arg(1)->asInt()->evalInt(); + + if (groupID < 0 || groupID >= INSTR_SCRIPT_EVENT_GROUPS) { + errMsg("set_event_mark(): argument 2 is an invalid group id"); return errorResult(); } AbstractEngineChannel* pEngineChannel = static_cast(m_vm->m_event->cause.pEngineChannel); - RTList::Iterator itEvent = pEngineChannel->pEngine->EventByID(id); - if (!itEvent) return successResult(); + // check if the event/note still exists + switch (id.type()) { + case ScriptID::EVENT: { + RTList::Iterator itEvent = pEngineChannel->pEngine->EventByID( id.eventID() ); + if (!itEvent) return successResult(); + break; + } + case ScriptID::NOTE: { + NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); + if (!pNote) return successResult(); + break; + } + } + + pEngineChannel->pScript->eventGroups[groupID].insert(id); - Event e = *itEvent; - e.Type = Event::type_note_off; - e.Param.Note.Velocity = velocity; - memset(&e.Format, 0, sizeof(e.Format)); // init format speific stuff with zero + return successResult(); + } - int releaseEventID = pEngineChannel->ScheduleEvent(&e, 0); + InstrumentScriptVMFunction_delete_event_mark::InstrumentScriptVMFunction_delete_event_mark(InstrumentScriptVM* parent) + : m_vm(parent) + { + } + + VMFnResult* InstrumentScriptVMFunction_delete_event_mark::exec(VMFnArgs* args) { + const ScriptID id = args->arg(0)->asInt()->evalInt(); + const int groupID = args->arg(1)->asInt()->evalInt(); + + if (groupID < 0 || groupID >= INSTR_SCRIPT_EVENT_GROUPS) { + errMsg("delete_event_mark(): argument 2 is an invalid group id"); + return errorResult(); + } + + AbstractEngineChannel* pEngineChannel = + static_cast(m_vm->m_event->cause.pEngineChannel); + + pEngineChannel->pScript->eventGroups[groupID].erase(id); return successResult(); } + InstrumentScriptVMFunction_by_marks::InstrumentScriptVMFunction_by_marks(InstrumentScriptVM* parent) + : m_vm(parent) + { + } + + int InstrumentScriptVMFunction_by_marks::Result::arraySize() const { + return eventGroup->size(); + } + + int InstrumentScriptVMFunction_by_marks::Result::evalIntElement(uint i) { + return (*eventGroup)[i]; + } + + VMFnResult* InstrumentScriptVMFunction_by_marks::errorResult() { + m_result.eventGroup = NULL; + m_result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); + return &m_result; + } + + VMFnResult* InstrumentScriptVMFunction_by_marks::successResult(EventGroup* eventGroup) { + m_result.eventGroup = eventGroup; + m_result.flags = STMT_SUCCESS; + return &m_result; + } + + VMFnResult* InstrumentScriptVMFunction_by_marks::exec(VMFnArgs* args) { + int groupID = args->arg(0)->asInt()->evalInt(); + + if (groupID < 0 || groupID >= INSTR_SCRIPT_EVENT_GROUPS) { + errMsg("by_marks(): argument is an invalid group id"); + return errorResult(); + } + + AbstractEngineChannel* pEngineChannel = + static_cast(m_vm->m_event->cause.pEngineChannel); + + return successResult( &pEngineChannel->pScript->eventGroups[groupID] ); + } + } // namespace LinuxSampler