-
Notifications
You must be signed in to change notification settings - Fork 203
/
send.py
66 lines (55 loc) · 1.94 KB
/
send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
import pyaudio
import quietnet
import options
import psk
FORMAT = pyaudio.paInt16
CHANNELS = options.channels
RATE = options.rate
FREQ = options.freq
FREQ_OFF = 0
FRAME_LENGTH = options.frame_length
DATASIZE = options.datasize
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, output=True)
user_input = input if sys.version_info.major >= 3 else raw_input
def make_buffer_from_bit_pattern(pattern, on_freq, off_freq):
""" Takes a pattern and returns an audio buffer that encodes that pattern """
# the key's middle value is the bit's value and the left and right bits are the bits before and after
# the buffers are enveloped to cleanly blend into each other
last_bit = pattern[-1]
output_buffer = []
offset = 0
for i in range(len(pattern)):
bit = pattern[i]
if i < len(pattern) - 1:
next_bit = pattern[i 1]
else:
next_bit = pattern[0]
freq = on_freq if bit == '1' else off_freq
tone = quietnet.tone(freq, DATASIZE, offset=offset)
output_buffer = quietnet.envelope(tone, left=last_bit=='0', right=next_bit=='0')
offset = DATASIZE
last_bit = bit
return quietnet.pack_buffer(output_buffer)
def play_buffer(buffer):
output = ''.join(buffer)
stream.write(output)
if __name__ == "__main__":
print("Welcome to quietnet. Use ctrl-c to exit")
try:
# get user input and play message
while True:
message = user_input("> ")
try:
pattern = psk.encode(message)
buffer = make_buffer_from_bit_pattern(pattern, FREQ, FREQ_OFF)
play_buffer(buffer)
except KeyError:
print("Messages may only contain printable ASCII characters.")
except KeyboardInterrupt:
# clean up our streams and exit
stream.stop_stream()
stream.close()
p.terminate()
print("exited cleanly")