Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import librosa
|
3 |
+
import soundfile as sf
|
4 |
+
import numpy as np
|
5 |
+
from io import BytesIO
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
def process_audio(audio_file, pitch_factor=8):
|
9 |
+
# Load the audio file
|
10 |
+
y, sr = librosa.load(audio_file)
|
11 |
+
|
12 |
+
# Pitch shift using librosa (female voice typically higher pitch)
|
13 |
+
y_shifted = librosa.effects.pitch_shift(y, sr=sr, n_steps=pitch_factor)
|
14 |
+
|
15 |
+
# Apply some feminine characteristics
|
16 |
+
# Smooth the audio slightly
|
17 |
+
y_smooth = librosa.effects.preemphasis(y_shifted)
|
18 |
+
|
19 |
+
# Normalize audio
|
20 |
+
y_normalized = librosa.util.normalize(y_smooth)
|
21 |
+
|
22 |
+
return y_normalized, sr
|
23 |
+
|
24 |
+
def save_audio(audio_data, sr):
|
25 |
+
# Save processed audio to BytesIO object
|
26 |
+
buffer = BytesIO()
|
27 |
+
sf.write(buffer, audio_data, sr, format='WAV')
|
28 |
+
return buffer
|
29 |
+
|
30 |
+
st.title("Voice Changer - Female Voice Conversion")
|
31 |
+
|
32 |
+
# File uploader
|
33 |
+
uploaded_file = st.file_uploader("Upload an audio file", type=['wav', 'mp3'])
|
34 |
+
|
35 |
+
if uploaded_file is not None:
|
36 |
+
# Save uploaded file temporarily
|
37 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
|
38 |
+
tmp_file.write(uploaded_file.getvalue())
|
39 |
+
tmp_path = tmp_file.name
|
40 |
+
|
41 |
+
# Pitch adjustment slider
|
42 |
+
pitch_factor = st.slider("Pitch Adjustment", 0.0, 12.0, 8.0, 0.5)
|
43 |
+
|
44 |
+
if st.button("Convert to Female Voice"):
|
45 |
+
# Process the audio
|
46 |
+
try:
|
47 |
+
processed_audio, sr = process_audio(tmp_path, pitch_factor)
|
48 |
+
|
49 |
+
# Save processed audio
|
50 |
+
audio_buffer = save_audio(processed_audio, sr)
|
51 |
+
|
52 |
+
# Create download button
|
53 |
+
st.audio(audio_buffer, format='audio/wav')
|
54 |
+
|
55 |
+
# Add download button
|
56 |
+
st.download_button(
|
57 |
+
label="Download Converted Audio",
|
58 |
+
data=audio_buffer,
|
59 |
+
file_name="female_voice.wav",
|
60 |
+
mime="audio/wav"
|
61 |
+
)
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
st.error(f"Error processing audio: {str(e)}")
|
65 |
+
|
66 |
+
# Add instructions
|
67 |
+
st.markdown("""
|
68 |
+
### Instructions:
|
69 |
+
1. Upload a WAV or MP3 audio file
|
70 |
+
2. Adjust the pitch slider (higher values = more feminine voice)
|
71 |
+
3. Click 'Convert to Female Voice'
|
72 |
+
4. Play the converted audio
|
73 |
+
5. Download the result if satisfied
|
74 |
+
|
75 |
+
### Notes:
|
76 |
+
- Best results with clear audio input
|
77 |
+
- Recommended pitch adjustment: 6-8 for natural-sounding results
|
78 |
+
- Larger files may take longer to process
|
79 |
+
""")
|