#ifndef UDO_SEQUENCING_SCHEDULING #define UDO_SEQUENCING_SCHEDULING ## /* Sequenced scheduling This file is part of the SONICS UDO collection by Richard Knight 2022 License: GPL-2.0-or-later http://1bpm.net */ #include "sequencing.udo" /* Helper for sequenced scheduling calls */ opcode _seq_form_pfields, S, i[] ipfields[] xin Sdata = "" index = 0 while (index < lenarray(ipfields)) do Sdata strcat Sdata, sprintf("%f ", ipfields[index]) index += 1 od xout Sdata endop /* schedule a scoreline for the next beat, bar or bargroup */ instr _seq_next imode = p4 Scoreline = p5 if ((imode == 0 && gkseq_beat == 1) || (imode == 1 && gkseq_bar_trig == 1) || (imode == 2 && gkseq_bargroup_trig == 1)) then scoreline Scoreline, 1 turnoff endif endin opcode _seq_next, 0, iSi[] imode, Sinstrument, ipfields[] xin scoreline_i sprintf("i\"_seq_next\" %d 0 3600 \"i \\\"%s\\\" %s\"", imode, Sinstrument, _seq_form_pfields(ipfields)) endop instr _seq_turnoff imode = p4 instrnum = p5 if ((imode == 0 && gkseq_beat == 1) || (imode == 1 && gkseq_bar_trig == 1) || (imode == 2 && gkseq_bargroup_trig == 1)) then turnoff2 instrnum, 4, 0 turnoff endif endin opcode _seq_turnoff, 0, ii imode, instrnum xin event_i "i", "_seq_turnoff", 0, 600, imode, instrnum endop /* Schedule an instrument for the next beat beat_next Sinstrument, ipfields[] Sinstrument instrument name ipfields[] p fields */ opcode beat_next, 0, Si[] Sinstrument, ipfields[] xin _seq_next 0, Sinstrument, ipfields endop /* Schedule an instrument for the next bar bar_next Sinstrument, ipfields[] Sinstrument instrument name ipfields[] p fields */ opcode bar_next, 0, Si[] Sinstrument, ipfields[] xin _seq_next 1, Sinstrument, ipfields endop /* Schedule an instrument for the next bar group bargroup_next Sinstrument, ipfields[] Sinstrument instrument name ipfields[] p fields */ opcode bargroup_next, 0, Si[] Sinstrument, ipfields[] xin _seq_next 2, Sinstrument, ipfields endop /* Turn off an instrument at the next beat beat_next_turnoff instrnum instrnum the instrument number; fractional if required */ opcode beat_next_turnoff, 0, i instrnum xin _seq_turnoff 0, instrnum endop /* Turn off an instrument at the next bar next_bar_turnoff instrnum instrnum the instrument number; fractional if required */ opcode bar_next_turnoff, 0, i instrnum xin _seq_turnoff 1, instrnum endop /* Turn off an instrument at the next bar group next_bargroup_turnoff instrnum instrnum the instrument number; fractional if required */ opcode bargroup_next_turnoff, 0, i instrnum xin _seq_turnoff 2, instrnum endop instr bargroup_lastof Scoreline = p4 p3 = 600 if (gkseq_bargroup == giseq_bargrouplength - 1) then scoreline Scoreline, 1 turnoff endif endin opcode nextbeatxof, k, i ibeats xin kbeatnum init ibeats ktrig init 0 kactive init 1 if (gkseq_beat == 1 && kactive == 1) then if (kbeatnum == 0) then kactive = 0 ktrig = 1 else kbeatnum -= 1 endif endif xout ktrig endop opcode lastbeatxof, k, ii itotalbeats, ibeats xin ktrig = 0 kbeatnum init itotalbeats kactive init 1 if (kactive == 1) then if (gkseq_beat == 1) then if (kbeatnum - 1 == ibeats) then ktrig = 1 kactive = 0 else kbeatnum -= 1 endif endif endif xout ktrig endop opcode bar_lastbeatxof, k, i ibeats xin kactive init 1 ktrig = 0 kbeats init ibeats if (kactive == 1) then kreducing = (kbeats > giseq_barlength) ? 1 : 0 if (kreducing == 1 && gkseq_beat == 1 && gkseq_barbeat == giseq_barlength - 1) then kbeats -= giseq_barlength endif if (kreducing == 0 && gkseq_beat == 1 && (gkseq_barbeat+kbeats) == giseq_barlength) then ktrig = 1 kactive = 0 endif endif xout ktrig endop opcode bargroup_lastbeatxof, k, io ibeats, icontinuous xin kactive init 1 ktrig = 0 ibargroupbeats = giseq_bargrouplength * giseq_barlength kbeats init ibeats if (kactive == 1) then kreducing = (kbeats > ibargroupbeats) ? 1 : 0 if (kreducing == 1 && gkseq_bar_trig == 1 && gkseq_bargroup == giseq_bargrouplength - 1) then kbeats -= ibargroupbeats endif if (kreducing == 0 && gkseq_beat == 1 && (((gkseq_bargroup)*giseq_bargrouplength)+gkseq_barbeat+kbeats) == ibargroupbeats) then ktrig = 1 if (icontinuous == 0) then kactive = 0 else kbeats = ibeats endif endif endif xout ktrig endop opcode bar_lastbeatxof_launch, 0, iSi[] ibeats, Sinstrument, ipfields[] xin scoreline_i sprintf("i\"_bar_lastbeatxof\" 0 600 %d \"i \\\"%s\\\" %s\"", ibeats, Sinstrument, _seq_form_pfields(ipfields)) endop opcode bargroup_lastbeatxof_launch, 0, iSi[] ibeats, Sinstrument, ipfields[] xin scoreline_i sprintf("i\"_bargroup_lastbeatxof\" 0 600 %d \"i \\\"%s\\\" %s\"", ibeats, Sinstrument, _seq_form_pfields(ipfields)) endop ; launch an instrument on the next relevant X beats before a bar start instr _bar_lastbeatxof ibeats = p4 Scoreline = p5 p3 = 600 if (bar_lastbeatxof(ibeats) == 1) then scoreline Scoreline, 1 turnoff endif endin ; launch an instrument on the next relevant X beats before a bar group start instr _bargroup_lastbeatxof ibeats = p4 Scoreline = p5 p3 = 600 if (bargroup_lastbeatxof(ibeats) == 1) then scoreline Scoreline, 1 turnoff endif endin #end