Algorithmvoice / app.py
Artificial-superintelligence's picture
Update app.py
4851afd verified
import streamlit as st
import librosa
import soundfile as sf
import numpy as np
from io import BytesIO
import tempfile
def process_audio(audio_file, pitch_factor=8):
# Load the audio file
y, sr = librosa.load(audio_file)
# Pitch shift using librosa (female voice typically higher pitch)
y_shifted = librosa.effects.pitch_shift(y, sr=sr, n_steps=pitch_factor)
# Apply some feminine characteristics
# Smooth the audio slightly
y_smooth = librosa.effects.preemphasis(y_shifted)
# Normalize audio
y_normalized = librosa.util.normalize(y_smooth)
return y_normalized, sr
def save_audio(audio_data, sr):
# Save processed audio to BytesIO object
buffer = BytesIO()
sf.write(buffer, audio_data, sr, format='WAV')
return buffer
st.title("Voice Changer - Female Voice Conversion")
# File uploader
uploaded_file = st.file_uploader("Upload an audio file", type=['wav', 'mp3'])
if uploaded_file is not None:
# Save uploaded file temporarily
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
tmp_file.write(uploaded_file.getvalue())
tmp_path = tmp_file.name
# Pitch adjustment slider
pitch_factor = st.slider("Pitch Adjustment", 0.0, 12.0, 8.0, 0.5)
if st.button("Convert to Female Voice"):
# Process the audio
try:
processed_audio, sr = process_audio(tmp_path, pitch_factor)
# Save processed audio
audio_buffer = save_audio(processed_audio, sr)
# Create download button
st.audio(audio_buffer, format='audio/wav')
# Add download button
st.download_button(
label="Download Converted Audio",
data=audio_buffer,
file_name="female_voice.wav",
mime="audio/wav"
)
except Exception as e:
st.error(f"Error processing audio: {str(e)}")
# Add instructions
st.markdown("""
### Instructions:
1. Upload a WAV or MP3 audio file
2. Adjust the pitch slider (higher values = more feminine voice)
3. Click 'Convert to Female Voice'
4. Play the converted audio
5. Download the result if satisfied
### Notes:
- Best results with clear audio input
- Recommended pitch adjustment: 6-8 for natural-sounding results
- Larger files may take longer to process
""")