Morse code

Revision as of 06:27, 24 August 2010 by rosettacode>Abu (New)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

According to Wikipedia, Morse code is one of the simplest and most versatile methods of telecommunication in existence. It has been in use for more than 160 years — longer than any other electronic encoding system.

Task
Morse code
You are encouraged to solve this task according to the task description, using any language you may know.

The task: Send a file as audible morse code to the PC speaker.

As the standard Morse code does not contain all possible characters, you may either ignore unknown characters in the file, or indicate them somehow (e.g. with a different pitch).

PicoLisp

The following simply uses the 'beep' pc-speaker beeper utility. <lang PicoLisp># *Morse *Dit *Dah

(balance '*Morse

  (mapcar
     '((L)
        (def (car L)
           (mapcar = (chop (cadr L)) '("." .)) ) )
     (quote
        ("!"  "---.")     ("\"" ".-..-.")   ("$" "...-..-")   ("'" ".----.")
        ("(" "-.--.")     (")" "-.--.-")    ("+" ".-.-.")     ("," "--..--")
        ("-" "-....-")    ("." ".-.-.-")    ("/" "-..-.")
        ("0" "-----")     ("1" ".----")     ("2" "..---")     ("3" "...--")
        ("4" "....-")     ("5" ".....")     ("6" "-....")     ("7" "--...")
        ("8" "---..")     ("9" "----.")
        (":" "---...")    (";" "-.-.-.")    ("=" "-...-")     ("?" "..--..")
        ("@" ".--.-.")
        ("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. Words per minute

(de wpm (N)

  (setq *Dit (*/ 1200 N)  *Dah (* 3 *Dit)) )

(wpm 20)

  1. Morse a single character

(de mrsChar (C)

  (cond
     ((sp? C) (wait (+ *Dah *Dit)))         # White space: Pause
     ((idx '*Morse (uppc C))                # Known character
        (for Flg (val (car @))
           (call "/usr/bin/beep" "-D" *Dit "-l" (if Flg *Dit *Dah)) ) )
     (T (call "/usr/bin/beep" "-f" 370)) )  # Unkown character
  (wait (- *Dah *Dit)) )
  1. Morse a complete file

(de morse (File)

  (in File
     (while (char)
        (mrsChar @) ) ) )</lang>