--- linuxsampler/trunk/src/engines/common/InstrumentScriptVMFunctions.cpp 2016/07/16 11:24:39 2953 +++ linuxsampler/trunk/src/engines/common/InstrumentScriptVMFunctions.cpp 2017/05/25 15:16:28 3216 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2016 Christian Schoenebeck + * Copyright (c) 2014-2017 Christian Schoenebeck * * http://www.linuxsampler.org * @@ -136,9 +136,9 @@ AbstractEngineChannel* pEngineChannel = static_cast(m_vm->m_event->cause.pEngineChannel); - if (args->arg(0)->exprType() == INT_EXPR) { - const ScriptID id = args->arg(0)->asInt()->evalInt(); - if (!id) { + if (args->argsCount() == 0 || args->arg(0)->exprType() == INT_EXPR) { + const ScriptID id = (args->argsCount() >= 1) ? args->arg(0)->asInt()->evalInt() : m_vm->m_event->id; + if (!id && args->argsCount() >= 1) { 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(); @@ -359,12 +359,13 @@ if (iArg == 0) return type == INT_EXPR || type == INT_ARR_EXPR; else - return INT_EXPR; + return type == INT_EXPR; } VMFnResult* InstrumentScriptVMFunction_change_vol::exec(VMFnArgs* args) { int volume = args->arg(1)->asInt()->evalInt(); // volume change in milli dB bool relative = (args->argsCount() >= 3) ? (args->arg(2)->asInt()->evalInt() & 1) : false; + const float fVolumeLin = RTMath::DecibelToLinRatio(float(volume) / 1000.f); AbstractEngineChannel* pEngineChannel = static_cast(m_vm->m_event->cause.pEngineChannel); @@ -383,38 +384,14 @@ NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); if (!pNote) return successResult(); - const float fVolumeLin = RTMath::DecibelToLinRatio(float(volume) / 1000.f); - // commented out, performed by EngineBase::ProcessNoteSynthParam() for time accuracy behavior - /*if (relative) - pNote->Override.Volume *= fVolumeLin; - else - pNote->Override.Volume = fVolumeLin;*/ - - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_volume; - e.Param.NoteSynthParam.Delta = fVolumeLin; - e.Param.NoteSynthParam.Relative = relative; - - 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; - - const float fVolumeLin = RTMath::DecibelToLinRatio(float(volume) / 1000.f); - // commented out, performed by EngineBase::ProcessNoteSynthParam() for time accuracy behavior - /*if (relative) + // if change_vol() was called immediately after note was triggered + // then immediately apply the volume to note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + if (relative) pNote->Override.Volume *= fVolumeLin; else - pNote->Override.Volume = fVolumeLin;*/ - + pNote->Override.Volume = fVolumeLin; + } else { // otherwise schedule the volume change ... Event e = m_vm->m_event->cause; // copy to get fragment time for "now" e.Init(); // clear IDs e.Type = Event::type_note_synth_param; @@ -425,6 +402,34 @@ 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; + + // if change_vol() was called immediately after note was triggered + // then immediately apply the volume to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + if (relative) + pNote->Override.Volume *= fVolumeLin; + else + pNote->Override.Volume = fVolumeLin; + } else { // otherwise schedule the volume change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_volume; + e.Param.NoteSynthParam.Delta = fVolumeLin; + e.Param.NoteSynthParam.Relative = relative; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } + } } return successResult(); @@ -441,12 +446,13 @@ if (iArg == 0) return type == INT_EXPR || type == INT_ARR_EXPR; else - return INT_EXPR; + return type == INT_EXPR; } VMFnResult* InstrumentScriptVMFunction_change_tune::exec(VMFnArgs* args) { int tune = args->arg(1)->asInt()->evalInt(); // tuning change in milli cents bool relative = (args->argsCount() >= 3) ? (args->arg(2)->asInt()->evalInt() & 1) : false; + const float fFreqRatio = RTMath::CentsToFreqRatioUnlimited(float(tune) / 1000.f); AbstractEngineChannel* pEngineChannel = static_cast(m_vm->m_event->cause.pEngineChannel); @@ -465,38 +471,14 @@ NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); if (!pNote) return successResult(); - const float fFreqRatio = RTMath::CentsToFreqRatioUnlimited(float(tune) / 1000.f); - // commented out, performed by EngineBase::ProcessNoteSynthParam() for time accuracy behavior - /*if (relative) - pNote->Override.Pitch *= fFreqRatio; - else - pNote->Override.Pitch = fFreqRatio;*/ - - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_pitch; - e.Param.NoteSynthParam.Delta = fFreqRatio; - e.Param.NoteSynthParam.Relative = relative; - - 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; - - const float fFreqRatio = RTMath::CentsToFreqRatioUnlimited(float(tune) / 1000.f); - // commented out, performed by EngineBase::ProcessNoteSynthParam() for time accuracy behavior - /*if (relative) + // if change_tune() was called immediately after note was triggered + // then immediately apply the tuning to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + if (relative) pNote->Override.Pitch *= fFreqRatio; else - pNote->Override.Pitch = fFreqRatio;*/ - + pNote->Override.Pitch = fFreqRatio; + } else { // otherwise schedule tuning change ... Event e = m_vm->m_event->cause; // copy to get fragment time for "now" e.Init(); // clear IDs e.Type = Event::type_note_synth_param; @@ -507,6 +489,34 @@ 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; + + // if change_tune() was called immediately after note was triggered + // then immediately apply the tuning to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + if (relative) + pNote->Override.Pitch *= fFreqRatio; + else + pNote->Override.Pitch = fFreqRatio; + } else { // otherwise schedule tuning change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_pitch; + e.Param.NoteSynthParam.Delta = fFreqRatio; + e.Param.NoteSynthParam.Relative = relative; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } + } } return successResult(); @@ -523,7 +533,7 @@ if (iArg == 0) return type == INT_EXPR || type == INT_ARR_EXPR; else - return INT_EXPR; + return type == INT_EXPR; } VMFnResult* InstrumentScriptVMFunction_change_pan::exec(VMFnArgs* args) { @@ -537,6 +547,7 @@ wrnMsg("change_pan(): argument 2 may not be smaller than -1000"); pan = -1000; } + const float fPan = float(pan) / 1000.f; AbstractEngineChannel* pEngineChannel = static_cast(m_vm->m_event->cause.pEngineChannel); @@ -555,42 +566,16 @@ NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); if (!pNote) return successResult(); - const float fPan = float(pan) / 1000.f; - // commented out, performed by EngineBase::ProcessNoteSynthParam() for time accuracy behavior - /*if (relative) { - pNote->Override.Pan = RTMath::RelativeSummedAvg(pNote->Override.Pan, fPan, ++pNote->Override.PanSources); - } else { - pNote->Override.Pan = fPan; - pNote->Override.PanSources = 1; // only relevant on subsequent change_pan() calls on same note with 'relative' being set - }*/ - - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_pan; - e.Param.NoteSynthParam.Delta = fPan; - e.Param.NoteSynthParam.Relative = relative; - - 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; - - const float fPan = float(pan) / 1000.f; - // commented out, performed by EngineBase::ProcessNoteSynthParam() for time accuracy behavior - /*if (relative) { + // if change_pan() was called immediately after note was triggered + // then immediately apply the panning to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + if (relative) { pNote->Override.Pan = RTMath::RelativeSummedAvg(pNote->Override.Pan, fPan, ++pNote->Override.PanSources); } else { pNote->Override.Pan = fPan; pNote->Override.PanSources = 1; // only relevant on subsequent change_pan() calls on same note with 'relative' being set - }*/ - + } + } else { // otherwise schedule panning change ... Event e = m_vm->m_event->cause; // copy to get fragment time for "now" e.Init(); // clear IDs e.Type = Event::type_note_synth_param; @@ -601,6 +586,36 @@ 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; + + // if change_pan() was called immediately after note was triggered + // then immediately apply the panning to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + if (relative) { + pNote->Override.Pan = RTMath::RelativeSummedAvg(pNote->Override.Pan, fPan, ++pNote->Override.PanSources); + } else { + pNote->Override.Pan = fPan; + pNote->Override.PanSources = 1; // only relevant on subsequent change_pan() calls on same note with 'relative' being set + } + } else { // otherwise schedule panning change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_pan; + e.Param.NoteSynthParam.Delta = fPan; + e.Param.NoteSynthParam.Relative = relative; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } + } } return successResult(); @@ -620,7 +635,7 @@ if (iArg == 0) return type == INT_EXPR || type == INT_ARR_EXPR; else - return INT_EXPR; + return type == INT_EXPR; } VMFnResult* InstrumentScriptVMFunction_change_cutoff::exec(VMFnArgs* args) { @@ -632,6 +647,7 @@ wrnMsg("change_cutoff(): argument 2 may not be negative"); cutoff = 0; } + const float fCutoff = float(cutoff) / float(VM_FILTER_PAR_MAX_VALUE); AbstractEngineChannel* pEngineChannel = static_cast(m_vm->m_event->cause.pEngineChannel); @@ -650,17 +666,21 @@ NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); if (!pNote) return successResult(); - const float fCutoff = float(cutoff) / float(VM_FILTER_PAR_MAX_VALUE); - - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_cutoff; - e.Param.NoteSynthParam.Delta = fCutoff; - e.Param.NoteSynthParam.Relative = false; + // if change_cutoff() was called immediately after note was triggered + // then immediately apply cutoff to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Cutoff = fCutoff; + } else { // otherwise schedule cutoff change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_cutoff; + e.Param.NoteSynthParam.Delta = fCutoff; + e.Param.NoteSynthParam.Relative = false; - pEngineChannel->ScheduleEventMicroSec(&e, 0); + 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) { @@ -670,17 +690,21 @@ NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); if (!pNote) continue; - const float fCutoff = float(cutoff) / float(VM_FILTER_PAR_MAX_VALUE); + // if change_cutoff() was called immediately after note was triggered + // then immediately apply cutoff to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Cutoff = fCutoff; + } else { // otherwise schedule cutoff change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_cutoff; + e.Param.NoteSynthParam.Delta = fCutoff; + e.Param.NoteSynthParam.Relative = false; - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_cutoff; - e.Param.NoteSynthParam.Delta = fCutoff; - e.Param.NoteSynthParam.Relative = false; - - pEngineChannel->ScheduleEventMicroSec(&e, 0); + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } } } @@ -698,7 +722,7 @@ if (iArg == 0) return type == INT_EXPR || type == INT_ARR_EXPR; else - return INT_EXPR; + return type == INT_EXPR; } VMFnResult* InstrumentScriptVMFunction_change_reso::exec(VMFnArgs* args) { @@ -710,6 +734,7 @@ wrnMsg("change_reso(): argument 2 may not be negative"); resonance = 0; } + const float fResonance = float(resonance) / float(VM_FILTER_PAR_MAX_VALUE); AbstractEngineChannel* pEngineChannel = static_cast(m_vm->m_event->cause.pEngineChannel); @@ -728,17 +753,21 @@ NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); if (!pNote) return successResult(); - const float fResonance = float(resonance) / float(VM_FILTER_PAR_MAX_VALUE); - - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_resonance; - e.Param.NoteSynthParam.Delta = fResonance; - e.Param.NoteSynthParam.Relative = false; + // if change_reso() was called immediately after note was triggered + // then immediately apply resonance to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Resonance = fResonance; + } else { // otherwise schedule resonance change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_resonance; + e.Param.NoteSynthParam.Delta = fResonance; + e.Param.NoteSynthParam.Relative = false; - pEngineChannel->ScheduleEventMicroSec(&e, 0); + 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) { @@ -748,17 +777,21 @@ NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); if (!pNote) continue; - const float fResonance = float(resonance) / float(VM_FILTER_PAR_MAX_VALUE); - - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_resonance; - e.Param.NoteSynthParam.Delta = fResonance; - e.Param.NoteSynthParam.Relative = false; + // if change_reso() was called immediately after note was triggered + // then immediately apply resonance to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Resonance = fResonance; + } else { // otherwise schedule resonance change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_resonance; + e.Param.NoteSynthParam.Delta = fResonance; + e.Param.NoteSynthParam.Relative = false; - pEngineChannel->ScheduleEventMicroSec(&e, 0); + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } } } @@ -776,7 +809,7 @@ if (iArg == 0) return type == INT_EXPR || type == INT_ARR_EXPR; else - return INT_EXPR; + return type == INT_EXPR; } VMFnResult* InstrumentScriptVMFunction_change_attack::exec(VMFnArgs* args) { @@ -805,17 +838,23 @@ } NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); - if (!pNote) return successResult(); + if (!pNote) return successResult(); - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_attack; - e.Param.NoteSynthParam.Delta = fAttack; - e.Param.NoteSynthParam.Relative = false; + // if change_attack() was called immediately after note was triggered + // then immediately apply attack to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Attack = fAttack; + } else { // otherwise schedule attack change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_attack; + e.Param.NoteSynthParam.Delta = fAttack; + e.Param.NoteSynthParam.Relative = false; - pEngineChannel->ScheduleEventMicroSec(&e, 0); + 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) { @@ -825,15 +864,21 @@ NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); if (!pNote) continue; - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_attack; - e.Param.NoteSynthParam.Delta = fAttack; - e.Param.NoteSynthParam.Relative = false; + // if change_attack() was called immediately after note was triggered + // then immediately apply attack to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Attack = fAttack; + } else { // otherwise schedule attack change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_attack; + e.Param.NoteSynthParam.Delta = fAttack; + e.Param.NoteSynthParam.Relative = false; - pEngineChannel->ScheduleEventMicroSec(&e, 0); + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } } } @@ -851,7 +896,7 @@ if (iArg == 0) return type == INT_EXPR || type == INT_ARR_EXPR; else - return INT_EXPR; + return type == INT_EXPR; } VMFnResult* InstrumentScriptVMFunction_change_decay::exec(VMFnArgs* args) { @@ -880,17 +925,23 @@ } NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); - if (!pNote) return successResult(); + if (!pNote) return successResult(); - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_decay; - e.Param.NoteSynthParam.Delta = fDecay; - e.Param.NoteSynthParam.Relative = false; + // if change_decay() was called immediately after note was triggered + // then immediately apply decay to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Decay = fDecay; + } else { // otherwise schedule decay change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_decay; + e.Param.NoteSynthParam.Delta = fDecay; + e.Param.NoteSynthParam.Relative = false; - pEngineChannel->ScheduleEventMicroSec(&e, 0); + 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) { @@ -900,15 +951,21 @@ NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); if (!pNote) continue; - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_decay; - e.Param.NoteSynthParam.Delta = fDecay; - e.Param.NoteSynthParam.Relative = false; + // if change_decay() was called immediately after note was triggered + // then immediately apply decay to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Decay = fDecay; + } else { // otherwise schedule decay change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_decay; + e.Param.NoteSynthParam.Delta = fDecay; + e.Param.NoteSynthParam.Relative = false; - pEngineChannel->ScheduleEventMicroSec(&e, 0); + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } } } @@ -926,7 +983,7 @@ if (iArg == 0) return type == INT_EXPR || type == INT_ARR_EXPR; else - return INT_EXPR; + return type == INT_EXPR; } VMFnResult* InstrumentScriptVMFunction_change_release::exec(VMFnArgs* args) { @@ -955,17 +1012,23 @@ } NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); - if (!pNote) return successResult(); + if (!pNote) return successResult(); - Event e = m_vm->m_event->cause; // copy to get fragment time for "now" - e.Init(); // clear IDs - e.Type = Event::type_note_synth_param; - e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_release; - e.Param.NoteSynthParam.Delta = fRelease; - e.Param.NoteSynthParam.Relative = false; + // if change_release() was called immediately after note was triggered + // then immediately apply relase to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Release = fRelease; + } else { // otherwise schedule release change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_release; + e.Param.NoteSynthParam.Delta = fRelease; + e.Param.NoteSynthParam.Relative = false; - pEngineChannel->ScheduleEventMicroSec(&e, 0); + 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) { @@ -975,16 +1038,641 @@ NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); if (!pNote) continue; + // if change_release() was called immediately after note was triggered + // then immediately apply relase to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Release = fRelease; + } else { // otherwise schedule release change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_release; + e.Param.NoteSynthParam.Delta = fRelease; + e.Param.NoteSynthParam.Relative = false; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } + } + } + + return successResult(); + } + + bool VMChangeSynthParamFunction::acceptsArgType(int iArg, ExprType_t type) const { + if (iArg == 0) + return type == INT_EXPR || type == INT_ARR_EXPR; + else + return type == INT_EXPR; + } + + // Arbitrarily chosen constant value symbolizing "no limit". + #define NO_LIMIT 1315916909 + + template + VMFnResult* VMChangeSynthParamFunction::execTemplate(VMFnArgs* args, const char* functionName) { + int value = args->arg(1)->asInt()->evalInt(); + if (T_maxValue != NO_LIMIT && value > T_maxValue) { + wrnMsg(String(functionName) + "(): argument 2 may not be larger than " + ToString(T_maxValue)); + value = T_maxValue; + } else if (T_minValue != NO_LIMIT && value < T_minValue) { + if (T_minValue == 0) + wrnMsg(String(functionName) + "(): argument 2 may not be negative"); + else + wrnMsg(String(functionName) + "(): argument 2 may not be smaller than " + ToString(T_minValue)); + value = T_minValue; + } + const float fValue = (T_isNormalizedParam) ? + float(value) / float(T_maxValue) : // convert to 0.0 .. 1.0 value range + float(value) / 1000000.f; // assuming microseconds here, convert to seconds + + AbstractEngineChannel* pEngineChannel = + static_cast(m_vm->m_event->cause.pEngineChannel); + + if (args->arg(0)->exprType() == INT_EXPR) { + const ScriptID id = args->arg(0)->asInt()->evalInt(); + if (!id) { + wrnMsg(String(functionName) + "(): note ID for argument 1 may not be zero"); + return successResult(); + } + if (!id.isNoteID()) { + wrnMsg(String(functionName) + "(): argument 1 is not a note ID"); + return successResult(); + } + + NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); + if (!pNote) return successResult(); + + // if this change_*() script function was called immediately after + // note was triggered then immediately apply the synth parameter + // change to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.*T_noteParam = fValue; + } else { // otherwise schedule this synth parameter change ... Event e = m_vm->m_event->cause; // copy to get fragment time for "now" e.Init(); // clear IDs e.Type = Event::type_note_synth_param; e.Param.NoteSynthParam.NoteID = id.noteID(); - e.Param.NoteSynthParam.Type = Event::synth_param_release; - e.Param.NoteSynthParam.Delta = fRelease; + e.Param.NoteSynthParam.Type = (Event::synth_param_t) T_synthParam; + e.Param.NoteSynthParam.Delta = fValue; e.Param.NoteSynthParam.Relative = false; 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; + + // if this change_*() script function was called immediately after + // note was triggered then immediately apply the synth parameter + // change to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.*T_noteParam = fValue; + } else { // otherwise schedule this synth parameter change ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = (Event::synth_param_t) T_synthParam; + e.Param.NoteSynthParam.Delta = fValue; + e.Param.NoteSynthParam.Relative = false; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } + } + } + + return successResult(); + } + + // change_amp_lfo_depth() function + + VMFnResult* InstrumentScriptVMFunction_change_amp_lfo_depth::exec(VMFnArgs* args) { + return VMChangeSynthParamFunction::execTemplate< + &NoteBase::_Override::AmpLFODepth, + Event::synth_param_amp_lfo_depth, + true, 1000000, 0>( args, "change_amp_lfo_depth" ); + } + + // change_amp_lfo_freq() function + + VMFnResult* InstrumentScriptVMFunction_change_amp_lfo_freq::exec(VMFnArgs* args) { + return VMChangeSynthParamFunction::execTemplate< + &NoteBase::_Override::AmpLFOFreq, + Event::synth_param_amp_lfo_freq, + true, 1000000, 0>( args, "change_amp_lfo_freq" ); + } + + // change_pitch_lfo_depth() function + + VMFnResult* InstrumentScriptVMFunction_change_pitch_lfo_depth::exec(VMFnArgs* args) { + return VMChangeSynthParamFunction::execTemplate< + &NoteBase::_Override::PitchLFODepth, + Event::synth_param_pitch_lfo_depth, + true, 1000000, 0>( args, "change_pitch_lfo_depth" ); + } + + // change_pitch_lfo_freq() function + + VMFnResult* InstrumentScriptVMFunction_change_pitch_lfo_freq::exec(VMFnArgs* args) { + return VMChangeSynthParamFunction::execTemplate< + &NoteBase::_Override::PitchLFOFreq, + Event::synth_param_pitch_lfo_freq, + true, 1000000, 0>( args, "change_pitch_lfo_freq" ); + } + + // change_vol_time() function + + VMFnResult* InstrumentScriptVMFunction_change_vol_time::exec(VMFnArgs* args) { + return VMChangeSynthParamFunction::execTemplate< + &NoteBase::_Override::VolumeTime, + Event::synth_param_volume_time, + false, NO_LIMIT, 0>( args, "change_vol_time" ); + } + + // change_tune_time() function + + VMFnResult* InstrumentScriptVMFunction_change_tune_time::exec(VMFnArgs* args) { + return VMChangeSynthParamFunction::execTemplate< + &NoteBase::_Override::PitchTime, + Event::synth_param_pitch_time, + false, NO_LIMIT, 0>( args, "change_tune_time" ); + } + + // fade_in() function + + InstrumentScriptVMFunction_fade_in::InstrumentScriptVMFunction_fade_in(InstrumentScriptVM* parent) + : m_vm(parent) + { + } + + bool InstrumentScriptVMFunction_fade_in::acceptsArgType(int iArg, ExprType_t type) const { + if (iArg == 0) + return type == INT_EXPR || type == INT_ARR_EXPR; + else + return type == INT_EXPR; + } + + VMFnResult* InstrumentScriptVMFunction_fade_in::exec(VMFnArgs* args) { + int duration = args->arg(1)->asInt()->evalInt(); + if (duration < 0) { + wrnMsg("fade_in(): argument 2 may not be negative"); + duration = 0; + } + const float fDuration = float(duration) / 1000000.f; // convert microseconds to seconds + + AbstractEngineChannel* pEngineChannel = + static_cast(m_vm->m_event->cause.pEngineChannel); + + if (args->arg(0)->exprType() == INT_EXPR) { + const ScriptID id = args->arg(0)->asInt()->evalInt(); + if (!id) { + wrnMsg("fade_in(): note ID for argument 1 may not be zero"); + return successResult(); + } + if (!id.isNoteID()) { + wrnMsg("fade_in(): argument 1 is not a note ID"); + return successResult(); + } + + NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); + if (!pNote) return successResult(); + + // if fade_in() was called immediately after note was triggered + // then immediately apply a start volume of zero to Note object, + // as well as the fade in duration + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Volume = 0.f; + pNote->Override.VolumeTime = fDuration; + } else { // otherwise schedule a "volume time" change with the requested fade in duration ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_volume_time; + e.Param.NoteSynthParam.Delta = fDuration; + e.Param.NoteSynthParam.Relative = false; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } + // and finally schedule a "volume" change, simply one time slice + // ahead, with the final fade in volume (1.0) + { + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_volume; + e.Param.NoteSynthParam.Delta = 1.f; + e.Param.NoteSynthParam.Relative = false; + + // scheduling with 0 delay would also work here, but +1 is more + // safe regarding potential future implementation changes of the + // scheduler (see API comments of RTAVLTree::insert()) + pEngineChannel->ScheduleEventMicroSec(&e, 1); + } + } 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; + + // if fade_in() was called immediately after note was triggered + // then immediately apply a start volume of zero to Note object, + // as well as the fade in duration + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.Volume = 0.f; + pNote->Override.VolumeTime = fDuration; + } else { // otherwise schedule a "volume time" change with the requested fade in duration ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_volume_time; + e.Param.NoteSynthParam.Delta = fDuration; + e.Param.NoteSynthParam.Relative = false; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } + // and finally schedule a "volume" change, simply one time slice + // ahead, with the final fade in volume (1.0) + { + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_volume; + e.Param.NoteSynthParam.Delta = 1.f; + e.Param.NoteSynthParam.Relative = false; + + // scheduling with 0 delay would also work here, but +1 is more + // safe regarding potential future implementation changes of the + // scheduler (see API comments of RTAVLTree::insert()) + pEngineChannel->ScheduleEventMicroSec(&e, 1); + } + } + } + + return successResult(); + } + + // fade_out() function + + InstrumentScriptVMFunction_fade_out::InstrumentScriptVMFunction_fade_out(InstrumentScriptVM* parent) + : m_vm(parent) + { + } + + bool InstrumentScriptVMFunction_fade_out::acceptsArgType(int iArg, ExprType_t type) const { + if (iArg == 0) + return type == INT_EXPR || type == INT_ARR_EXPR; + else + return type == INT_EXPR; + } + + VMFnResult* InstrumentScriptVMFunction_fade_out::exec(VMFnArgs* args) { + int duration = args->arg(1)->asInt()->evalInt(); + if (duration < 0) { + wrnMsg("fade_out(): argument 2 may not be negative"); + duration = 0; + } + const float fDuration = float(duration) / 1000000.f; // convert microseconds to seconds + + bool stop = (args->argsCount() >= 3) ? (args->arg(2)->asInt()->evalInt() & 1) : true; + + AbstractEngineChannel* pEngineChannel = + static_cast(m_vm->m_event->cause.pEngineChannel); + + if (args->arg(0)->exprType() == INT_EXPR) { + const ScriptID id = args->arg(0)->asInt()->evalInt(); + if (!id) { + wrnMsg("fade_out(): note ID for argument 1 may not be zero"); + return successResult(); + } + if (!id.isNoteID()) { + wrnMsg("fade_out(): argument 1 is not a note ID"); + return successResult(); + } + + NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); + if (!pNote) return successResult(); + + // if fade_out() was called immediately after note was triggered + // then immediately apply fade out duration to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.VolumeTime = fDuration; + } else { // otherwise schedule a "volume time" change with the requested fade out duration ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_volume_time; + e.Param.NoteSynthParam.Delta = fDuration; + e.Param.NoteSynthParam.Relative = false; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } + // now schedule a "volume" change, simply one time slice ahead, with + // the final fade out volume (0.0) + { + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_volume; + e.Param.NoteSynthParam.Delta = 0.f; + e.Param.NoteSynthParam.Relative = false; + + // scheduling with 0 delay would also work here, but +1 is more + // safe regarding potential future implementation changes of the + // scheduler (see API comments of RTAVLTree::insert()) + pEngineChannel->ScheduleEventMicroSec(&e, 1); + } + // and finally if stopping the note was requested after the fade out + // completed, then schedule to kill the voice after the requested + // duration + if (stop) { + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_kill_note; + e.Param.Note.ID = id.noteID(); + e.Param.Note.Key = pNote->hostKey; + + pEngineChannel->ScheduleEventMicroSec(&e, duration + 1); + } + } 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; + + // if fade_out() was called immediately after note was triggered + // then immediately apply fade out duration to Note object + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->Override.VolumeTime = fDuration; + } else { // otherwise schedule a "volume time" change with the requested fade out duration ... + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_volume_time; + e.Param.NoteSynthParam.Delta = fDuration; + e.Param.NoteSynthParam.Relative = false; + + pEngineChannel->ScheduleEventMicroSec(&e, 0); + } + // now schedule a "volume" change, simply one time slice ahead, with + // the final fade out volume (0.0) + { + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_note_synth_param; + e.Param.NoteSynthParam.NoteID = id.noteID(); + e.Param.NoteSynthParam.Type = Event::synth_param_volume; + e.Param.NoteSynthParam.Delta = 0.f; + e.Param.NoteSynthParam.Relative = false; + + // scheduling with 0 delay would also work here, but +1 is more + // safe regarding potential future implementation changes of the + // scheduler (see API comments of RTAVLTree::insert()) + pEngineChannel->ScheduleEventMicroSec(&e, 1); + } + // and finally if stopping the note was requested after the fade out + // completed, then schedule to kill the voice after the requested + // duration + if (stop) { + Event e = m_vm->m_event->cause; // copy to get fragment time for "now" + e.Init(); // clear IDs + e.Type = Event::type_kill_note; + e.Param.Note.ID = id.noteID(); + e.Param.Note.Key = pNote->hostKey; + + pEngineChannel->ScheduleEventMicroSec(&e, duration + 1); + } + } + } + + return successResult(); + } + + // get_event_par() function + + InstrumentScriptVMFunction_get_event_par::InstrumentScriptVMFunction_get_event_par(InstrumentScriptVM* parent) + : m_vm(parent) + { + } + + VMFnResult* InstrumentScriptVMFunction_get_event_par::exec(VMFnArgs* args) { + AbstractEngineChannel* pEngineChannel = + static_cast(m_vm->m_event->cause.pEngineChannel); + + const ScriptID id = args->arg(0)->asInt()->evalInt(); + if (!id) { + wrnMsg("get_event_par(): note ID for argument 1 may not be zero"); + return successResult(0); + } + if (!id.isNoteID()) { + wrnMsg("get_event_par(): argument 1 is not a note ID"); + return successResult(0); + } + + NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); + if (!pNote) { + wrnMsg("get_event_par(): no note alive with that note ID of argument 1"); + return successResult(0); + } + + const int parameter = args->arg(1)->asInt()->evalInt(); + switch (parameter) { + case EVENT_PAR_NOTE: + return successResult(pNote->cause.Param.Note.Key); + case EVENT_PAR_VELOCITY: + return successResult(pNote->cause.Param.Note.Velocity); + case EVENT_PAR_VOLUME: + return successResult( + RTMath::LinRatioToDecibel(pNote->Override.Volume) * 1000.f + ); + case EVENT_PAR_TUNE: + return successResult( + RTMath::FreqRatioToCents(pNote->Override.Pitch) * 1000.f + ); + case EVENT_PAR_0: + return successResult(pNote->userPar[0]); + case EVENT_PAR_1: + return successResult(pNote->userPar[1]); + case EVENT_PAR_2: + return successResult(pNote->userPar[2]); + case EVENT_PAR_3: + return successResult(pNote->userPar[3]); + } + + wrnMsg("get_event_par(): argument 2 is an invalid event parameter"); + return successResult(0); + } + + // set_event_par() function + + InstrumentScriptVMFunction_set_event_par::InstrumentScriptVMFunction_set_event_par(InstrumentScriptVM* parent) + : m_vm(parent) + { + } + + VMFnResult* InstrumentScriptVMFunction_set_event_par::exec(VMFnArgs* args) { + AbstractEngineChannel* pEngineChannel = + static_cast(m_vm->m_event->cause.pEngineChannel); + + const ScriptID id = args->arg(0)->asInt()->evalInt(); + if (!id) { + wrnMsg("set_event_par(): note ID for argument 1 may not be zero"); + return successResult(); + } + if (!id.isNoteID()) { + wrnMsg("set_event_par(): argument 1 is not a note ID"); + return successResult(); + } + + NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); + if (!pNote) return successResult(); + + const int parameter = args->arg(1)->asInt()->evalInt(); + const int value = args->arg(2)->asInt()->evalInt(); + + switch (parameter) { + case EVENT_PAR_NOTE: + if (value < 0 || value > 127) { + wrnMsg("set_event_par(): note number of argument 3 is out of range"); + return successResult(); + } + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->cause.Param.Note.Key = value; + m_vm->m_event->cause.Param.Note.Key = value; + } else { + wrnMsg("set_event_par(): note number can only be changed when note is new"); + } + return successResult(); + case EVENT_PAR_VELOCITY: + if (value < 0 || value > 127) { + wrnMsg("set_event_par(): velocity of argument 3 is out of range"); + return successResult(); + } + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->cause.Param.Note.Velocity = value; + m_vm->m_event->cause.Param.Note.Velocity = value; + } else { + wrnMsg("set_event_par(): velocity can only be changed when note is new"); + } + return successResult(); + case EVENT_PAR_VOLUME: + wrnMsg("set_event_par(): changing volume by this function is currently not supported, use change_vol() instead"); + return successResult(); + case EVENT_PAR_TUNE: + wrnMsg("set_event_par(): changing tune by this function is currently not supported, use change_tune() instead"); + return successResult(); + case EVENT_PAR_0: + pNote->userPar[0] = value; + return successResult(); + case EVENT_PAR_1: + pNote->userPar[1] = value; + return successResult(); + case EVENT_PAR_2: + pNote->userPar[2] = value; + return successResult(); + case EVENT_PAR_3: + pNote->userPar[3] = value; + return successResult(); + } + + wrnMsg("set_event_par(): argument 2 is an invalid event parameter"); + return successResult(); + } + + // change_note() function + + InstrumentScriptVMFunction_change_note::InstrumentScriptVMFunction_change_note(InstrumentScriptVM* parent) + : m_vm(parent) + { + } + + VMFnResult* InstrumentScriptVMFunction_change_note::exec(VMFnArgs* args) { + AbstractEngineChannel* pEngineChannel = + static_cast(m_vm->m_event->cause.pEngineChannel); + + const ScriptID id = args->arg(0)->asInt()->evalInt(); + if (!id) { + wrnMsg("change_note(): note ID for argument 1 may not be zero"); + return successResult(); + } + if (!id.isNoteID()) { + wrnMsg("change_note(): argument 1 is not a note ID"); + return successResult(); + } + + NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); + if (!pNote) return successResult(); + + const int value = args->arg(1)->asInt()->evalInt(); + if (value < 0 || value > 127) { + wrnMsg("change_note(): note number of argument 2 is out of range"); + return successResult(); + } + + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->cause.Param.Note.Key = value; + m_vm->m_event->cause.Param.Note.Key = value; + } else { + wrnMsg("change_note(): note number can only be changed when note is new"); + } + + return successResult(); + } + + // change_velo() function + + InstrumentScriptVMFunction_change_velo::InstrumentScriptVMFunction_change_velo(InstrumentScriptVM* parent) + : m_vm(parent) + { + } + + VMFnResult* InstrumentScriptVMFunction_change_velo::exec(VMFnArgs* args) { + AbstractEngineChannel* pEngineChannel = + static_cast(m_vm->m_event->cause.pEngineChannel); + + const ScriptID id = args->arg(0)->asInt()->evalInt(); + if (!id) { + wrnMsg("change_velo(): note ID for argument 1 may not be zero"); + return successResult(); + } + if (!id.isNoteID()) { + wrnMsg("change_velo(): argument 1 is not a note ID"); + return successResult(); + } + + NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); + if (!pNote) return successResult(); + + const int value = args->arg(1)->asInt()->evalInt(); + if (value < 0 || value > 127) { + wrnMsg("change_velo(): velocity of argument 2 is out of range"); + return successResult(); + } + + if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { + pNote->cause.Param.Note.Velocity = value; + m_vm->m_event->cause.Param.Note.Velocity = value; + } else { + wrnMsg("change_velo(): velocity can only be changed when note is new"); } return successResult(); @@ -1055,7 +1743,7 @@ (args->argsCount() >= 2) ? (args->arg(1)->asInt()->evalInt() == 1) : false; pEngineChannel->ScheduleResumeOfScriptCallback( - itCallback, m_vm->m_event->cause.SchedTime(), disableWaitForever + itCallback, m_vm->m_event->scheduleTime, disableWaitForever ); return successResult();