Spaces:
Running
Running
import numpy as np | |
import gradio as gr | |
from scipy.io.wavfile import write | |
from pydub import AudioSegment | |
# Define the core Animalese function | |
def animalese_synthesize(text, shorten=False, pitch=1.0): | |
def shorten_word(word): | |
if len(word) > 1: | |
return word[0] + word[-1] | |
return word | |
# Shorten words if the option is enabled | |
processed_script = text | |
if shorten: | |
processed_script = ''.join([shorten_word(w) for w in text.split()]) | |
# Generate the waveform (This is a simplified version) | |
sample_rate = 44100 | |
data = [] | |
library_letter_secs = 0.15 | |
library_samples_per_letter = int(library_letter_secs * sample_rate) | |
output_letter_secs = 0.075 | |
output_samples_per_letter = int(output_letter_secs * sample_rate) | |
# Use a basic sine wave to simulate the sounds | |
for c in processed_script.upper(): | |
if 'A' <= c <= 'Z': | |
freq = 440 + (ord(c) - ord('A')) * 10 # Simple mapping A-Z to frequencies | |
t = np.linspace(0, output_letter_secs, output_samples_per_letter, False) | |
wave = 0.5 * np.sin(2 * np.pi * freq * t * pitch) | |
data.extend(wave) | |
else: | |
data.extend([0] * output_samples_per_letter) | |
# Convert to numpy array and save as WAV | |
data = np.array(data, dtype=np.float32) | |
scaled = np.int16(data/np.max(np.abs(data)) * 32767) | |
return scaled, sample_rate | |
def generate_wav_file(text, shorten, pitch): | |
data, sample_rate = animalese_synthesize(text, shorten, pitch) | |
output_file = "animalese_output.wav" | |
write(output_file, sample_rate, data) | |
return output_file | |
# Define the Gradio interface | |
def preview_audio(text, shorten, pitch): | |
output_file = generate_wav_file(text, shorten, pitch) | |
return output_file | |
gr_interface = gr.Interface( | |
fn=preview_audio, | |
inputs=[ | |
gr.Textbox(label="Text to Synthesize"), | |
gr.Checkbox(label="Shorten Words"), | |
gr.Slider(0.2, 2.0, step=0.1, label="Pitch", value=1.0) | |
], | |
outputs=gr.Audio(label="Preview Animalese Audio") | |
) | |
if __name__ == "__main__": | |
gr_interface.launch() | |