cue()#
functionoav-p5cue(bar, name, [every])
Plans something for a bar: at bar bar the function cueFired(name, bar) is called and cueName becomes name; with every, again every that many bars.
The drum machine counts the bars, so it can run a plan: cue(1, 'calm', 12); cue(5, 'bloom', 12); cue(9, 'storm', 12); in setup() makes three scenes that come round every 12 bars. When a cue is due, cueFired(name, bar) is called and cueName holds the name; read it in draw() if you prefer that to a callback. Several cues on one step fire in the order you declared them, before barStarted(), stepStarted() and the hits of that step.
Calling cue() twice with the same values adds nothing, so it is safe in draw() too. Counting starts with startDrums(), and a cue only fires when the machine passes its place: declare the cues right after startDrums() in setup() so that a cue at bar 1 is not missed. After the machine is stopped and started again, the cues fire again from bar 1.
Parameters
| Name | Type | Description |
|---|---|---|
bar | Number | The bar, counted from 1 (the first bar). A fraction is a place inside a bar: 4.5 is halfway through bar 4 (the nearest of the 16 steps is used). A value below 1, or one that is not a number, prints a warning in the console and adds nothing. |
name | Any | What to call it, usually a string. It is passed to cueFired() and becomes cueName. |
every | Number | Repeat every this many bars after the first time (fractions work, at least one step). 0 or left out: only once. optional default 0 |
Returns
undefined Nothing. OAVp5.drums.cue() is the same function and returns the cue it made.
Notes
- A cue declared after its bar has passed fires at its next repeat if it has an
every, otherwise never until the machine is started again. Without a running machine no cue fires at all.
Examples
let scene = 'calm';
function setup() {
createCanvas(400, 300);
startDrums('four on floor', 140);
cue(1, 'calm', 4); // bars 1, 5, 9 ...
cue(3, 'storm', 4); // bars 3, 7, 11 ...
textAlign(CENTER, CENTER);
textSize(32);
}
function cueFired(name, bar) {
scene = name;
}
function draw() {
background(scene === 'storm' ? color(130, 30, 40) : color(20, 40, 100));
fill(255);
text(scene + ' (bar ' + drumBar + ')', width / 2, height / 2);
}let flash = 0;
function setup() {
createCanvas(400, 300);
startDrums('breakbeat', 130);
cue(2.5, 'flash', 2); // halfway through bar 2, then every 2 bars
textAlign(CENTER, CENTER);
}
function cueFired(name, bar) {
if (name === 'flash') flash = 1;
}
function draw() {
flash *= 0.92;
background(20 + flash * 200);
fill(255 - flash * 255);
text('a flash halfway through every second bar (bar ' + drumBar + ')', width / 2, height / 2);
}function setup() {
createCanvas(400, 300);
startDrums('latin', 120);
textAlign(CENTER, CENTER);
textSize(28);
}
function draw() {
cue(2, 'drop', 4); // the same call every frame adds nothing
background(20);
fill(255);
text(cueName === '' ? 'waiting for the first cue' : 'last cue: ' + cueName, width / 2, height / 2);
}Related
cueFired()An event you define: it is called when the bar of a cue declared withcue()has come, with the cue's name and the bar.cueNameThe name of the last cue that fired, or an empty string before the first one.barStarted()An event you define: it is called at the first step of every bar of the drum machine, with the bar number.drumBarWhich bar the drum machine is in, counted from 1 when it started.startDrums()Starts a drum machine: a 16-step grid on the page that plays a preset, your own grid or one line of rhythm, and counts the bars for your sketch.OAVp5.drums.cue()The same ascue(bar, name, every), but it returns the cue it made (or the one that was already there), andnullwhenbaris not valid.