1 # DAH should be three DOTs.
2 # Space between DOTs and DAHs should be one DOT.
3 # Space between two letters should be one DAH.
4 # Space between two words should be DOT DAH DAH.
6 import sys
, math
, audiodev
10 OCTAVE
= 2 # 1 == 441 Hz, 2 == 882 Hz, ...
14 'B': '-...', 'b': '-...',
15 'C': '-.-.', 'c': '-.-.',
16 'D': '-..', 'd': '-..',
18 'F': '..-.', 'f': '..-.',
19 'G': '--.', 'g': '--.',
20 'H': '....', 'h': '....',
22 'J': '.---', 'j': '.---',
23 'K': '-.-', 'k': '-.-',
24 'L': '.-..', 'l': '.-..',
27 'O': '---', 'o': '---',
28 'P': '.--.', 'p': '.--.',
29 'Q': '--.-', 'q': '--.-',
30 'R': '.-.', 'r': '.-.',
31 'S': '...', 's': '...',
33 'U': '..-', 'u': '..-',
34 'V': '...-', 'v': '...-',
35 'W': '.--', 'w': '.--',
36 'X': '-..-', 'x': '-..-',
37 'Y': '-.--', 'y': '-.--',
38 'Z': '--..', 'z': '--..',
63 # If we play at 44.1 kHz (which we do), then if we produce one sine
64 # wave in 100 samples, we get a tone of 441 Hz. If we produce two
65 # sine waves in these 100 samples, we get a tone of 882 Hz. 882 Hz
66 # appears to be a nice one for playing morse code.
68 global sinewave
, nowave
71 val
= int(math
.sin(math
.pi
* float(i
) * octave
/ 50.0) * 30000)
72 sinewave
= sinewave
+ chr((val
>> 8) & 255) + chr(val
& 255)
80 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'o:p:')
82 sys
.stderr
.write('Usage ' + sys
.argv
[0] +
83 ' [ -o outfile ] [ args ] ...\n')
89 dev
= aifc
.open(a
, 'w')
90 dev
.setframerate(44100)
94 mkwave(string
.atoi(a
))
97 dev
= audiodev
.AudioDev()
102 dev
.writeframesraw
= dev
.writeframes
104 line
= string
.join(args
)
106 line
= sys
.stdin
.readline()
110 if hasattr(dev
, 'wait'):
113 line
= sys
.stdin
.readline()
118 # Convert a string to morse code with \001 between the characters in
124 res
= res
+ morsetab
[c
] + '\001'
129 # Play a line of morse code.
137 pause(dev
, DAH
+ DOT
)
140 def sine(dev
, length
):
141 for i
in range(length
):
142 dev
.writeframesraw(sinewave
)
144 def pause(dev
, length
):
145 for i
in range(length
):
146 dev
.writeframesraw(nowave
)
148 if __name__
== '__main__' or sys
.argv
[0] == __name__
: