-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathio.py
141 lines (111 loc) · 3.35 KB
/
io.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
import numpy
import os
import subprocess
import sys
import wave
def play(path, cmd='play', shell=False, wait=True):
"""
Executes custom command with specified .wav file path as an argument.
Parameters
----------
path : string
File path with or without the .wav extension.
cmd : string
Command to execute, e.g. play (http://sox.sourceforge.net).
shell : bool
If true, the command will be executed through the shell.
wait : bool
If true, wait for child process to terminate.
"""
if path.startswith('~'):
path = os.path.expanduser(path)
if not path.lower().endswith('.wav'):
path += '.wav'
process = subprocess.Popen([cmd, path], shell=shell)
if wait:
return process.wait()
else:
return process.pid
def read(path):
"""
Reads a .wav file.
Parameters
----------
path : string
File path with or without the .wav extension.
Returns
-------
data : ndarray
Content of the .wav file.
sr : integer
Sample rate in hertz.
"""
if path.startswith('~'):
path = os.path.expanduser(path)
if not path.lower().endswith('.wav'):
path += '.wav'
with wave.open(path, 'rb') as file:
sr = file.getframerate()
bytes = file.getsampwidth()
channels = file.getnchannels()
data = file.readframes(file.getnframes())
assert bytes in [1, 2, 3, 4]
bits = bytes * 8
scaler = 2 ** (bits - 1) - 1
data = numpy.frombuffer(data, dtype=numpy.uint8).reshape(-1, bytes)
data = numpy.asarray([
int.from_bytes(frame, signed=(bits != 8), byteorder=sys.byteorder)
for frame in data])
data = data.astype(float).reshape(-1, channels)
data -= 128 if bits == 8 else 0
data = (data + 0.5) / (scaler + 0.5)
data = data.clip(-1, +1)
return data.flatten() if channels == 1 else data, sr
def write(path, data, sr, bits=32):
"""
Writes a .wav file.
Parameters
----------
path : string
File path with or without the .wav extension.
data : ndarray
Content of the .wav file.
sr : integer
Sample rate in hertz.
bits : integer, optional
Sample bitwidth.
"""
if path.startswith('~'):
path = os.path.expanduser(path)
if not path.lower().endswith('.wav'):
path += '.wav'
data = numpy.asarray(data)
assert data.dtype in [float, complex]
assert data.ndim in [1, 2]
assert data.size > 0
if numpy.iscomplex(data).any():
assert data.ndim == 1
data = numpy.stack((numpy.real(data), numpy.imag(data)))
if data.ndim == 2:
if data.shape[0] == 2:
channels = data.shape[0]
data = data.ravel('F')
else:
channels = data.shape[1]
data = data.ravel('C')
else:
channels = 1
assert bits in [8, 16, 24, 32]
bytes = bits // 8
scaler = 2 ** (bits - 1) - 1
data = data.clip(-1, +1)
data = (data * (scaler + 0.5)) - 0.5
data += 128 if bits == 8 else 0
data = b''.join([
int(frame).to_bytes(length=bytes, signed=(bits != 8), byteorder=sys.byteorder)
for frame in data])
with wave.open(path, 'wb') as file:
file.setframerate(sr)
file.setsampwidth(bytes)
file.setnchannels(channels)
file.writeframes(data)