Skip to content

Instantly share code, notes, and snippets.

@thunderpoot
Created July 11, 2024 22:37
Show Gist options
  • Save thunderpoot/c06a22a114ddf0b3439613569dfc2bd9 to your computer and use it in GitHub Desktop.
Save thunderpoot/c06a22a114ddf0b3439613569dfc2bd9 to your computer and use it in GitHub Desktop.
Simple Python program to simulate playing a track at a different speed on a turntable
#!/usr/bin/env python3
# _ _
# __ __ (_) _ __ _ _ | | _ __ _ _
# \ \ / / | | | '_ \ | | | | | | | '_ \ | | | |
# \ V / | | | | | | | |_| | | | _ | |_) | | |_| |
# \_/ |_| |_| |_| \__, | |_| (_) | .__/ \__, |
# |___/ |_| |___/
# This command-line program allows you to change the playback speed of an
# audio file to simulate playing it at 33 RPM, 45 RPM, or 78 RPM, similar
# to a turntable. Accepts any common audio format for input and output.
# Usage:
# ./vinyl.py <input-file> <output-file> <original-speed> <target-speed>
# Examples
# ========
# ./vinyl.py input.mp3 output.mp3 33 45
# ./vinyl.py input.aif output.wav 78 45
# ./vinyl.py input.flac output.ogg 45 33
import argparse
from pydub import AudioSegment
def change_speed(sound, start_speed, target_speed):
speed_ratio = target_speed / start_speed
sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={
"frame_rate": int(sound.frame_rate * speed_ratio)
})
return sound_with_altered_frame_rate.set_frame_rate(sound.frame_rate)
def process_audio(input_file, output_file, start_speed, target_speed):
sound = AudioSegment.from_file(input_file)
altered_sound = change_speed(sound, start_speed, target_speed)
altered_sound.export(output_file, format=output_file.split('.')[-1])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Change the speed of a sound file from a starting RPM to a target RPM.')
parser.add_argument('input_file', type=str, help='The input sound file')
parser.add_argument('output_file', type=str, help='The output sound file')
parser.add_argument('start_speed', type=int, choices=[33, 45, 78], help='The starting speed in RPM')
parser.add_argument('target_speed', type=int, choices=[33, 45, 78], help='The target speed in RPM')
args = parser.parse_args()
process_audio(args.input_file, args.output_file, args.start_speed, args.target_speed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment