-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
58 lines (46 loc) · 1.56 KB
/
main.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
import numpy as np
from scipy.io import wavfile
import ctypes
from timeit import default_timer as timer
import matplotlib.pyplot as plt
num = 1
infile = f"./data/test{num}.wav"
outfile = f"./data/out{num}-f.wav"
fun = ctypes.CDLL("./bin/main")
fun.process.argtypes = [ctypes.POINTER(
ctypes.c_int16), ctypes.c_uint32, ctypes.c_uint16]
samplerate, data = wavfile.read(infile)
flat = data.flatten("A")
# flat = (flat / (2**32)) * (2**15)
flat = flat.astype(np.int16)
flat_p = flat.ctypes.data_as(ctypes.POINTER(ctypes.c_int16))
len_c = ctypes.c_uint32(len(data))
sample_c = ctypes.c_uint16(samplerate)
acc = 0
count = 1
for i in range(count):
start = timer()
returnVale = fun.process(flat_p, len_c, sample_c)
end = timer()
acc += (end - start)
print(
f"processed {2 * len(data)} samples {count} times in {round((acc) * 1000, 3)}ms")
print(f"{2 * len(data) * count} samples, with {round(((acc) * 1000 * 1000*1000) / (2*len(data)*count), 3)}ns per sample")
restored = flat.reshape(-1, 2)
wavfile.write(outfile, samplerate, restored)
print(data[:, 0])
print(restored[:, 0])
plt.figure(1)
plt.subplot(411)
plt.specgram(restored[:, 0], Fs=samplerate, scale="dB",
mode="magnitude", cmap="rainbow")
plt.subplot(412)
plt.specgram(restored[:, 1], Fs=samplerate, scale="dB",
mode="magnitude", cmap="rainbow")
plt.subplot(413)
plt.specgram(data[:, 0], Fs=samplerate, scale="dB",
mode="magnitude", cmap="rainbow")
plt.subplot(414)
plt.specgram(data[:, 1], Fs=samplerate, scale="dB",
mode="magnitude", cmap="rainbow")
plt.show()