Team Ai
Apppublic

Artificial-superintelligence/Algorithmvoice

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py79 linesDownload Raw Back to root
1import streamlit as st2import librosa3import soundfile as sf4import numpy as np5from io import BytesIO6import tempfile7 8def process_audio(audio_file, pitch_factor=8):9    # Load the audio file10    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 characteristics16    # Smooth the audio slightly17    y_smooth = librosa.effects.preemphasis(y_shifted)18    19    # Normalize audio20    y_normalized = librosa.util.normalize(y_smooth)21    22    return y_normalized, sr23 24def save_audio(audio_data, sr):25    # Save processed audio to BytesIO object26    buffer = BytesIO()27    sf.write(buffer, audio_data, sr, format='WAV')28    return buffer29 30st.title("Voice Changer - Female Voice Conversion")31 32# File uploader33uploaded_file = st.file_uploader("Upload an audio file", type=['wav', 'mp3'])34 35if uploaded_file is not None:36    # Save uploaded file temporarily37    with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:38        tmp_file.write(uploaded_file.getvalue())39        tmp_path = tmp_file.name40 41    # Pitch adjustment slider42    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 audio46        try:47            processed_audio, sr = process_audio(tmp_path, pitch_factor)48            49            # Save processed audio50            audio_buffer = save_audio(processed_audio, sr)51            52            # Create download button53            st.audio(audio_buffer, format='audio/wav')54            55            # Add download button56            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 instructions67st.markdown("""68### Instructions:691. Upload a WAV or MP3 audio file702. Adjust the pitch slider (higher values = more feminine voice)713. Click 'Convert to Female Voice'724. Play the converted audio735. Download the result if satisfied74 75### Notes:76- Best results with clear audio input77- Recommended pitch adjustment: 6-8 for natural-sounding results78- Larger files may take longer to process79""")