morse-to-audio / app.py
Blane187's picture
Update app.py
9efabee verified
import gradio as gr
from pydub import AudioSegment
from pydub.generators import Sine
import tempfile
def morse_to_audio(morse_code, frequency=700, dot_duration=100):
# Morse code dictionary
morse_dict = {
'.': dot_duration,
'-': dot_duration * 3,
' ': dot_duration * 7 # space between words
}
# Create an empty audio segment
audio = AudioSegment.silent(duration=0)
# Generate the audio for each symbol
for symbol in morse_code:
if symbol in morse_dict:
duration = morse_dict[symbol]
if symbol != ' ':
tone = Sine(frequency).to_audio_segment(duration=duration)
silence = AudioSegment.silent(duration=dot_duration) # silence between dots/dashes
audio += tone + silence
else:
audio += AudioSegment.silent(duration=duration) # silence between words
# Save the audio to a temporary file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
audio.export(temp_file.name, format="wav")
return temp_file.name
with gr.Blocks(title="MTA", theme="nevreal/blues") as demo:
gr.Markdown(" # Morse To Audio")
with gr.Column():
morse_input = gr.Textbox(label="Enter Morse Code")
freq_slider = gr.Slider(minimum=300, maximum=1000, value=700, label="Frequency (Hz)")
dot_duration_slider = gr.Slider(minimum=50, maximum=500, value=100, label="Dot Duration (ms)")
convert_button = gr.Button("Convert to Audio")
with gr.Column():
audio_output = gr.Audio(label="Morse Code Audio")
convert_button.click(morse_to_audio, inputs=[morse_input, freq_slider, dot_duration_slider], outputs=audio_output)
demo.launch()