-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixfiles.py
executable file
·41 lines (35 loc) · 1.15 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
#!/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')
f3 = wave.open(c, 'w')
f3.setnchannels(chann)
f3.setsampwidth(2)
f3.setframerate(44100)
f3.setcomptype('NONE', 'Not Compressed')
frames = min(f1.getnframes(), f2.getnframes())
print("Mixing files, total length %.2f s..." % (frames / 44100.))
d1 = f1.readframes(frames)
d2 = f2.readframes(frames)
for n in range(frames):
if not n%(5*44100): print(n // 44100, '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)