Skip to content

Instantly share code, notes, and snippets.

@JonathanHarford
Created October 16, 2023 18:31
Show Gist options
  • Save JonathanHarford/da6bf82cd1ca94cf9ca4734cadb5f9b7 to your computer and use it in GitHub Desktop.
Save JonathanHarford/da6bf82cd1ca94cf9ca4734cadb5f9b7 to your computer and use it in GitHub Desktop.
Haunted gramophone
from pydub import AudioSegment
from pydub.playback import play
from pydub.generators import WhiteNoise
import random
sound = AudioSegment.from_wav('src.wav')
audio_len = len(sound)
base_frame_rate = sound.frame_rate
glitched = AudioSegment.empty()
glitch_start, glitch_end = 0, 0
speed = sound.frame_rate
while glitch_start < audio_len:
glitch_start = glitch_end
coeff = 0.95 + 0.1 * random.random()
glitch_end += coeff * 1000
if glitch_end > audio_len:
glitch_end = audio_len
slice = sound[glitch_start:glitch_end]
# Apply random speed up, slow down, or skipping
speed = int(speed * coeff)
if random.random() > 0.96:
# Record skip, reset speed
glitched += WhiteNoise().to_audio_segment(duration=10)
speed = base_frame_rate
print(glitch_start // 1000, '-', glitch_end // 1000, speed)
slice = slice._spawn(slice.raw_data, overrides={'frame_rate': speed})
slice.set_frame_rate(base_frame_rate) # Is this necessary?
glitched += slice
glitched.export('glitched.wav', format='wav')
play(glitched)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment