/[svn]/linuxsampler/trunk/src/engines/common/InstrumentScriptVMFunctions.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/common/InstrumentScriptVMFunctions.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3188 by schoenebeck, Fri May 19 14:23:12 2017 UTC revision 3335 by schoenebeck, Sun Jul 30 14:33:15 2017 UTC
# Line 23  namespace LinuxSampler { Line 23  namespace LinuxSampler {
23      VMFnResult* InstrumentScriptVMFunction_play_note::exec(VMFnArgs* args) {      VMFnResult* InstrumentScriptVMFunction_play_note::exec(VMFnArgs* args) {
24          int note = args->arg(0)->asInt()->evalInt();          int note = args->arg(0)->asInt()->evalInt();
25          int velocity = (args->argsCount() >= 2) ? args->arg(1)->asInt()->evalInt() : 127;          int velocity = (args->argsCount() >= 2) ? args->arg(1)->asInt()->evalInt() : 127;
         int sampleoffset = (args->argsCount() >= 3) ? args->arg(2)->asInt()->evalInt() : 0;  
26          int duration = (args->argsCount() >= 4) ? args->arg(3)->asInt()->evalInt() : 0; //TODO: -1 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
27    
28          if (note < 0 || note > 127) {          if (note < 0 || note > 127) {
# Line 36  namespace LinuxSampler { Line 35  namespace LinuxSampler {
35              return errorResult(0);              return errorResult(0);
36          }          }
37    
38          if (sampleoffset < 0) {          if (duration < -2) {
39              errMsg("play_note(): argument 3 may not be a negative sample offset");              errMsg("play_note(): argument 4 must be a duration value of at least -2 or higher");
             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");  
40              return errorResult(0);              return errorResult(0);
41          }          }
42    
# Line 63  namespace LinuxSampler { Line 55  namespace LinuxSampler {
55                  return errorResult(0);                  return errorResult(0);
56              }              }
57              e.Param.Note.ParentNoteID = m_vm->m_event->cause.Param.Note.ID;              e.Param.Note.ParentNoteID = m_vm->m_event->cause.Param.Note.ID;
58                // check if that requested parent note is actually still alive
59                NoteBase* pParentNote =
60                    pEngineChannel->pEngine->NoteByID( e.Param.Note.ParentNoteID );
61                // if parent note is already gone then this new note is not required anymore
62                if (!pParentNote)
63                    return successResult(0);
64          }          }
65    
66          const note_id_t id = pEngineChannel->ScheduleNoteMicroSec(&e, 0);          const note_id_t id = pEngineChannel->ScheduleNoteMicroSec(&e, 0);
67    
68            // if a sample offset is supplied, assign the offset as override
69            // to the previously created Note object
70            if (args->argsCount() >= 3) {
71                int sampleoffset = args->arg(2)->asInt()->evalInt();
72                if (sampleoffset >= 0) {
73                    NoteBase* pNote = pEngineChannel->pEngine->NoteByID(id);
74                    if (pNote) {
75                        pNote->Override.SampleOffset = sampleoffset;
76                    }
77                } else if (sampleoffset < -1) {
78                    errMsg("play_note(): sample offset of argument 3 may not be less than -1");
79                }
80            }
81    
82          // if a duration is supplied (and play-note event was scheduled          // if a duration is supplied (and play-note event was scheduled
83          // successfully above), then schedule a subsequent stop-note event          // successfully above), then schedule a subsequent stop-note event
84          if (id && duration > 0) {          if (id && duration > 0) {
# Line 136  namespace LinuxSampler { Line 148  namespace LinuxSampler {
148          AbstractEngineChannel* pEngineChannel =          AbstractEngineChannel* pEngineChannel =
149                  static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);                  static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
150    
151          if (args->arg(0)->exprType() == INT_EXPR) {          if (args->argsCount() == 0 || args->arg(0)->exprType() == INT_EXPR) {
152              const ScriptID id = args->arg(0)->asInt()->evalInt();              const ScriptID id = (args->argsCount() >= 1) ? args->arg(0)->asInt()->evalInt() : m_vm->m_event->id;
153              if (!id) {              if (!id && args->argsCount() >= 1) {
154                  wrnMsg("ignore_event(): event ID argument may not be zero");                  wrnMsg("ignore_event(): event ID argument may not be zero");
155                  // not errorResult(), because that would abort the script, not intentional in this case                  // not errorResult(), because that would abort the script, not intentional in this case
156                  return successResult();                  return successResult();
# Line 385  namespace LinuxSampler { Line 397  namespace LinuxSampler {
397              if (!pNote) return successResult();              if (!pNote) return successResult();
398    
399              // if change_vol() was called immediately after note was triggered              // if change_vol() was called immediately after note was triggered
400              // then immediately apply the volume to note object              // then immediately apply the volume to note object, but only if
401              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              // change_vol_time() has not been called before
402                if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime &&
403                    pNote->Override.VolumeTime <= DEFAULT_NOTE_VOLUME_TIME_S)
404                {
405                  if (relative)                  if (relative)
406                      pNote->Override.Volume *= fVolumeLin;                      pNote->Override.Volume *= fVolumeLin;
407                  else                  else
# Line 412  namespace LinuxSampler { Line 427  namespace LinuxSampler {
427                  if (!pNote) continue;                  if (!pNote) continue;
428    
429                  // if change_vol() was called immediately after note was triggered                  // if change_vol() was called immediately after note was triggered
430                  // then immediately apply the volume to Note object                  // then immediately apply the volume to Note object, but only if
431                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  // change_vol_time() has not been called before
432                    if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime &&
433                        pNote->Override.VolumeTime <= DEFAULT_NOTE_VOLUME_TIME_S)
434                    {
435                      if (relative)                      if (relative)
436                          pNote->Override.Volume *= fVolumeLin;                          pNote->Override.Volume *= fVolumeLin;
437                      else                      else
# Line 472  namespace LinuxSampler { Line 490  namespace LinuxSampler {
490              if (!pNote) return successResult();              if (!pNote) return successResult();
491    
492              // if change_tune() was called immediately after note was triggered              // if change_tune() was called immediately after note was triggered
493              // then immediately apply the tuning to Note object              // then immediately apply the tuning to Note object, but only if
494              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              // change_tune_time() has not been called before
495                if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime &&
496                    pNote->Override.PitchTime <= DEFAULT_NOTE_PITCH_TIME_S)
497                {
498                  if (relative)                  if (relative)
499                      pNote->Override.Pitch *= fFreqRatio;                      pNote->Override.Pitch *= fFreqRatio;
500                  else                  else
# Line 499  namespace LinuxSampler { Line 520  namespace LinuxSampler {
520                  if (!pNote) continue;                  if (!pNote) continue;
521    
522                  // if change_tune() was called immediately after note was triggered                  // if change_tune() was called immediately after note was triggered
523                  // then immediately apply the tuning to Note object                  // then immediately apply the tuning to Note object, but only if
524                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  // change_tune_time() has not been called before
525                    if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime &&
526                        pNote->Override.PitchTime <= DEFAULT_NOTE_PITCH_TIME_S)
527                    {
528                      if (relative)                      if (relative)
529                          pNote->Override.Pitch *= fFreqRatio;                          pNote->Override.Pitch *= fFreqRatio;
530                      else                      else
# Line 568  namespace LinuxSampler { Line 592  namespace LinuxSampler {
592    
593              // if change_pan() was called immediately after note was triggered              // if change_pan() was called immediately after note was triggered
594              // then immediately apply the panning to Note object              // then immediately apply the panning to Note object
595              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
596                  if (relative) {                  if (relative) {
597                      pNote->Override.Pan = RTMath::RelativeSummedAvg(pNote->Override.Pan, fPan, ++pNote->Override.PanSources);                      pNote->Override.Pan = RTMath::RelativeSummedAvg(pNote->Override.Pan, fPan, ++pNote->Override.PanSources);
598                  } else {                  } else {
# Line 597  namespace LinuxSampler { Line 621  namespace LinuxSampler {
621    
622                  // if change_pan() was called immediately after note was triggered                  // if change_pan() was called immediately after note was triggered
623                  // then immediately apply the panning to Note object                  // then immediately apply the panning to Note object
624                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
625                      if (relative) {                      if (relative) {
626                          pNote->Override.Pan = RTMath::RelativeSummedAvg(pNote->Override.Pan, fPan, ++pNote->Override.PanSources);                          pNote->Override.Pan = RTMath::RelativeSummedAvg(pNote->Override.Pan, fPan, ++pNote->Override.PanSources);
627                      } else {                      } else {
# Line 668  namespace LinuxSampler { Line 692  namespace LinuxSampler {
692    
693              // if change_cutoff() was called immediately after note was triggered              // if change_cutoff() was called immediately after note was triggered
694              // then immediately apply cutoff to Note object              // then immediately apply cutoff to Note object
695              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
696                  pNote->Override.Cutoff = fCutoff;                  pNote->Override.Cutoff = fCutoff;
697              } else { // otherwise schedule cutoff change ...              } else { // otherwise schedule cutoff change ...
698                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 692  namespace LinuxSampler { Line 716  namespace LinuxSampler {
716    
717                  // if change_cutoff() was called immediately after note was triggered                  // if change_cutoff() was called immediately after note was triggered
718                  // then immediately apply cutoff to Note object                  // then immediately apply cutoff to Note object
719                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
720                      pNote->Override.Cutoff = fCutoff;                      pNote->Override.Cutoff = fCutoff;
721                  } else { // otherwise schedule cutoff change ...                  } else { // otherwise schedule cutoff change ...
722                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 755  namespace LinuxSampler { Line 779  namespace LinuxSampler {
779    
780              // if change_reso() was called immediately after note was triggered              // if change_reso() was called immediately after note was triggered
781              // then immediately apply resonance to Note object              // then immediately apply resonance to Note object
782              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
783                  pNote->Override.Resonance = fResonance;                  pNote->Override.Resonance = fResonance;
784              } else { // otherwise schedule resonance change ...              } else { // otherwise schedule resonance change ...
785                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 779  namespace LinuxSampler { Line 803  namespace LinuxSampler {
803    
804                  // if change_reso() was called immediately after note was triggered                  // if change_reso() was called immediately after note was triggered
805                  // then immediately apply resonance to Note object                  // then immediately apply resonance to Note object
806                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
807                      pNote->Override.Resonance = fResonance;                      pNote->Override.Resonance = fResonance;
808                  } else { // otherwise schedule resonance change ...                  } else { // otherwise schedule resonance change ...
809                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 814  namespace LinuxSampler { Line 838  namespace LinuxSampler {
838    
839      VMFnResult* InstrumentScriptVMFunction_change_attack::exec(VMFnArgs* args) {      VMFnResult* InstrumentScriptVMFunction_change_attack::exec(VMFnArgs* args) {
840          int attack = args->arg(1)->asInt()->evalInt();          int attack = args->arg(1)->asInt()->evalInt();
841          if (attack > VM_EG_PAR_MAX_VALUE) {          // note: intentionally not checking against a max. value here!
842              wrnMsg("change_attack(): argument 2 may not be larger than 1000000");          // (to allow i.e. passing 2000000 for doubling the attack time)
843              attack = VM_EG_PAR_MAX_VALUE;          if (attack < 0) {
         } else if (attack < 0) {  
844              wrnMsg("change_attack(): argument 2 may not be negative");              wrnMsg("change_attack(): argument 2 may not be negative");
845              attack = 0;              attack = 0;
846          }          }
# Line 842  namespace LinuxSampler { Line 865  namespace LinuxSampler {
865    
866              // if change_attack() was called immediately after note was triggered              // if change_attack() was called immediately after note was triggered
867              // then immediately apply attack to Note object              // then immediately apply attack to Note object
868              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
869                  pNote->Override.Attack = fAttack;                  pNote->Override.Attack = fAttack;
870              } else { // otherwise schedule attack change ...              } else { // otherwise schedule attack change ...
871                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 866  namespace LinuxSampler { Line 889  namespace LinuxSampler {
889    
890                  // if change_attack() was called immediately after note was triggered                  // if change_attack() was called immediately after note was triggered
891                  // then immediately apply attack to Note object                  // then immediately apply attack to Note object
892                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
893                      pNote->Override.Attack = fAttack;                      pNote->Override.Attack = fAttack;
894                  } else { // otherwise schedule attack change ...                  } else { // otherwise schedule attack change ...
895                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 901  namespace LinuxSampler { Line 924  namespace LinuxSampler {
924    
925      VMFnResult* InstrumentScriptVMFunction_change_decay::exec(VMFnArgs* args) {      VMFnResult* InstrumentScriptVMFunction_change_decay::exec(VMFnArgs* args) {
926          int decay = args->arg(1)->asInt()->evalInt();          int decay = args->arg(1)->asInt()->evalInt();
927          if (decay > VM_EG_PAR_MAX_VALUE) {          // note: intentionally not checking against a max. value here!
928              wrnMsg("change_decay(): argument 2 may not be larger than 1000000");          // (to allow i.e. passing 2000000 for doubling the decay time)
929              decay = VM_EG_PAR_MAX_VALUE;          if (decay < 0) {
         } else if (decay < 0) {  
930              wrnMsg("change_decay(): argument 2 may not be negative");              wrnMsg("change_decay(): argument 2 may not be negative");
931              decay = 0;              decay = 0;
932          }          }
# Line 929  namespace LinuxSampler { Line 951  namespace LinuxSampler {
951    
952              // if change_decay() was called immediately after note was triggered              // if change_decay() was called immediately after note was triggered
953              // then immediately apply decay to Note object              // then immediately apply decay to Note object
954              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
955                  pNote->Override.Decay = fDecay;                  pNote->Override.Decay = fDecay;
956              } else { // otherwise schedule decay change ...              } else { // otherwise schedule decay change ...
957                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 953  namespace LinuxSampler { Line 975  namespace LinuxSampler {
975    
976                  // if change_decay() was called immediately after note was triggered                  // if change_decay() was called immediately after note was triggered
977                  // then immediately apply decay to Note object                  // then immediately apply decay to Note object
978                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
979                      pNote->Override.Decay = fDecay;                      pNote->Override.Decay = fDecay;
980                  } else { // otherwise schedule decay change ...                  } else { // otherwise schedule decay change ...
981                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 988  namespace LinuxSampler { Line 1010  namespace LinuxSampler {
1010    
1011      VMFnResult* InstrumentScriptVMFunction_change_release::exec(VMFnArgs* args) {      VMFnResult* InstrumentScriptVMFunction_change_release::exec(VMFnArgs* args) {
1012          int release = args->arg(1)->asInt()->evalInt();          int release = args->arg(1)->asInt()->evalInt();
1013          if (release > VM_EG_PAR_MAX_VALUE) {          // note: intentionally not checking against a max. value here!
1014              wrnMsg("change_release(): argument 2 may not be larger than 1000000");          // (to allow i.e. passing 2000000 for doubling the release time)
1015              release = VM_EG_PAR_MAX_VALUE;          if (release < 0) {
         } else if (release < 0) {  
1016              wrnMsg("change_release(): argument 2 may not be negative");              wrnMsg("change_release(): argument 2 may not be negative");
1017              release = 0;              release = 0;
1018          }          }
# Line 1016  namespace LinuxSampler { Line 1037  namespace LinuxSampler {
1037    
1038              // if change_release() was called immediately after note was triggered              // if change_release() was called immediately after note was triggered
1039              // then immediately apply relase to Note object              // then immediately apply relase to Note object
1040              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1041                  pNote->Override.Release = fRelease;                  pNote->Override.Release = fRelease;
1042              } else { // otherwise schedule release change ...              } else { // otherwise schedule release change ...
1043                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 1040  namespace LinuxSampler { Line 1061  namespace LinuxSampler {
1061    
1062                  // if change_release() was called immediately after note was triggered                  // if change_release() was called immediately after note was triggered
1063                  // then immediately apply relase to Note object                  // then immediately apply relase to Note object
1064                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1065                      pNote->Override.Release = fRelease;                      pNote->Override.Release = fRelease;
1066                  } else { // otherwise schedule release change ...                  } else { // otherwise schedule release change ...
1067                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 1059  namespace LinuxSampler { Line 1080  namespace LinuxSampler {
1080          return successResult();          return successResult();
1081      }      }
1082    
1083        // template for change_*() functions
1084    
1085      bool VMChangeSynthParamFunction::acceptsArgType(int iArg, ExprType_t type) const {      bool VMChangeSynthParamFunction::acceptsArgType(int iArg, ExprType_t type) const {
1086          if (iArg == 0)          if (iArg == 0)
1087              return type == INT_EXPR || type == INT_ARR_EXPR;              return type == INT_EXPR || type == INT_ARR_EXPR;
# Line 1066  namespace LinuxSampler { Line 1089  namespace LinuxSampler {
1089              return type == INT_EXPR;              return type == INT_EXPR;
1090      }      }
1091    
1092        // Arbitrarily chosen constant value symbolizing "no limit".
1093        #define NO_LIMIT 1315916909
1094    
1095      template<float NoteBase::_Override::*T_noteParam, int T_synthParam,      template<float NoteBase::_Override::*T_noteParam, int T_synthParam,
1096               bool T_isNormalizedParam, int T_maxValue, int T_minValue>               bool T_isNormalizedParam, int T_maxValue, int T_minValue>
1097      VMFnResult* VMChangeSynthParamFunction::execTemplate(VMFnArgs* args, const char* functionName) {      VMFnResult* VMChangeSynthParamFunction::execTemplate(VMFnArgs* args, const char* functionName) {
1098          int value = args->arg(1)->asInt()->evalInt();          int value = args->arg(1)->asInt()->evalInt();
1099          if (value > T_maxValue) {          if (T_maxValue != NO_LIMIT && value > T_maxValue) {
1100              wrnMsg(String(functionName) + "(): argument 2 may not be larger than " + ToString(T_maxValue));              wrnMsg(String(functionName) + "(): argument 2 may not be larger than " + ToString(T_maxValue));
1101              value = T_maxValue;              value = T_maxValue;
1102          } else if (value < T_minValue) {          } else if (T_minValue != NO_LIMIT && value < T_minValue) {
1103              if (T_minValue == 0)              if (T_minValue == 0)
1104                  wrnMsg(String(functionName) + "(): argument 2 may not be negative");                  wrnMsg(String(functionName) + "(): argument 2 may not be negative");
1105              else              else
# Line 1104  namespace LinuxSampler { Line 1130  namespace LinuxSampler {
1130              // if this change_*() script function was called immediately after              // if this change_*() script function was called immediately after
1131              // note was triggered then immediately apply the synth parameter              // note was triggered then immediately apply the synth parameter
1132              // change to Note object              // change to Note object
1133              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1134                  pNote->Override.*T_noteParam = fValue;                  pNote->Override.*T_noteParam = fValue;
1135              } else { // otherwise schedule this synth parameter change ...              } else { // otherwise schedule this synth parameter change ...
1136                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 1129  namespace LinuxSampler { Line 1155  namespace LinuxSampler {
1155                  // if this change_*() script function was called immediately after                  // if this change_*() script function was called immediately after
1156                  // note was triggered then immediately apply the synth parameter                  // note was triggered then immediately apply the synth parameter
1157                  // change to Note object                  // change to Note object
1158                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1159                      pNote->Override.*T_noteParam = fValue;                      pNote->Override.*T_noteParam = fValue;
1160                  } else { // otherwise schedule this synth parameter change ...                  } else { // otherwise schedule this synth parameter change ...
1161                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 1148  namespace LinuxSampler { Line 1174  namespace LinuxSampler {
1174          return successResult();          return successResult();
1175      }      }
1176    
1177        // change_sustain() function
1178    
1179        VMFnResult* InstrumentScriptVMFunction_change_sustain::exec(VMFnArgs* args) {
1180            return VMChangeSynthParamFunction::execTemplate<
1181            &NoteBase::_Override::Sustain,
1182            Event::synth_param_sustain,
1183            true, NO_LIMIT, 0>( args, "change_sustain" );
1184        }
1185    
1186      // change_amp_lfo_depth() function      // change_amp_lfo_depth() function
1187    
1188      VMFnResult* InstrumentScriptVMFunction_change_amp_lfo_depth::exec(VMFnArgs* args) {      VMFnResult* InstrumentScriptVMFunction_change_amp_lfo_depth::exec(VMFnArgs* args) {
1189          return VMChangeSynthParamFunction::execTemplate<          return VMChangeSynthParamFunction::execTemplate<
1190                      &NoteBase::_Override::AmpLFODepth,                      &NoteBase::_Override::AmpLFODepth,
1191                      Event::synth_param_amp_lfo_depth, true>( args, "change_amp_lfo_depth" );                      Event::synth_param_amp_lfo_depth,
1192                        true, 1000000, 0>( args, "change_amp_lfo_depth" );
1193      }      }
1194    
1195      // change_amp_lfo_freq() function      // change_amp_lfo_freq() function
# Line 1161  namespace LinuxSampler { Line 1197  namespace LinuxSampler {
1197      VMFnResult* InstrumentScriptVMFunction_change_amp_lfo_freq::exec(VMFnArgs* args) {      VMFnResult* InstrumentScriptVMFunction_change_amp_lfo_freq::exec(VMFnArgs* args) {
1198          return VMChangeSynthParamFunction::execTemplate<          return VMChangeSynthParamFunction::execTemplate<
1199                      &NoteBase::_Override::AmpLFOFreq,                      &NoteBase::_Override::AmpLFOFreq,
1200                      Event::synth_param_amp_lfo_freq, true>( args, "change_amp_lfo_freq" );                      Event::synth_param_amp_lfo_freq,
1201                        true, 1000000, 0>( args, "change_amp_lfo_freq" );
1202      }      }
1203    
1204      // change_pitch_lfo_depth() function      // change_pitch_lfo_depth() function
# Line 1169  namespace LinuxSampler { Line 1206  namespace LinuxSampler {
1206      VMFnResult* InstrumentScriptVMFunction_change_pitch_lfo_depth::exec(VMFnArgs* args) {      VMFnResult* InstrumentScriptVMFunction_change_pitch_lfo_depth::exec(VMFnArgs* args) {
1207          return VMChangeSynthParamFunction::execTemplate<          return VMChangeSynthParamFunction::execTemplate<
1208                      &NoteBase::_Override::PitchLFODepth,                      &NoteBase::_Override::PitchLFODepth,
1209                      Event::synth_param_pitch_lfo_depth, true>( args, "change_pitch_lfo_depth" );                      Event::synth_param_pitch_lfo_depth,
1210                        true, 1000000, 0>( args, "change_pitch_lfo_depth" );
1211      }      }
1212    
1213      // change_pitch_lfo_freq() function      // change_pitch_lfo_freq() function
# Line 1177  namespace LinuxSampler { Line 1215  namespace LinuxSampler {
1215      VMFnResult* InstrumentScriptVMFunction_change_pitch_lfo_freq::exec(VMFnArgs* args) {      VMFnResult* InstrumentScriptVMFunction_change_pitch_lfo_freq::exec(VMFnArgs* args) {
1216          return VMChangeSynthParamFunction::execTemplate<          return VMChangeSynthParamFunction::execTemplate<
1217                      &NoteBase::_Override::PitchLFOFreq,                      &NoteBase::_Override::PitchLFOFreq,
1218                      Event::synth_param_pitch_lfo_freq, true>( args, "change_pitch_lfo_freq" );                      Event::synth_param_pitch_lfo_freq,
1219                        true, 1000000, 0>( args, "change_pitch_lfo_freq" );
1220      }      }
1221    
1222      // change_vol_time() function      // change_vol_time() function
# Line 1185  namespace LinuxSampler { Line 1224  namespace LinuxSampler {
1224      VMFnResult* InstrumentScriptVMFunction_change_vol_time::exec(VMFnArgs* args) {      VMFnResult* InstrumentScriptVMFunction_change_vol_time::exec(VMFnArgs* args) {
1225          return VMChangeSynthParamFunction::execTemplate<          return VMChangeSynthParamFunction::execTemplate<
1226                      &NoteBase::_Override::VolumeTime,                      &NoteBase::_Override::VolumeTime,
1227                      Event::synth_param_volume_time, false>( args, "change_vol_time" );                      Event::synth_param_volume_time,
1228                        false, NO_LIMIT, 0>( args, "change_vol_time" );
1229      }      }
1230    
1231      // change_tune_time() function      // change_tune_time() function
# Line 1193  namespace LinuxSampler { Line 1233  namespace LinuxSampler {
1233      VMFnResult* InstrumentScriptVMFunction_change_tune_time::exec(VMFnArgs* args) {      VMFnResult* InstrumentScriptVMFunction_change_tune_time::exec(VMFnArgs* args) {
1234          return VMChangeSynthParamFunction::execTemplate<          return VMChangeSynthParamFunction::execTemplate<
1235                      &NoteBase::_Override::PitchTime,                      &NoteBase::_Override::PitchTime,
1236                      Event::synth_param_pitch_time, false>( args, "change_tune_time" );                      Event::synth_param_pitch_time,
1237                        false, NO_LIMIT, 0>( args, "change_tune_time" );
1238        }
1239    
1240        // change_pan_time() function
1241    
1242        VMFnResult* InstrumentScriptVMFunction_change_pan_time::exec(VMFnArgs* args) {
1243            return VMChangeSynthParamFunction::execTemplate<
1244            &NoteBase::_Override::PanTime,
1245            Event::synth_param_pan_time,
1246            false, NO_LIMIT, 0>( args, "change_pan_time" );
1247        }
1248    
1249        // template for change_*_curve() functions
1250    
1251        bool VMChangeFadeCurveFunction::acceptsArgType(int iArg, ExprType_t type) const {
1252            if (iArg == 0)
1253                return type == INT_EXPR || type == INT_ARR_EXPR;
1254            else
1255                return type == INT_EXPR;
1256        }
1257    
1258        template<fade_curve_t NoteBase::_Override::*T_noteParam, int T_synthParam>
1259        VMFnResult* VMChangeFadeCurveFunction::execTemplate(VMFnArgs* args, const char* functionName) {
1260            int value = args->arg(1)->asInt()->evalInt();
1261            switch (value) {
1262                case FADE_CURVE_LINEAR:
1263                case FADE_CURVE_EASE_IN_EASE_OUT:
1264                    break;
1265                default:
1266                    wrnMsg(String(functionName) + "(): invalid curve type passed as argument 2");
1267                    return successResult();
1268            }
1269    
1270            AbstractEngineChannel* pEngineChannel =
1271                static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
1272    
1273            if (args->arg(0)->exprType() == INT_EXPR) {
1274                const ScriptID id = args->arg(0)->asInt()->evalInt();
1275                if (!id) {
1276                    wrnMsg(String(functionName) + "(): note ID for argument 1 may not be zero");
1277                    return successResult();
1278                }
1279                if (!id.isNoteID()) {
1280                    wrnMsg(String(functionName) + "(): argument 1 is not a note ID");
1281                    return successResult();
1282                }
1283    
1284                NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() );
1285                if (!pNote) return successResult();
1286    
1287                // if this change_*_curve() script function was called immediately after
1288                // note was triggered then immediately apply the synth parameter
1289                // change to Note object
1290                if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1291                    pNote->Override.*T_noteParam = (fade_curve_t) value;
1292                } else { // otherwise schedule this synth parameter change ...
1293                    Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
1294                    e.Init(); // clear IDs
1295                    e.Type = Event::type_note_synth_param;
1296                    e.Param.NoteSynthParam.NoteID   = id.noteID();
1297                    e.Param.NoteSynthParam.Type     = (Event::synth_param_t) T_synthParam;
1298                    e.Param.NoteSynthParam.Delta    = value;
1299                    e.Param.NoteSynthParam.Relative = false;
1300    
1301                    pEngineChannel->ScheduleEventMicroSec(&e, 0);
1302                }
1303            } else if (args->arg(0)->exprType() == INT_ARR_EXPR) {
1304                VMIntArrayExpr* ids = args->arg(0)->asIntArray();
1305                for (int i = 0; i < ids->arraySize(); ++i) {
1306                    const ScriptID id = ids->evalIntElement(i);
1307                    if (!id || !id.isNoteID()) continue;
1308    
1309                    NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() );
1310                    if (!pNote) continue;
1311    
1312                    // if this change_*_curve() script function was called immediately after
1313                    // note was triggered then immediately apply the synth parameter
1314                    // change to Note object
1315                    if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1316                        pNote->Override.*T_noteParam = (fade_curve_t) value;
1317                    } else { // otherwise schedule this synth parameter change ...
1318                        Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
1319                        e.Init(); // clear IDs
1320                        e.Type = Event::type_note_synth_param;
1321                        e.Param.NoteSynthParam.NoteID   = id.noteID();
1322                        e.Param.NoteSynthParam.Type     = (Event::synth_param_t) T_synthParam;
1323                        e.Param.NoteSynthParam.Delta    = value;
1324                        e.Param.NoteSynthParam.Relative = false;
1325    
1326                        pEngineChannel->ScheduleEventMicroSec(&e, 0);
1327                    }
1328                }
1329            }
1330    
1331            return successResult();
1332        }
1333    
1334        // change_vol_curve() function
1335    
1336        VMFnResult* InstrumentScriptVMFunction_change_vol_curve::exec(VMFnArgs* args) {
1337            return VMChangeFadeCurveFunction::execTemplate<
1338                        &NoteBase::_Override::VolumeCurve,
1339                        Event::synth_param_volume_curve>( args, "change_vol_curve" );
1340        }
1341    
1342        // change_tune_curve() function
1343    
1344        VMFnResult* InstrumentScriptVMFunction_change_tune_curve::exec(VMFnArgs* args) {
1345            return VMChangeFadeCurveFunction::execTemplate<
1346                        &NoteBase::_Override::PitchCurve,
1347                        Event::synth_param_pitch_curve>( args, "change_tune_curve" );
1348        }
1349    
1350        // change_pan_curve() function
1351    
1352        VMFnResult* InstrumentScriptVMFunction_change_pan_curve::exec(VMFnArgs* args) {
1353            return VMChangeFadeCurveFunction::execTemplate<
1354            &NoteBase::_Override::PanCurve,
1355            Event::synth_param_pan_curve>( args, "change_pan_curve" );
1356      }      }
1357    
1358      // fade_in() function      // fade_in() function
# Line 1238  namespace LinuxSampler { Line 1397  namespace LinuxSampler {
1397              // if fade_in() was called immediately after note was triggered              // if fade_in() was called immediately after note was triggered
1398              // then immediately apply a start volume of zero to Note object,              // then immediately apply a start volume of zero to Note object,
1399              // as well as the fade in duration              // as well as the fade in duration
1400              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1401                  pNote->Override.Volume = 0.f;                  pNote->Override.Volume = 0.f;
1402                  pNote->Override.VolumeTime = fDuration;                  pNote->Override.VolumeTime = fDuration;
1403              } else { // otherwise schedule a "volume time" change with the requested fade in duration ...              } else { // otherwise schedule a "volume time" change with the requested fade in duration ...
# Line 1280  namespace LinuxSampler { Line 1439  namespace LinuxSampler {
1439                  // if fade_in() was called immediately after note was triggered                  // if fade_in() was called immediately after note was triggered
1440                  // then immediately apply a start volume of zero to Note object,                  // then immediately apply a start volume of zero to Note object,
1441                  // as well as the fade in duration                  // as well as the fade in duration
1442                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1443                      pNote->Override.Volume = 0.f;                      pNote->Override.Volume = 0.f;
1444                      pNote->Override.VolumeTime = fDuration;                      pNote->Override.VolumeTime = fDuration;
1445                  } else { // otherwise schedule a "volume time" change with the requested fade in duration ...                  } else { // otherwise schedule a "volume time" change with the requested fade in duration ...
# Line 1359  namespace LinuxSampler { Line 1518  namespace LinuxSampler {
1518    
1519              // if fade_out() was called immediately after note was triggered              // if fade_out() was called immediately after note was triggered
1520              // then immediately apply fade out duration to Note object              // then immediately apply fade out duration to Note object
1521              if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {              if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1522                  pNote->Override.VolumeTime = fDuration;                  pNote->Override.VolumeTime = fDuration;
1523              } else { // otherwise schedule a "volume time" change with the requested fade out duration ...              } else { // otherwise schedule a "volume time" change with the requested fade out duration ...
1524                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                  Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 1411  namespace LinuxSampler { Line 1570  namespace LinuxSampler {
1570    
1571                  // if fade_out() was called immediately after note was triggered                  // if fade_out() was called immediately after note was triggered
1572                  // then immediately apply fade out duration to Note object                  // then immediately apply fade out duration to Note object
1573                  if (m_vm->m_event->cause.SchedTime() == pNote->triggerSchedTime) {                  if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1574                      pNote->Override.VolumeTime = fDuration;                      pNote->Override.VolumeTime = fDuration;
1575                  } else { // otherwise schedule a "volume time" change with the requested fade out duration ...                  } else { // otherwise schedule a "volume time" change with the requested fade out duration ...
1576                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"                      Event e = m_vm->m_event->cause; // copy to get fragment time for "now"
# Line 1458  namespace LinuxSampler { Line 1617  namespace LinuxSampler {
1617          return successResult();          return successResult();
1618      }      }
1619    
1620        // get_event_par() function
1621    
1622        InstrumentScriptVMFunction_get_event_par::InstrumentScriptVMFunction_get_event_par(InstrumentScriptVM* parent)
1623            : m_vm(parent)
1624        {
1625        }
1626    
1627        VMFnResult* InstrumentScriptVMFunction_get_event_par::exec(VMFnArgs* args) {
1628            AbstractEngineChannel* pEngineChannel =
1629                static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
1630    
1631            const ScriptID id = args->arg(0)->asInt()->evalInt();
1632            if (!id) {
1633                wrnMsg("get_event_par(): note ID for argument 1 may not be zero");
1634                return successResult(0);
1635            }
1636            if (!id.isNoteID()) {
1637                wrnMsg("get_event_par(): argument 1 is not a note ID");
1638                return successResult(0);
1639            }
1640    
1641            NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() );
1642            if (!pNote) {
1643                wrnMsg("get_event_par(): no note alive with that note ID of argument 1");
1644                return successResult(0);
1645            }
1646    
1647            const int parameter = args->arg(1)->asInt()->evalInt();
1648            switch (parameter) {
1649                case EVENT_PAR_NOTE:
1650                    return successResult(pNote->cause.Param.Note.Key);
1651                case EVENT_PAR_VELOCITY:
1652                    return successResult(pNote->cause.Param.Note.Velocity);
1653                case EVENT_PAR_VOLUME:
1654                    return successResult(
1655                        RTMath::LinRatioToDecibel(pNote->Override.Volume) * 1000.f
1656                    );
1657                case EVENT_PAR_TUNE:
1658                    return successResult(
1659                         RTMath::FreqRatioToCents(pNote->Override.Pitch) * 1000.f
1660                    );
1661                case EVENT_PAR_0:
1662                    return successResult(pNote->userPar[0]);
1663                case EVENT_PAR_1:
1664                    return successResult(pNote->userPar[1]);
1665                case EVENT_PAR_2:
1666                    return successResult(pNote->userPar[2]);
1667                case EVENT_PAR_3:
1668                    return successResult(pNote->userPar[3]);
1669            }
1670    
1671            wrnMsg("get_event_par(): argument 2 is an invalid event parameter");
1672            return successResult(0);
1673        }
1674    
1675        // set_event_par() function
1676    
1677        InstrumentScriptVMFunction_set_event_par::InstrumentScriptVMFunction_set_event_par(InstrumentScriptVM* parent)
1678            : m_vm(parent)
1679        {
1680        }
1681    
1682        VMFnResult* InstrumentScriptVMFunction_set_event_par::exec(VMFnArgs* args) {
1683            AbstractEngineChannel* pEngineChannel =
1684                static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
1685    
1686            const ScriptID id = args->arg(0)->asInt()->evalInt();
1687            if (!id) {
1688                wrnMsg("set_event_par(): note ID for argument 1 may not be zero");
1689                return successResult();
1690            }
1691            if (!id.isNoteID()) {
1692                wrnMsg("set_event_par(): argument 1 is not a note ID");
1693                return successResult();
1694            }
1695    
1696            NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() );
1697            if (!pNote) return successResult();
1698    
1699            const int parameter = args->arg(1)->asInt()->evalInt();
1700            const int value     = args->arg(2)->asInt()->evalInt();
1701    
1702            switch (parameter) {
1703                case EVENT_PAR_NOTE:
1704                    if (value < 0 || value > 127) {
1705                        wrnMsg("set_event_par(): note number of argument 3 is out of range");
1706                        return successResult();
1707                    }
1708                    if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1709                        pNote->cause.Param.Note.Key = value;
1710                        m_vm->m_event->cause.Param.Note.Key = value;
1711                    } else {
1712                        wrnMsg("set_event_par(): note number can only be changed when note is new");
1713                    }
1714                    return successResult();
1715                case EVENT_PAR_VELOCITY:
1716                    if (value < 0 || value > 127) {
1717                        wrnMsg("set_event_par(): velocity of argument 3 is out of range");
1718                        return successResult();
1719                    }
1720                    if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1721                        pNote->cause.Param.Note.Velocity = value;
1722                        m_vm->m_event->cause.Param.Note.Velocity = value;
1723                    } else {
1724                        wrnMsg("set_event_par(): velocity can only be changed when note is new");
1725                    }
1726                    return successResult();
1727                case EVENT_PAR_VOLUME:
1728                    wrnMsg("set_event_par(): changing volume by this function is currently not supported, use change_vol() instead");
1729                    return successResult();
1730                case EVENT_PAR_TUNE:
1731                    wrnMsg("set_event_par(): changing tune by this function is currently not supported, use change_tune() instead");
1732                    return successResult();
1733                case EVENT_PAR_0:
1734                    pNote->userPar[0] = value;
1735                    return successResult();
1736                case EVENT_PAR_1:
1737                    pNote->userPar[1] = value;
1738                    return successResult();
1739                case EVENT_PAR_2:
1740                    pNote->userPar[2] = value;
1741                    return successResult();
1742                case EVENT_PAR_3:
1743                    pNote->userPar[3] = value;
1744                    return successResult();
1745            }
1746    
1747            wrnMsg("set_event_par(): argument 2 is an invalid event parameter");
1748            return successResult();
1749        }
1750    
1751        // change_note() function
1752    
1753        InstrumentScriptVMFunction_change_note::InstrumentScriptVMFunction_change_note(InstrumentScriptVM* parent)
1754        : m_vm(parent)
1755        {
1756        }
1757    
1758        VMFnResult* InstrumentScriptVMFunction_change_note::exec(VMFnArgs* args) {
1759            AbstractEngineChannel* pEngineChannel =
1760                static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
1761    
1762            const ScriptID id = args->arg(0)->asInt()->evalInt();
1763            if (!id) {
1764                wrnMsg("change_note(): note ID for argument 1 may not be zero");
1765                return successResult();
1766            }
1767            if (!id.isNoteID()) {
1768                wrnMsg("change_note(): argument 1 is not a note ID");
1769                return successResult();
1770            }
1771    
1772            NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() );
1773            if (!pNote) return successResult();
1774    
1775            const int value = args->arg(1)->asInt()->evalInt();
1776            if (value < 0 || value > 127) {
1777                wrnMsg("change_note(): note number of argument 2 is out of range");
1778                return successResult();
1779            }
1780    
1781            if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1782                pNote->cause.Param.Note.Key = value;
1783                m_vm->m_event->cause.Param.Note.Key = value;
1784            } else {
1785                wrnMsg("change_note(): note number can only be changed when note is new");
1786            }
1787    
1788            return successResult();
1789        }
1790    
1791        // change_velo() function
1792    
1793        InstrumentScriptVMFunction_change_velo::InstrumentScriptVMFunction_change_velo(InstrumentScriptVM* parent)
1794        : m_vm(parent)
1795        {
1796        }
1797    
1798        VMFnResult* InstrumentScriptVMFunction_change_velo::exec(VMFnArgs* args) {
1799            AbstractEngineChannel* pEngineChannel =
1800                static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
1801    
1802            const ScriptID id = args->arg(0)->asInt()->evalInt();
1803            if (!id) {
1804                wrnMsg("change_velo(): note ID for argument 1 may not be zero");
1805                return successResult();
1806            }
1807            if (!id.isNoteID()) {
1808                wrnMsg("change_velo(): argument 1 is not a note ID");
1809                return successResult();
1810            }
1811    
1812            NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() );
1813            if (!pNote) return successResult();
1814    
1815            const int value = args->arg(1)->asInt()->evalInt();
1816            if (value < 0 || value > 127) {
1817                wrnMsg("change_velo(): velocity of argument 2 is out of range");
1818                return successResult();
1819            }
1820    
1821            if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) {
1822                pNote->cause.Param.Note.Velocity = value;
1823                m_vm->m_event->cause.Param.Note.Velocity = value;
1824            } else {
1825                wrnMsg("change_velo(): velocity can only be changed when note is new");
1826            }
1827    
1828            return successResult();
1829        }
1830    
1831        // change_play_pos() function
1832    
1833        InstrumentScriptVMFunction_change_play_pos::InstrumentScriptVMFunction_change_play_pos(InstrumentScriptVM* parent)
1834        : m_vm(parent)
1835        {
1836        }
1837    
1838        VMFnResult* InstrumentScriptVMFunction_change_play_pos::exec(VMFnArgs* args) {
1839            const ScriptID id = args->arg(0)->asInt()->evalInt();
1840            if (!id) {
1841                wrnMsg("change_play_pos(): note ID for argument 1 may not be zero");
1842                return successResult();
1843            }
1844            if (!id.isNoteID()) {
1845                wrnMsg("change_play_pos(): argument 1 is not a note ID");
1846                return successResult();
1847            }
1848    
1849            const int pos = args->arg(1)->asInt()->evalInt();
1850            if (pos < 0) {
1851                wrnMsg("change_play_pos(): playback position of argument 2 may not be negative");
1852                return successResult();
1853            }
1854    
1855            AbstractEngineChannel* pEngineChannel =
1856                static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
1857    
1858            NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() );
1859            if (!pNote) return successResult();
1860    
1861            pNote->Override.SampleOffset = pos;
1862    
1863            return successResult();
1864        }
1865    
1866      // event_status() function      // event_status() function
1867    
1868      InstrumentScriptVMFunction_event_status::InstrumentScriptVMFunction_event_status(InstrumentScriptVM* parent)      InstrumentScriptVMFunction_event_status::InstrumentScriptVMFunction_event_status(InstrumentScriptVM* parent)
# Line 1483  namespace LinuxSampler { Line 1888  namespace LinuxSampler {
1888          return successResult(pNote ? EVENT_STATUS_NOTE_QUEUE : EVENT_STATUS_INACTIVE);          return successResult(pNote ? EVENT_STATUS_NOTE_QUEUE : EVENT_STATUS_INACTIVE);
1889      }      }
1890    
1891        // callback_status() function
1892    
1893        InstrumentScriptVMFunction_callback_status::InstrumentScriptVMFunction_callback_status(InstrumentScriptVM* parent)
1894            : m_vm(parent)
1895        {
1896        }
1897    
1898        VMFnResult* InstrumentScriptVMFunction_callback_status::exec(VMFnArgs* args) {
1899            const script_callback_id_t id = args->arg(0)->asInt()->evalInt();
1900            if (!id) {
1901                wrnMsg("callback_status(): callback ID for argument 1 may not be zero");
1902                return successResult();
1903            }
1904    
1905            AbstractEngineChannel* pEngineChannel =
1906                static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
1907    
1908            RTList<ScriptEvent>::Iterator itCallback = pEngineChannel->ScriptCallbackByID(id);
1909            if (!itCallback)
1910                return successResult(CALLBACK_STATUS_TERMINATED);
1911    
1912            return successResult(
1913                (m_vm->m_event->execCtx == itCallback->execCtx) ?
1914                    CALLBACK_STATUS_RUNNING : CALLBACK_STATUS_QUEUE
1915            );
1916        }
1917    
1918      // wait() function (overrides core wait() implementation)      // wait() function (overrides core wait() implementation)
1919    
1920      InstrumentScriptVMFunction_wait::InstrumentScriptVMFunction_wait(InstrumentScriptVM* parent)      InstrumentScriptVMFunction_wait::InstrumentScriptVMFunction_wait(InstrumentScriptVM* parent)
# Line 1523  namespace LinuxSampler { Line 1955  namespace LinuxSampler {
1955              (args->argsCount() >= 2) ? (args->arg(1)->asInt()->evalInt() == 1) : false;              (args->argsCount() >= 2) ? (args->arg(1)->asInt()->evalInt() == 1) : false;
1956    
1957          pEngineChannel->ScheduleResumeOfScriptCallback(          pEngineChannel->ScheduleResumeOfScriptCallback(
1958              itCallback, m_vm->m_event->cause.SchedTime(), disableWaitForever              itCallback, m_vm->m_event->scheduleTime, disableWaitForever
1959          );          );
1960    
1961          return successResult();          return successResult();
1962      }      }
1963    
1964        // abort() function
1965    
1966        InstrumentScriptVMFunction_abort::InstrumentScriptVMFunction_abort(InstrumentScriptVM* parent)
1967            : m_vm(parent)
1968        {
1969        }
1970    
1971        VMFnResult* InstrumentScriptVMFunction_abort::exec(VMFnArgs* args) {
1972            const script_callback_id_t id = args->arg(0)->asInt()->evalInt();
1973            if (!id) {
1974                wrnMsg("abort(): callback ID for argument 1 may not be zero");
1975                return successResult();
1976            }
1977    
1978            AbstractEngineChannel* pEngineChannel =
1979                static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
1980    
1981            RTList<ScriptEvent>::Iterator itCallback = pEngineChannel->ScriptCallbackByID(id);
1982            if (!itCallback) return successResult(); // ignore if callback is i.e. not alive anymore
1983    
1984            itCallback->execCtx->signalAbort();
1985    
1986            return successResult();
1987        }
1988    
1989        // fork() function
1990    
1991        InstrumentScriptVMFunction_fork::InstrumentScriptVMFunction_fork(InstrumentScriptVM* parent)
1992            : m_vm(parent)
1993        {
1994        }
1995    
1996        VMFnResult* InstrumentScriptVMFunction_fork::exec(VMFnArgs* args) {
1997            // check if this is actually the parent going to fork, or rather one of
1998            // the children which is already forked
1999            if (m_vm->m_event->forkIndex != 0) { // this is the entry point for a child ...
2000                int forkResult = m_vm->m_event->forkIndex;
2001                // reset so that this child may i.e. also call fork() later on
2002                m_vm->m_event->forkIndex = 0;
2003                return successResult(forkResult);
2004            }
2005    
2006            // if we are here, then this is the parent, so we must fork this parent
2007    
2008            const int n =
2009                (args->argsCount() >= 1) ? args->arg(0)->asInt()->evalInt() : 1;
2010            const bool bAutoAbort =
2011                (args->argsCount() >= 2) ? args->arg(1)->asInt()->evalInt() : true;
2012    
2013            if (m_vm->m_event->countChildHandlers() + n > MAX_FORK_PER_SCRIPT_HANDLER) {
2014                wrnMsg("fork(): requested amount would exceed allowed limit per event handler");
2015                return successResult(-1);
2016            }
2017    
2018            AbstractEngineChannel* pEngineChannel =
2019                static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel);
2020    
2021            if (!pEngineChannel->hasFreeScriptCallbacks(n)) {
2022                wrnMsg("fork(): global limit of event handlers exceeded");
2023                return successResult(-1);
2024            }
2025    
2026            for (int iChild = 0; iChild < n; ++iChild) {
2027                RTList<ScriptEvent>::Iterator itChild =
2028                    pEngineChannel->forkScriptCallback(m_vm->m_event, bAutoAbort);
2029                if (!itChild) { // should never happen, otherwise its a bug ...
2030                    errMsg("fork(): internal error while allocating child");
2031                    return errorResult(-1); // terminate script
2032                }
2033                // since both parent, as well all child script execution instances
2034                // all land in this exect() method, the following is (more or less)
2035                // the only feature that lets us distinguish the parent and
2036                // respective children from each other in this exect() method
2037                itChild->forkIndex = iChild + 1;
2038            }
2039    
2040            return successResult(0);
2041        }
2042    
2043  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.3188  
changed lines
  Added in v.3335

  ViewVC Help
Powered by ViewVC