-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPiNS.py
executable file
·207 lines (145 loc) · 4.81 KB
/
PiNS.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/python
import subprocess # for calling console
import math
import time
import wave
import sys
import random
import os.path
import ConfigParser
configFile = "default.ini"
if (len(sys.argv) > 1):
# if arguments present
configFile = sys.argv[1]
if not configFile.endswith(".ini"):
configFile = configFile + ".ini"
config = ConfigParser.ConfigParser()
config.read(configFile)
transmitter_binary = config.get('transmitter', 'binary')
repeat = True
repeat_infinite = config.getboolean('repeat', 'infinite')
repeat_counter = config.getint('repeat', 'exit_after')
repeat_interval_break_seconds = config.getint('repeat', 'delay')
freq = config.get('general', 'freq')
audio_prepend = config.get('audio', 'prepend')
audio_append = config.get('audio', 'append')
# Give the pifm extension executable rights.
subprocess.call(["sudo", "chmod", "+x", transmitter_binary])
# Message to synthesize and broadcast
message = "123456789 abcdefghijklmnopqrstuvwxyz"
loadFromFile = True
# Sounds for digits/numbers.
sounds = ["zero.wav", "one.wav", "two.wav", "three.wav", "four.wav", "five.wav", "six.wav", "seven.wav", "eight.wav", "nine.wav"]
# Sounds for alphanumeric. Use NATO Phonetic
alpha = ["alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf",
"hotel", "india", "juliet", "kilo", "lima", "mike", "november", "oscar",
"papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor",
"whiskey", "x-ray", "yankee", "zulu"]
def main():
print("PiNumberStation started...")
print("Load configuration from %s" % configFile)
print("Broadcast on " + str(freq))
'''
for x in range(0, len(message)):
print(str(message[x]))
'''
if (os.path.isfile(sys.path[0] + "/vo/alpha/alpha.wav") == False):
print("Synthesis Failure: NO ALPHANUMERIC SUPPORT")
time.sleep(5)
return
def constructWavFromFile( fileName):
fMsg = open(sys.path[0] + "/" + fileName, 'r')
strMessage = fMsg.read()
constructWav( strMessage )
return
def playMessage():
print("Broadcast Begin..")
if transmitter_binary == "pifm":
subprocess.call(["sudo", sys.path[0] + "/pifm", sys.path[0] + "/message.wav", freq])
else:
subprocess.call(["sudo", sys.path[0] + "/" + transmitter_binary, "-f", freq, sys.path[0] + "/message.wav"])
return
def getVO( character ):
if character.isdigit() == True:
return sounds[int(character)]
else:
if character == ',' or character == ' ':
return "_comma.wav"
if character == '.':
return "_period.wav"
if character == '\n':
return "nova.wav"
if character.isalpha() == True:
return "/alpha/" + str(alpha[ord(character) - ord('a')] + ".wav")
def constructWav( strMessage ):
print("Synthesizing Message..")
infiles = []
#if enable_encryption == True:
# strMessageOut = encrypt(strMessage, encKey)
#else:
# strMessageOut = strMessage
strMessageOut = strMessage
'''
import audiolab, scipy
a, fs, enc = audiolab.wavread('file1.wav')
b, fs, enc = audiolab.wavread('file2.wav')
c = scipy.vstack((a,b))
audiolab.wavwrite(c, 'file3.wav', fs, enc)
'''
# determine infiles for message.
for file in audio_prepend.split(","):
if not file[0] == "/" or not file[0] == ".":
file = sys.path[0] + "/" + file
if os.path.exists(file):
infiles.append(file)
else:
print "File %s not exists ... skipped!" % file
for character in strMessageOut:
char_sound = sys.path[0] + "/vo/" + getVO(character)
infiles.append(char_sound)
print char_sound
for file in audio_append.split(","):
if not file[0] == "/" or not file[0] == ".":
file = sys.path[0] + "/" + file
if os.path.exists(file):
infiles.append(file)
else:
print "File %s not exists ... skipped!" % file
infiles.append(file)
infiles.append(sys.path[0] + "/vo/off3.wav")
outfile = sys.path[0] + "/message.wav"
data = []
for infile in infiles:
w = wave.open(infile, 'rb')
data.append( [w.getparams(), w.readframes(w.getnframes())] )
w.close()
output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
for x in range(0, len(infiles)):
output.writeframes(data[x][1])
#output.writeframes(data[0][1])
#output.writeframes(data[1][1])
#output.writeframes(data[2][1])
output.close()
print("Synthesis Complete..")
return
# START:
main()
while (True):
if (loadFromFile == False):
constructWav(message)
else:
constructWavFromFile("message.txt")
print "Construction Complete.."
print "Playing ..."
playMessage()
if not repeat_infinite:
repeat_counter -= 1
if repeat_counter <= 0:
break
if repeat_interval_break_seconds > 0:
print "Sleep ", repeat_interval_break_seconds, " secs ..."
time.sleep(repeat_interval_break_seconds)
#kill pifm because it doesn't kill itself, for some stupid reason.
subprocess.call(["sudo", "killall", transmitter_binary])
print("Done")