FabGL2 2.0.0
Graphics, audio, GUI and retro-computing library for the ESP32 (ESP-IDF)
Loading...
Searching...
No Matches
DrumMachine Class Reference

A sample based drum machine with a step sequencer. More...

#include <drummachine.h>

Public Member Functions

void begin (SoundGenerator *soundGenerator)
 Attaches the drum machine to the sound generator and starts the sound generator.
WaveformGenerator & channel ()
 Gets the sound generator channel that plays the drum machine.
int chokeGroup (int instrument)
 Gets the choke group of an instrument.
int decay (int instrument)
 Gets the decay of an instrument (0..100).
uint8_t hitCount (int instrument)
 Counts the hits of an instrument (live and sequencer), to show the activity.
int level (int instrument)
 Gets the level of an instrument (0..100).
int loops ()
 Counts the completed repetitions of the playing pattern.
bool metronome ()
 Determines whether the metronome is enabled.
bool mute (int instrument)
 Determines whether an instrument is muted.
int outputLevel ()
 Gets the output level (peak, with a short decay), to show a meter.
DrumPattern * pattern ()
 Gets the playing pattern.
void play ()
 Starts playing the pattern from the first step.
bool playing ()
 Determines whether the sequencer is playing.
bool playingSong ()
 Determines whether the sequencer is playing the song.
void playSong (int position=0)
 Starts playing the song.
DrumPattern * queuedPattern ()
 Gets the queued pattern, nullptr if none.
void queuePattern (DrumPattern *pattern)
 Sets the pattern to play at the end of the current one (immediately when stopped).
int recordHit (int instrument, int velocity=2)
 Records a hit in the playing pattern, quantized to the nearest step, and plays it.
DrumSample const * sample (int instrument)
 Gets the sound of an instrument.
int sampleRate ()
 Gets the sample rate of the sound generator.
void setChokeGroup (int instrument, int group)
 Sets the choke group of an instrument.
void setDecay (int instrument, int value)
 Sets the decay of an instrument.
void setErase (uint32_t mask)
 Erases the hits of instruments while the playhead passes them.
void setLevel (int instrument, int value)
 Sets the level of an instrument.
void setMetronome (bool value, int volume=60)
 Enables the metronome: a click on every quarter note, accented on the first beat.
void setMute (int instrument, bool value)
 Mutes an instrument: the sequencer doesn't play it (trigger() still does).
void setNoiseGate (int level)
 Sets the noise gate of the voices.
void setPattern (DrumPattern *pattern)
 Sets the pattern to play (now).
void setSample (int instrument, DrumSample const *sample)
 Sets the sound of an instrument.
void setSong (DrumPattern *const *patterns, int count, bool loop=true)
 Sets the song: a list of patterns played in sequence by playSong().
void setSwing (int percent)
 Sets the swing of the playing pattern.
void setTempo (int bpm)
 Sets the tempo of the playing pattern.
void setTune (int instrument, int cents)
 Sets the tuning of an instrument.
void setVelocityGains (int soft, int normal, int accent)
 Sets the gain of the velocities.
int songPosition ()
 Gets the song entry that is playing (-1 = not playing the song).
bool sounding (int instrument)
 Determines whether an instrument is sounding.
int step ()
 Gets the step that is playing (-1 = stopped).
void stop ()
 Stops the sequencer (the sounds play until they end).
void trigger (int instrument, int velocity=2)
 Plays an instrument now.
int tune (int instrument)
 Gets the tuning of an instrument in cents.

Static Public Member Functions

static int stepDurationMS (int tempo)
 Calculates the duration of a step.

Static Public Attributes

static constexpr int BLOCK_SIZE = 16
 Samples rendered at once: the timing resolution of the sequencer.
static constexpr int INSTRUMENTS = DrumPattern::INSTRUMENTS
 Number of instruments.
static constexpr int MAX_SONG_STEPS = 64
 Maximum number of entries of a song.
static constexpr int MAX_TEMPO = 300
 Maximum tempo (BPM).
static constexpr int MIN_TEMPO = 30
 Minimum tempo (BPM).
static constexpr int VOICES = 24
 Number of voices (sounds playing at the same time).

Detailed Description

A sample based drum machine with a step sequencer.

DrumMachine plays 16 instruments (each one a DrumSample) with up to 24 voices. Every instrument has a level, a tuning (resampling, up to one octave up or down), a decay (shortens the sound) and a mute. Instruments of the same choke group cut each other (for example the closed hi-hat stops the open hi-hat).

The sequencer runs inside the sound generator: the timing is exact (16 sample resolution) whatever the application does. It plays a DrumPattern (pattern mode) or a list of patterns (song mode), with tempo and swing of the playing pattern. Patterns can be changed while playing: queuePattern() switches at the end of the current pattern, like the classic drum machines. recordHit() records a hit in real time, quantized to the nearest 1/16 step; setErase() removes the hits of an instrument while the playhead passes them.

The parameters can be changed at any time from the application task. The mix has 16 bit resolution, with a soft limiter: enable the oversampled output of the sound generator (SoundGenerator::setOversampling(16)) to keep it on the 8 bit DAC, otherwise the quiet tails of the sounds get a buzzing distortion at their pitch. Without oversampling a noise gate per voice (setNoiseGate()) stops a sound when its tail falls to the last steps of the 8 bit output.

Example:

SoundGenerator soundGenerator(22050, GPIO_NUM_25, SoundGenMethod::DAC);
DrumMachine    drums;
DrumSample     kick(kickData, sizeof(kickData), 22050);
DrumPattern    pattern;

soundGenerator.setOversampling(16);   // before begin(): 16 bit resolution on the 8 bit DAC
drums.begin(&soundGenerator);
drums.setSample(0, &kick);
pattern.setRow(0, "X...x...X...x...");
drums.setPattern(&pattern);
drums.play();

Definition at line 315 of file drummachine.h.


The documentation for this class was generated from the following files: