import streamlit as st import librosa import numpy as np import soundfile as sf import tempfile import matplotlib.pyplot as plt st.set_page_config(page_title="AudioShift", layout="centered") st.title("🎵 AudioShift") st.caption("Hybrid humanization with entropy validation") # ---------- Utility: Spectral Entropy ---------- def spectral_entropy(audio, sr, hop=1024, n_fft=2048): S = np.abs(librosa.stft(audio, n_fft=n_fft, hop_length=hop)) + 1e-9 S_norm = S / np.sum(S, axis=0, keepdims=True) entropy = -np.sum(S_norm * np.log2(S_norm), axis=0) return entropy # ---------- Upload ---------- uploaded = st.file_uploader("Upload audio (WAV or MP3)", type=["wav", "mp3"]) if uploaded: audio, sr = librosa.load(uploaded, mono=True) st.subheader("Controls") style = st.selectbox("Style", ["Lo‑Fi", "Rock", "Jazz", "EDM"]) pitch = st.slider("Pitch shift (semitones)", -6, 6, 0) human_blend = st.slider( "AI → Human blend", 0, 100, 65, help="Higher values add natural timing, dynamics, and texture variation" ) # ---------- Style processing ---------- if style == "Lo‑Fi": audio = librosa.effects.time_stretch(audio, 0.9) elif style == "Rock": audio = librosa.effects.time_stretch(audio, 1.05) elif style == "EDM": audio = librosa.effects.time_stretch(audio, 1.1) audio = librosa.effects.pitch_shift(audio, sr, pitch) # ---------- Entropy BEFORE ---------- entropy_before = spectral_entropy(audio, sr) # ---------- Humanization Engine (Hybrid, Bounded) ---------- blend = human_blend / 100.0 # 1) Temporal micro‑drift time_variation = np.random.normal( 1.0, 0.005 + 0.012 * blend, len(audio) ) # 2) Dynamic envelope breathing amp_variation = np.random.normal( 1.0, 0.01 + 0.03 * blend, len(audio) ) audio = audio * time_variation * amp_variation # 3) Spectral texture decoherence stft = librosa.stft(audio) mag, phase = np.abs(stft), np.angle(stft) texture = np.random.normal( 1.0, 0.002 + 0.008 * blend, mag.shape ) mag = mag * texture audio = librosa.istft(mag * np.exp(1j * phase)) # Normalize safely audio = audio / (np.max(np.abs(audio)) + 1e-9) # ---------- Entropy AFTER ---------- entropy_after = spectral_entropy(audio, sr) # ---------- Save output ---------- tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav") sf.write(tmp.name, audio, sr) # ---------- Output ---------- st.subheader("Result") st.audio(tmp.name) st.download_button( "Download AudioShift Output", tmp.name, file_name="audioshift_output.wav" ) # ---------- Entropy Plot ---------- st.subheader("Spectral Entropy (Validation)") fig, ax = plt.subplots() ax.plot(entropy_before, label="Before (more AI‑stable)", alpha=0.8) ax.plot(entropy_after, label="After (human‑leaning hybrid)", alpha=0.9) ax.set_xlabel("Time window") ax.set_ylabel("Spectral entropy") ax.legend() ax.grid(True) st.pyplot(fig)