Turn Your Macbook Into A Drum
One day I was scrolling on reels when I saw this reel:
Reel
While watching this reel I was tapping my table like Its a drum, and then I decided to make My laptop detect where I am tapping it, using the Inbuilt accelerometer that this reel's app slapmac uses, (I am guessing)
So, I reversed-engineered the concept and built macDrum—a tiny, low-latency, machine-learning-powered drum kit that turns different zones of your MacBook chassis into live MIDI/audio triggers.
Instead of using the microphone or camera, macDrum intercepts raw physical vibrations traveling through the aluminum unibody of the Mac. Here is the engineering breakdown of the pipeline:
MacBooks have an internal IMU (Inertial Measurement Unit) to detect sudden drops (historically to park hard drive heads). Using the macimu bridge, this project hooks directly into Apple's internal Shared Memory (SHM) segment. This bypasses high-overhead OS layers to read raw, high-frequency linear acceleration across three spatial dimensions:
The system idle-polls gravity (resting at roughly
A single slap yields a 2D matrix of shape (40, 3)—representing 40 sequential moments in time, each with 3 spatial axes. To feed this into a classical classifier, the matrix is programmatically linearized into a 1D Feature Vector of 120 dimensions:
The core classification is handled by a Supervised Random Forest Classifier (scikit-learn).
- Rather than relying on rigid mathematical formulas, the model uses an ensemble of 100 independent Decision Trees.
- During training, trees evaluate splits using Gini Impurity to discover exactly how a shockwave dissipates through the chassis.
- When you hit a zone, the 100 trees run parallel evaluations and hold a majority vote to instantly classify the vibration signature into one of five categories:
top_left,top_right,bottom_left,bottom_right, ornone.
collect_data.py: The data acquisition script. It prompts you for a label, registers the slaps, flattens the vectors, and appends them to the training set.train_model.py: The training engine. It ingests your raw data, splits it into an 80/20 train/test distribution, prints an evaluation report, and serializes the model.play_drums.py: The real-time live engine. It loads the compiled brain, continuously polls the IMU, runs real-time inference, and triggers local audio files.drum_data.csv: The local dataset containing your raw, flattened 120-feature vibration fingerprints.drum_model.pkl: The compiled, pickled binary representation of your trained Random Forest model.
- Virtual MIDI Loopback: Replace native
afplayexecution with a virtual MIDI port driver (mido), allowingmacDrumto drive professional DAWs like Logic Pro, Ableton Live, or GarageBand as a physical hardware controller. - CoreML Compilation: Export the trained Random Forest directly into Apple's native
.mlmodelformat to leverage the Apple Neural Engine (ANE) for hardware-accelerated inference. - Dynamic Calibration Loop: Implement a 5-second automatic threshold calibration routine upon script startup to read ambient environment noise and adjust
DELTA_THRESHOLDalgorithmically.