-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwav.py
150 lines (94 loc) · 3.2 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
import train
import wave
import numpy as np
import math
import struct
import random
import os
import glob
from array import array
import matplotlib.pyplot as plt
def create_wav(data=None):
# debug function for saving training and test pcm data as wav files
file = "test.wav"
framerate = train.samplerate
if data == None:
data, framerate = read_wav(files[0], 400)
w = wave.open(file, mode="wb")
w.setframerate(framerate)
w.setnchannels(1) # mono
w.setsampwidth(3)
buf = bytes()
for val in data:
buf += struct.pack('i', val)[1:4]
w.writeframes(buf)
w.close()
def get_training_filenames():
# get all .wav files from the notes dir
trainingfiles = []
path = os.path.abspath("notes")
os.chdir(path)
for file in glob.glob("*.wav"):
trainingfiles.append("notes/"+file)
os.chdir("..")
return sorted(trainingfiles)
def read_wav(filename, sec):
# parse wav and return frames and framerate
print ( "reading " + filename )
wvf = wave.open(filename)
nframes = wvf.getnframes()
framerate = wvf.getframerate()
if(sec > 0):
nframes = min(nframes, sec*framerate)
pcm = wvf.readframes(nframes) # list of frames
data = array('i')
for i in range(0,nframes*3,3):
data.append(struct.unpack('<i', b'\x00'+ pcm[i:i+3])[0])
return data, framerate
def compute_spectrogram(wvd, startidx, sec, framerate, numfreq, overlap):
# compute values for plotting a spectogram of a recording
N = 2*numfreq
incr = math.ceil( (1-overlap / 100.0)*N )
spec = []
times = []
freqs = [ ((i+1)*(framerate/2.0) / numfreq) for i in range(numfreq) ]
i = startidx
while i+N < startidx + sec*framerate:
if i+N >= len(wvd):
break
data = np.asarray( wvd[i:i+N] )
freqval = np.fft.rfft(data)
ffts = []
for k in range(numfreq):
ffts.append(np.abs( freqval[k] ))
spec.append(ffts)
times.append( i / framerate)
i = i + incr
return np.asarray(spec), np.asarray(times), np.asarray(freqs)
def compute_fft(wvd, startidx, sec, framerate, numfreq, overlap):
# compute fast fourier transformation of a recording
fft, t, f = compute_spectrogram( wvd, startidx, sec, framerate, numfreq, overlap )
return fft
def plot_wav(x, samplerate, Sxx, t, f, time):
# plot wave file to a diagram
# debug function
print("plot wav")
plt.figure(1)
plt.subplot(211)
plt.plot(time, x)
plt.subplot(212)
plt.pcolormesh(t, f, np.transpose( Sxx ))
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()
print(x)
if __name__ == '__main__':
# prints a spectogram
print("read in file")
x, samplerate = read_wav("examples/summertime.wav",400)
print("compute_spectrogram")
Sxx, t, f = compute_spectrogram(x, 0, 400, samplerate, 2048, 0)
print("test")
x = np.asarray(x)
time = np.arange(len(x)) / samplerate
plot_wav(x, samplerate, Sxx, t, f, time)