Bytebeat¹ outputs unsigned 8-bit audio at 8000Hz. The variable t increments by 1 for every sample. The environment truncate output to 8 bits, values above 255 wrap back to 0 creating a sawtooth wave.
| Integer | Binary | Truncated | Decimal |
|---|---|---|---|
| 254 | 0011111110 | 11111110 | 254 |
| 255 | 0011111111 | 11111111 | 255 |
| 256 | 0100000000 | 00000000 | 0 |
t
Arithmetic directly alters the phase and frequency of the wrapping sawtooth wave.
t + 128 shifts phase. -t flips the wave slope.
t * 2 = octave up. t / 2 = octave down.
Forces early wraps.t%128 halves volume.
Yields 1 or 0. Acts as a boolean gate.
Example of operators:
t % 8000<4000 ? t : t*5
Bitwise operators act as logic mixers and toggles values on and off to sculpt complex timbres and melodies.
Masking: t & 128 isolates the top bit, outputting a pure square wave. Acts as a volume gate.
Mixing: t | (t >> 2) preserves high bits of both signals, smearing harmonics for lo-fi chords.
Exclusive logic. Yields metallic phase-distortion and ring-modulation effects.
Inversion. Mirrors the binary waveform exactly.
Example of operators:
t>>3 | (t>>6) * t & t + (t>>14&4)
Bitwise operations are computationally identical to arithmetic operations. Bitshiftting is multiplying or dividing by powers of 2. For array looping, bitwise & acts as an ultra-fast modulo for array lengths that are exact powers of 2 (2, 4, 8, ...).
| Bitshit | Arithmetic | Effect / Usage |
|---|---|---|
| t << 1 | t * 2 | +1 Octave Up (Double freq) |
| t >> 1 | t / 2 | -1 Octave Down (Halve freq) |
| x & 7 | x % 8 | Array loop (Only for 2^n length) |
| x & 3 | x % 4 | Array loop (Only for 2^n length) |
| ~t | -t-1 | Phase inversion |
Bitshifting t scales time. t>>11 is the standard demoscene master clock advancing approximately every 0.25 seconds. To trigger rhythm sections: (t>>11)&3 creates a 4-step sequence.
s=[1,2,3,4], t * s[t>>11&3]BPM Syncing
For exact tempo control, use integer division instead of bitshifting. Calculate Samples Per Beat (Sample Rate * 60 / BPM). For 120 BPM at 8000Hz, SPB is 4000 divide by t and use | 0 to floor the value.
s=[1,2,3], t * s[(t/4000|0)%3]
Whole integer multipliers t * 2, t * 3 jump entirely different octaves. To sequence a normal diatonic scale within a single octave, multiply t by specific fractions between 1.0 and 2.0. To change the root note, apply a base multiplier before the fraction, for example t*1.5 shifts root to G.
| Note | Interval | Frac | Note | Interval | Frac |
|---|---|---|---|---|---|
| C | Unison | 1 | G | Perf 5th | 3/2 |
| D | Maj 2nd | 9/8 | A | Maj 6th | 5/3 |
| E | Maj 3rd | 5/4 | B | Maj 7th | 15/8 |
| F | Perf 4th | 4/3 | C' | Octave | 2 |
s=[1, 9/8, 5/4, 4/3, 3/2, 5/3, 15/8, 2], t * s[(t/2000|0)%8]
The first bytebeat formula created by viznut.
t*((t>>12|t>>8)&63&t>>4)
The classic melodic bytebeat melody discovered by many.
t*(42&t>>10)
Secondary counter dynamically shrinks the square duty cycle.
(t%100>=t/200%100)*255
The -1 offset realigns phase, -t-1 is same as ~t.
v=t*6, (v&511)>255 ? v : ~v
Floatbeat outputs a signed 32-bit float strictly between -1.0 and +1.0. The variable t increments infinitely. Raw values outside these bounds result in hard-clipped silence.
Modulo wraps the infinite counter.
t%100/50-1.0
Scaling time into a smooth sine oscillator.
sin(t/20)
Continuous mathematical operations are the primary tools used to shape floating-point audio.
Primary smooth oscillators. Phase must be divided down (t/20).
Distorted harmonics due to infinite periodic peaks.
Exponential curves for volume decays, like: exp(0.0-t/1000)
Log curves. Ideal for sharp percussive transients.
Non-linear wave shapers.
Clamps signals like: max(0, x)
Abrupt phase resets via modulo create loud DC clicks. Applying an exp() or log() decay dampens the amplitude to 0.0 before the cycle restarts, ensuring clean transients.
Dampens completely over a fixed sample duration with a smooth tail.
b=t%16000, sin(b/20)*exp(0.0-b/2000)
Creates a highly percussive, sharp initial drop. max() clamps the tail.
b=t%16000, sin(b/20)*max(0, 1-log(b/100+1)/5)
Loop and decay scale seamlessly with the sample rate.
b=t%(sr/3), sin(b/20)*exp(0.0-b/(sr/24))
To compose using exact western tuning (where MIDI Note 69 = A440), the following formula can be used:
f=(n)=>440 * 2 ** ((n-69) / 12), sin(t * f(72) * 6.28 / 48000)BPM Sync
To sequence melodies accurately calculate samples per beat: (CurrentSample Rate * 60 / BPM). For 120 BPM: 24000 samples.
| Tempo | Math (SR * 60 / BPM) | 48kHz SPB |
|---|---|---|
| 120 BPM | 48000 * (60/120) | 24,000 |
| 140 BPM | 48000 * (60/140) | 20,571 |
b = t % 24000, sin(b/20) * exp(0.0 - b/2000)
Classic single line FM.
m=t/10,sin(m+sin(m*2.0)*2.0)
Decay applied inside the phase array for a rapid pitch drop.
b=t%16000, sin(b*exp(0.0-b/1500)/15)*exp(0.1-b/4000)
Dense multi-oscillator chords using an array lookup grid.
gc=t/(t/[12,14,16][(t>>14)%3]), (sin(t/gc)+sin(t/20)+tan(sin(t/t-t/(gc+.01))))/4
Clean, repeating physical model with exact damping curve.
b=t%8000, sin(b/25)*exp(0.0-b/1800)
Metallic decay with a complex inharmonic spectrum.
b=t%16000,e=exp(-b/2000),sin(b/5+e*4*sin(b/4))*e