I wrote this little hack to convert text into International Morse
Code. Apparently sometime in the last 8 years, /dev/audio went away,
so it writes to /dev/dsp.
#!/usr/bin/python
import sys, string
round = lambda afloat: int(afloat + 0.5)
#wave = '@[EMAIL PROTECTED]'
wave = 'Samuel F. B. Morse\n'
emptywave = ' \n'
ditlen = 0.0468 # seconds; 0.05 sec is 22.5 wpm, so 53 * ditlen is the time
for a word.
# 24wpm was the old standard for a "radio certificate for the merchant navy"
apparently
# and 5wpm is where people start
# and 65wpm seems to be about the limit
#ditlen = 0.0124 # 90wpm; you can hear why this is difficult
#(ditlen, wave, emptywave) = (0.0124, '~\n', '\n')
#ditlen = 0.224 # 5wpm
# note that this assumes your /dev/dsp is set to ISDN standards
bytes_per_sec = 8000
dit = wave * round(ditlen * bytes_per_sec / len(wave))
space = emptywave * round(ditlen * bytes_per_sec / len(emptywave))
dah = dit * 3
letterspace = space * 3
# wordspace gets spaces on each side, so it's really 7 dit-times
wordspace = space * 5
morsechart = {
'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': '-----', ':': '---...'}
soundchart = { '.': dit, '-': dah }
def charsound(char):
if char == ' ': return wordspace
if char in string.ascii_uppercase: return charsound(char.lower())
return space.join([soundchart[item] for item in morsechart[char]])
def strsound(astr):
return letterspace.join([charsound(char) for char in astr])
def main(argv):
out = file('/dev/dsp', 'w')
out.write(strsound(' '.join(argv[1:]) or
'Ken a Tartar entente art. Tar a tent tart. Tar ten.'))
if __name__ == '__main__': main(sys.argv)
I called the above module 'morse.py', and wrote this little script to
find words consisting entirely of alternating dashes and dots:
#!/usr/bin/python
import morse, sys
# Filter to find input words that consist only of alternating dots and
# dashes.
def is_iambic(word):
last_sound = None
for char in word:
sounds = morse.morsechart.get(char)
if sounds is None: return False
if '--' in sounds or '..' in sounds: return False
if sounds[0] == last_sound: return False
last_sound = sounds[-1]
return True
def main():
while 1:
line = sys.stdin.readline() # 'for line in sys.stdin' interactive: bad
if not line: return
if line.endswith('\n'): line = line[:-1]
if is_iambic(line): print line
if __name__ == '__main__': main()