Play music on your trackpad, in your browser
This free trackpad instrument turns the trackpad of your laptop (or the screen of a phone or tablet) into two instruments: a melody pad that works like an XY synthesizer or a theremin, and a grid of 16 drum pads. Every sound is generated live in your browser with Web Audio. There are no recordings and no sample libraries, nothing is uploaded, and there is nothing to install.
The idea comes from nospeaker, a small Mac program on GitHub that makes a MacBook trackpad play tones through its haptic motor. On this page the sound comes out of your speakers or headphones and the trackpad is the instrument you play. If you also want to feel the notes in the trackpad itself, there is an optional Mac helper, described below.
Why can’t a web page make the trackpad buzz?
A MacBook trackpad has no speaker, only a small haptic motor that gives you the feeling of a click. According to the nospeaker README, Apple’s public haptics API offers only a few fixed taps, so nospeaker uses a private macOS framework to play custom tones on the motor, and it needs a Force Touch trackpad. A browser has no access to that. As far as I know there is no web API that drives a laptop trackpad’s haptic motor: navigator.vibrate exists in some Android browsers for phone vibration, but not on desktop and not in Safari on iPhone. A web page on its own therefore cannot make the trackpad buzz.
Feel the notes: the Mac helper
What a page can do is talk to a program on your own computer. The trackpad-haptics helper is a single Python file that you download from the Haptics tab and run in Terminal. It listens on 127.0.0.1, which only your own computer can reach, and it only accepts connections from pages on jasperbernaers.com. While you play, the page sends it short messages: start a note at this pitch and strength, change it, stop it, or play a drum hit. The helper turns them into vibrations on the trackpad motor, using the same private macOS framework as nospeaker. Held notes are played as a series of short overlapping buzzes at the pitch of your finger, and drum hits are short bursts, with a different pitch and length for each drum.
- What you need: a Mac with a Force Touch trackpad (a MacBook from 2015 or later, or a Magic Trackpad 2) and Python 3, which comes with the Xcode command line tools.
- Which browser: Chrome, Edge or Firefox are the safer choice. Safari may refuse to let a secure page connect to a program on your own computer. Chrome may ask for permission to connect to devices on your network.
- Safety: the helper never reads your touches, files or screen. It only plays vibrations, it stops by itself when the page goes quiet, and the file is short enough to read before you run it. You can also cap the strength with
--max-level.
- Fragile by nature: the framework is undocumented, so a macOS update can break it. The helper then keeps running and the page shows “no motor”. Run
python3 trackpad-haptics.py --test to check the motor without the page.
- What you will feel: the motor is clearest between roughly 100 Hz and 4 kHz and loudest around 1 kHz, so low notes feel like a soft hum, drums like taps and thumps, and high notes like a buzz.
What a browser can read from a trackpad
A web page does not see your fingers on a laptop trackpad. It sees one pointer, like a mouse, plus scroll and pinch gestures. That is why the instrument is built around a single moving point, and why you can play several fingers at once only on a touch screen.
| Input | Laptop trackpad | Touch screen | Used for |
| Pointer position | One pointer | Every finger | Pitch and brightness, which pad you hit |
| Press force | Safari on a Force Touch trackpad: physically click, then press harder. Other browsers report none. | Apple Pencil, or a screen that reports pressure. Most phones report none. | Volume and hit strength |
| Two-finger scroll | Yes, as scroll events | Not used | Octave, key and tempo |
| Pinch | Yes: Ctrl+scroll in Chrome, Edge and Firefox, a gesture event in Safari | Not used | Range of the melody pad |
| Several fingers at once | No, the browser gives one pointer | Yes | Chords, several pads |
The Trackpad tab shows live what your own device reports (pointer type, pressure, press force, scroll and pinch), so you can see what works on your hardware. Without press force the page plays at a fixed volume in melody mode, and in drum mode it uses where you hit the pad: the centre is a hard hit and the edge is a soft one.
The melody pad: pitch, scale and magnet
The horizontal axis of the pad is pitch. It spans 1 to 4 octaves, starting at the octave you choose. The vertical lines are the notes of the selected scale and key: the bright line is the root note, the dimmer lines are the other scale notes. You can pick major, natural minor, major and minor pentatonic, blues, dorian, mixolydian, harmonic minor, hirajoshi, whole tone, or chromatic.
The Magnet slider sets how strongly your finger is pulled to the notes of the scale. At 100% the pitch jumps from note to note, like a keyboard. At 0% the pitch is completely free, like a theremin or a slide guitar, and every note in between is possible. The Slide time slider sets how fast the pitch follows your finger. A minor pentatonic scale with the magnet at 75% is a safe place to start: every note you land on sounds right.
The vertical axis can control brightness (a filter that opens as you go up), vibrato depth or volume. Choose which one on the Pad tab. With Latch on, a note keeps sounding when you lift your finger, so you can leave a drone and play with the other controls.
The seven sounds
- Soft lead: two slightly detuned sawtooth waves through a filter, a classic synth lead.
- Theremin: a smooth sine wave with a slow vibrato.
- String pad: three detuned sawtooth waves with a slow attack.
- Electric piano: a two-operator FM voice that rings and fades.
- Breath flute: a triangle wave with a little filtered noise for the breath.
- Haptic buzz: a nod to the trackpad motor, two sine tones through a soft clipper with a fast tremolo, so it buzzes.
- Chip pulse: a 25% pulse wave, an 8-bit lead.
The drum pads and the loop recorder
The drum mode turns the pad into a 4 by 4 grid. The bottom row holds kick, snare, closed hat and open hat; above them are clap, rim, two toms, crash, ride, cowbell, shaker, two congas, clave and a zap. A closed hat chokes an open hat, as on a real kit. All drums are synthesised, with three kits (studio, TR style and lo-fi) and sliders for tune, decay and drive.
The loop recorder works like a simple beat machine. Press Rec and, after a one-bar count-in, your hits are recorded into a loop of 1, 2 or 4 bars at the tempo you set. With Quantise on, hits snap to the nearest sixteenth note. Press Rec again to add another layer, Undo removes the last layer. On the Beat tab you can also load a ready-made beat (rock, hip hop, house, trap, bossa nova or reggae), set the tempo with a tap, and then switch to the melody pad to play over it: the loop keeps running.
The loop records drum hits only. Melodies are played live over it.
Tips for a better sound
- Use headphones if you can: the delay between your finger and the sound is shorter and the echo and reverb are clearer.
- Click the pad once before you use two-finger scroll or pinch. The page only captures scroll and pinch over the pad after you have clicked it (or in fullscreen), so normal page scrolling still works.
- On a MacBook, fullscreen gives you a pad as big as the screen, which is much more expressive than the small preview.
- If you hold a note and nothing seems to happen when you press harder, check the Trackpad tab: it shows whether your browser reports any force at all.
Privacy
Everything runs on your device. The page does not upload anything and does not record your playing. It stores your settings (sound, scale, kit, tempo, haptics on or off) in this browser only, under the key jb-trackpad. The loop you record is kept in memory and is gone when you close the tab. If you turn haptics on, the page connects to ws://127.0.0.1 on your own computer, and nothing leaves it.
Honest limits
The page itself cannot make the trackpad buzz: that needs the optional Mac helper, which is experimental. A laptop trackpad gives the page one finger, so this is a one-finger instrument unless you use a touch screen. Press force depends on Safari and a Force Touch trackpad, and the page was tested with a mouse and simulated touch, not with every trackpad. The sounds are synthesised and simple: they are good for jamming and sketching, not a replacement for a real synthesizer or a sample-based drum kit. There is no MIDI input or output on this page; for that, see the virtual drum kit and the virtual piano.
More music tools
Play a drum kit from a MIDI e-drum or pad controller with the virtual drum kit, play music in the air with the hand tracking instrument, or browse all music tools.