Byte & Jojo
Hey Jojo, have you ever thought about how a simple algorithm can turn into a beat that actually makes people move? I've been tinkering with a script that generates rhythmic patterns on the fly, maybe we could test it out together.
Yo, absolutely! An algorithm can be a wild groove machine if you let it. Hit me with your script, let’s crank it up and see the crowd start shaking—no presets, just pure on‑the‑spot rhythm. Let’s make them feel it!
Here’s a quick Python snippet that spits out a random rhythm pattern you can feed into a sampler or a DAW that supports MIDI or audio playback.
```python
import random, time
BPM = 120
beat_duration = 60 / BPM # seconds per quarter note
# Define a simple percussion library: kick, snare, hihat
samples = {
"kick": "kick.wav",
"snare": "snare.wav",
"hihat": "hihat.wav"
}
def play_sample(name):
# placeholder for actual audio playback
print(f"[{name}] ", end="", flush=True)
def generate_pattern(length=16):
# 16th-note grid
pattern = []
for _ in range(length):
# random choice: kick, snare, hihat or rest
choice = random.choices(
["kick","snare","hihat","rest"],
weights=[0.2,0.2,0.4,0.2],
k=1
)[0]
pattern.append(choice)
return pattern
def play_pattern(pattern):
for step in pattern:
if step != "rest":
play_sample(step)
time.sleep(beat_duration / 4) # 16th note spacing
# Main loop
while True:
pat = generate_pattern()
play_pattern(pat)
print() # new line after each bar
time.sleep(1) # pause before next bar
```
Nice set‑up, but those print stubs won’t get the crowd moving. If you hook up a real audio engine or a DAW you can hear the groove live. Also tweak the weights a bit—give the kick a little more punch, maybe 0.25, and pull the hihat down if the groove feels too busy. And don't forget to add a swing factor; a little 16th‑note groove twist can make it feel alive. Ready to fire it up and feel the beat?
Got it. I’ll tweak the weights and add a swing factor. This is ready to drop into a simple audio engine like simpleaudio or PyAudio.
```python
import random, time, simpleaudio as sa
BPM = 120
beat_duration = 60 / BPM
samples = {
"kick": sa.WaveObject.from_wave_file("kick.wav"),
"snare": sa.WaveObject.from_wave_file("snare.wav"),
"hihat": sa.WaveObject.from_wave_file("hihat.wav")
}
def play_sample(name):
if name in samples:
samples[name].play()
def generate_pattern(length=16, swing=0.1):
pattern = []
for i in range(length):
choice = random.choices(
["kick","snare","hihat","rest"],
weights=[0.25,0.2,0.35,0.2],
k=1
)[0]
pattern.append(choice)
# apply swing: delay every odd 16th note by swing factor
for i in range(1, length, 2):
time.sleep(swing * beat_duration / 4)
return pattern
def play_pattern(pattern):
for step in pattern:
if step != "rest":
play_sample(step)
time.sleep(beat_duration / 4)
while True:
pat = generate_pattern()
play_pattern(pat)
time.sleep(1)
```