Spaces:
Sleeping
Sleeping
Create runfile.py
Browse files- runfile.py +67 -0
runfile.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import librosa
|
| 2 |
+
import numpy as np
|
| 3 |
+
import soundfile as sf
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import argparse
|
| 6 |
+
|
| 7 |
+
def auto_tune(audio_path, output_path=None, target_pitch=440.0):
|
| 8 |
+
# Load the audio file
|
| 9 |
+
y, sr = librosa.load(audio_path)
|
| 10 |
+
|
| 11 |
+
# Extract pitch using librosa's piptrack method
|
| 12 |
+
pitches, magnitudes = librosa.core.piptrack(y=y, sr=sr)
|
| 13 |
+
|
| 14 |
+
# Select pitches with higher magnitudes
|
| 15 |
+
pitches = [p for p, m in zip(pitches, magnitudes) if m > np.median(magnitudes)]
|
| 16 |
+
|
| 17 |
+
# Remove outliers
|
| 18 |
+
pitches = np.array(pitches)
|
| 19 |
+
pitches = pitches[pitches > 0] # Keep only positive values
|
| 20 |
+
|
| 21 |
+
# Calculate the tuning ratio to shift the pitch to the target pitch
|
| 22 |
+
median_pitch = np.median(pitches)
|
| 23 |
+
tuning_ratio = target_pitch / median_pitch
|
| 24 |
+
|
| 25 |
+
# Adjust the pitch of the audio
|
| 26 |
+
y_tuned = librosa.effects.pitch_shift(y, sr, n_steps=np.log2(tuning_ratio))
|
| 27 |
+
|
| 28 |
+
# Save the tuned audio if output path is specified
|
| 29 |
+
if output_path:
|
| 30 |
+
sf.write(output_path, y_tuned, sr)
|
| 31 |
+
|
| 32 |
+
return y_tuned, sr
|
| 33 |
+
|
| 34 |
+
def run_cli():
|
| 35 |
+
parser = argparse.ArgumentParser(description="Auto-tune a voice recording.")
|
| 36 |
+
parser.add_argument("input", type=str, help="Path to the input audio file")
|
| 37 |
+
parser.add_argument("output", type=str, help="Path to save the tuned audio file")
|
| 38 |
+
parser.add_argument("--target_pitch", type=float, default=440.0, help="Target pitch (default: 440 Hz)")
|
| 39 |
+
|
| 40 |
+
args = parser.parse_args()
|
| 41 |
+
y_tuned, sr = auto_tune(args.input, args.output, args.target_pitch)
|
| 42 |
+
print(f"Saved tuned audio to {args.output}")
|
| 43 |
+
|
| 44 |
+
def run_gradio():
|
| 45 |
+
def gradio_interface(audio, target_pitch):
|
| 46 |
+
audio_path = "input_audio.wav"
|
| 47 |
+
output_path = "output_tuned.wav"
|
| 48 |
+
sf.write(audio_path, audio[1], audio[0])
|
| 49 |
+
y_tuned, sr = auto_tune(audio_path, target_pitch=target_pitch)
|
| 50 |
+
return (sr, y_tuned)
|
| 51 |
+
|
| 52 |
+
iface = gr.Interface(
|
| 53 |
+
fn=gradio_interface,
|
| 54 |
+
inputs=[gr.Audio(source="microphone", type="numpy"), gr.Number(value=440.0, label="Target Pitch")],
|
| 55 |
+
outputs=gr.Audio(type="numpy", label="Tuned Audio"),
|
| 56 |
+
title="Auto-Tune Voice",
|
| 57 |
+
description="Upload or record your voice and apply auto-tune.",
|
| 58 |
+
)
|
| 59 |
+
iface.launch()
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
# Check if running in CLI or as a Gradio app
|
| 63 |
+
import sys
|
| 64 |
+
if len(sys.argv) > 1:
|
| 65 |
+
run_cli()
|
| 66 |
+
else:
|
| 67 |
+
run_gradio()
|