3 # DAH should be three DOTs.
4 # Space between DOTs and DAHs should be one DOT.
5 # Space between two letters should be one DAH.
6 # Space between two words should be DOT DAH DAH.
8 import sys
, math
, audiodev
12 OCTAVE
= 2 # 1 == 441 Hz, 2 == 882 Hz, ...
16 'B': '-...', 'b': '-...',
17 'C': '-.-.', 'c': '-.-.',
18 'D': '-..', 'd': '-..',
20 'F': '..-.', 'f': '..-.',
21 'G': '--.', 'g': '--.',
22 'H': '....', 'h': '....',
24 'J': '.---', 'j': '.---',
25 'K': '-.-', 'k': '-.-',
26 'L': '.-..', 'l': '.-..',
29 'O': '---', 'o': '---',
30 'P': '.--.', 'p': '.--.',
31 'Q': '--.-', 'q': '--.-',
32 'R': '.-.', 'r': '.-.',
33 'S': '...', 's': '...',
35 'U': '..-', 'u': '..-',
36 'V': '...-', 'v': '...-',
37 'W': '.--', 'w': '.--',
38 'X': '-..-', 'x': '-..-',
39 'Y': '-.--', 'y': '-.--',
40 'Z': '--..', 'z': '--..',
41 '0': '-----', ',': '--..--',
42 '1': '.----', '.': '.-.-.-',
43 '2': '..---', '?': '..--..',
44 '3': '...--', ';': '-.-.-.',
45 '4': '....-', ':': '---...',
46 '5': '.....', "'": '.----.',
47 '6': '-....', '-': '-....-',
48 '7': '--...', '/': '-..-.',
49 '8': '---..', '(': '-.--.-',
50 '9': '----.', ')': '-.--.-',
51 ' ': ' ', '_': '..--.-',
56 # If we play at 44.1 kHz (which we do), then if we produce one sine
57 # wave in 100 samples, we get a tone of 441 Hz. If we produce two
58 # sine waves in these 100 samples, we get a tone of 882 Hz. 882 Hz
59 # appears to be a nice one for playing morse code.
63 val
= int(math
.sin(math
.pi
* i
* octave
/ 50.0) * 30000)
64 sinewave
+= chr((val
>> 8) & 255) + chr(val
& 255)
67 defaultwave
= mkwave(OCTAVE
)
72 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'o:p:')
74 sys
.stderr
.write('Usage ' + sys
.argv
[0] +
75 ' [ -o outfile ] [ -p octave ] [ words ] ...\n')
82 dev
= aifc
.open(a
, 'w')
83 dev
.setframerate(44100)
90 dev
= audiodev
.AudioDev()
95 dev
.writeframesraw
= dev
.writeframes
97 source
= [' '.join(args
)]
99 source
= iter(sys
.stdin
.readline
, '')
102 play(mline
, dev
, wave
)
103 if hasattr(dev
, 'wait'):
107 # Convert a string to morse code with \001 between the characters in
113 res
+= morsetab
[c
] + '\001'
118 # Play a line of morse code.
119 def play(line
, dev
, wave
):
126 pause(dev
, DAH
+ DOT
)
129 def sine(dev
, length
, wave
):
130 for i
in range(length
):
131 dev
.writeframesraw(wave
)
133 def pause(dev
, length
):
134 for i
in range(length
):
135 dev
.writeframesraw(nowave
)
137 if __name__
== '__main__':