Skip to content

Music Demo Example

Interactive audio demo showcasing the PixelRoot32 audio subsystem with instrument presets and pre-composed multi-part music. Features a UI-based interface for testing sound presets and playing layered MusicTrack arrangements.

When PIXELROOT32_ENABLE_UI_SYSTEM is on (default in PlatformDefaults.h), the demo exposes touch UI: UIButton for instrument preset selection and melody playback, UILabel for navigation hints, and UIVerticalLayout for organized button placement — see MusicDemoScene.h.

Requirements (build flags)

  • PIXELROOT32_ENABLE_AUDIO — required for audio subsystem and music playback
  • PIXELROOT32_ENABLE_UI_SYSTEM — required for UI widgets (buttons, labels, layouts)
  • PIXELROOT32_ENABLE_TOUCH=1 — set for native in platformio.ini so touch APIs compile and mouse/touch can drive the demo

See platformio.ini for native and esp32dev.

Platforms

EnvironmentDisplay / input
nativeSDL2, 240×240; touch flag enables the same code paths with simulated touch
esp32devST7789 240×240 (use keyboard/GPIO per your platforms/esp32_dev.h)

Controls / interaction

  • Navigate menus — use your InputManager layout to move between UI buttons (see scene update / button handling)
  • Select / Play — trigger button callbacks to play instrument sounds or melodies
  • Same melody again — pressing the currently playing melody button stops playback (toggle)
  • BackB stops music if playing, then returns to the previous menu level

Melody assets (src/assets/)

Melodies are generated by the PixelRoot32 Tool Suite from their .pr32music sources. Each asset is self-contained: it carries its own LIB_* InstrumentPreset definitions, beat constants (S/E/Q/H/W), and a four-part MusicTrack arrangement:

FileUI buttonRole
blaster_ridge.hBlaster Ridgemusicdemo::blaster_ridge::kTrack — fast action theme
coinleaf_grove.hCoinleaf Grovemusicdemo::coinleaf_grove::kTrack — light adventure theme
lightworld_march.hLightworld Marchmusicdemo::lightworld_march::kTrack — heroic march
moonwell_hymn.hMoonwell Hymnmusicdemo::moonwell_hymn::kTrack — dreamy hymn

Each .h has a companion .h.pr32music sidecar (name, BPM, patterns) used to generate it.

Each arrangement layers main lead + secondVoice (bass) + thirdVoice (harmony) + percussion, mapped by MusicPlayer to ApuCore music slots 0–3 (SFX keeps slots 4–7). The per-track LIB_* presets are tuned so loops stay full after beat-accurate gates.

Melodies (UI labels vs. engine)

UI buttonBPM (see MusicDemoScene::playMelody)Loop length (beats)Layers (summary)
Blaster Ridge16832SAW lead (LIB_SAW_EDGE/BRIGHT/RAZOR), SAW growl bass (LIB_SAW_GROWL_BASS), PULSE harmony hits (LIB_PULSE_HERO_BRASS), noise drums with clap / crash / tom / zap fills
Coinleaf Grove15232PULSE lead (LIB_PULSE_BRIGHT/SPARK/THIN), TRIANGLE thump bass (LIB_TRIANGLE_THUMP), pulse harmony, noise drums with clap and open-hat accents
Lightworld March12832PULSE lead (LIB_PULSE_BRIGHT), TRIANGLE thump bass, pulse hero-brass / warm-pad harmony, noise drums with crash, tom fills and open hats
Moonwell Hymn10432SINE flute lead (LIB_SINE_FLUTE, bell/glass accents), TRIANGLE sub bass (LIB_TRIANGLE_SUB_BASS), sine dream/air-pad harmony, noise drums with ride, crash, soft snare and open hats

Features

  • 10 engine instrument presets (one-shot tests) — Pulse Lead, Pulse Harmony, Triangle Bass, Pulse Bass, Triangle Lead, Triangle Pad, Pulse Pad, Kick, Snare, Hi-hat
  • 4 four-part demo tracks — each layers lead + bass (secondVoice) + harmony (thirdVoice) + percussion, generated from tool-suite .pr32music assets with distinct BPM and texture (see table above)
  • Audio Lab menupulse frequency sweep (Phase A), SINE / SAW chord one-shots (Phase B), and master bitcrush cycling via AudioEngine::setMasterBitcrush
  • UI-based sound testing — play individual instrument sounds on demand
  • Modular audio architecture — demonstrates InstrumentPreset, per-asset LIB_* preset tuning, melody sequencing, AudioEvent sweep fields, and audio scheduling

Build

From examples/music_demo:

bash
pio run -e native
pio run -e esp32dev

Upload (ESP32)

bash
pio run -e esp32dev --target upload

Source code: https://github.com/PixelRoot32-Game-Engine/PixelRoot32-Game-Engine/tree/main/examples/music_demo

Released under the MIT License.