# DrumMachine **FabGL Drum**: a sample based drum machine in the style of the classic early eighties drum machines, the LinnDrum and its modern recreation, the Behringer LM Drum. 16 instruments with level faders, tuning and decay, a 32 step sequencer with three velocities and swing, real time recording with quantization, pattern chaining and song mode. It comes with 8 factory kits (a recorded acoustic kit and kits in the style of the TR-808, TR-909, CR-78, LM-1, LinnDrum LM-2 and DMX) and 32 factory patterns; user sounds (WAV files) are loaded from the micro SD card. ## Screenshots | | | |:---:|:---:| | <br>Main screen: faders and pads of the 16 instruments, the step grid of pattern 01 ROCK 1 and the FD-16 kit | <br>Playing: the highlighted column follows the sequencer step | | <br>LOAD WAV: loads a user sound, or a whole kit folder, from the SD card | | ## Screen A black panel with wooden end cheeks, cream legends and red seven segment displays: - **Header**: the KIT selector (under the logo, the arrows or `F4` select the factory kit). The displays show the playing pattern, the tempo (BPM) and the playing step. Below them a status line: song position, queued pattern, REC, ERASE. Knobs: TEMPO (30-300 BPM) and SWING (50-75 %) of the selected pattern, master VOLUME. Transport pads: PLAY (green led while playing), STOP, REC (red led while recording) - **Mixer**: one strip for each instrument: a level fader and a pad. Clicking a pad plays the instrument and selects it (VOICE panel). Pads light up when their instrument plays. Every pad shows its key (`1`...`8`, `Q`...`I`, and `SPC`, `F1`, `F2`, `F3` on the transport and mode pads) - **Step grid**: one row for each instrument, 16 steps (a page: 1-16 or 17-32). Empty cells are gray (every other beat a darker shade), hits are dark red (soft), red (normal) or orange (accent). The playing step is framed in yellow. Steps after the pattern length are black. The row labels show the selected instrument, the activity (orange mark) and the muted instruments (dim label, red mark) - **Pattern**: number and name of the selected pattern (a `*` follows the name when it has been changed), the arrow buttons select the previous / next pattern. Buttons: SAVE, CLEAR (removes all hits), SONG (song editor), INIT (factory reset) - **Length**: number of steps of the pattern (1-32). **Steps**: shows steps 1-16 or 17-32 in the grid - **METRO**: metronome (a click on every beat, accented on the first step). **SONG**: song mode (plays the song instead of the selected pattern) - **Voice**: the selected instrument, its sound (the kit name or the user file name, duration), LOAD WAV and KIT SND (back to the sound of the selected kit), TUNE (-12..+12 semitones) and DECAY (FULL = the whole sound, lower values fade it out faster) Knobs: drag up or down with the mouse button pressed, or use the mouse wheel; double click restores the initial value. Faders: drag the cap or click the slot, mouse wheel; double click restores the initial level. ## Grid editing | Mouse | Action | |---|---| | Click an empty cell | Adds a normal hit. Drag with the button pressed to add more hits | | Click a hit | Removes it. Drag to remove more hits | | Right click or wheel on a hit | Changes its velocity: soft, normal, accent | | Click an instrument name | Selects the instrument and plays it | | Right click an instrument name | Mutes / unmutes the instrument (the sequencer skips it, pads still play it) | ## Keys | Key | Action | |---|---| | `1` ... `8` | Pads 1-8: BASS, SNARE, RIM, CLAPS, CLOSED HAT, OPEN HAT, CRASH, RIDE | | `Q` `W` `E` `R` `T` `Y` `U` `I` | Pads 9-16: HI TOM, MID TOM, LO TOM, HI CONGA, LO CONGA, COWBELL, TAMB, CABASA | | `SHIFT` / `CTRL` + pad key | Accented / soft hit | | `SPACE` | Play / stop | | `F1` | Record on / off | | `F2` | Song mode on / off | | `F3` | Metronome on / off | | `F4` | Next factory kit | | `PGUP` / `PGDN` | Previous / next pattern | | `LEFT` / `RIGHT` | Steps 1-16 / 17-32 | | `+` / `-` | Tempo | | `BACKSPACE` + pad key | While recording: erases the instrument while the playhead passes (keep both keys pressed) | ## Playing and recording PLAY starts the selected pattern from the first step (in song mode: the song from the first entry). Selecting another pattern while playing queues it: it starts at the end of the current pattern (the status line shows `NEXT nn`), so patterns can be chained live, like on the hardware drum machines. Record mode (REC or `F1`): while playing, every pad hit (mouse or keyboard) is written in the pattern at the nearest 1/16 step (quantization) and played immediately; a hit recorded just before a step is not played twice. Turn on the metronome to hear the beats. Hold `BACKSPACE` and a pad key to erase that instrument as the playhead passes. The grid follows the recording. Swing delays the even 1/16 notes (the second and the fourth of every beat): 50 % is straight, 66 % is a triplet feel. Tempo and swing are stored in each pattern. The shuffle patterns are programmed in 1/8 notes at half tempo. The hi-hats are a choke group: the closed hi-hat stops the open hi-hat, like on a real hi-hat. ## Patterns and kit settings 64 pattern slots: the first 32 contain the factory patterns, the others are empty. Changes are kept in memory while the application runs (a `*` marks the changed patterns) and are stored by SAVE: - **SAVE** opens a dialog: choose the destination slot (by default the current one) and type the name (up to 15 characters, stored in upper case). Saving to another slot copies the pattern there, and the original pattern goes back to its stored version. SAVE also stores the kit settings: levels, tuning, decay, mutes, master volume, metronome and the user sounds. - **INIT** restores the factory patterns, the factory song and the factory kit (the stored data is erased). Everything is stored in the NVS flash partition (namespace `drums`) and survives power off. Erasing the flash (`idf.py erase-flash`) also restores the factory data. | # | Patterns | |---|---| | 01-05 | ROCK 1, ROCK 2, ROCK RIDE, HARD ROCK, ROCK FILL | | 06-08 | POP 1, POP 2, BALLAD | | 09-13 | DISCO 1, DISCO 2, FUNK 1, FUNK 2, FUNK 3 | | 14-16 | NEW WAVE, SYNTH POP, ELECTRO | | 17-19 | HIP HOP 1, HIP HOP 2, R&B | | 20-22 | SHUFFLE, BLUES, REGGAE | | 23-27 | BOSSA NOVA, SAMBA, SALSA, CHA CHA, LATIN PERC | | 28-30 | MOTOWN, MARCH, DANCE | | 31-32 | TOM FILL, SNARE ROLL | The factory patterns are defined as text in `main/patterns.h` (one string per instrument, `.` = no hit, `-` = soft, `x` = normal, `X` = accent). ## Song A song is a list of up to 64 patterns. SONG opens the editor: select a pattern with the arrows and INSERT it before the selected entry, REPLACE the selected entry or DELETE it; OK stores the song. In song mode (SONG pad or `F2`) PLAY plays the song once from the beginning; the selected pattern follows the song, so the grid shows the playing pattern. The factory song plays ROCK 1 (3 times), ROCK FILL, ROCK 2 (3 times), TOM FILL, ROCK RIDE (3 times) and SNARE ROLL. ## Sounds Every factory kit has the 16 instruments of the classic kit: bass drum, snare, rimshot, claps, closed and open hi-hat, crash, ride, three toms, two congas, cowbell, tambourine and cabasa. Where a machine didn't have an instrument, the kit has the closest sound of that machine (for example maracas and claves on the TR-808). | # | Kit | Sounds | |---|---|---| | 1 | FD-16 | Synthesized, in the style of the early eighties sampling drum machines (LinnDrum, LM Drum) | | 2 | ACOUSTIC | Recorded acoustic kit: close and overhead microphones mixed, auxiliary percussion | | 3 | TR-808 | Synthesized like the Roland TR-808 analog circuits: long bass drum, snappy snare, six oscillator metallic hi-hats and cymbal, cowbell, claps, maracas (TAMB), claves (CABASA) | | 4 | TR-909 | Synthesized analog drums like the Roland TR-909 (bass drum with a fast pitch sweep, noisy snare), hi-hats and cymbals sampled at 6 bit like on the original | | 5 | CR-78 | Synthesized like the Roland CompuRhythm CR-78: short soft drums, bongos and conga on the toms, cowbell, tambourine, maracas | | 6 | LM-1 | Recorded drums processed in the style of the Linn LM-1: tuned, tight, band limited, deep toms, big claps | | 7 | LM-2 | Recorded drums processed in the style of the LinnDrum LM-2: snare with gated room, full cymbals | | 8 | DMX | Recorded drums processed in the style of the Oberheim DMX: compressed and saturated, gated snare | The kits only recall those machines: no sample of the original machines is used. The selected kit is stored by SAVE; the instruments with a user sound keep it when the kit changes (KIT SND restores the sound of the kit). The kits (`main/kit.h`) are generated by `tools/make_kit.py` and stored at 22050 Hz as 16 bit samples (3 MB of flash: the example has its own partition table, `partitions.csv`, with a 3.9 MB app partition on the 4 MB flash; flashing takes about 80 seconds). The recorded kits use samples of [Virtuosity Drums](https://github.com/sfzinstruments/virtuosity_drums) by Versilian Studios and Karoryfer Samples (CC0 1.0, public domain). To change the kits: `tools/fetch_samples.sh <dir>` downloads the 31 files used (12 MB), `python3 tools/make_kit.py --samples <dir>` writes `main/kit.h` (it needs `sox`; `--wav <dir>` also writes every sound as a WAV file, `--kits fd16,tr808` generates only some kits, `--mulaw` makes 8 bit mu-law kits: half the size, but the quantization noise is audible on the tails). The drum machine mixes with 16 bit resolution and the sound generator runs the DAC at 352.8 kHz (16 times the sample rate) with a noise shaper: the DAC has only 8 bits, and without it the quiet tails of the sounds get a buzzing distortion at their pitch. ### User sounds from the micro SD card LOAD WAV opens a file browser on the SD card (`/sdcard`): - **LOAD SOUND** (or double click a file) loads the selected WAV file into the selected instrument - **LOAD FOLDER AS KIT** loads, from the current folder, every file named like an instrument: | Instrument | File | Instrument | File | |---|---|---|---| | BASS | `kick.wav` | TOM HI | `tomhi.wav` | | SNARE | `snare.wav` | TOM MID | `tommid.wav` | | RIMSHOT | `rimshot.wav` | TOM LO | `tomlo.wav` | | CLAPS | `clap.wav` | CONGA HI | `congahi.wav` | | CL HAT | `closedhat.wav` | CONGA LO | `congalo.wav` | | OP HAT | `openhat.wav` | COWBELL | `cowbell.wav` | | CRASH | `crash.wav` | TAMB | `tambourine.wav` | | RIDE | `ride.wav` | CABASA | `cabasa.wav` | Instruments without a file keep their sound. Any WAV file works: PCM 8, 16, 24 or 32 bit, 32 bit float, mono or stereo (mixed to mono), any sample rate (converted to 22050 Hz). Sounds are normalized and truncated to 4 seconds. They are loaded into PSRAM when the board has it (without PSRAM only short sounds fit in memory). The file names are stored with the kit settings and the sounds are loaded again at startup (when the SD card is present); KIT SND restores the sound of the selected kit. ## What it demonstrates - `fabgl::DrumMachine`: 16 sample based instruments played by 24 voices (tuning by resampling with linear interpolation, decay envelope, choke groups, click-free retrigger, 16 bit mix) and a sequencer running inside the sound generator (exact timing, swing, pattern queue, song, quantized real time recording, erase), in fixed point - 8 factory kits selected at run time (`DrumMachine::setSample()` while playing) - `fabgl::DrumSample` (16 bit or mu-law samples from flash or loaded from WAV files, with sample rate conversion) and `fabgl::DrumPattern` (32 steps, 3 velocities, text definitions) - The drum machine controls of the GUI: `uiFader`, `uiPad` (momentary pads with led and key label), `uiSegmentDisplay` and `uiStepGrid`, together with `uiKnob`, `uiStepper` and `uiTileSelector` - A `uiStyle` that themes all the controls through style class IDs, also used by the message boxes (`uiAppProps::dialogStyleClasses`) - Modal dialogs with `uiFileBrowser`, `uiListBox`, `uiStepper`, `uiTextEdit`; data stored in NVS with `fabgl::Preferences` - The oversampled output of `fabgl::SoundGenerator` (`setOversampling(16)`): 16 bit resolution on the 8 bit DAC - A custom palette on `VGA16Controller` (`setPaletteItem()`) ## Hardware - VGA output (default pins) - Audio output on GPIO25 - PS/2 keyboard on port 0 and PS/2 mouse on port 1 - Micro SD card (optional, user sounds). Default connections (TTGO VGA32 slot): MISO = GPIO16 (2 on PICO-D4 boards), MOSI = GPIO17 (12 on PICO-D4), CLK = GPIO14, CS = GPIO13 - PSRAM (optional, for user sounds longer than a few hundred milliseconds) ## Build See [Building the examples](../../../README.md#building-the-examples) in the main README.
To create a project from this example, run:
idf.py create-project-from-example "fdivitto/fabgl=2.0.0:DrumMachine"