How Our Lofi Generator Composes Music in Your Browser (No Samples, No AI)
People often ask us how our lofi generator works, and the first thing most of them say after pressing play on the lofi generator is: “Is this AI?” No. There is no machine-learning model in it, no recorded samples and no server somewhere in the cloud making the music for you.
Every sound you hear is composed and synthesised live, inside your own browser, from a set of musical rules we wrote by hand.
This article walks through how our lofi generator works, for curious listeners, music students and developers. You don’t need to know any coding to follow along, and developers will find links to the right documentation on the way.
Key takeaways: how our lofi generator works
- The generator uses the browser’s built-in Web Audio API to create every sound from oscillators and filtered noise.
- A seed number drives all the “random” choices, so the same beat number always plays the same music.
- A tiny step sequencer plays 16 steps per bar and schedules notes slightly ahead of time on the audio clock, which keeps the timing tight.
- Chords, melodies and drums all follow musical rules: named progressions, smooth voice-leading, motif-based melodies and drum patterns specific to each style.
- Effects such as sidechain, tape saturation, bit-crushing, reverb and vinyl crackle are built from Web Audio nodes too.
In this article
1. The instrument is your browser
Every modern browser ships with the Web Audio API, a sound engine you can program. It hands you building blocks called nodes: oscillators that generate tones, filters that shape them, gain nodes that control volume, delays, compressors and more. You wire them together the way you would patch cables on a synthesiser.
Our generator is a small JavaScript file (it loads only when you first start the generator) that builds a whole virtual band out of these nodes. That is why it starts instantly, needs no downloads beyond the page itself and keeps playing even if the PG wifi drops after the page has loaded.
2. The seed: controlled randomness
Music needs variety, but truly “random” music mostly sounds like noise. Our approach is controlled randomness.
Every beat has a seed, the number you see as “Beat #4171”. That seed starts a small pseudo-random number generator (a compact algorithm called mulberry32), which produces the same sequence of numbers every time it gets the same seed. Whenever the generator has to make a choice, such as which kick pattern to use, whether the melody goes up or down, or whether to add a ghost note, it asks this number generator.
That is also what makes share links possible. When you copy one, we pack your style, key, tempo, mix settings, any custom drum pattern and the seed into the link. Your friend’s browser replays the exact same sequence of choices, so a batchmate you send it to at midnight hears exactly the beat you are hearing.
3. The clock: a sequencer that looks ahead
Timing is where a lot of browser music projects fall apart. JavaScript timers are sloppy: if your laptop is busy, a timer meant to fire in 25 ms might fire in 60. Beats built directly on those timers wobble.
The fix is described in Chris Wilson’s well-known article “A tale of two clocks”, and in MDN’s guide to sequencing audio. It uses two clocks:
- A loose JavaScript timer wakes up every 25 milliseconds.
- Each time it wakes, it looks a little way into the future and schedules every note due in that window on the precise audio clock.
Our sequencer divides each bar into 16 steps (sixteenth notes) and schedules them about 0.18 seconds ahead. If the tab goes into the background, where browsers slow timers down, it looks further ahead (1.5 seconds) so the music doesn’t stutter while you check your mock test result in another tab.
Swing comes from nudging every second step slightly late. That lazy, behind-the-beat feel is a big part of why lofi sounds the way it does.
4. Harmony: chords that move smoothly
Each of the eleven styles comes with a few named chord progressions. Our main Lofi style, for example, offers ii–V–I–vi, I–vi–ii–V, IV–iii–ii–I and vi–IV–I–V. These are the kinds of progressions you find in jazz and neo-soul, which is where lofi borrows much of its warmth.
Chords are stored with their “colour” notes in order of importance: root, third, fifth, seventh, then ninth, eleventh or thirteenth. The Harmony slider decides how many of those notes get used:
- Triads are three notes, simple and open.
- 7ths sound richer and jazzier.
- With 9ths and Lush you get extended chords. Here the generator leaves out the root when the bass is already playing it, a trick jazz pianists call a rootless voicing, and it only adds a ninth, eleventh or thirteenth if that note belongs to the key. No two notes in a chord are ever placed a semitone apart, which is what stops rich chords from turning muddy.
Then comes voice-leading. Rather than jumping each chord to a fixed position, the generator tries the different ways of stacking each chord and picks the one closest to the previous chord, keeping the top note smooth and the whole shape in a warm middle range of the keyboard. The chords seem to glide into each other, which is what a good keyboard player does without thinking.
5. Melody: motifs, not random notes
Purely random melodies wander about with no sense of direction. Real melodies repeat an idea and develop it, the way the mukhda keeps coming back in a film song. So the generator writes melodies in motifs:
- It creates a one-bar motif: a short rhythm and a contour that rises and then falls, using only notes that belong to the key and sit comfortably over the current chord.
- It arranges four bars as A – A′ – A – B: the motif, a slightly varied copy, the motif again and a contrasting answer.
- The first and third beats of every bar always land on a note of the chord, leaps stay within a fifth, and the answer bar comes to rest on the root or third, so each phrase sounds finished.
The Melody slider in the Groove tab sets how busy these motifs are, anywhere from a few sparse bell notes to a chattier line.
6. The band: every sound is synthesised
There is not a single audio sample in the generator. Every instrument is built from oscillators and noise:
| Sound | How it’s made |
|---|---|
| Kick drum | A sine wave whose pitch drops quickly from roughly 120 Hz to about 44 Hz, with a short volume envelope. That falling pitch is what makes the “thump”. |
| Snare | A burst of white noise through a band-pass filter, plus a short triangle-wave tone for body |
| Hi-hats | Very short bursts of high-pass-filtered noise; Chill trap adds rapid rolls |
| Rhodes-style keys | Two slightly detuned oscillators (triangle and sine) with a soft attack and a hint of the octave above |
| Pad | Two sawtooth waves detuned against each other, with a slow fade-in |
| Guitar pluck | A sawtooth wave through a low-pass filter that closes quickly, the classic “pluck” |
| 808 bass | A sine wave that can glide from the previous note, as in trap |
| Upright bass | Short triangle and sine tones, walking on each beat in Jazz hop |
Each style also has its own rhythms. Chill trap uses a half-time feel with the snare on the third beat, Synthwave uses a four-on-the-floor kick, Bossa lofi uses a clave-style rim pattern, and Ambient drops the drums entirely. If you would rather write your own drums, the 16-step grid in the Groove tab lets you tap in a kick, snare and hi-hat pattern, and the generator plays that instead.
7. The “lofi” in lofi: effects
A clean synthesiser does not sound lofi. The character comes from the effects chain, all of it built from Web Audio nodes:
- Wow & flutter: a slow wobble in pitch, like a worn tape machine.
- Sidechain pump: every kick briefly dips the volume of the keys and melody, which gives that breathing, pulsing feel.
- Tape saturation: a gentle waveshaping curve that rounds off the peaks and adds warmth.
- Bit-crush: reduces the sample rate and bit depth for a gritty, sampler-like texture. This one runs in an AudioWorklet, a small audio program that processes sound sample by sample.
- Reverb, muffle and vinyl: shared with the rest of the site. Reverb adds space, “muffle” is a low-pass filter that makes the music sound like it is playing in the next room, and vinyl adds crackle and hiss.
At the very end, a gentle compressor glues everything together and a limiter keeps loud combinations from distorting. Put together, that is how our lofi generator works: a set of rules, a seed and a chain of small synthesisers.
8. Song form: more than a loop
In Song mode, the generator arranges music into sections: a short intro with the filter closed, verses, hooks where the melody comes in, a break where the drums drop out and the filter sweeps down, then back again. A snare fill leads into each new section and a soft cymbal marks the change. The endless loop starts to feel like something composed.
See how our lofi generator works: try it (and take it apart)
Once you know how our lofi generator works, the sliders stop feeling like magic, and ten minutes of playing will teach you more than this article:
Free · no sign-up · works in your browser
Free lofi beat generator
Endless lofi composed live in your browser: no samples, no sign-up. Pick a style and it keeps playing while you browse.
A few things to try:
- Switch Harmony from Triads to Lush while a Jazz hop beat plays, and listen to the chords open up.
- Turn Swing from 50% up to about 65% and feel the groove loosen.
- Tap your own pattern into the drum grid, then press ↻ New beat. The chords and melody change, but your drums stay.
- In Synthwave, push Sidechain up for that pumping, retro sound.
To explore the ambience side as well, add rain sounds for studying under the beat. If you prefer songs with words, our Hindi lofi radio plays our original tracks. And if you want to know whether any of this actually helps you focus, read Does lofi really help you study?.
Why we built it this way
We could have used an AI model or a library of samples. We went with rules and synthesis for three reasons:
- It’s instant and endless. Nothing has to render, and the track never runs out.
- You’re in control. Every choice is a setting you can change while it plays.
- It’s light and private. No audio streams from a server; the music is made on your own device.
Honestly, it is also good fun, the same kind of fun as a jam session in a hostel room on a borrowed Casio keyboard, only written in code.
Written by the Loop & Drift team.
Loop & Drift
