DEV Community

Cover image for Cobra Music Studio: a music player, a DAW and a graphics engine, all written in KingCobra
Divyanshu Sinha
Divyanshu Sinha

Posted on

Cobra Music Studio: a music player, a DAW and a graphics engine, all written in KingCobra

Cobra Music Studio is three programs in one window: a music player, a music production studio, and a graphics stack built from scratch. It is written entirely in KingCobra, my programming language, in about 7,000 lines with no native extensions.

Repo: https://github.com/DivyanshuSinha136/Cobra-Music

Cobra Music Studio: library and player
The library and player: a sortable library, a waveform seek bar and a mini spectrum.

What it is

  • A music player with a library, playlists, a queue, an equalizer and live visualizers.
  • A production studio with a step sequencer, a piano-roll editor and a multi-track mixer.
  • A graphics stack with vector icons, TrueType text and anti-aliased shapes, built on top of the plain 2D canvas of std.game.

The app doesn't use std.gui. The widget toolkit, the SVG renderer and the font renderer are all KingCobra code. Everything else comes from the standard library: std.game for the canvas and input, std.music_note for synthesis and DSP, std.system for processes and files, and std.json for saved state.

The player

  • Library: built-in generated tracks, notation scores (.kcm) and any .wav in your folders. With ffmpeg installed, it also imports mp3, flac, ogg, m4a and opus. It has search across title, artist, album and genre, sortable columns, favorites, play counts, and recently and most played lists.
  • Playback: click or drag the waveform to seek. Speed runs from 0.5x to 1.5x with pitch preserved, and there's shuffle, repeat, a sleep timer and loudness matching that evens out track levels without pushing peaks into clipping.
  • Never freezes: heavy renders run in a background KingCobra process, so the window stays responsive, and the next queued track is prefetched.
  • No audio device? It plays through the system player (pw-play, paplay, aplay, ffplay, afplay or the Windows SoundPlayer), and with none available it falls back to a simulated clock, so everything stays usable, even on a server.

Equalizer and effects
The equalizer, with the Bass Boost preset and a live response curve.

Equalizer and effects. Ten bands from 32 Hz to 16 kHz, presets, and a live frequency-response curve with the spectrum behind it. There are also a pre-amp, a bass shelf, stereo width, reverb, a compressor and a limiter, and you can export the processed track to WAV.

Visualizers. Six modes (bars, mirror, scope, radial, waterfall and rings) are driven by a real 2048-point FFT of the audio that's playing. There are five palettes, stereo VU meters with peak hold, and beat detection that pulses the logo and cover art.

Radial visualizer in the Neon theme
The radial visualizer with the rainbow palette in the Neon theme.

The production studio

Studio: step sequencer
The step sequencer: drum lanes with accents and ghost notes, next to the mixer.

  • Step sequencer: 8 drum lanes, up to 8 bars, drag-paint, with accents and ghost notes.
  • Piano roll: scale highlighting, note preview, drag to move and resize, mouse wheel for velocity, a chord stamp (triad, 7th, power, sus4) and 808 slide.
  • Mixer: up to 8 tracks, 20 synth instruments or your own WAV sampler, per-track volume, pan, reverb, mute and solo, sidechain ducking from the kick, humanize, swing and mastering presets.
  • Generators: House, Trap, Funk and Lo-fi templates, plus random patterns, arpeggios, chord progressions, basslines and drum fills.
  • Workflow: 40-step undo, a Live mode that re-renders while you edit, project save and open, and crash-recovery autosave.
  • Export: WAV, MIDI, one WAV per track (stems), or add the score to your library as .kcm.

Studio: piano roll
The piano roll, with velocity, chord stamp, and drag to move and resize notes.

The graphics stack, from scratch

std.game draws rectangles, bitmaps and an 8x8 pixel font. Everything beyond that is KingCobra:

  • src/vec.kc parses SVG (paths with M L H V C S Q T A Z, shapes, groups, transforms, fill, stroke, opacity and style) and rasterises it with exact-area anti-aliasing.
  • src/ttf.kc reads TrueType fonts: head, maxp, hhea, hmtx, cmap (formats 4 and 12), loca and glyf, including composite glyphs.
  • src/gfx.kc keeps glyphs, icons, discs and rounded corners in one texture atlas and draws them as tinted sprites, with proportional UTF-8 text in regular, bold and display weights.

The first frame takes about 0.25 s to warm up, and after that frames take 2 to 7 ms. Icons are plain .svg files that the theme tints at draw time, so adding one takes a few lines:

ui.icon("bell", cx, cy, 20, T.accent);   // any theme colour
Enter fullscreen mode Exit fullscreen mode

Score view
The score view: a piano roll of the playing arrangement.

Five themes

Midnight is the default. The others are Neon, Sunset, Forest and Paper.

Neon theme
Neon

Paper theme
Paper

A design lesson from building it

KingCobra creates a separate module instance for every import, so module-level variables aren't shared between files. The app handles that by keeping all application state in one object that is passed around explicitly. The UI is an immediate-mode widget toolkit, and a single guarded frame loop runs everything.

That guard matters. Every frame is wrapped, so a bug in one screen is written to error.log and the music keeps playing. Repeated faults fall back to the library screen.

Tested by driving the real app

The tests run the real application headlessly: they inject mouse and keyboard events, render frames and assert on the result. There are four suites with 123 checks in all:

Suite Checks Covers
tests/ui_test.kc 38 search, sorting, menus, playback, seeking, volume, EQ, playlists, persistence
tests/studio_test.kc 18 sequencer and piano-roll clicks, undo, render, save and open, exports
tests/mature_test.kc 38 velocity, sidechain, sampler, stems, drag editing, crash guard, cache limit
tests/gfx_test.kc 29 SVG rasteriser accuracy, TrueType parsing, text measurement, atlas

Even the README banner, badges, architecture diagram and screenshots are produced by KingCobra programs (tools/readme_assets.kc and tools/screenshots.kc).

Try it

You need KingCobra 1.0 or newer. An audio player and ffmpeg are optional.

kingcobra run main.kc                       # open the window (1280x720)
kingcobra run main.kc --size 1600x900       # larger window
KC_MUSIC_SILENT=1 kingcobra run main.kc     # no audio device: simulated playback
kingcobra run main.kc --headless            # no display: render frames headlessly
Enter fullscreen mode Exit fullscreen mode

Add your music in Settings, or drop files into the music/ folder. The Studio is on key 8, and 1 to 8 switch screens. Space plays and pauses, Left and Right seek, and / searches.

Honest limits

  • Changing volume, EQ or speed re-renders the rest of the track, so there's a short gap (about 0.1 to 0.4 s) when you release a control.
  • The window size is fixed at start-up (--size WxH), because std.game has no live-resize call.
  • Fonts have no kerning or hinting, only grayscale anti-aliasing.

Get it

MIT licensed code, with fonts under the SIL Open Font License. If you try it, tell me what breaks, especially audio setups I haven't seen. A star helps other people find it.

Top comments (0)