szili2011's picture
Update app.py
8eac32b verified
import gradio as gr
from pydub import AudioSegment, effects
import numpy as np
import random
import io
import os
# Ensure FFmpeg is installed
os.system("apt update && apt install -y ffmpeg")
def apply_glitch_effects(audio, max_intensity):
"""Applies progressive glitch effects to the audio over time."""
duration = len(audio)
# Pick a random start time between 14.5s - 30s
glitch_start = random.randint(int(14.5 * 1000), 30 * 1000)
corrupted_audio = audio[:glitch_start] # First part remains normal
time_pointer = glitch_start
elapsed_time = 0
intensity = 1 # Start at min intensity
while time_pointer < duration:
# Gradually increase intensity
intensity = min(1 + (elapsed_time / (duration - glitch_start)) * (max_intensity - 1), max_intensity)
glitch_type = random.choice([
"cut_repeat", "bitcrush", "speed_warp", "pitch_shift",
"reverse", "static_noise", "mute_section", "overdrive",
"stutter", "bsod_loop", "reverb_washout", "deep_pitch_drop",
"time_stretch", "distortion", "glitch_silence"
])
glitch_duration = int((intensity / 10) * random.randint(200, 4000)) # Increase over time
end_point = min(time_pointer + glitch_duration, duration)
if glitch_type == "cut_repeat":
corrupted_audio += audio[time_pointer:time_pointer + 100] * int(intensity) # Repeat small chunks
elif glitch_type == "bitcrush":
corrupted_audio += audio[time_pointer:end_point].low_pass_filter(300 + int(1000 / intensity))
elif glitch_type == "speed_warp":
corrupted_audio += audio[time_pointer:end_point].speedup(playback_speed=random.uniform(0.6, 2.5), crossfade=0)
elif glitch_type == "pitch_shift":
corrupted_audio += audio[time_pointer:end_point].apply_gain(random.uniform(-10, 10))
elif glitch_type == "reverse":
corrupted_audio += audio[time_pointer:end_point].reverse()
elif glitch_type == "static_noise":
noise_samples = (np.random.randn(glitch_duration * 44100 // 1000) * 32767).astype(np.int16)
noise = AudioSegment(
noise_samples.tobytes(), frame_rate=44100, sample_width=2, channels=1
)
corrupted_audio += audio[time_pointer:end_point].overlay(noise - 5 * intensity)
elif glitch_type == "mute_section":
corrupted_audio += AudioSegment.silent(duration=glitch_duration)
elif glitch_type == "overdrive":
corrupted_audio += effects.normalize(audio[time_pointer:end_point]).apply_gain(intensity * 5)
elif glitch_type == "stutter":
for _ in range(int(intensity * 2)):
corrupted_audio += audio[time_pointer:time_pointer + 50]
elif glitch_type == "bsod_loop":
corrupted_audio += audio[time_pointer:time_pointer + 200] * random.randint(3, 8)
elif glitch_type == "reverb_washout":
corrupted_audio += audio[time_pointer:end_point].fade_in(500).fade_out(500)
elif glitch_type == "deep_pitch_drop":
corrupted_audio += audio[time_pointer:end_point].apply_gain(-random.uniform(5, 15))
elif glitch_type == "time_stretch":
try:
corrupted_audio += audio[time_pointer:end_point].speedup(playback_speed=random.uniform(0.7, 1.5), crossfade=0)
except:
corrupted_audio += audio[time_pointer:end_point]
elif glitch_type == "distortion":
gain = 10 + (intensity * 2)
distorted_audio = effects.normalize(audio[time_pointer:end_point]).apply_gain(gain)
corrupted_audio += distorted_audio
elif glitch_type == "glitch_silence":
corrupted_audio += AudioSegment.silent(duration=glitch_duration) + audio[time_pointer:end_point]
elapsed_time += glitch_duration
time_pointer += glitch_duration + random.randint(500, 3000) # Small breaks between glitches
return corrupted_audio + audio[time_pointer:] # Append remaining audio
def glitch_music(uploaded_file, intensity):
"""Processes the uploaded music and applies progressive glitch effects."""
try:
audio = AudioSegment.from_file(uploaded_file).set_frame_rate(44100)
glitched_audio = apply_glitch_effects(audio, intensity)
# Save the audio to a temporary file
temp_path = "/tmp/glitched_audio.mp3"
glitched_audio.export(temp_path, format="mp3")
return temp_path
except Exception as e:
return f"Error processing file: {str(e)}"
# Gradio UI
interface = gr.Interface(
fn=glitch_music,
inputs=[gr.File(label="Upload Music (MP3/WAV)"), gr.Slider(1, 10, 5, label="Glitch Intensity")],
outputs=gr.File(label="Download Glitched Music"),
title="Error Sans Music Corruptor",
description="Upload a song and watch it slowly break into chaos! Starts at minimal corruption and intensifies over time."
)
interface.launch()