-
Notifications
You must be signed in to change notification settings - Fork 13
/
arrayWav.py
103 lines (64 loc) · 2.11 KB
/
arrayWav.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
import wave
import numpy as np
from audiotsm.io import base
class ArrReader(base.Reader):
pointer = 0
def __init__(self, arr, channels, samplerate, samplewidth):
self.samples = arr
self._channels = channels
self.samplerate = samplerate
self.samplewidth = samplewidth
@property
def channels(self):
return self._channels
@property
def empty(self):
return self.samples.shape[0] <= self.pointer
def close(self):
pass
def read(self, buffer):
if buffer.shape[0] != self.channels:
raise ValueError("the buffer should have the same number of "
"channels as the ArrReader")
end = self.pointer + buffer.shape[1]
frames = self.samples[self.pointer:end].T.astype(np.float32)
n = frames.shape[1]
np.copyto(buffer[:, :n], frames)
del frames
self.pointer = end
return n
def skip(self, n):
pastPointer = self.pointer
self.pointer += n
return self.pointer - pastPointer
def __enter__(self):
return self
def __exit__(self, _1, _2, _3):
self.close()
class ArrWriter(base.Writer):
pointer = 0
def __init__(self, arr, channels, samplerate, samplewidth):
self._channels = channels
self.output = arr
@property
def channels(self):
return self._channels
def close(self):
pass
def write(self, buffer):
if buffer.shape[0] != self.channels:
raise ValueError("the buffer should have the same number of"
"channels as the ArrWriter")
end = self.pointer + buffer.shape[1]
# if buffer.size > 0:
# if np.max(buffer) > 0:
# import pdb; pdb.set_trace()
changedBuffer = buffer.T.astype(np.int16)
n = buffer.shape[1]
self.output = np.concatenate((self.output, changedBuffer))
self.pointer = end
return n
def __enter__(self):
return self
def __exit__(self, _1, _2, _3):
self.close()