-
Notifications
You must be signed in to change notification settings - Fork 4
/
fft.py
93 lines (70 loc) · 1.82 KB
/
fft.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
#!/usr/bin/env python
from __future__ import print_function
import struct
import wave
import sys
import csv
import pickle
import os
import glob
import numpy as np
FPS = 25.0
nFFT = 64#512
BUF_SIZE = 4 * nFFT
SAMPLE_SIZE = 2
CHANNELS = 1
RATE = 16000
numpixels = 60
def animate(i, wf, MAX_y):
N = (int((i + 1) * RATE / FPS) - wf.tell()) / nFFT
if not N:
return line,
N *= nFFT
data = wf.readframes(N)
# print('{:5.1f}% - V: {:5,d} - A: {:10,d} / {:10,d}'.format(
# 100.0 * wf.tell() / wf.getnframes(), i, wf.tell(), wf.getnframes()
# ))
# Unpack data
y = np.array(struct.unpack("%dh" % (len(data) / SAMPLE_SIZE), data)) / MAX_y
Y_L = np.fft.fft(y, nFFT)
Y = list(abs(Y_L[-nFFT / 2:-1]))
return Y
if Y[0] > 0.001:
#print(Y)
return Y
def fft(file):
MAX_y = 2.0 ** (SAMPLE_SIZE * 8 - 1)
wf = wave.open(file, 'rb')
assert wf.getnchannels() == CHANNELS
assert wf.getsampwidth() == SAMPLE_SIZE
assert wf.getframerate() == RATE
frames = wf.getnframes()
output = list()
for i in xrange(int(frames / RATE * FPS)):
frame = animate(i, wf, MAX_y)
if frame[0] > 0.001:
output.append(frame)
wf.close()
return output
def getData(files, midi):
for item in files:
data = fft(item)
with open("test.csv", 'a') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for item in data:
writer.writerow(item)
finaldata = list(csv.reader(open("test.csv")))
# print(finaldata)
# print('-' * 79)
with open(midi, 'a') as f:
pickle.dump(finaldata,f)
os.remove("test.csv")
if __name__ == '__main__':
MIDI = "089"
files = list()
for file in os.listdir("./Guitar-notes"):
if MIDI in os.path.join("./Guitar-notes", file):
files.append(os.path.join("./Guitar-notes", file))
print(files)
print(len(files))
getData(files, "Data/" + MIDI)