-
Notifications
You must be signed in to change notification settings - Fork 0
/
wave_rm4.py
executable file
·218 lines (184 loc) · 5.5 KB
/
wave_rm4.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
207
208
209
210
211
212
213
214
215
216
217
218
# Copyright (c) 2020 cybernesto
import sys, re, struct, wave
def help():
print('Usage: python wave_rm4.py <robi_file.rm4>')
def calcWaveRMS(wavefile, steps):
try:
ifile = wave.open(wavefile)
print "Processing " + wavefile
sw = ifile.getsampwidth()
fmts = (None, "=B", "=h", None, "=l")
fmt = fmts[sw]
dcs = (None, 128, 0, None, 0)
dc = dcs[sw]
samplerate = ifile.getparams()[2]
ms = 0.
rms = 0
j=0
k=0
rmslist = []
for i in range(ifile.getnframes()):
if ms < steps[j]:
iframe=ifile.readframes(1)
rms = rms + (struct.unpack(fmt,iframe)[0]-dc)**2
ms = ms+1000./samplerate
k=k+1
else:
if j < len(steps) - 1:
rmslist.append((rms/k)**.5)
j=j+1
else:
print "pose too short!"
rms =0
ms=0
k=0
# Pad longer poses with 0s
rmslist = rmslist + [0]*(len(steps)-j)
mean = sum(rmslist) / float(len(rmslist))
## print rmslist, mean
## print steps
## print [x > mean/2 for x in rmslist]
## print
return [x > mean/2 for x in rmslist]
except:
print "Cannot read file or file not found !"
print wavefile
finally:
ifile.close()
def main():
print('Vocal luminiscence calculator for Robi v1.0b')
print('cybernesto 2019')
print
if len(sys.argv) < 2:
help()
return
try:
f = open(sys.argv[1],"r")
lines = f.readlines()
print "Reading " + sys.argv[1]
f.close()
except:
print "Cannot read file or file not found !"
print sys.argv[1]
exit()
if len(sys.argv) > 2 and sys.argv[2] == "-b":
# save a backup file
try:
f = open(sys.argv[1][:-4]+'.BAK',"w")
f.writelines(lines)
f.close()
except:
print "Cannot write file or file not found !"
print sys.argv[1][:-4]+'.BAK'
exit()
#Read binary for address calculation
f = open(sys.argv[1],"rb")
try:
byte = f.read(1)
add = 0
lineadds = [0]
while byte:
# Find EOL
if byte == '\n':
lineadds.append(add+1)
byte = f.read(1)
add = add+1
finally:
f.close()
blocks = {}
tempBlock = {}
splitregex = r'[<,>,=,"]'
poseParse = False
endtagpos = 0
#Read text for RE Block parsing
for i, line in enumerate(lines):
if re.search(r'[^/]medit', line): #block
# tempBlock was not added yet, because no jump was found but a new block
# has started. Set the current address as the jump address of the last block
# and add it to the list.
if tempBlock != {} and 'jumpAdd' not in tempBlock:
tempBlock['jumpAdd'] = lineadds[i]
blocks[tempBlock['address']] = tempBlock
tempBlock = {}
tempBlock['address'] = lineadds[i]
tempBlock['line'] = i
if re.search(r'block name', line): #name
tempBlock['name'] = re.split(splitregex, line)[3]
if re.search(r'\b(0x0b00)', line): #wait
tempBlock['wait'] = int(re.split(splitregex, line)[8],0)*20
if poseParse:
if re.search(r'0x[0-9A-Fa-f]{4}', line):
pose = pose + re.findall(r'0x[0-9A-Fa-f]{4}', line)
if re.search(r'\b(0x0a00)', line): #pose
pose = []
poseParse = True
if re.search(r'\b(play)', line): #play sound
line = re.sub(r'\\','/',line)
tempBlock['wavefile'] = "./" + re.split(splitregex, line)[3]
if re.search(r'\b(jump)', line): #jumps
tempBlock['jumpAdd'] = int(re.split(splitregex, line)[3],0)
blocks[tempBlock['address']] = tempBlock
if re.search(r'[/]mem_w', line) and poseParse:
tempBlock['pose'] = pose
poseParse = False
if re.search(r'<',line): #tag begin
if endtagpos < lineadds[i]:
endtagpos = lineadds[i]
# Check the wavefile list and go through the jump lines followed
fixlines = []
old = {}
old['delays'] = []
old['wavefile'] = []
old['mouthRMS'] = []
for b in blocks:
if 'wavefile' in blocks[b]:
delays = []
mouth = []
waitLines = []
waitAdds = []
tempAdds = [] # Adress list to avoid endless loops
address = blocks[b]['jumpAdd']
while address <> endtagpos and address not in tempAdds and 'wavefile' not in blocks[address]:
tempAdds.append(address)
#this could be sent to a translating service
# if 'name' in blocks[address]:
# print blocks[address]['name'], address
if 'wait' in blocks[address]:
delays.append(blocks[address]['wait'])
mouth.append(int(blocks[address]['pose'][70],0) != 0)
waitAdds.append(address)
#consider a fix distance from block begin and line with mouth data
waitLines.append(blocks[address]['line']+6)
address = blocks[address]['jumpAdd']
if delays == old['delays'] and blocks[b]['wavefile'] == old['wavefile']:
mouthRMS = old['mouthRMS']
else:
try:
#use the collected delays to calculate the RMS values
mouthRMS = calcWaveRMS(blocks[b]['wavefile'], delays)
old['mouthRMS'] = mouthRMS
old['delays'] = delays
old['wavefile'] = blocks[b]['wavefile']
except:
print "Command aborted"
if mouth != mouthRMS:
#print mouth, "should be", mouthRMS
fixlines = fixlines + waitLines
for i, line in enumerate(waitLines):
a = blocks[waitAdds[i]]['pose'][48:72]
if mouthRMS[i]:
a[-2] = "0x0032"
else:
a[-2] = "0x0000"
#lines[i] = ','.join(format(x, '#06x') for x in a)
lines[line] = '\t'+','.join(a)+',\r\n'
# save the corrected file
try:
f = open(sys.argv[1],"w")
f.writelines(lines)
f.close()
except:
print "Cannot write file or file not found !"
print sys.argv[1]
exit()
main()