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) { |
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 |
|
|
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) { |
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(); |
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->scheduleTime == 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 |
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->scheduleTime == 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 |
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->scheduleTime == 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 |
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->scheduleTime == 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 |
1083 |
return successResult(); |
return successResult(); |
1084 |
} |
} |
1085 |
|
|
1086 |
|
// template for change_*() functions |
1087 |
|
|
1088 |
bool VMChangeSynthParamFunction::acceptsArgType(int iArg, ExprType_t type) const { |
bool VMChangeSynthParamFunction::acceptsArgType(int iArg, ExprType_t type) const { |
1089 |
if (iArg == 0) |
if (iArg == 0) |
1090 |
return type == INT_EXPR || type == INT_ARR_EXPR; |
return type == INT_EXPR || type == INT_ARR_EXPR; |
1231 |
false, NO_LIMIT, 0>( args, "change_tune_time" ); |
false, NO_LIMIT, 0>( args, "change_tune_time" ); |
1232 |
} |
} |
1233 |
|
|
1234 |
|
// template for change_*_curve() functions |
1235 |
|
|
1236 |
|
bool VMChangeFadeCurveFunction::acceptsArgType(int iArg, ExprType_t type) const { |
1237 |
|
if (iArg == 0) |
1238 |
|
return type == INT_EXPR || type == INT_ARR_EXPR; |
1239 |
|
else |
1240 |
|
return type == INT_EXPR; |
1241 |
|
} |
1242 |
|
|
1243 |
|
template<fade_curve_t NoteBase::_Override::*T_noteParam, int T_synthParam> |
1244 |
|
VMFnResult* VMChangeFadeCurveFunction::execTemplate(VMFnArgs* args, const char* functionName) { |
1245 |
|
int value = args->arg(1)->asInt()->evalInt(); |
1246 |
|
switch (value) { |
1247 |
|
case FADE_CURVE_LINEAR: |
1248 |
|
case FADE_CURVE_EASE_IN_EASE_OUT: |
1249 |
|
break; |
1250 |
|
default: |
1251 |
|
wrnMsg(String(functionName) + "(): invalid curve type passed as argument 2"); |
1252 |
|
return successResult(); |
1253 |
|
} |
1254 |
|
|
1255 |
|
AbstractEngineChannel* pEngineChannel = |
1256 |
|
static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel); |
1257 |
|
|
1258 |
|
if (args->arg(0)->exprType() == INT_EXPR) { |
1259 |
|
const ScriptID id = args->arg(0)->asInt()->evalInt(); |
1260 |
|
if (!id) { |
1261 |
|
wrnMsg(String(functionName) + "(): note ID for argument 1 may not be zero"); |
1262 |
|
return successResult(); |
1263 |
|
} |
1264 |
|
if (!id.isNoteID()) { |
1265 |
|
wrnMsg(String(functionName) + "(): argument 1 is not a note ID"); |
1266 |
|
return successResult(); |
1267 |
|
} |
1268 |
|
|
1269 |
|
NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); |
1270 |
|
if (!pNote) return successResult(); |
1271 |
|
|
1272 |
|
// if this change_*_curve() script function was called immediately after |
1273 |
|
// note was triggered then immediately apply the synth parameter |
1274 |
|
// change to Note object |
1275 |
|
if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { |
1276 |
|
pNote->Override.*T_noteParam = (fade_curve_t) value; |
1277 |
|
} else { // otherwise schedule this synth parameter change ... |
1278 |
|
Event e = m_vm->m_event->cause; // copy to get fragment time for "now" |
1279 |
|
e.Init(); // clear IDs |
1280 |
|
e.Type = Event::type_note_synth_param; |
1281 |
|
e.Param.NoteSynthParam.NoteID = id.noteID(); |
1282 |
|
e.Param.NoteSynthParam.Type = (Event::synth_param_t) T_synthParam; |
1283 |
|
e.Param.NoteSynthParam.Delta = value; |
1284 |
|
e.Param.NoteSynthParam.Relative = false; |
1285 |
|
|
1286 |
|
pEngineChannel->ScheduleEventMicroSec(&e, 0); |
1287 |
|
} |
1288 |
|
} else if (args->arg(0)->exprType() == INT_ARR_EXPR) { |
1289 |
|
VMIntArrayExpr* ids = args->arg(0)->asIntArray(); |
1290 |
|
for (int i = 0; i < ids->arraySize(); ++i) { |
1291 |
|
const ScriptID id = ids->evalIntElement(i); |
1292 |
|
if (!id || !id.isNoteID()) continue; |
1293 |
|
|
1294 |
|
NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); |
1295 |
|
if (!pNote) continue; |
1296 |
|
|
1297 |
|
// if this change_*_curve() script function was called immediately after |
1298 |
|
// note was triggered then immediately apply the synth parameter |
1299 |
|
// change to Note object |
1300 |
|
if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { |
1301 |
|
pNote->Override.*T_noteParam = (fade_curve_t) value; |
1302 |
|
} else { // otherwise schedule this synth parameter change ... |
1303 |
|
Event e = m_vm->m_event->cause; // copy to get fragment time for "now" |
1304 |
|
e.Init(); // clear IDs |
1305 |
|
e.Type = Event::type_note_synth_param; |
1306 |
|
e.Param.NoteSynthParam.NoteID = id.noteID(); |
1307 |
|
e.Param.NoteSynthParam.Type = (Event::synth_param_t) T_synthParam; |
1308 |
|
e.Param.NoteSynthParam.Delta = value; |
1309 |
|
e.Param.NoteSynthParam.Relative = false; |
1310 |
|
|
1311 |
|
pEngineChannel->ScheduleEventMicroSec(&e, 0); |
1312 |
|
} |
1313 |
|
} |
1314 |
|
} |
1315 |
|
|
1316 |
|
return successResult(); |
1317 |
|
} |
1318 |
|
|
1319 |
|
// change_vol_curve() function |
1320 |
|
|
1321 |
|
VMFnResult* InstrumentScriptVMFunction_change_vol_curve::exec(VMFnArgs* args) { |
1322 |
|
return VMChangeFadeCurveFunction::execTemplate< |
1323 |
|
&NoteBase::_Override::VolumeCurve, |
1324 |
|
Event::synth_param_volume_curve>( args, "change_vol_curve" ); |
1325 |
|
} |
1326 |
|
|
1327 |
|
// change_tune_curve() function |
1328 |
|
|
1329 |
|
VMFnResult* InstrumentScriptVMFunction_change_tune_curve::exec(VMFnArgs* args) { |
1330 |
|
return VMChangeFadeCurveFunction::execTemplate< |
1331 |
|
&NoteBase::_Override::PitchCurve, |
1332 |
|
Event::synth_param_pitch_curve>( args, "change_tune_curve" ); |
1333 |
|
} |
1334 |
|
|
1335 |
// fade_in() function |
// fade_in() function |
1336 |
|
|
1337 |
InstrumentScriptVMFunction_fade_in::InstrumentScriptVMFunction_fade_in(InstrumentScriptVM* parent) |
InstrumentScriptVMFunction_fade_in::InstrumentScriptVMFunction_fade_in(InstrumentScriptVM* parent) |
1682 |
wrnMsg("set_event_par(): note number of argument 3 is out of range"); |
wrnMsg("set_event_par(): note number of argument 3 is out of range"); |
1683 |
return successResult(); |
return successResult(); |
1684 |
} |
} |
1685 |
if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) |
if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { |
1686 |
pNote->cause.Param.Note.Key = value; |
pNote->cause.Param.Note.Key = value; |
1687 |
else |
m_vm->m_event->cause.Param.Note.Key = value; |
1688 |
|
} else { |
1689 |
wrnMsg("set_event_par(): note number can only be changed when note is new"); |
wrnMsg("set_event_par(): note number can only be changed when note is new"); |
1690 |
|
} |
1691 |
return successResult(); |
return successResult(); |
1692 |
case EVENT_PAR_VELOCITY: |
case EVENT_PAR_VELOCITY: |
1693 |
if (value < 0 || value > 127) { |
if (value < 0 || value > 127) { |
1694 |
wrnMsg("set_event_par(): velocity of argument 3 is out of range"); |
wrnMsg("set_event_par(): velocity of argument 3 is out of range"); |
1695 |
return successResult(); |
return successResult(); |
1696 |
} |
} |
1697 |
if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) |
if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { |
1698 |
pNote->cause.Param.Note.Velocity = value; |
pNote->cause.Param.Note.Velocity = value; |
1699 |
else |
m_vm->m_event->cause.Param.Note.Velocity = value; |
1700 |
|
} else { |
1701 |
wrnMsg("set_event_par(): velocity can only be changed when note is new"); |
wrnMsg("set_event_par(): velocity can only be changed when note is new"); |
1702 |
|
} |
1703 |
return successResult(); |
return successResult(); |
1704 |
case EVENT_PAR_VOLUME: |
case EVENT_PAR_VOLUME: |
1705 |
wrnMsg("set_event_par(): changing volume by this function is currently not supported, use change_vol() instead"); |
wrnMsg("set_event_par(): changing volume by this function is currently not supported, use change_vol() instead"); |
1725 |
return successResult(); |
return successResult(); |
1726 |
} |
} |
1727 |
|
|
1728 |
|
// change_note() function |
1729 |
|
|
1730 |
|
InstrumentScriptVMFunction_change_note::InstrumentScriptVMFunction_change_note(InstrumentScriptVM* parent) |
1731 |
|
: m_vm(parent) |
1732 |
|
{ |
1733 |
|
} |
1734 |
|
|
1735 |
|
VMFnResult* InstrumentScriptVMFunction_change_note::exec(VMFnArgs* args) { |
1736 |
|
AbstractEngineChannel* pEngineChannel = |
1737 |
|
static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel); |
1738 |
|
|
1739 |
|
const ScriptID id = args->arg(0)->asInt()->evalInt(); |
1740 |
|
if (!id) { |
1741 |
|
wrnMsg("change_note(): note ID for argument 1 may not be zero"); |
1742 |
|
return successResult(); |
1743 |
|
} |
1744 |
|
if (!id.isNoteID()) { |
1745 |
|
wrnMsg("change_note(): argument 1 is not a note ID"); |
1746 |
|
return successResult(); |
1747 |
|
} |
1748 |
|
|
1749 |
|
NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); |
1750 |
|
if (!pNote) return successResult(); |
1751 |
|
|
1752 |
|
const int value = args->arg(1)->asInt()->evalInt(); |
1753 |
|
if (value < 0 || value > 127) { |
1754 |
|
wrnMsg("change_note(): note number of argument 2 is out of range"); |
1755 |
|
return successResult(); |
1756 |
|
} |
1757 |
|
|
1758 |
|
if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { |
1759 |
|
pNote->cause.Param.Note.Key = value; |
1760 |
|
m_vm->m_event->cause.Param.Note.Key = value; |
1761 |
|
} else { |
1762 |
|
wrnMsg("change_note(): note number can only be changed when note is new"); |
1763 |
|
} |
1764 |
|
|
1765 |
|
return successResult(); |
1766 |
|
} |
1767 |
|
|
1768 |
|
// change_velo() function |
1769 |
|
|
1770 |
|
InstrumentScriptVMFunction_change_velo::InstrumentScriptVMFunction_change_velo(InstrumentScriptVM* parent) |
1771 |
|
: m_vm(parent) |
1772 |
|
{ |
1773 |
|
} |
1774 |
|
|
1775 |
|
VMFnResult* InstrumentScriptVMFunction_change_velo::exec(VMFnArgs* args) { |
1776 |
|
AbstractEngineChannel* pEngineChannel = |
1777 |
|
static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel); |
1778 |
|
|
1779 |
|
const ScriptID id = args->arg(0)->asInt()->evalInt(); |
1780 |
|
if (!id) { |
1781 |
|
wrnMsg("change_velo(): note ID for argument 1 may not be zero"); |
1782 |
|
return successResult(); |
1783 |
|
} |
1784 |
|
if (!id.isNoteID()) { |
1785 |
|
wrnMsg("change_velo(): argument 1 is not a note ID"); |
1786 |
|
return successResult(); |
1787 |
|
} |
1788 |
|
|
1789 |
|
NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); |
1790 |
|
if (!pNote) return successResult(); |
1791 |
|
|
1792 |
|
const int value = args->arg(1)->asInt()->evalInt(); |
1793 |
|
if (value < 0 || value > 127) { |
1794 |
|
wrnMsg("change_velo(): velocity of argument 2 is out of range"); |
1795 |
|
return successResult(); |
1796 |
|
} |
1797 |
|
|
1798 |
|
if (m_vm->m_event->scheduleTime == pNote->triggerSchedTime) { |
1799 |
|
pNote->cause.Param.Note.Velocity = value; |
1800 |
|
m_vm->m_event->cause.Param.Note.Velocity = value; |
1801 |
|
} else { |
1802 |
|
wrnMsg("change_velo(): velocity can only be changed when note is new"); |
1803 |
|
} |
1804 |
|
|
1805 |
|
return successResult(); |
1806 |
|
} |
1807 |
|
|
1808 |
|
// change_play_pos() function |
1809 |
|
|
1810 |
|
InstrumentScriptVMFunction_change_play_pos::InstrumentScriptVMFunction_change_play_pos(InstrumentScriptVM* parent) |
1811 |
|
: m_vm(parent) |
1812 |
|
{ |
1813 |
|
} |
1814 |
|
|
1815 |
|
VMFnResult* InstrumentScriptVMFunction_change_play_pos::exec(VMFnArgs* args) { |
1816 |
|
const ScriptID id = args->arg(0)->asInt()->evalInt(); |
1817 |
|
if (!id) { |
1818 |
|
wrnMsg("change_play_pos(): note ID for argument 1 may not be zero"); |
1819 |
|
return successResult(); |
1820 |
|
} |
1821 |
|
if (!id.isNoteID()) { |
1822 |
|
wrnMsg("change_play_pos(): argument 1 is not a note ID"); |
1823 |
|
return successResult(); |
1824 |
|
} |
1825 |
|
|
1826 |
|
const int pos = args->arg(1)->asInt()->evalInt(); |
1827 |
|
if (pos < 0) { |
1828 |
|
wrnMsg("change_play_pos(): playback position of argument 2 may not be negative"); |
1829 |
|
return successResult(); |
1830 |
|
} |
1831 |
|
|
1832 |
|
AbstractEngineChannel* pEngineChannel = |
1833 |
|
static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel); |
1834 |
|
|
1835 |
|
NoteBase* pNote = pEngineChannel->pEngine->NoteByID( id.noteID() ); |
1836 |
|
if (!pNote) return successResult(); |
1837 |
|
|
1838 |
|
pNote->Override.SampleOffset = pos; |
1839 |
|
|
1840 |
|
return successResult(); |
1841 |
|
} |
1842 |
|
|
1843 |
// event_status() function |
// event_status() function |
1844 |
|
|
1845 |
InstrumentScriptVMFunction_event_status::InstrumentScriptVMFunction_event_status(InstrumentScriptVM* parent) |
InstrumentScriptVMFunction_event_status::InstrumentScriptVMFunction_event_status(InstrumentScriptVM* parent) |
1865 |
return successResult(pNote ? EVENT_STATUS_NOTE_QUEUE : EVENT_STATUS_INACTIVE); |
return successResult(pNote ? EVENT_STATUS_NOTE_QUEUE : EVENT_STATUS_INACTIVE); |
1866 |
} |
} |
1867 |
|
|
1868 |
|
// callback_status() function |
1869 |
|
|
1870 |
|
InstrumentScriptVMFunction_callback_status::InstrumentScriptVMFunction_callback_status(InstrumentScriptVM* parent) |
1871 |
|
: m_vm(parent) |
1872 |
|
{ |
1873 |
|
} |
1874 |
|
|
1875 |
|
VMFnResult* InstrumentScriptVMFunction_callback_status::exec(VMFnArgs* args) { |
1876 |
|
const script_callback_id_t id = args->arg(0)->asInt()->evalInt(); |
1877 |
|
if (!id) { |
1878 |
|
wrnMsg("callback_status(): callback ID for argument 1 may not be zero"); |
1879 |
|
return successResult(); |
1880 |
|
} |
1881 |
|
|
1882 |
|
AbstractEngineChannel* pEngineChannel = |
1883 |
|
static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel); |
1884 |
|
|
1885 |
|
RTList<ScriptEvent>::Iterator itCallback = pEngineChannel->ScriptCallbackByID(id); |
1886 |
|
if (!itCallback) |
1887 |
|
return successResult(CALLBACK_STATUS_TERMINATED); |
1888 |
|
|
1889 |
|
return successResult( |
1890 |
|
(m_vm->m_event->execCtx == itCallback->execCtx) ? |
1891 |
|
CALLBACK_STATUS_RUNNING : CALLBACK_STATUS_QUEUE |
1892 |
|
); |
1893 |
|
} |
1894 |
|
|
1895 |
// wait() function (overrides core wait() implementation) |
// wait() function (overrides core wait() implementation) |
1896 |
|
|
1897 |
InstrumentScriptVMFunction_wait::InstrumentScriptVMFunction_wait(InstrumentScriptVM* parent) |
InstrumentScriptVMFunction_wait::InstrumentScriptVMFunction_wait(InstrumentScriptVM* parent) |
1938 |
return successResult(); |
return successResult(); |
1939 |
} |
} |
1940 |
|
|
1941 |
|
// abort() function |
1942 |
|
|
1943 |
|
InstrumentScriptVMFunction_abort::InstrumentScriptVMFunction_abort(InstrumentScriptVM* parent) |
1944 |
|
: m_vm(parent) |
1945 |
|
{ |
1946 |
|
} |
1947 |
|
|
1948 |
|
VMFnResult* InstrumentScriptVMFunction_abort::exec(VMFnArgs* args) { |
1949 |
|
const script_callback_id_t id = args->arg(0)->asInt()->evalInt(); |
1950 |
|
if (!id) { |
1951 |
|
wrnMsg("abort(): callback ID for argument 1 may not be zero"); |
1952 |
|
return successResult(); |
1953 |
|
} |
1954 |
|
|
1955 |
|
AbstractEngineChannel* pEngineChannel = |
1956 |
|
static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel); |
1957 |
|
|
1958 |
|
RTList<ScriptEvent>::Iterator itCallback = pEngineChannel->ScriptCallbackByID(id); |
1959 |
|
if (!itCallback) return successResult(); // ignore if callback is i.e. not alive anymore |
1960 |
|
|
1961 |
|
itCallback->execCtx->signalAbort(); |
1962 |
|
|
1963 |
|
return successResult(); |
1964 |
|
} |
1965 |
|
|
1966 |
|
// fork() function |
1967 |
|
|
1968 |
|
InstrumentScriptVMFunction_fork::InstrumentScriptVMFunction_fork(InstrumentScriptVM* parent) |
1969 |
|
: m_vm(parent) |
1970 |
|
{ |
1971 |
|
} |
1972 |
|
|
1973 |
|
VMFnResult* InstrumentScriptVMFunction_fork::exec(VMFnArgs* args) { |
1974 |
|
// check if this is actually the parent going to fork, or rather one of |
1975 |
|
// the children which is already forked |
1976 |
|
if (m_vm->m_event->forkIndex != 0) { // this is the entry point for a child ... |
1977 |
|
int forkResult = m_vm->m_event->forkIndex; |
1978 |
|
// reset so that this child may i.e. also call fork() later on |
1979 |
|
m_vm->m_event->forkIndex = 0; |
1980 |
|
return successResult(forkResult); |
1981 |
|
} |
1982 |
|
|
1983 |
|
// if we are here, then this is the parent, so we must fork this parent |
1984 |
|
|
1985 |
|
const int n = |
1986 |
|
(args->argsCount() >= 1) ? args->arg(0)->asInt()->evalInt() : 1; |
1987 |
|
const bool bAutoAbort = |
1988 |
|
(args->argsCount() >= 2) ? args->arg(1)->asInt()->evalInt() : true; |
1989 |
|
|
1990 |
|
if (m_vm->m_event->countChildHandlers() + n > MAX_FORK_PER_SCRIPT_HANDLER) { |
1991 |
|
wrnMsg("fork(): requested amount would exceed allowed limit per event handler"); |
1992 |
|
return successResult(-1); |
1993 |
|
} |
1994 |
|
|
1995 |
|
AbstractEngineChannel* pEngineChannel = |
1996 |
|
static_cast<AbstractEngineChannel*>(m_vm->m_event->cause.pEngineChannel); |
1997 |
|
|
1998 |
|
if (!pEngineChannel->hasFreeScriptCallbacks(n)) { |
1999 |
|
wrnMsg("fork(): global limit of event handlers exceeded"); |
2000 |
|
return successResult(-1); |
2001 |
|
} |
2002 |
|
|
2003 |
|
for (int iChild = 0; iChild < n; ++iChild) { |
2004 |
|
RTList<ScriptEvent>::Iterator itChild = |
2005 |
|
pEngineChannel->forkScriptCallback(m_vm->m_event, bAutoAbort); |
2006 |
|
if (!itChild) { // should never happen, otherwise its a bug ... |
2007 |
|
errMsg("fork(): internal error while allocating child"); |
2008 |
|
return errorResult(-1); // terminate script |
2009 |
|
} |
2010 |
|
// since both parent, as well all child script execution instances |
2011 |
|
// all land in this exect() method, the following is (more or less) |
2012 |
|
// the only feature that lets us distinguish the parent and |
2013 |
|
// respective children from each other in this exect() method |
2014 |
|
itChild->forkIndex = iChild + 1; |
2015 |
|
} |
2016 |
|
|
2017 |
|
return successResult(0); |
2018 |
|
} |
2019 |
|
|
2020 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |