forked from cuu508/PySynth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixfiles.py
executable file
·47 lines (39 loc) · 1.27 KB
/
mixfiles.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
#!/usr/bin/env python
# Mix two mono files to get a stereo file
import sys, wave, struct
def mix_files(a, b, c, chann = 2, phase = -1.):
f1 = wave.open(a, 'r')
f2 = wave.open(b, 'r')
r1, r2 = f1.getframerate(), f2.getframerate()
if r1 != r2:
print("Error: frame rates must be the same!")
sys.exit(1)
f3 = wave.open(c, 'w')
f3.setnchannels(chann)
f3.setsampwidth(2)
f3.setframerate(r1)
f3.setcomptype('NONE', 'Not Compressed')
frames = min(f1.getnframes(), f2.getnframes())
print("Mixing files, total length %.2f s..." % (frames / float(r1)))
d1 = f1.readframes(frames)
d2 = f2.readframes(frames)
for n in range(frames):
if not n % (5 * r1): print(n // r1, 's')
if chann < 2:
d3 = struct.pack('h', int(
.5 * (struct.unpack('h', d1[2*n:2*n+2])[0] +
struct.unpack('h', d2[2*n:2*n+2])[0])))
else:
d3 = ( struct.pack('h', int(
phase * .3 * struct.unpack('h', d1[2*n:2*n+2])[0] +
.7 * struct.unpack('h', d2[2*n:2*n+2])[0])) +
struct.pack('h', int(
.7 * struct.unpack('h', d1[2*n:2*n+2])[0] +
phase * .3 * struct.unpack('h', d2[2*n:2*n+2])[0])) )
f3.writeframesraw(d3)
f3.close()
if __name__ == '__main__':
if len(sys.argv) == 4:
a, b, c = sys.argv[1:]
print("Mixing %s and %s, output will be %s" % (a, b, c))
mix_files(a, b, c)