Skip to content
ByapakSigdelPublic

About

Portable DJ-turntable scratch / scrub engine in raw C, with a Python testbench.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

Repository files navigation

Scratch

A small, portable DJ-turntable scratch / scrub engine in raw C. Turn a jog wheel (or drag the on-screen platter) and the audio scrubs forward and backward with the sound of a vinyl record — pitched-up "zip" forward, reverse playback backward, frozen when you hold still — then glides back to normal play when you let go.

It's designed for embedded jog-wheel music players — a microcontroller with a magnetic rotary encoder on the wheel — but the engine is hardware- and codec-agnostic: it just reads PCM samples at a variable, bidirectional rate.

Scratch testbench demo

Dragging the platter scrubs the track with the vinyl scratch sound — forward zip, reverse, freeze, then a motor glide back to play. ▶ Watch with sound (demo.mp4) · drive it yourself with python python/platter_demo.py.

 rate = +1.0   normal forward          rate =  0.0   held still (stopped record)
 rate = +3.0   fast forward  ZZIP↑      rate = -2.0   reverse       ~PIWWO

How it works

The engine is a variable-rate, bidirectional interpolating resampler. A fractional read-head walks the PCM; each output sample reads source[playhead] (Hermite-interpolated) and advances playhead += rate. The platter's angular velocity sets rate, so pitch and direction fall out for free — exactly like a needle in a groove. A tiny state machine adds turntable-style "motor" glides (brake on grab, spin-up on release). Full design notes: docs/superpowers/specs/2026-06-28-jog-scratch-design.md.

Layout

core/    scratch_core.c/.h   the engine — pure C, zero deps, no hot-path malloc
                             (this is the file that ships to firmware unchanged)
capi/    scratch_capi.c      flat C ABI + buffer-backed source -> libscratch.{dll,so}
python/  scratch.py          ctypes binding
         platter_demo.py     interactive testbench (turntable UI + sounddevice)
         ui.py               the testbench's rendering (shared with the recorder)
         render_gesture.py   headless: render a scripted scratch to a .wav
         record_demo.py      headless: render the README demo gif + mp4
         audio_source.py      decode a song (miniaudio) or synthesize a test loop
         selftest.py         headless check of the whole Python -> C -> DSP path
tests/   test_scratch_core.c C unit tests (TDD)
build.bat / Makefile         build + run tests, then build the shared library

Build

Windows: build.bat (uses the bundled tools/w64devkit gcc if present, else gcc from PATH). Linux/macOS: make. Both run the C tests, then build build/libscratch.*.

Run the testbench

python -m venv .venv
.venv\Scripts\python -m pip install -r python/requirements.txt   # numpy sounddevice pygame miniaudio
.venv\Scripts\python python/platter_demo.py            # synth test loop
.venv\Scripts\python python/platter_demo.py song.mp3   # your own track

Drag the platter to scratch; release to resume. Load a real song any time — drag an audio file onto the window, or press o to browse. Tune the feel live:

key tunable key tunable
Up / Down SPIN_SCRUB (s per turn) , / . input smoothing ms
Left / Right release glide ms - / = max pitch (x)
[ / ] grab-brake ms i Hermite / linear
o open a song (drag-drop too) r reset tunables

No display or audio device? Render headlessly instead:

.venv\Scripts\python python/render_gesture.py   # a scratch gesture -> build/scratch_demo.wav
.venv\Scripts\python python/record_demo.py      # the demo -> assets/demo.gif + demo.mp4

Default feel (tune to taste, then freeze into firmware)

SPIN_SCRUB 2 s/turn (turntable-realistic, so scratches stay musical and you can hear the vocal) · release glide 150 ms · grab-brake 50 ms · input smoothing 6 ms · max pitch 16x · Hermite interpolation.

Porting to a microcontroller

core/ is the only thing that crosses over. Implement one ScratchSource (e.g. a small ring-buffer in RAM that re-decodes the track as the playhead nears its edges), feed your encoder's angle into scratch_move/scratch_release, and pull scratch_render from your audio (I²S DMA) callback. capi/scratch_capi.c is a working integration template. Nothing in core/ allocates or blocks in the audio path.

License

MIT — see LICENSE.

About

Portable DJ-turntable scratch / scrub engine in raw C, with a Python testbench.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages