1 import sys
, math
, audiodev
5 OCTAVE
= 2 # 1 == 441 Hz, 2 == 882 Hz, ...
13 'B': '-...', 'b': '-...',
14 'C': '-.-.', 'c': '-.-.',
15 'D': '-..', 'd': '-..',
17 'F': '..-.', 'f': '..-.',
18 'G': '--.', 'g': '--.',
19 'H': '....', 'h': '....',
21 'J': '.---', 'j': '.---',
22 'K': '-.-', 'k': '-.-',
23 'L': '.-..', 'l': '.-..',
26 'O': '---', 'o': '---',
27 'P': '.--.', 'p': '.--.',
28 'Q': '--.-', 'q': '--.-',
29 'R': '.-.', 'r': '.-.',
30 'S': '...', 's': '...',
32 'U': '..-', 'u': '..-',
33 'V': '...-', 'v': '...-',
34 'W': '.--', 'w': '.--',
35 'X': '-..-', 'x': '-..-',
36 'Y': '-.--', 'y': '-.--',
37 'Z': '--..', 'z': '--..',
62 # If we play at 44.1 kHz (which we do), then if we produce one sine
63 # wave in 100 samples, we get a tone of 441 Hz. If we produce two
64 # sine waves in these 100 samples, we get a tone of 882 Hz. 882 Hz
65 # appears to be a nice one for playing morse code.
67 global sinewave
, nowave
69 n
= int(FRAMERATE
/ BASEFREQ
)
71 val
= int(math
.sin(2 * math
.pi
* i
* octave
/ n
) * 0x7fff)
72 sample
= chr((val
>> 8) & 255) + chr(val
& 255)
73 sinewave
= sinewave
+ sample
[:SAMPWIDTH
]
74 nowave
= '\0' * (n
*SAMPWIDTH
)
78 class BufferedAudioDev
:
79 def __init__(self
, *args
):
81 self
._base
= apply(audiodev
.AudioDev
, args
)
84 self
._addmethods
(self
._base
, self
._base
.__class
__)
85 def _addmethods(self
, inst
, cls
):
86 for name
in cls
.__dict
__.keys():
87 if not hasattr(self
, name
):
89 setattr(self
, name
, getattr(inst
, name
))
92 for basecls
in cls
.__bases
__:
93 self
._addmethods
(self
, inst
, basecls
)
94 def writeframesraw(self
, frames
):
95 self
._buffer
.append(frames
)
96 self
._filled
= self
._filled
+ len(frames
)
97 if self
._filled
>= QSIZE
:
103 print 'flush: %d blocks, %d bytes' % (len(self
._buffer
), self
._filled
)
106 self
._base
.writeframes(string
.joinfields(self
._buffer
, ''))
110 def main(args
= sys
.argv
[1:]):
111 import getopt
, string
113 opts
, args
= getopt
.getopt(args
, 'o:p:')
115 sys
.stderr
.write('Usage ' + sys
.argv
[0] +
116 ' [ -o outfile ] [ args ] ...\n')
122 dev
= aifc
.open(a
, 'w')
123 dev
.setframerate(FRAMERATE
)
124 dev
.setsampwidth(SAMPWIDTH
)
127 mkwave(string
.atoi(a
))
129 dev
= BufferedAudioDev()
130 dev
.setoutrate(FRAMERATE
)
131 dev
.setsampwidth(SAMPWIDTH
)
135 line
= string
.join(args
)
137 line
= sys
.stdin
.readline()
143 if hasattr(dev
, 'wait'):
146 line
= sys
.stdin
.readline()
151 # Convert a string to morse code with \001 between the characters in
157 res
= res
+ morsetab
[c
] + '\001'
162 # Play a line of morse code.
173 def sine(dev
, length
):
174 dev
.writeframesraw(sinewave
*length
)
176 def pause(dev
, length
):
177 dev
.writeframesraw(nowave
*length
)
179 if __name__
== '__main__' or sys
.argv
[0] == __name__
: