-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwave.py
544 lines (449 loc) · 14.3 KB
/
jwave.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# Handle Microsoft RIFF chunks, as in wave files.
import math
import time
import profile
import warnings
import struct
import jtime
def v2dB(v):
if (v == 0):
return None
return 20.0 * math.log10(abs(v))
def v2dB24(v):
return v2dB((float(v))/0x7fffff)
def v2dB16(v):
return v2dB((float(v))/0x7fff)
def dB2v24(db):
return int(math.exp(db * math.log(10) / 20) * 0x7fffff)
def dB2v16(db):
return int(math.exp(db * math.log(10) / 20) * 0x7fff)
def get_sint24(ifile):
bytes = ifile.read(3)
val = (((
ord(bytes[2]) << 8)
+ ord(bytes[1]) << 8)
+ ord(bytes[0]))
if val >= 0x800000:
return val - 0x1000000
return val
def put_sint24(ofile, val):
bytes = ""
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
ofile.write(bytes)
def get_sint16(file):
# return struct.unpack("<h", file.read(2))[0]
val = get_uint16(file)
if val >= 0x8000:
return val - 0x10000
return val
def put_sint16(file):
bytes = ""
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
ofile.write(bytes)
def get_sint8(file):
print "8-bit format unsupported"
sys.exit(1)
def get_uint32(file):
bytes = file.read(4)
return ((((0L
+ ord(bytes[3]) << 8)
+ ord(bytes[2]) << 8)
+ ord(bytes[1]) << 8)
+ ord(bytes[0]))
def put_uint32(ofile, val):
bytes = ""
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
ofile.write(bytes)
def get_uint24(file):
bytes = file.read(3)
return (((
ord(bytes[2]) << 8)
+ ord(bytes[1]) << 8)
+ ord(bytes[0]))
def get_uint16(file):
# return struct.unpack("<H", file.read(2))[0]
bytes = file.read(2)
return (ord(bytes[1]) << 8) + ord(bytes[0])
def put_uint16(ofile, val):
bytes = ""
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
ofile.write(bytes)
def get_uint8(file):
return ord(file.read(1))
class Chunk:
def __init__(self, inf=None, outf=None):
self.type = None
self.inf = inf
self.outf = outf
self.data = []
self.readIx = 0
self.writeIx = 0
self.chunks = {}
def readHeader(self):
self.type = self.inf.read(4)
def writeHeader(self):
# print "writing header:", self.type
self.outf.write(self.type)
def printHeader(self):
print self.type,
class RiffChunk(Chunk):
def __init__(self, inf=None, outf=None):
Chunk.__init__(self, inf, outf)
def readHeader(self):
Chunk.readHeader(self)
self.size = get_uint32(self.inf)
def writeHeader(self):
Chunk.writeHeader(self)
# print "writing size: 0x%08x" % self.size
put_uint32(self.outf, self.size)
def printHeader(self):
Chunk.printHeader(self)
print "size = 0x%x" % self.size
class WaveChunk(Chunk):
def __init__(self, riff=None, inf=None, outf=None):
Chunk.__init__(self, inf, outf)
self.riff = riff
def readHeader(self):
if self.riff == None:
self.riff = RiffChunk(self.inf)
self.riff.readHeader()
Chunk.readHeader(self)
fmt = RiffChunk(self.inf)
fmt.readHeader()
if fmt.type != "fmt ":
print "%%%%error: fmt chunk wasn't first"
sys.exit(1)
if fmt.size < 16:
print "%%%%error: fmt chunk too small"
sys.exit(1)
fmt.compCode = get_uint16(self.inf)
fmt.numChan = get_uint16(self.inf)
fmt.sampleRate = get_uint32(self.inf)
fmt.aveBytesPerSec = get_uint32(self.inf)
fmt.blockAlign = get_uint16(self.inf)
fmt.bitsPerSample = get_uint16(self.inf)
if fmt.size > 16:
fmt.extraFmtBLen = get_uint16(self.inf)
fmt.extraFmtBytes = self.inf.read(fmt.extraFmtBLen)
self.fmt = fmt
bytesPerVal = (fmt.bitsPerSample + 7) / 8
self.bytesPerVal = bytesPerVal
if (bytesPerVal * fmt.numChan != fmt.blockAlign):
print ("Unsupported format: bytesPerVal = %d, blockAlign = %d, numChan = %d" %
(bytesPerVal, fmt.blockAlign, fmt.numChan))
sys.exit(1)
if bytesPerVal == 2:
self.setup16()
elif bytesPerVal == 3:
self.setup24()
else:
print "Warning: Unsupported format"
while True:
data = RiffChunk(self.inf)
data.readHeader()
if data.type == "data":
break
print "Skipping unexpected WAVE file chunk:",
data.printHeader()
self.inf.seek(data.size, 1) # skip this chunk!
# %%% should save skipped (unrecognized) chunks
else:
print "No wave data chunk found!"
sys.exit(1)
self.data = data
self.numSamples = self.data.size / self.fmt.blockAlign
# self.start = self.inf.tell() ## %%% why doesn't this work ???
self.start = 28 + fmt.size
def writeHeader(self, nsamples=0):
fmt = self.fmt
self.riff.size = 28 + fmt.size + nsamples * fmt.blockAlign
self.riff.writeHeader()
Chunk.writeHeader(self)
fmt.writeHeader()
put_uint16(self.outf, fmt.compCode)
put_uint16(self.outf, fmt.numChan)
put_uint32(self.outf, fmt.sampleRate)
put_uint32(self.outf, fmt.aveBytesPerSec)
put_uint16(self.outf, fmt.blockAlign)
put_uint16(self.outf, fmt.bitsPerSample)
if fmt.size > 16:
fmt.extraFmtBLen = put_uint16(self.outf, fmt.extraFmtBLen)
fmt.extraFmtBytes = self.outf.write(fmt.extraFmtBytes)
data = RiffChunk(outf=self.outf)
data.type = "data"
data.size = fmt.blockAlign * nsamples
data.writeHeader()
self.start = 28 + fmt.size
# copy samples from given input wave file to self's output wave file
# Assume seek has already happened on self
def copySamples(self, iwave, start_sn, end_sn):
if (iwave.fmt.blockAlign != self.fmt.blockAlign
or iwave.fmt.numChan != iwave.fmt.numChan
or iwave.fmt.bitsPerSample != self.fmt.bitsPerSample):
print "copySamples: wave formats must match"
iwave.seekSample(start_sn)
sampcount = end_sn + 1 - start_sn
# test #####
if False:
for ix in range(0,sampcount):
samp = iwave.readSample()
self.writeSample(samp)
return
# fast way
bytecount = sampcount * self.fmt.blockAlign
blksize = 4096
while bytecount > blksize:
self.outf.write(iwave.inf.read(blksize))
bytecount -= blksize
self.outf.write(iwave.inf.read(bytecount))
def copyHeader(self, src):
self.riff = src.riff
self.riff.inf = self.inf
self.riff.outf = self.outf
self.type = src.type
fmt = RiffChunk(self.inf, self.outf)
if src.fmt.type != "fmt ":
print "%%%%error: copying non-wave to wave"
sys.exit(1)
fmt.type = src.fmt.type
fmt.size = src.fmt.size
fmt.compCode = src.fmt.compCode
fmt.numChan = src.fmt.numChan
fmt.sampleRate = src.fmt.sampleRate
fmt.aveBytesPerSec = src.fmt.aveBytesPerSec
fmt.blockAlign = src.fmt.blockAlign
fmt.bitsPerSample = src.fmt.bitsPerSample
if src.fmt.size > 16:
fmt.extraFmtBLen = src.fmt.extraFmtBLen
fmt.extraFmtBytes = src.fmt.extraFmtBytes
self.fmt = fmt
self.getval = src.getval
self.putval = src.putval
self.dB2v = src.dB2v
self.v2dB = src.v2dB
data = RiffChunk(self.inf, self.outf)
self.data = []
self.start = 28 + fmt.size
def setup16(self):
self.getval = get_sint16
self.putval = put_sint16
self.dB2v = dB2v16
self.v2dB = v2dB16
def setup24(self):
self.getval = get_sint24
self.putval = put_sint24
self.dB2v = dB2v24
self.v2dB = v2dB24
def printHeader(self):
RiffChunk.printHeader(self.riff)
Chunk.printHeader(self)
print
print " ",
self.fmt.printHeader()
print " compCode =", self.fmt.compCode
print " numChan =", self.fmt.numChan
print " sampleRate =", self.fmt.sampleRate
print " blockAlign =", self.fmt.blockAlign
print " bitsPerSample =", self.fmt.bitsPerSample
if self.fmt.size > 16:
print " extraFmtBLen =", self.fmt.extraFmtBLen
print " ",
self.data.printHeader()
print " (samples) =", self.numSamples
print " (duration) =", jtime.hmsm(
self.numSamples, self.fmt.sampleRate)
def seekSample(self, n):
loc = self.start + (n * self.fmt.blockAlign)
self.inf.seek(loc)
def getSample(self, n):
loc = self.start + (n * self.fmt.blockAlign)
self.inf.seek(loc)
samp = []
for ix in range(0, self.fmt.numChan):
samp.append(self.getval(self.inf))
return samp
def readSample(self):
samp = []
for ix in range(0, self.fmt.numChan):
samp.append(self.getval(self.inf))
return samp
def writeSample(self, samp):
for ix in range(0, self.fmt.numChan):
self.putval(self.outf, samp[ix])
def readChan(self, chan, start, end):
samps = []
skip_bytes = (self.fmt.numChan - 1) * self.bytesPerVal
loc = self.start + (start * self.fmt.blockAlign) + chan
self.inf.seek(loc)
# print "self.start =", self.start
# print "loc =", loc
# print "start, end", start, end
if skip_bytes:
for sampn in range(start, end):
samps.append(self.getval(self.inf))
self.inf.read(skip_bytes)
else:
for sampn in range(start, end):
samps.append(self.getval(self.inf))
return samps
# sample generator -- not used
def samples(self):
while True:
samp = []
for ix in range(0, self.fmt.numChan):
try:
v = self.getval(self.inf)
except:
return
samp.append(v)
yield samp
class Rmsbuf:
def __init__(self, wave, maxlen=0):
if maxlen == 0:
maxlen = wave.fmt.sampleRate
self.maxlen = maxlen
self.calcInterval = wave.fmt.sampleRate / 10 # %%% should be 5
self.v2dB = wave.v2dB
self.dB2v = wave.dB2v
self.data = []
self.len = 0
self.add = Rmsbuf.add_notfull
self.maxval = 0
self.maxrms = 0
self.minrms = 0x7fffffff
self.sumvsquared = 0L
self.t = 0
self.full = False
def add_notfull(self, val):
self.data.append(val)
###
# keep these lines same as below
self.t += 1
absval = abs(val)
self.sumvsquared += (absval + 0L) * absval
if absval > self.maxval:
self.maxvt = self.t
self.maxval = absval
###
self.len += 1
if self.len == self.maxlen:
self.add = Rmsbuf.add_full
self.full = True
self.index = self.maxlen - 1
def add_full(self, val):
self.index = (self.index + 1) % self.maxlen
absold = abs(self.data[self.index]) + 0L
self.sumvsquared -= absold * absold
###
# keep these lines same as above
self.t += 1
absval = abs(val)
self.sumvsquared += (absval + 0L) * absval
if absval > self.maxval:
self.maxvt = self.t
self.maxval = absval
####
self.data[self.index] = val
if self.t % self.calcInterval == 0:
rms = math.sqrt(self.sumvsquared / self.maxlen)
# print "%s: %-7.3f" % (jtime.hms(self.t, self.maxlen), self.v2dB(rms))
if rms > self.maxrms:
self.maxrmst = self.t
self.maxrms = rms
if rms < self.minrms:
self.minrmst = self.t
self.minrms = rms
def getRms(self):
if self.sumvsquared < self.len * 2:
return self.v2dB(2)
return self.v2dB(math.sqrt(self.sumvsquared / self.len))
def getPeak(self):
return self.v2dB(self.maxval)
def findPrevCrossing(self):
t_offset = self.t - 1 - self.index
ix = self.index
samp = self.data[ix]
last_pos = samp >= 0
if samp == 0:
return self.t
while ix >= 0:
samp = self.data[ix]
pos = samp >= 0
if last_pos != pos:
return ix + t_offset
ix -= 1
if not self.full:
return self.t
t_offset -= self.len
ix = self.len - 1
while ix > self.index:
samp = self.data[ix]
pos = samp >= 0
if last_pos != pos:
return ix + t_offset
ix -= 1
return self.t
def dbTest(wave):
print "%9s %10s %9s" % ("dB", "dB2v(dB)", "v2dB(dB2v(dB))")
for val in (0.0, -6.02, -12.04, -18.06, -48.16, -50.0, -90.31):
print ("%9.5f 0x%08x %9.2f"
% (val,
wave.dB2v(val),
wave.v2dB(wave.dB2v(val))
)
)
def dbTest2(wave):
for val in (0.25, 0.5, 0.75, 0.9999999):
print ("%9.5f %9.2f -- 0x%6x %9.2f"
% (val, v2dB(val),
int(val * 0x7fffff),
wave.v2dB(int(val * 0x7fffff))
)
)
def riffdump(args):
if len(args) < 2:
print "usage: %s <infile> <formatfile> -- dumps RIFF file" % args[0]
print
print " <formatfile> contains a list of RIFF chunk names of"
print " chunks containing subchunks."
sys.exit(1)
infname = args[1]
del args[1]
try:
inf = file(infname, "rb")
except IOError, msg:
print msg
sys.exit(1)
riff = RiffChunk(inf)
riff.readHeader()
riff.printHeader()
def wavedump(args):
infname = args[1]
del args[1]
try:
inf = file(infname, "rb")
except IOError, msg:
print msg
sys.exit(1)
# riff = RiffChunk(inf)
# riff.readHeader()
wave = WaveChunk(inf=inf)
wave.readHeader()
wave.printHeader()
if __name__ == "__main__":
import sys
wavedump(sys.argv)