Untitled

 avatar
unknown
plain_text
a year ago
4.1 kB
4
Indexable
# Edge AI Staffing paceMod-4.py
# Nangyal Version


import tkinter as tk
from tkinter import filedialog
from pydub import AudioSegment

# Global variables to hold the adjustments
pitch_adjustment = 0
volume_adjustment = 0
speed_adjustment = 1.0
original_audio = None

# Main Tkinter window
root = tk.Tk()
root.title("Edge paceMod")

correct_pitch_after_speed_var = tk.IntVar()  # Checkbox variable

# Function to load an MP3 file
def load_file():
    global original_audio
    file_path = filedialog.askopenfilename(filetypes=[("MP3 files", "*.mp3")])
    if file_path:
        original_audio = AudioSegment.from_mp3(file_path)
        reset_adjustments()
        print("File loaded")

# Function to save the modified audio file
def save_file():
    global original_audio, pitch_adjustment, volume_adjustment, speed_adjustment
    if original_audio:
        print("Starting audio processing...")
        modified_audio = apply_adjustments(original_audio, pitch_adjustment, volume_adjustment, speed_adjustment)
        print("Audio processing complete. Saving file...")
        file_path = filedialog.asksaveasfilename(defaultextension=".mp3")
        if file_path:
            modified_audio.export(file_path, format="mp3")
            print("File saved successfully!")

def apply_adjustments(audio, pitch, volume, speed):
    original_frame_rate = audio.frame_rate

    # Change speed
    audio = change_speed(audio, speed)

    # Correct pitch if speed was changed and checkbox is selected
    if speed != 1.0 and correct_pitch_after_speed_var.get() == 1:
        audio = correct_pitch(audio, original_frame_rate)

    # Apply pitch change
    if pitch != 0:
        audio = audio._spawn(audio.raw_data, overrides={
            "frame_rate": int(audio.frame_rate * (2 ** (pitch / 12)))
        }).set_frame_rate(audio.frame_rate)

    # Apply volume change
    if volume != 0:
        audio += volume

    return audio

# Update pitch adjustment
def update_pitch(val):
    global pitch_adjustment
    pitch_adjustment = float(val)
    print("Pitch adjustment:", pitch_adjustment)

# Update volume adjustment
def update_volume(val):
    global volume_adjustment
    volume_adjustment = float(val)

# Update speed adjustment
def update_speed(val):
    global speed_adjustment
    speed_adjustment = float(val)

def change_speed(audio, speed):
    if speed != 1.0:
        # Adjust the frame rate to change speed
        new_frame_rate = int(audio.frame_rate * speed)
        # Resample audio to new frame rate
        audio = audio.set_frame_rate(new_frame_rate)

    return audio

# Correct pitch after changing speed
def correct_pitch(audio, original_frame_rate):
    return audio._spawn(audio.raw_data, overrides={
        "frame_rate": original_frame_rate
    }).set_frame_rate(original_frame_rate)

# Reset adjustments
def reset_adjustments():
    global pitch_adjustment, volume_adjustment, speed_adjustment
    pitch_adjustment = 0
    volume_adjustment = 0
    speed_adjustment = 1.0
    pitch_scale.set(0)
    volume_scale.set(0)
    speed_scale.set(1.0)

# Load and save buttons
load_button = tk.Button(root, text="Load MP3", command=load_file)
load_button.pack()
save_button = tk.Button(root, text="Save MP3", command=save_file)
save_button.pack()

# Pitch control
pitch_scale = tk.Scale(root, from_=-12, to=12, orient='horizontal', label='Pitch', command=update_pitch)
pitch_scale.pack()

# Volume control
volume_scale = tk.Scale(root, from_=-10, to=10, orient='horizontal', label='Volume (dB)', command=update_volume)
volume_scale.pack()

# Speed control
speed_scale = tk.Scale(root, from_=0.5, to=2.0, resolution=0.1, orient='horizontal', label='Speed', command=update_speed)
speed_scale.pack()

# Checkbox for pitch correction
pitch_correction_checkbox = tk.Checkbutton(root, text="Correct Pitch after Speed", variable=correct_pitch_after_speed_var)
pitch_correction_checkbox.pack()

# Initialize audio
original_audio = None

# Run the application
root.mainloop()
Editor is loading...
Leave a Comment