Sleep
3 min readApr 7, 2024

Rap Beat Automation

This script loads drum and melody samples using pydub, generates random patterns for drums and melodies, overlays them to create a final mix, and exports the mix as an MP3 file. Remember, for this to work, you'll need the drum and melody WAV files (kick.wav, snare.wav, hihat.wav, clap.wav, melody1.wav, and melody2.wav) in the same directory as your script, and you'll also need to have pydub installed (pip install pydub). Additionally, pydub might require you to install ffmpeg to handle MP3 files.

# My first version of this script T.me/SleepTheGod do not contact me unless
# It is about something important do not waste my time thank you.

"""
# Rap Beat Automation By ClumsyLulz
import random
import numpy as np
import librosa
import soundfile as sf
import os
import uuid

# Load the drum samples
kick = librosa.load("kick.wav")[0]
snare = librosa.load("snare.wav")[0]
hihat = librosa.load("hihat.wav")[0]
clap = librosa.load("clap.wav")[0]

# Create a function to generate a random drum pattern in the style of SparkMasterTape
def generate_drum_pattern(length):
pattern = np.zeros(length)
for i in range(length):
if random.random() < 0.5:
pattern[i] = kick
elif random.random() < 0.75:
pattern[i] = snare
elif random.random() < 0.85:
pattern[i] = hihat
else:
pattern[i] = clap

# Add some swing to the pattern
if random.random() < 0.2:
pattern[i] = np.roll(pattern[i], 1)

return pattern

# Generate a random drum pattern
drum_pattern = generate_drum_pattern(10000)

# Load the melody samples
melody1 = librosa.load("melody1.wav")[0]
melody2 = librosa.load("melody2.wav")[0]

# Create a function to generate a random melody pattern in the style of SparkMasterTape
def generate_melody_pattern(length):
pattern = np.zeros(length)
for i in range(length):
if random.random() < 0.5:
pattern[i] = melody1
else:
pattern[i] = melody2

# Add some randomness to the pattern
if random.random() < 0.2:
pattern[i] = np.roll(pattern[i], 1)

return pattern

# Generate a random melody pattern
melody_pattern = generate_melody_pattern(10000)

# Mix the drum and melody patterns together
mixed_pattern = drum_pattern + melody_pattern

# Normalize the mixed pattern
mixed_pattern = mixed_pattern / np.max(mixed_pattern)

# Add some reverb to the mixed pattern
mixed_pattern = librosa.effects.reverb(mixed_pattern, reverb_time=1)

# Generate a unique filename for the MP3 file
filename = str(uuid.uuid4()) + ".mp3"

# Save the mixed pattern to an MP3 file
sf.write(filename, mixed_pattern, 44100)

print("MP3 file saved as:", filename)
"""

# The enhanced and functional version of the script.

import random
import numpy as np
from pydub import AudioSegment
import uuid

# Helper function to load samples
def load_sample(filename):
return AudioSegment.from_file(filename)

# Load the drum samples
kick = load_sample("kick.wav")
snare = load_sample("snare.wav")
hihat = load_sample("hihat.wav")
clap = load_sample("clap.wav")

# Create a function to generate a random drum pattern
def generate_drum_pattern(duration_ms):
pattern = AudioSegment.silent(duration=duration_ms)
beats_per_minute = 120
beat_duration_ms = int(60000 / beats_per_minute)

for i in range(0, duration_ms, beat_duration_ms):
if random.random() < 0.5:
pattern = pattern.overlay(kick, position=i)
if random.random() < 0.3:
pattern = pattern.overlay(snare, position=i + beat_duration_ms//2)
if random.random() < 0.7:
pattern = pattern.overlay(hihat, position=i)
if random.random() < 0.2:
pattern = pattern.overlay(clap, position=i)

return pattern

# Generate a random drum pattern
drum_pattern = generate_drum_pattern(30000) # 30 seconds long

# Load the melody samples
melody1 = load_sample("melody1.wav")
melody2 = load_sample("melody2.wav")

# Create a function to generate a random melody pattern
def generate_melody_pattern(duration_ms):
pattern = AudioSegment.silent(duration=duration_ms)
segment_duration_ms = 4000 # Each melody segment lasts for 4 seconds

for i in range(0, duration_ms, segment_duration_ms):
if random.random() < 0.5:
pattern = pattern.overlay(melody1, position=i)
else:
pattern = pattern.overlay(melody2, position=i)

return pattern

# Generate a random melody pattern
melody_pattern = generate_melody_pattern(30000) # 30 seconds long

# Mix the drum and melody patterns together
mixed_pattern = drum_pattern.overlay(melody_pattern)

# Generate a unique filename for the MP3 file
filename = f"{uuid.uuid4()}.mp3"

# Export the mixed pattern to an MP3 file
mixed_pattern.export(filename, format="mp3")

print("MP3 file saved as:", filename)

No responses yet