mirror of
https://github.com/blazblue547-afk/aero3.git
synced 2026-12-02 14:24:17 -05:00
143 lines
5.0 KiB
Python
Executable File
143 lines
5.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Generate a subtle, seamless lofi loop for the AeroOS home screen.
|
|
|
|
Pure standard library (math/struct/wave) so it runs on any host with python3.
|
|
Synthesizes a mellow jazzy chord progression with soft keys, a quiet kick,
|
|
brushed hats and gentle vinyl crackle, then writes a 16-bit stereo WAV that
|
|
loops seamlessly (whole number of bars).
|
|
|
|
Usage: generate_lofi.py OUTPUT.wav
|
|
"""
|
|
import math
|
|
import random
|
|
import struct
|
|
import sys
|
|
import wave
|
|
|
|
SR = 44100
|
|
BPM = 72
|
|
BEAT = 60.0 / BPM
|
|
BAR = BEAT * 4
|
|
BARS = 8 # loop length in bars -> seamless
|
|
DUR = BAR * BARS
|
|
|
|
# Jazzy ii-V-I-vi style progression (root freqs, Hz) per 2 bars.
|
|
# Each chord is a list of semitone offsets from the root.
|
|
A = 220.0
|
|
def note(semis, base=A):
|
|
return base * (2 ** (semis / 12.0))
|
|
|
|
CHORDS = [ # (root_semitone_from_A, [chord tones as semitone offsets])
|
|
(3, [0, 4, 7, 11]), # Cmaj7
|
|
(0, [0, 3, 7, 10]), # Am7
|
|
(5, [0, 3, 7, 10]), # Dm7
|
|
(10, [0, 4, 7, 10]), # G7
|
|
]
|
|
|
|
def soft_clip(x):
|
|
return math.tanh(x * 1.2)
|
|
|
|
def main(out_path):
|
|
random.seed(7)
|
|
n = int(DUR * SR)
|
|
left = [0.0] * n
|
|
right = [0.0] * n
|
|
|
|
chord_len = DUR / len(CHORDS)
|
|
|
|
# --- pad / electric-piano-ish chords (detuned sines + slow tremolo) ---
|
|
for ci, (root, tones) in enumerate(CHORDS):
|
|
start = int(ci * chord_len * SR)
|
|
end = int((ci + 1) * chord_len * SR)
|
|
for t in tones:
|
|
f = note(root + t, base=A) / 2.0 # one octave down, warm
|
|
detune = 1.003
|
|
for i in range(start, end):
|
|
tt = (i - start) / SR
|
|
env = min(tt / 0.04, 1.0) * math.exp(-tt * 0.25) # soft pluck
|
|
trem = 0.92 + 0.08 * math.sin(2 * math.pi * 4.7 * tt)
|
|
s = (math.sin(2 * math.pi * f * tt)
|
|
+ 0.5 * math.sin(2 * math.pi * f * detune * tt)
|
|
+ 0.25 * math.sin(2 * math.pi * f * 2 * tt))
|
|
s *= 0.085 * env * trem
|
|
# gentle stereo spread by tone index
|
|
pan = 0.5 + 0.12 * math.sin(t)
|
|
left[i] += s * (1 - pan + 0.5)
|
|
right[i] += s * (pan + 0.5)
|
|
|
|
# --- bass (root, one more octave down) ---
|
|
for ci, (root, _tones) in enumerate(CHORDS):
|
|
start = int(ci * chord_len * SR)
|
|
end = int((ci + 1) * chord_len * SR)
|
|
f = note(root, base=A) / 4.0
|
|
for i in range(start, end):
|
|
tt = (i - start) / SR
|
|
env = min(tt / 0.02, 1.0) * math.exp(-tt * 0.6)
|
|
s = 0.16 * env * soft_clip(math.sin(2 * math.pi * f * tt))
|
|
left[i] += s
|
|
right[i] += s
|
|
|
|
# --- drums: soft kick on 1 & 3, brushed hat on offbeats ---
|
|
total_beats = int(DUR / BEAT)
|
|
for b in range(total_beats):
|
|
bt = b * BEAT
|
|
# kick
|
|
if b % 2 == 0:
|
|
ks = int(bt * SR)
|
|
for i in range(ks, min(ks + int(0.18 * SR), n)):
|
|
tt = (i - ks) / SR
|
|
fk = 110 * math.exp(-tt * 24) + 45
|
|
s = 0.5 * math.exp(-tt * 12) * math.sin(2 * math.pi * fk * tt)
|
|
left[i] += s; right[i] += s
|
|
# hat (off-beat)
|
|
hs = int((bt + BEAT / 2) * SR)
|
|
for i in range(hs, min(hs + int(0.05 * SR), n)):
|
|
tt = (i - hs) / SR
|
|
s = 0.06 * math.exp(-tt * 60) * (random.random() * 2 - 1)
|
|
left[i] += s; right[i] += s
|
|
|
|
# --- vinyl crackle + tape hiss (very subtle) ---
|
|
for i in range(n):
|
|
hiss = (random.random() * 2 - 1) * 0.006
|
|
crackle = 0.0
|
|
if random.random() < 0.0009:
|
|
crackle = (random.random() * 2 - 1) * 0.18
|
|
left[i] += hiss + crackle
|
|
right[i] += hiss + crackle
|
|
|
|
# --- simple one-pole low-pass for that warm muffled lofi tone ---
|
|
def lowpass(buf, a=0.18):
|
|
prev = 0.0
|
|
for i in range(len(buf)):
|
|
prev = prev + a * (buf[i] - prev)
|
|
buf[i] = prev
|
|
lowpass(left); lowpass(right)
|
|
|
|
# --- crossfade head/tail so the loop is seamless ---
|
|
xf = int(0.25 * SR)
|
|
for i in range(xf):
|
|
g = i / xf
|
|
left[i] = left[i] * g + left[n - xf + i] * (1 - g)
|
|
right[i] = right[i] * g + right[n - xf + i] * (1 - g)
|
|
|
|
# --- master gain + soft clip, write 16-bit stereo ---
|
|
peak = max(1e-6, max(max(abs(x) for x in left), max(abs(x) for x in right)))
|
|
gain = 0.5 / peak # keep it subtle (headroom)
|
|
with wave.open(out_path, "wb") as w:
|
|
w.setnchannels(2)
|
|
w.setsampwidth(2)
|
|
w.setframerate(SR)
|
|
frames = bytearray()
|
|
for i in range(n):
|
|
l = int(soft_clip(left[i] * gain) * 30000)
|
|
r = int(soft_clip(right[i] * gain) * 30000)
|
|
frames += struct.pack("<hh", max(-32768, min(32767, l)),
|
|
max(-32768, min(32767, r)))
|
|
w.writeframes(bytes(frames))
|
|
print(f"wrote {out_path}: {DUR:.1f}s, {n} frames")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 2:
|
|
sys.exit("usage: generate_lofi.py OUTPUT.wav")
|
|
main(sys.argv[1])
|