SonixMetronome

Metronome for practice mode with click track playback.

What is SonixMetronome?

A metronome provides a steady click track to help musicians keep time. SonixMetronome supports:

  • Configurable BPM (beats per minute)

  • Distinct sama (downbeat) and regular beat sounds

  • Talam support with configurable beats per cycle

  • Runtime tempo and volume adjustments

When to Use

ScenarioUse This?Why
Practice mode timingYesCore use case
Sync UI to beatYesUse onBeat callback
Play backing trackNoUse SonixPlayer or SonixMixer
Record with metronomeYesCombine with SonixRecorder

Quick Start

Kotlin

// Create metronome with default settings
val metronome = SonixMetronome.create(
samaSamplePath = "/path/to/sama.wav",
beatSamplePath = "/path/to/beat.wav"
)

// Wait for samples to load
metronome.isInitialized.collect { ready ->
if (ready) metronome.start()
}

// Control playback
metronome.stop()
metronome.setBpm(140f)
metronome.volume = 0.5f

// Release when done
metronome.release()

Swift

// Create metronome with default settings
let metronome = SonixMetronome.create(
samaSamplePath: "/path/to/sama.wav",
beatSamplePath: "/path/to/beat.wav",
bpm: 120,
beatsPerCycle: 4
)

// Observe initialization
for await ready in metronome.isInitialized {
if ready { metronome.start() }
}

// Control playback
metronome.stop()
metronome.setBpm(bpm: 140)
metronome.volume = 0.5

// Release when done
metronome.release()

Builder Pattern (Advanced)

Kotlin

val metronome = SonixMetronome.Builder()
.samaSamplePath("/path/to/sama.wav")
.beatSamplePath("/path/to/beat.wav")
.bpm(120f)
.beatsPerCycle(4)
.volume(0.8f)
.onBeat { beatIndex -> updateUI(beatIndex) }
.onError { error -> showError(error) }
.build()

Swift

let metronome = SonixMetronome.Builder()
.samaSamplePath(path: "/path/to/sama.wav")
.beatSamplePath(path: "/path/to/beat.wav")
.bpm(bpm: 120)
.beatsPerCycle(count: 4)
.volume(volume: 0.8)
.build()

StateFlows

StateFlowTypeDescription
isInitializedBooleanTrue when samples are loaded
isPlayingBooleanTrue while metronome is running
currentBeatIntCurrent beat index (0-based)
bpmFloatCurrent tempo
errorSonixError?Error state, null if no error

Platform Notes

iOS

  • Uses AVAudioEngine for low-latency playback

  • Audio samples must be WAV format

Android

  • Uses OpenSL ES or AAudio for low-latency playback

  • Audio samples must be WAV format

Common Pitfalls

  1. Starting before initialized: Wait for isInitialized to be true

  2. Forgetting to release: Call release() when done to free resources

  3. Invalid sample paths: Ensure audio files exist at specified paths

  4. BPM range: Valid range is 30-300 BPM

See also

For audio file playback

For multi-track mixing

Types

Link copied to clipboard
class Builder

Builder for advanced SonixMetronome configuration.

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Number of beats per cycle (set at creation time).

Link copied to clipboard
val bpm: StateFlow<Float>

Current BPM as observable state.

Link copied to clipboard
val currentBeat: StateFlow<Int>

Current beat index (0-based, wraps at beatsPerCycle).

Link copied to clipboard
val error: StateFlow<SonixError?>

Error state, null if no error.

Link copied to clipboard
val isInitialized: StateFlow<Boolean>

Whether metronome samples are loaded and ready to start.

Link copied to clipboard
val isPlaying: StateFlow<Boolean>

Whether metronome is currently running.

Link copied to clipboard

Current volume setting.

Functions

Link copied to clipboard
fun release()

Release all resources.

Link copied to clipboard
fun setBpm(bpm: Float)

Set tempo in beats per minute.

Link copied to clipboard
fun start()

Start the metronome.

Link copied to clipboard
fun stop()

Stop the metronome.