File size: 39,172 Bytes
c8a4052 10adbb1 f22bd8f 9e8347c f22bd8f 549ee96 9e8347c 549ee96 f22bd8f bff1887 f22bd8f 10adbb1 f22bd8f 10adbb1 c8a4052 10adbb1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 |
import gradio as gr
import numpy as np
import io
import tempfile
import os
from scipy.io import wavfile
import speech_recognition as sr
import soundfile as sf
# Morse code dictionary (ITU standard)
MORSE_CODE_DICT = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..',
'9': '----.', '0': '-----', ' ': '/'
}
MORSE_TO_CHAR = {v: k for k, v in MORSE_CODE_DICT.items()}
# Morse code timing (in seconds)
DIT_DURATION = 0.1 # 100ms for dit
DAH_DURATION = 3 * DIT_DURATION
SPACE_DURATION = 7 * DIT_DURATION
CHAR_SPACE = DIT_DURATION
SAMPLE_RATE = 44100
# Decode Morse from audio beeps
def decode_morse_from_audio(audio_data, is_file=False):
if audio_data is None:
return "", ""
if is_file:
sample_rate, data = wavfile.read(audio_data)
else:
sample_rate, data = audio_data
if len(data.shape) > 1:
data = data.mean(axis=1)
data = data / np.max(np.abs(data))
threshold = 0.1
signal = data > threshold
morse_code, decoded_text = "", ""
i = 0
while i < len(signal) - int(SAMPLE_RATE * DIT_DURATION):
if signal[i]:
start = i
while i < len(signal) and signal[i]:
i += 1
duration = (i - start) / sample_rate
morse_code += "-" if duration >= DAH_DURATION else "."
else:
start = i
while i < len(signal) and not signal[i]:
i += 1
pause = (i - start) / sample_rate
if pause >= SPACE_DURATION and morse_code:
decoded_text += " "
morse_code = ""
elif pause >= DIT_DURATION and morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
morse_code = ""
i += 1
if morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
return morse_code, decoded_text.strip()
# Decode Morse from text (e.g., ".... . .-.. .-.. ---")
def decode_morse_from_text(morse_text):
if not morse_text:
return ""
words = morse_text.split("/")
decoded_text = ""
for word in words:
chars = word.strip().split()
for char in chars:
decoded_text += MORSE_TO_CHAR.get(char, "?")
decoded_text += " "
return decoded_text.strip()
# Convert text to Morse code
def text_to_morse(text):
text = text.upper()
morse = " ".join(MORSE_CODE_DICT.get(char, "?") for char in text if char in MORSE_CODE_DICT)
return morse
# Generate Morse audio from Morse code string
def generate_morse_audio(morse):
audio = []
frequency = 750
for symbol in morse.split():
if symbol == "/":
audio.extend([0] * int(SAMPLE_RATE * SPACE_DURATION))
else:
for char in symbol:
duration = DAH_DURATION if char == "-" else DIT_DURATION
t = np.linspace(0, duration, int(SAMPLE_RATE * duration), False)
tone = 0.5 * np.sin(2 * np.pi * frequency * t)
audio.extend(tone)
audio.extend([0] * int(SAMPLE_RATE * DIT_DURATION))
audio.extend([0] * int(SAMPLE_RATE * CHAR_SPACE))
audio = np.array(audio, dtype=np.float32)
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
sf.write(temp_file.name, audio, SAMPLE_RATE, format="wav")
return temp_file.name
# Speech to text (for both plain speech and spoken Morse)
def speech_to_text(audio_path):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
try:
return recognizer.recognize_google(audio_data)
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError:
return "Speech recognition service unavailable"
# Highlight alphabet
def generate_alphabet_html(decoded_text):
html = "<div style='font-family: monospace; font-size: 16px;'>"
for char in MORSE_CODE_DICT.keys():
color = "red" if char in decoded_text.upper() else "black"
html += f"<span style='color: {color}; margin: 5px;'>{char}: {MORSE_CODE_DICT[char]}</span>"
if char in "AEIMQUZ":
html += "<br>"
html += "</div>"
return html
# Main processing function
def process_input(text=None, speech=None, live_audio=None, upload_audio=None, morse_text=None, morse_speech=None, morse_audio=None):
morse_output, text_output, audio_output = "", "", None
# Encoding: Text/Speech/Audio to Morse
if text: # Plain text to Morse
morse_output = text_to_morse(text)
text_output = text
audio_output = generate_morse_audio(morse_output)
elif speech: # Spoken text to Morse
transcribed = speech_to_text(speech)
morse_output = text_to_morse(transcribed)
text_output = transcribed
audio_output = generate_morse_audio(morse_output)
elif live_audio: # Live audio to Morse (assuming plain audio, not Morse)
# For simplicity, treat as Morse audio for decoding instead
morse_output, text_output = decode_morse_from_audio(live_audio, is_file=False)
elif upload_audio: # Uploaded audio to Morse (assuming plain audio, not Morse)
# For simplicity, treat as Morse audio for decoding instead
morse_output, text_output = decode_morse_from_audio(upload_audio, is_file=True)
# Decoding: Morse Text/Speech/Audio to Text
elif morse_text: # Morse text to plain text
morse_output = morse_text
text_output = decode_morse_from_text(morse_text)
audio_output = generate_morse_audio(morse_text)
elif morse_speech: # Spoken Morse (e.g., "dit dah") to text
transcribed = speech_to_text(morse_speech).replace("dit", ".").replace("dah", "-").replace("slash", "/")
morse_output = transcribed
text_output = decode_morse_from_text(transcribed)
audio_output = generate_morse_audio(transcribed)
elif morse_audio: # Morse audio file to text
morse_output, text_output = decode_morse_from_audio(morse_audio, is_file=True)
alphabet_html = generate_alphabet_html(text_output)
return morse_output, text_output, alphabet_html, audio_output
# Gradio UI
with gr.Blocks(title="Morse Code Converter") as demo:
gr.Markdown("# Morse Code Converter")
gr.Markdown("Encode text/speech to Morse, or decode Morse text/speech/audio to text!")
# Encode Section
with gr.Tab("Encode Text to Morse"):
text_input = gr.Textbox(label="Enter Text", placeholder="e.g., HELLO")
text_btn = gr.Button("Convert to Morse")
with gr.Row():
with gr.Column():
text_morse_out = gr.Textbox(label="Morse Code", interactive=False)
text_text_out = gr.Textbox(label="Original Text", interactive=False)
text_alphabet = gr.HTML(label="Alphabet (Highlighted)")
text_audio = gr.Audio(label="Morse Audio", interactive=False)
text_btn.click(fn=process_input, inputs=[text_input], outputs=[text_morse_out, text_text_out, text_alphabet, text_audio])
with gr.Tab("Encode Speech to Morse"):
speech_input = gr.Audio(type="filepath", label="Record Speech (e.g., say 'HELLO')")
speech_btn = gr.Button("Convert to Morse")
with gr.Row():
with gr.Column():
speech_morse_out = gr.Textbox(label="Morse Code", interactive=False)
speech_text_out = gr.Textbox(label="Transcribed Text", interactive=False)
speech_alphabet = gr.HTML(label="Alphabet (Highlighted)")
speech_audio = gr.Audio(label="Morse Audio", interactive=False)
speech_btn.click(fn=process_input, inputs=[speech_input], outputs=[speech_morse_out, speech_text_out, speech_alphabet, speech_audio])
# Decode Section
with gr.Tab("Decode Morse Text"):
morse_text_input = gr.Textbox(label="Enter Morse Text", placeholder="e.g., .... . .-.. .-.. ---")
morse_text_btn = gr.Button("Decode Morse")
with gr.Row():
with gr.Column():
morse_text_morse_out = gr.Textbox(label="Morse Code", interactive=False)
morse_text_text_out = gr.Textbox(label="Decoded Text", interactive=False)
morse_text_alphabet = gr.HTML(label="Alphabet (Highlighted)")
morse_text_audio = gr.Audio(label="Morse Audio", interactive=False)
morse_text_btn.click(fn=process_input, inputs=[morse_text_input], outputs=[morse_text_morse_out, morse_text_text_out, morse_text_alphabet, morse_text_audio])
with gr.Tab("Decode Morse Speech"):
morse_speech_input = gr.Audio(type="filepath", label="Record Morse Speech (e.g., say 'dit dit dit dit dit')")
morse_speech_btn = gr.Button("Decode Morse")
with gr.Row():
with gr.Column():
morse_speech_morse_out = gr.Textbox(label="Transcribed Morse", interactive=False)
morse_speech_text_out = gr.Textbox(label="Decoded Text", interactive=False)
morse_speech_alphabet = gr.HTML(label="Alphabet (Highlighted)")
morse_speech_audio = gr.Audio(label="Morse Audio", interactive=False)
morse_speech_btn.click(fn=process_input, inputs=[morse_speech_input], outputs=[morse_speech_morse_out, morse_speech_text_out, morse_speech_alphabet, morse_speech_audio])
with gr.Tab("Decode Morse Audio"):
morse_audio_input = gr.Audio(type="filepath", label="Upload Morse Audio (WAV of beeps)")
morse_audio_btn = gr.Button("Decode Morse")
with gr.Row():
with gr.Column():
morse_audio_morse_out = gr.Textbox(label="Detected Morse", interactive=False)
morse_audio_text_out = gr.Textbox(label="Decoded Text", interactive=False)
morse_audio_alphabet = gr.HTML(label="Alphabet (Highlighted)")
morse_audio_btn.click(fn=process_input, inputs=[morse_audio_input], outputs=[morse_audio_morse_out, morse_audio_text_out, morse_audio_alphabet, gr.Audio(visible=False)])
with gr.Tab("Decode Live Morse Audio"):
live_audio_input = gr.Audio(type="numpy", label="Live Morse Input (Use Microphone)", streaming=True)
with gr.Row():
with gr.Column():
live_morse_out = gr.Textbox(label="Detected Morse", interactive=False)
live_text_out = gr.Textbox(label="Decoded Text", interactive=False)
live_alphabet = gr.HTML(label="Alphabet (Highlighted)")
live_audio_input.stream(fn=process_input, inputs=[live_audio_input], outputs=[live_morse_out, live_text_out, live_alphabet, gr.Audio(visible=False)])
demo.launch()
'''
import gradio as gr
import numpy as np
import io
import tempfile
import os
from scipy.io import wavfile
import speech_recognition as sr
import soundfile as sf
# Morse code dictionary (ITU standard)
MORSE_CODE_DICT = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..',
'9': '----.', '0': '-----', ' ': '/'
}
MORSE_TO_CHAR = {v: k for k, v in MORSE_CODE_DICT.items()}
# Morse code timing (in seconds)
DIT_DURATION = 0.1 # 100ms for dit
DAH_DURATION = 3 * DIT_DURATION # 300ms for dah
SPACE_DURATION = 7 * DIT_DURATION # 700ms for word space
CHAR_SPACE = DIT_DURATION # Space between characters
SAMPLE_RATE = 44100 # Standard audio sample rate
# Decode Morse from audio (live or uploaded)
def decode_morse_from_audio(audio_data, is_file=False):
if audio_data is None:
return "", ""
if is_file:
# For uploaded files, audio_data is a file path
sample_rate, data = wavfile.read(audio_data)
else:
# For live streaming, audio_data is (sample_rate, data)
sample_rate, data = audio_data
if len(data.shape) > 1:
data = data.mean(axis=1)
data = data / np.max(np.abs(data))
threshold = 0.1
signal = data > threshold
morse_code, decoded_text = "", ""
i = 0
while i < len(signal) - int(SAMPLE_RATE * DIT_DURATION):
if signal[i]:
start = i
while i < len(signal) and signal[i]:
i += 1
duration = (i - start) / sample_rate
morse_code += "-" if duration >= DAH_DURATION else "."
else:
start = i
while i < len(signal) and not signal[i]:
i += 1
pause = (i - start) / sample_rate
if pause >= SPACE_DURATION and morse_code:
decoded_text += " "
morse_code = ""
elif pause >= DIT_DURATION and morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
morse_code = ""
i += 1
if morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
return morse_code, decoded_text.strip()
# Convert text to Morse code
def text_to_morse(text):
text = text.upper()
morse = " ".join(MORSE_CODE_DICT.get(char, "?") for char in text if char in MORSE_CODE_DICT)
return morse
# Generate Morse code audio and save to temporary file
def generate_morse_audio(morse):
audio = []
frequency = 750 # Hz for Morse tone
for symbol in morse.split():
if symbol == "/":
audio.extend([0] * int(SAMPLE_RATE * SPACE_DURATION))
else:
for char in symbol:
duration = DAH_DURATION if char == "-" else DIT_DURATION
t = np.linspace(0, duration, int(SAMPLE_RATE * duration), False)
tone = 0.5 * np.sin(2 * np.pi * frequency * t)
audio.extend(tone)
audio.extend([0] * int(SAMPLE_RATE * DIT_DURATION)) # Space between dits/dahs
audio.extend([0] * int(SAMPLE_RATE * CHAR_SPACE)) # Space between characters
audio = np.array(audio, dtype=np.float32)
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
sf.write(temp_file.name, audio, SAMPLE_RATE, format="wav")
temp_file_path = temp_file.name
return temp_file_path
# Speech to text
def speech_to_text(audio_path):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
try:
return recognizer.recognize_google(audio_data)
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError:
return "Speech recognition service unavailable"
# Highlight alphabet in UI
def generate_alphabet_html(decoded_text):
html = "<div style='font-family: monospace; font-size: 16px;'>"
for char in MORSE_CODE_DICT.keys():
color = "red" if char in decoded_text.upper() else "black"
html += f"<span style='color: {color}; margin: 5px;'>{char}: {MORSE_CODE_DICT[char]}</span>"
if char in "AEIMQUZ":
html += "<br>"
html += "</div>"
return html
# Combined processing function
def process_input(text=None, speech=None, live_audio=None, upload_audio=None):
morse, decoded_text, audio_output = "", "", None
if text: # Text input
morse = text_to_morse(text)
decoded_text = text
audio_output = generate_morse_audio(morse)
elif speech: # Speech input
text = speech_to_text(speech)
morse = text_to_morse(text)
decoded_text = text
audio_output = generate_morse_audio(morse)
elif live_audio: # Live audio input
morse, decoded_text = decode_morse_from_audio(live_audio, is_file=False)
elif upload_audio: # Uploaded audio file
morse, decoded_text = decode_morse_from_audio(upload_audio, is_file=True)
alphabet_html = generate_alphabet_html(decoded_text)
return morse, decoded_text, alphabet_html, audio_output
# Gradio UI with Blocks
with gr.Blocks(title="Morse Code Decoder & Generator") as demo:
gr.Markdown("# Morse Code Decoder & Generator")
gr.Markdown("Decode live or uploaded Morse audio, or generate Morse from text/speech!")
with gr.Tab("Decode Live Audio"):
audio_input = gr.Audio(type="numpy", label="Live Audio Input (Use Microphone)", streaming=True)
with gr.Row():
with gr.Column():
morse_output = gr.Textbox(label="Detected Morse Code", interactive=False)
text_output = gr.Textbox(label="Decoded Text", interactive=False)
alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
audio_input.stream(
fn=process_input,
inputs=[audio_input],
outputs=[morse_output, text_output, alphabet_display, gr.Audio(visible=False)],
)
with gr.Tab("Generate from Text"):
text_input = gr.Textbox(label="Enter Text", placeholder="Type here...")
generate_btn = gr.Button("Generate Morse")
with gr.Row():
with gr.Column():
morse_gen_output = gr.Textbox(label="Morse Code", interactive=False)
text_gen_output = gr.Textbox(label="Original Text", interactive=False)
gen_alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
audio_playback = gr.Audio(label="Morse Audio Playback", interactive=False)
generate_btn.click(
fn=process_input,
inputs=[text_input],
outputs=[morse_gen_output, text_gen_output, gen_alphabet_display, audio_playback]
)
with gr.Tab("Generate from Speech"):
speech_input = gr.Audio(type="filepath", label="Speak Your Text (Record Audio)")
speech_btn = gr.Button("Convert Speech to Morse")
with gr.Row():
with gr.Column():
morse_speech_output = gr.Textbox(label="Morse Code", interactive=False)
text_speech_output = gr.Textbox(label="Transcribed Text", interactive=False)
speech_alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
speech_audio_playback = gr.Audio(label="Morse Audio Playback", interactive=False)
speech_btn.click(
fn=process_input,
inputs=[speech_input],
outputs=[morse_speech_output, text_speech_output, speech_alphabet_display, speech_audio_playback]
)
with gr.Tab("Decode from Audio Upload"):
upload_input = gr.Audio(type="filepath", label="Upload Morse Audio File (WAV)")
upload_btn = gr.Button("Decode Morse")
with gr.Row():
with gr.Column():
morse_upload_output = gr.Textbox(label="Detected Morse Code", interactive=False)
text_upload_output = gr.Textbox(label="Decoded Text", interactive=False)
upload_alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
upload_btn.click(
fn=process_input,
inputs=[upload_input],
outputs=[morse_upload_output, text_upload_output, upload_alphabet_display, gr.Audio(visible=False)]
)
# Launch the app
demo.launch()
'''
'''
import gradio as gr
import numpy as np
import io
import tempfile
import os
from scipy.io import wavfile
import speech_recognition as sr
import soundfile as sf
# Morse code dictionary (ITU standard)
MORSE_CODE_DICT = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..',
'9': '----.', '0': '-----', ' ': '/'
}
MORSE_TO_CHAR = {v: k for k, v in MORSE_CODE_DICT.items()}
# Morse code timing (in seconds)
DIT_DURATION = 0.1 # 100ms for dit
DAH_DURATION = 3 * DIT_DURATION # 300ms for dah
SPACE_DURATION = 7 * DIT_DURATION # 700ms for word space
CHAR_SPACE = DIT_DURATION # Space between characters
SAMPLE_RATE = 44100 # Standard audio sample rate
# Decode Morse from audio
def decode_morse_from_audio(audio_data):
if audio_data is None:
return "", ""
sample_rate, data = audio_data
if len(data.shape) > 1:
data = data.mean(axis=1)
data = data / np.max(np.abs(data))
threshold = 0.1
signal = data > threshold
morse_code, decoded_text = "", ""
i = 0
while i < len(signal) - int(SAMPLE_RATE * DIT_DURATION):
if signal[i]:
start = i
while i < len(signal) and signal[i]:
i += 1
duration = (i - start) / sample_rate
morse_code += "-" if duration >= DAH_DURATION else "."
else:
start = i
while i < len(signal) and not signal[i]:
i += 1
pause = (i - start) / sample_rate
if pause >= SPACE_DURATION and morse_code:
decoded_text += " "
morse_code = ""
elif pause >= DIT_DURATION and morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
morse_code = ""
i += 1
if morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
return morse_code, decoded_text.strip()
# Convert text to Morse code
def text_to_morse(text):
text = text.upper()
morse = " ".join(MORSE_CODE_DICT.get(char, "?") for char in text if char in MORSE_CODE_DICT)
return morse
# Generate Morse code audio and save to temporary file
def generate_morse_audio(morse):
audio = []
frequency = 750 # Hz for Morse tone
for symbol in morse.split():
if symbol == "/":
audio.extend([0] * int(SAMPLE_RATE * SPACE_DURATION))
else:
for char in symbol:
duration = DAH_DURATION if char == "-" else DIT_DURATION
t = np.linspace(0, duration, int(SAMPLE_RATE * duration), False)
tone = 0.5 * np.sin(2 * np.pi * frequency * t)
audio.extend(tone)
audio.extend([0] * int(SAMPLE_RATE * DIT_DURATION)) # Space between dits/dahs
audio.extend([0] * int(SAMPLE_RATE * CHAR_SPACE)) # Space between characters
audio = np.array(audio, dtype=np.float32)
# Save to a temporary file
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
sf.write(temp_file.name, audio, SAMPLE_RATE, format="wav")
temp_file_path = temp_file.name
return temp_file_path
# Speech to text
def speech_to_text(audio_path):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
try:
return recognizer.recognize_google(audio_data)
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError:
return "Speech recognition service unavailable"
# Highlight alphabet in UI
def generate_alphabet_html(decoded_text):
html = "<div style='font-family: monospace; font-size: 16px;'>"
for char in MORSE_CODE_DICT.keys():
color = "red" if char in decoded_text.upper() else "black"
html += f"<span style='color: {color}; margin: 5px;'>{char}: {MORSE_CODE_DICT[char]}</span>"
if char in "AEIMQUZ":
html += "<br>"
html += "</div>"
return html
# Combined processing function with cleanup
def process_input(text=None, speech=None, audio=None):
morse, decoded_text, audio_output = "", "", None
if text: # Text input
morse = text_to_morse(text)
decoded_text = text
audio_output = generate_morse_audio(morse)
elif speech: # Speech input
text = speech_to_text(speech)
morse = text_to_morse(text)
decoded_text = text
audio_output = generate_morse_audio(morse)
elif audio: # Live audio input
morse, decoded_text = decode_morse_from_audio(audio)
alphabet_html = generate_alphabet_html(decoded_text)
return morse, decoded_text, alphabet_html, audio_output
# Gradio UI with Blocks
with gr.Blocks(title="Morse Code Decoder & Generator") as demo:
gr.Markdown("# Morse Code Decoder & Generator")
gr.Markdown("Decode live Morse audio, or generate Morse from text/speech!")
with gr.Tab("Decode Live Audio"):
audio_input = gr.Audio(type="numpy", label="Live Audio Input (Use Microphone)", streaming=True)
with gr.Row():
with gr.Column():
morse_output = gr.Textbox(label="Detected Morse Code", interactive=False)
text_output = gr.Textbox(label="Decoded Text", interactive=False)
alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
audio_input.stream(
fn=process_input,
inputs=[audio_input],
outputs=[morse_output, text_output, alphabet_display, gr.Audio(visible=False)],
)
with gr.Tab("Generate from Text"):
text_input = gr.Textbox(label="Enter Text", placeholder="Type here...")
generate_btn = gr.Button("Generate Morse")
with gr.Row():
with gr.Column():
morse_gen_output = gr.Textbox(label="Morse Code", interactive=False)
text_gen_output = gr.Textbox(label="Original Text", interactive=False)
gen_alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
audio_playback = gr.Audio(label="Morse Audio Playback", interactive=False)
generate_btn.click(
fn=process_input,
inputs=[text_input],
outputs=[morse_gen_output, text_gen_output, gen_alphabet_display, audio_playback]
)
with gr.Tab("Generate from Speech"):
speech_input = gr.Audio(type="filepath", label="Speak Your Text (Record Audio)")
speech_btn = gr.Button("Convert Speech to Morse")
with gr.Row():
with gr.Column():
morse_speech_output = gr.Textbox(label="Morse Code", interactive=False)
text_speech_output = gr.Textbox(label="Transcribed Text", interactive=False)
speech_alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
speech_audio_playback = gr.Audio(label="Morse Audio Playback", interactive=False)
speech_btn.click(
fn=process_input,
inputs=[speech_input],
outputs=[morse_speech_output, text_speech_output, speech_alphabet_display, speech_audio_playback]
)
# Launch the app
demo.launch()
'''
'''
import gradio as gr
import numpy as np
import io
from scipy.io import wavfile
import speech_recognition as sr
import soundfile as sf
# Morse code dictionary (ITU standard)
MORSE_CODE_DICT = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..',
'9': '----.', '0': '-----', ' ': '/'
}
MORSE_TO_CHAR = {v: k for k, v in MORSE_CODE_DICT.items()}
# Morse code timing (in seconds)
DIT_DURATION = 0.1 # 100ms for dit
DAH_DURATION = 3 * DIT_DURATION # 300ms for dah
SPACE_DURATION = 7 * DIT_DURATION # 700ms for word space
CHAR_SPACE = DIT_DURATION # Space between characters
SAMPLE_RATE = 44100 # Standard audio sample rate
# Decode Morse from audio
def decode_morse_from_audio(audio_data):
if audio_data is None:
return "", ""
sample_rate, data = audio_data
if len(data.shape) > 1:
data = data.mean(axis=1)
data = data / np.max(np.abs(data))
threshold = 0.1
signal = data > threshold
morse_code, decoded_text = "", ""
i = 0
while i < len(signal) - int(SAMPLE_RATE * DIT_DURATION):
if signal[i]:
start = i
while i < len(signal) and signal[i]:
i += 1
duration = (i - start) / sample_rate
morse_code += "-" if duration >= DAH_DURATION else "."
else:
start = i
while i < len(signal) and not signal[i]:
i += 1
pause = (i - start) / sample_rate
if pause >= SPACE_DURATION and morse_code:
decoded_text += " "
morse_code = ""
elif pause >= DIT_DURATION and morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
morse_code = ""
i += 1
if morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
return morse_code, decoded_text.strip()
# Convert text to Morse code
def text_to_morse(text):
text = text.upper()
morse = " ".join(MORSE_CODE_DICT.get(char, "?") for char in text if char in MORSE_CODE_DICT)
return morse
# Generate Morse code audio
def generate_morse_audio(morse):
audio = []
frequency = 750 # Hz for Morse tone
for symbol in morse.split():
if symbol == "/":
audio.extend([0] * int(SAMPLE_RATE * SPACE_DURATION))
else:
for char in symbol:
duration = DAH_DURATION if char == "-" else DIT_DURATION
t = np.linspace(0, duration, int(SAMPLE_RATE * duration), False)
tone = 0.5 * np.sin(2 * np.pi * frequency * t)
audio.extend(tone)
audio.extend([0] * int(SAMPLE_RATE * DIT_DURATION)) # Space between dits/dahs
audio.extend([0] * int(SAMPLE_RATE * CHAR_SPACE)) # Space between characters
audio = np.array(audio, dtype=np.float32)
buffer = io.BytesIO()
sf.write(buffer, audio, SAMPLE_RATE, format="wav")
buffer.seek(0)
return buffer
# Speech to text
def speech_to_text(audio_path):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
try:
return recognizer.recognize_google(audio_data)
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError:
return "Speech recognition service unavailable"
# Highlight alphabet in UI
def generate_alphabet_html(decoded_text):
html = "<div style='font-family: monospace; font-size: 16px;'>"
for char in MORSE_CODE_DICT.keys():
color = "red" if char in decoded_text.upper() else "black"
html += f"<span style='color: {color}; margin: 5px;'>{char}: {MORSE_CODE_DICT[char]}</span>"
if char in "AEIMQUZ":
html += "<br>"
html += "</div>"
return html
# Combined processing function
def process_input(text=None, speech=None, audio=None):
morse, decoded_text, audio_output = "", "", None
if text: # Text input
morse = text_to_morse(text)
decoded_text = text
audio_output = generate_morse_audio(morse)
elif speech: # Speech input
text = speech_to_text(speech)
morse = text_to_morse(text)
decoded_text = text
audio_output = generate_morse_audio(morse)
elif audio: # Live audio input
morse, decoded_text = decode_morse_from_audio(audio)
alphabet_html = generate_alphabet_html(decoded_text)
return morse, decoded_text, alphabet_html, audio_output
# Gradio UI with Blocks
with gr.Blocks(title="Morse Code Decoder & Generator") as demo:
gr.Markdown("# Morse Code Decoder & Generator")
gr.Markdown("Decode live Morse audio, or generate Morse from text/speech!")
with gr.Tab("Decode Live Audio"):
audio_input = gr.Audio(type="numpy", label="Live Audio Input (Use Microphone)", streaming=True)
with gr.Row():
with gr.Column():
morse_output = gr.Textbox(label="Detected Morse Code", interactive=False)
text_output = gr.Textbox(label="Decoded Text", interactive=False)
alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
audio_input.stream(
fn=process_input,
inputs=[audio_input],
outputs=[morse_output, text_output, alphabet_display, gr.Audio(visible=False)],
)
with gr.Tab("Generate from Text"):
text_input = gr.Textbox(label="Enter Text", placeholder="Type here...")
generate_btn = gr.Button("Generate Morse")
with gr.Row():
with gr.Column():
morse_gen_output = gr.Textbox(label="Morse Code", interactive=False)
text_gen_output = gr.Textbox(label="Original Text", interactive=False)
gen_alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
audio_playback = gr.Audio(label="Morse Audio Playback", interactive=False)
generate_btn.click(
fn=process_input,
inputs=[text_input],
outputs=[morse_gen_output, text_gen_output, gen_alphabet_display, audio_playback]
)
with gr.Tab("Generate from Speech"):
speech_input = gr.Audio(type="filepath", label="Speak Your Text (Record Audio)")
speech_btn = gr.Button("Convert Speech to Morse")
with gr.Row():
with gr.Column():
morse_speech_output = gr.Textbox(label="Morse Code", interactive=False)
text_speech_output = gr.Textbox(label="Transcribed Text", interactive=False)
speech_alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
speech_audio_playback = gr.Audio(label="Morse Audio Playback", interactive=False)
speech_btn.click(
fn=process_input,
inputs=[speech_input],
outputs=[morse_speech_output, text_speech_output, speech_alphabet_display, speech_audio_playback]
)
# Launch the app
demo.launch()
'''
'''
import gradio as gr
import numpy as np
import time
from scipy.io import wavfile
import io
# Morse code dictionary (ITU standard)
MORSE_CODE_DICT = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..',
'9': '----.', '0': '-----', ' ': ' '
}
# Reverse dictionary for decoding
MORSE_TO_CHAR = {v: k for k, v in MORSE_CODE_DICT.items()}
# Function to process audio and detect Morse code (simplified)
def decode_morse_from_audio(audio_data):
if audio_data is None:
return "", ""
# Audio comes as (sample_rate, data) from Gradio
sample_rate, data = audio_data
if len(data.shape) > 1: # Convert stereo to mono if needed
data = data.mean(axis=1)
# Normalize audio data
data = data / np.max(np.abs(data))
# Threshold for detecting signal (tweak as needed)
threshold = 0.1
signal = data > threshold
# Timing parameters (in samples, adjust based on sample_rate)
dit_length = int(sample_rate * 0.1) # 100ms for a dit
dah_length = dit_length * 3 # 300ms for a dah
space_length = dit_length * 7 # 700ms for word space
morse_code = ""
decoded_text = ""
i = 0
while i < len(signal) - dit_length:
if signal[i]:
# Measure signal duration
start = i
while i < len(signal) and signal[i]:
i += 1
duration = i - start
# Classify as dit or dah
if duration >= dah_length:
morse_code += "-"
elif duration >= dit_length:
morse_code += "."
# Check for spaces (pauses)
else:
start = i
while i < len(signal) and not signal[i]:
i += 1
pause = i - start
if pause >= space_length:
if morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
decoded_text += " "
morse_code = ""
elif pause >= dit_length and morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
morse_code = ""
i += 1
# Handle any remaining code
if morse_code:
decoded_text += MORSE_TO_CHAR.get(morse_code, "?")
return morse_code, decoded_text.strip()
# Function to generate highlighted alphabet UI
def generate_alphabet_html(decoded_text):
html = "<div style='font-family: monospace; font-size: 16px;'>"
for char in MORSE_CODE_DICT.keys():
color = "red" if char in decoded_text.upper() else "black"
html += f"<span style='color: {color}; margin: 5px;'>{char}: {MORSE_CODE_DICT[char]}</span>"
if char in "AEIMQUZ":
html += "<br>"
html += "</div>"
return html
# Gradio streaming function
def stream_morse_decoder(audio):
morse, text = decode_morse_from_audio(audio)
alphabet_html = generate_alphabet_html(text)
return morse, text, alphabet_html
# Gradio UI with Blocks
with gr.Blocks(title="Morse Code Decoder") as demo:
gr.Markdown("# Morse Code Decoder")
gr.Markdown("Speak or play Morse code into your microphone to decode it live!")
with gr.Row():
audio_input = gr.Audio(source="microphone", type="numpy", streaming=True, label="Live Audio Input")
with gr.Row():
with gr.Column():
morse_output = gr.Textbox(label="Detected Morse Code", interactive=False)
text_output = gr.Textbox(label="Decoded Text", interactive=False)
with gr.Column():
alphabet_display = gr.HTML(label="Morse Alphabet (Highlighted)")
# Event listener for streaming audio
audio_input.stream(
fn=stream_morse_decoder,
inputs=[audio_input],
outputs=[morse_output, text_output, alphabet_display],
_js="() => { return [navigator.mediaDevices.getUserMedia({ audio: true })];"
)
# Launch the app
demo.launch()
''' |