import gradio as gr from pydub import AudioSegment import librosa import noisereduce as nr import numpy as np from io import BytesIO import tempfile import os # Function to load audio def load_audio(audio_file_path): # Open the file and read its bytes with open(audio_file_path, "rb") as f: audio_bytes = f.read() # Load the audio using pydub from the file bytes audio_segment = AudioSegment.from_file(BytesIO(audio_bytes)) audio_array = np.array(audio_segment.get_array_of_samples(), dtype=np.float32) sample_rate = audio_segment.frame_rate return audio_array, sample_rate # Function for noise reduction def reduce_noise(audio, sample_rate): reduced_noise_audio = nr.reduce_noise(y=audio, sr=sample_rate) return reduced_noise_audio # Function for pitch shifting def pitch_shift(audio, sample_rate, n_steps): shifted_audio = librosa.effects.pitch_shift(audio, sr=sample_rate, n_steps=n_steps) return shifted_audio # Function for time-stretching def time_stretch(audio, rate): stretched_audio = librosa.effects.time_stretch(audio, rate) return stretched_audio # Function to save audio to a temporary file and return the path def save_audio(audio, sample_rate): # Create a temporary file to save the audio temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav") temp_file_name = temp_file.name # Use pydub to save the audio to the temp file audio_segment = AudioSegment( np.int16(audio).tobytes(), frame_rate=sample_rate, sample_width=2, channels=1 ) audio_segment.export(temp_file_name, format="wav") temp_file.close() return temp_file_name # Main function to apply audio effects def apply_effects(audio_file, noise_reduction, pitch_shift_steps, time_stretch_rate): audio, sr = load_audio(audio_file) if noise_reduction: audio = reduce_noise(audio, sr) if pitch_shift_steps != 0: audio = pitch_shift(audio, sr, pitch_shift_steps) if time_stretch_rate != 1.0: audio = time_stretch(audio, time_stretch_rate) return save_audio(audio, sr) # Gradio UI def build_ui(): with gr.Blocks() as demo: # Center-aligned Title gr.Markdown("
Welcome to Shyam's AI Audio Studio!
This tool allows you to upload audio files and apply various effects like:
Experiment with the sliders to fine-tune the effects and get your desired sound!