-
Notifications
You must be signed in to change notification settings - Fork 0
/
wav.py
191 lines (156 loc) · 7.5 KB
/
wav.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
import logging
import cv2
import numpy as np
from chunk import Chunk
import struct
import math
from time import time
import os.path
from common import SushiError, clip
WAVE_FORMAT_PCM = 0x0001
WAVE_FORMAT_EXTENSIBLE = 0xFFFE
class DownmixedWavFile(object):
def __init__(self, path):
super(DownmixedWavFile, self).__init__()
self._file = None
self._file = open(path, 'rb')
try:
riff = Chunk(self._file, bigendian=False)
if riff.getname() != 'RIFF':
raise SushiError('File does not start with RIFF id')
if riff.read(4) != 'WAVE':
raise SushiError('Not a WAVE file')
fmt_chunk_read = False
data_chink_read = False
file_size = os.path.getsize(path)
while True:
try:
chunk = Chunk(self._file, bigendian=False)
except EOFError:
break
if chunk.getname() == 'fmt ':
self._read_fmt_chunk(chunk)
fmt_chunk_read = True
elif chunk.getname() == 'data':
if file_size > 0xFFFFFFFF:
# large broken wav
self.frames_count = (file_size - self._file.tell()) // self.frame_size
else:
self.frames_count = chunk.chunksize // self.frame_size
data_chink_read = True
break
chunk.skip()
if not fmt_chunk_read or not data_chink_read:
raise SushiError('Invalid WAV file')
except:
if self._file:
self._file.close()
raise
def __del__(self):
self.close()
def close(self):
if self._file:
self._file.close()
self._file = None
def readframes(self, count):
if not count:
return ''
data = self._file.read(count * self.frame_size)
if self.sample_width == 2:
unpacked = np.fromstring(data, dtype=np.int16)
elif self.sample_width == 3:
bytes = np.ndarray(len(data), 'int8', data)
unpacked = np.zeros(len(data) / 3, np.int16)
unpacked.view(dtype='int8')[0::2] = bytes[1::3]
unpacked.view(dtype='int8')[1::2] = bytes[2::3]
else:
raise SushiError('Unsupported sample width: {0}'.format(self.sample_width))
unpacked = unpacked.astype('float32')
if self.channels_count == 1:
return unpacked
else:
min_length = len(unpacked) // self.channels_count
real_length = len(unpacked) / float(self.channels_count)
if min_length != real_length:
logging.error("Length of audio channels didn't match. This might result in broken output")
channels = (unpacked[i::self.channels_count] for i in xrange(self.channels_count))
data = reduce(lambda a, b: a[:min_length]+b[:min_length], channels)
data /= float(self.channels_count)
return data
def _read_fmt_chunk(self, chunk):
wFormatTag, self.channels_count, self.framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('<HHLLH',
chunk.read(14))
if wFormatTag == WAVE_FORMAT_PCM or wFormatTag == WAVE_FORMAT_EXTENSIBLE: # ignore the rest
bits_per_sample = struct.unpack('<H', chunk.read(2))[0]
self.sample_width = (bits_per_sample + 7) // 8
else:
raise SushiError('unknown format: {0}'.format(wFormatTag))
self.frame_size = self.channels_count * self.sample_width
class WavStream(object):
READ_CHUNK_SIZE = 1 # one second, seems to be the fastest
PADDING_SECONDS = 10
def __init__(self, path, sample_rate=12000, sample_type='uint8'):
if sample_type not in ('float32', 'uint8'):
raise SushiError('Unknown sample type of WAV stream, must be uint8 or float32')
file = DownmixedWavFile(path)
total_seconds = file.frames_count / float(file.framerate)
downsample_rate = sample_rate / float(file.framerate)
self.sample_count = math.ceil(total_seconds * sample_rate)
self.sample_rate = sample_rate
# pre-allocating the data array and some place for padding
self.data = np.empty((1, self.PADDING_SECONDS * 2 * file.framerate + self.sample_count), np.float32)
self.padding_size = 10 * file.framerate
before_read = time()
try:
seconds_read = 0
samples_read = self.padding_size
while seconds_read < total_seconds:
data = file.readframes(int(self.READ_CHUNK_SIZE * file.framerate))
new_length = int(round(len(data) * downsample_rate))
dst_view = self.data[0][samples_read:samples_read+new_length]
if downsample_rate != 1:
data = data.reshape((1, len(data)))
data = cv2.resize(data, (new_length, 1), interpolation=cv2.INTER_NEAREST)[0]
np.copyto(dst_view, data, casting='no')
samples_read += new_length
seconds_read += self.READ_CHUNK_SIZE
# padding the audio from both sides
self.data[0][0:self.padding_size].fill(self.data[0][self.padding_size])
self.data[0][-self.padding_size:].fill(self.data[0][-self.padding_size-1])
# normalizing
# also clipping the stream by 3*median value from both sides of zero
max_value = np.median(self.data[self.data >= 0], overwrite_input=True) * 3
min_value = np.median(self.data[self.data <= 0], overwrite_input=True) * 3
np.clip(self.data, min_value, max_value, out=self.data)
self.data -= min_value
self.data /= (max_value - min_value)
if sample_type == 'uint8':
self.data *= 255.0
self.data += 0.5
self.data = self.data.astype('uint8')
except Exception as e:
raise SushiError('Error while loading {0}: {1}'.format(path, e))
finally:
file.close()
logging.info('Done reading WAV {0} in {1}s'.format(path, time() - before_read))
@property
def duration_seconds(self):
return self.sample_count / self.sample_rate
def get_substream(self, start, end):
start_off = self._get_sample_for_time(start)
end_off = self._get_sample_for_time(end)
return self.data[:, start_off:end_off]
def _get_sample_for_time(self, timestamp):
# this function gets REAL sample for time, taking padding into account
return int(self.sample_rate * timestamp) + self.padding_size
def find_substream(self, pattern, start_time, end_time):
start_time = clip(start_time, -self.PADDING_SECONDS, self.duration_seconds)
end_time = clip(end_time, 0, self.duration_seconds + self.PADDING_SECONDS)
start_sample = self._get_sample_for_time(start_time)
end_sample = self._get_sample_for_time(end_time) + len(pattern[0])
search_source = self.data[:, start_sample:end_sample]
search_source = cv2.GaussianBlur(search_source, (5, 5), 0)
pattern = cv2.GaussianBlur(pattern, (5, 5), 0)
result = cv2.matchTemplate(search_source, pattern, cv2.TM_SQDIFF_NORMED)
min_idx = result.argmin(axis=1)[0]
return result[0][min_idx], start_time + (min_idx / float(self.sample_rate))