open-audiovisual
Search GitHub

oav-p5 reference · Drum machine and rhythm

drumHit()#

eventoav-p5
function drumHit(lane, velocity) {}

An event you define: it is called for every hit of the drum machine, with the drum that sounded and how hard.

Define function drumHit(lane, velocity) { … } and oav-p5 calls it for every hit of every drum, the way it calls notePressed() for a key. It is called in the frame in which the hit is heard, right before draw().

The same hit also calls the microphone's functions, so you can pick the grain you want: kickDetected() for the kick, snareDetected() for snare, clap and rim, hatDetected() for the three hats, and beatDetected() (with beat true for a frame) for kick, snare, clap and rim, once per step. Toms, crash, ride and cowbell reach only drumHit(). On one step the order is: cueFired(), barStarted(), stepStarted(), then the hits.

Parameters

NameTypeDescription
laneStringThe drum that sounded: 'kick', 'snare', 'hat' or 'clap' for the grid, and also 'openhat', 'pedalhat', 'rim', 'lowtom', 'tom', 'hightom', 'crash', 'ride' or 'cowbell' for a line of rhythm.
velocityNumberHow hard, from 0.15 to 1. It varies a little from hit to hit, because the machine plays slightly softer or louder on purpose.

Notes

  • While the tab is hidden nobody sees a frame: the clock keeps counting, but no hit is told, and nothing is told twice when the tab comes back. An error thrown inside drumHit() is printed, with its file and line, and the next hit still comes.

Examples

jsOpenProcessing ↗
const COLORS = { kick: '#ff6b6b', snare: '#ffd166', hat: '#6fd3ff', clap: '#c792ff' };
const lit = { kick: 0, snare: 0, hat: 0, clap: 0 };

function setup() {
  createCanvas(400, 300);
  startDrums('breakbeat', 120);
  noStroke();
}

function drumHit(lane, velocity) {
  lit[lane] = velocity;
}

function draw() {
  background(20);
  Object.keys(lit).forEach((lane, i) => {
    fill(COLORS[lane]);
    circle(60 + i * 93, height / 2, 30 + lit[lane] * 70);
    lit[lane] *= 0.85;
  });
}
Four circles, one per drum: each swells with the hit and shrinks back, and a harder hit swells more.
jsOpenProcessing ↗
let last = '';

function setup() {
  createCanvas(400, 300);
  startDrums('bd*4, cr ~ ~ ~, lt(3,8), hh*8', 100);
  textAlign(CENTER, CENTER);
  textSize(32);
}

function drumHit(lane, velocity) {
  last = lane;
}

function draw() {
  background(20);
  fill(255);
  text(last, width / 2, height / 2);
}
The name of the latest drum that sounded: a line of rhythm brings crash and low tom, which only drumHit() reports.

Related

  • 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.
  • kickDetected() You define these; oav-p5 calls them when the microphone (or the drum machine) hears a kick, a snare or a hat.
  • beatDetected() You define this function; oav-p5 calls it each time the microphone (or the drum machine) hears a beat.
  • stepStarted() An event you define: it is called at every one of the 16 steps of the drum machine, with the step and the bar.
  • barStarted() An event you define: it is called at the first step of every bar of the drum machine, with the bar number.
  • OAVp5.drums.on() Listens to the drum machine from anywhere: OAVp5.drums.on('hit', fn) calls fn like drumHit does, and returns a function that stops listening.
  • notePressed() You define this function; oav-p5 calls it each time a key (or a pad) goes down.

p5/oav-p5.js, line 1527

↑ Back to top · All names