-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp.py
198 lines (140 loc) · 5.34 KB
/
tmp.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
192
193
194
195
196
197
198
from pyglet.gl import *
import time
from scipy import signal
import numpy as np
from scipy.io import wavfile
from scipy import fftpack
import matplotlib.pyplot as plt
settings = {}
settings['nbars'] = 100
settings['fmin'] = 50
settings['fmax'] = 8000
settings['music_file'] = 'test6.wav'
settings['sps'] = 40
settings['sample_length'] = 0.1
settings['delay'] = 0.05
class App(pyglet.window.Window):
def __init__(self,settings,music_file,*args, **kwargs):
self.settings = settings
self.audio = audio(self.settings)
super().__init__(*args, **kwargs)
self.set_minimum_size(400,300)
#glClearColor(0.2, 0.3, 0.2, 1.0)
self.mBars = musicBars(self.settings['nbars'])
self.player = pyglet.media.Player()
self.player.queue(
pyglet.resource.media(self.settings['music_file'], streaming=False)
)
self.playstate = 0
def on_draw(self):
self.clear()
#self.bar1.vertices.draw(GL_TRIANGLES)
for i in range(0, self.settings['nbars']):
self.mBars.Bar[i].vertices.draw(GL_TRIANGLES)
def on_resize(self, width, height):
glViewport(0,0,width,height)
def on_key_press(self, symbol, modifiers):
if symbol == 32:
if self.playstate == 0:
self.playstate = 1
self.player.play()
elif self.playstate == 1:
self.playstate = 0
self.player.pause()
class Bar:
def __init__(self, x0, width):
self.maxval = 1
self.y0 = -0.9
self.h0 = 0.01
self.x0 = x0
self.width = width
self.vertices = pyglet.graphics.vertex_list_indexed(
4,
[0,1,2, 2,3,0],
('v3f', [
self.x0, self.y0, 0,
self.x0+self.width, self.y0, 0,
self.x0+self.width, self.y0+self.h0, 0,
self.x0,self.y0+self.h0,0
]),
('c3f', [1,1,1, 1,1,1, 1,1,1, 1,1,1])
)
def updateHeight(self, heigth):
heigth = heigth/self.maxval
self.vertices.colors = [(1-heigth),0.5,0.8, (1-heigth),0.5,0.8,(1-heigth),0.5,0.8, (1-heigth),0.5,0.8]
self.vertices.vertices = [
self.x0, self.y0, 0,
self.x0+self.width, self.y0, 0,
self.x0+self.width, self.y0+heigth, 0,
self.x0,self.y0+heigth,0
]
class musicBars:
def __init__(self, n):
self.width = 1.8
self.spacing = 0.2 # space in percentage
self.number_bars = n
self.bar_width = (self.width*(1-self.spacing))/self.number_bars
self.space_width = (self.width*self.spacing)/(self.number_bars-1)
self.x_offset = -(self.width/2)
self.Bar = {}
self.x0_bar = {}
for i in range(0,n):
x0 = self.x_offset + (self.bar_width + self.space_width)*i
self.x0_bar[i] = x0
self.Bar[i] = Bar(x0, self.bar_width)
class audio:
def __init__(self, settings):
self.rate, self.audio = wavfile.read(settings['music_file'])
self.audio = np.mean(self.audio, axis=1)
self.settings = settings
def getFreqs(self, current_time):
dt = int(np.round( self.settings['sample_length'] *self.rate / 2))
current_index = int(np.round(current_time*self.rate + self.settings['delay']))
if (current_index > dt) & (current_index < len(self.audio)-dt):
snippet = self.audio[ current_index-dt:current_index+dt]
elif (current_index > dt):
snippet = self.audio[ current_index-dt:]
elif (current_index < len(self.audio)-dt):
snippet = self.audio[ :current_index+dt]
tax = np.linspace(0,snippet.shape[0]/self.rate,snippet.shape[0])
N = snippet.shape[0]
# fft_vals = np.fft.fft(snippet)
# freqs = np.fft.fftfreq(N, d=1/self.rate)
# mask = freqs > 0
# freq_out = freqs[mask]
# fft_out = 2.0*np.abs(fft_vals/2)[mask]
fft_vals = np.fft.fft(snippet)[0:int(N/2)]/N
fft_vals[1:] = 2*fft_vals[1:]
f_vec = np.abs(fft_vals)
R_a = ((12194.0**2)*np.power(f_vec,4))/(((np.power(f_vec,2)+20.6**2)*np.sqrt((np.power(f_vec,2)+107.7**2)*(np.power(f_vec,2)+737.9**2))*(np.power(f_vec,2)+12194.0**2)))
f = self.rate*np.arange( int(N/2))/N
# fft = np.fft.fft(snippet)
#
# f = np.linspace(0, self.rate,N)
# f = f[:N//2]
# a = np.abs(fft)[:N//2]*1/N
#
# a = a[1:]
# #f = np.log(f[1:])
# f = f[1:]
# a = a*f
idx = np.linspace(self.settings['fmin'], self.settings['fmax'], self.settings['nbars']+1)
val = np.empty(self.settings['nbars'])
prtstr = ''
for i in range(0, len(idx)-1):
# val[i] = np.mean(a[(f >= idx[i]) & (f < idx[i+1])])
# prtstr += '{:3.0f} '.format(val[i])
val[i] = np.mean(R_a[(f >= idx[i]) & (f < idx[i+1])])
#print(prtstr)
print(f[1],f[-1],idx[0],idx[-1], np.max(R_a))
return val
def updateHeights(inter,APP):
vals = APP.audio.getFreqs(APP.player.time)
for i in range(0, APP.settings['nbars']):
APP.mBars.Bar[i].updateHeight(vals[i])
if __name__ == '__main__':
file = 'test.wav'
APP = App(settings,file,600,600,'my windows', resizable=True)
APP.on_draw()
pyglet.clock.schedule_interval(updateHeights,1/settings['sps'],APP)
pyglet.app.run()