kmidi is a published Kotlin multiplatform library for parsing, building, and analyzing Standard MIDI files (SMF). It's a pragmatic implementation that prioritizes correct SMF handling over file robustness.
- Language: Kotlin 1.9.24 (multiplatform: JVM primary)
- Key Dependency: Apache License 2.0
- Architecture: No external runtime dependencies; pure Kotlin
- Distribution: Maven Central (via Sonatype Nexus Publishing)
The codebase is divided into clear functional areas:
-
Reader (
midi/reader/): Parse MIDI bytes intoStandardMidiFileobjects- Entry point:
StandardMidiFileReader.readByteArray()/readStream() - Features configurable policies for lenient vs strict parsing
- Key classes:
StandardMidiFileHeaderReader,StandardMidiFileTracksReader
- Entry point:
-
Builder (
midi/builder/): Create MIDI files programmatically- DSL-based:
smf { track { ... } } - Extension functions provide time denominators:
Int.quarter,Int.eighth,Int.sixteenth - Entry point:
smf()function inStandardMidiFileBuilder
- DSL-based:
-
Writer (
midi/StandardMidiFileWriter.kt): SerializeStandardMidiFileto bytes- Implements SMF specification 1.0 exactly
- Handles bit packing and running status
-
Analysis (
midi/analysis/): Extract insights from MIDI data- Example:
Polyphonyutility calculates note overlaps - Works on
Arc(NoteOn/NoteOff pairs extracted from raw events)
- Example:
- StandardMidiFile: Header + Tracks (immutable data class)
- Track: List of Events (immutable; properties:
name,notes,arcs) - Event hierarchy:
Event(base, hastick)MidiEvent(channel-based: NoteOn/Off, CC, Program, etc.)MetaEvent(SetTempo, TimeSignature, KeySignature, etc.)VirtualEvent(synthetic: Composite pitch bend, Parameter changes)
- Two time encoding modes: MetricalTime (ticks/quarter note, common) and TimecodeBasedTime (SMPTE frame-based)
- All events tracked in absolute ticks (no delta-time storage in memory)
StandardMidiFile.tpqshortcuts to header's ticksPerQuarterNote
Detekt enforces javadoc on all public APIs (excludes test paths):
/** Brief description here. */
public fun myFunction(): ResultPrivate functions need javadoc too. This is non-negotiable; builds fail otherwise.
Use sealed classes for event types and division types—enables when exhaustiveness:
public sealed class Division {
data class MetricalTime(...) : Division()
data class TimecodeBasedTime(...) : Division()
}When adding new event or structural types, always use sealed not open classes.
Combine Kotlin data classes with init blocks for spec compliance:
public data class Header(...) {
init {
require(chunkType == HEADER_MAGIC)
check(tracks.size == header.trackCount.toInt())
}
}StandardMidiFileReader.Policies defines lenient/strict modes. When adding file-reading logic:
- Lenient (default): Allow MIDI file variations/bugs
- Strict: Enforce SMF spec exactly
- Add policy flags to
Policiesdata class, not magic branches
The SMF spec defines "running status"—a byte optimization where channel messages can omit status bytes. Implementation must:
- Track
runningStatusacross events within a track - Reset on meta-events and sysex (per spec)
- Allow override via
allowRunningStatusAcrossNonMidiEventspolicy
VirtualEvent subclasses represent synthetic data not in the file:
VirtualCompositePitchBendEvent: Merges multiple CC/pitch eventsVirtualParameterNumberChangeEvent: Reconstructs NRPN/RPN from CC messages- Never include these in file output; they're analysis-only
# Full build with tests
./gradlew jvmTest
# Just compile (no tests)
./gradlew compileKotlinJvm
# Code quality checks
./gradlew detekt
# Publish to local Maven (for testing before upload)
./gradlew publishToMavenLocal
# Generate Dokka documentation
./gradlew dokka- commonTest: Kotlin tests running on all platforms (use
kotlin.test.*) - jvmTest: JVM-only tests (can use platform-specific libs)
- Tests are NOT documented (detekt excludes
*Testdirs) - Common patterns:
SmfExamples.example1byte arrays for regression
MIDI test files stored in src/jvmTest/resources/test_midi/:
- Used for round-trip testing (read → verify structure)
- Example:
11-radiohead_1997-lucky.mid
When implementing new features, understand these event base classes:
| Class | Purpose | Examples |
|---|---|---|
NoteEvent |
Note on/off pairs | NoteOn(tick, channel, note, velocity) |
MetaEvent |
Track metadata | SetTempo, TimeSignature, KeySignature |
MidiEvent |
Channel messages | ControlChangeEvent, ProgramEvent |
SysexEvent |
System exclusive | Manufacturer-specific raw bytes |
VirtualEvent |
Computed synthesis | Used only in analysis, never written |
- Extend
MidiEventorMetaEvent(depending on type) - Add to sealed hierarchy in
Event.kt - Update reader in
StandardMidiFileTracksReaderto parse it - Update writer in
StandardMidiFileWriterto serialize it - Add detekt-compliant javadoc
- Add tests covering both read and write round-trips
val reader = StandardMidiFileReader(Policies.lenient)
val smf = reader.readByteArray(myBytes)
smf.tracks[0].notes // Get all NoteOn/Off events
smf.tracks[0].arcs // Get paired start/end notesval midiFile = smf {
format = Format0
division = tpq(480) // 480 ticks per quarter note
track {
add(MetaEvent.SetTempo(0, 500000)) // Tempo in microseconds
add(NoteEvent.NoteOn(0, channel=0, note=60, velocity=100))
add(NoteEvent.NoteOff(480, channel=0, note=60, velocity=100))
}
}
StandardMidiFileWriter().writeByteArray(midiFile)- Use
readByteArray()when you have full file in memory (faster, supports seeking) - Use
readStream()for large files or network streams (sequential, memory-efficient) - Both return identical
StandardMidiFileobjects
- Code format: IntelliJ Kotlin conventions
- Max warnings allowed: None (detekt blocks the build)
- Magic numbers: Ignored for -1, 0, 1, 2, 8 (configured in detekt.yml)
- Suppress suppressions sparingly: Use
@Suppress("MagicNumber")only when unavoidable - Imports: Wildcard imports are allowed (detekt disabled)
When releasing:
- Update version in
build.gradle.kts(semantic versioning) - Ensure
detektpasses - All tests pass on JVM
- Dokka generates without errors
./gradlew publishuploads to Sonatype (requires GPG/OSSRH credentials via env vars)
- Credentials:
OSSRH_USERNAME,OSSRH_PASSWORD,OSSRH_GPG_SECRET_KEY_ID,OSSRH_GPG_SECRET_KEY,OSSRH_GPG_SECRET_KEY_PASSWORD