-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrequencyview.py
72 lines (55 loc) · 1.87 KB
/
frequencyview.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
#!/usr/bin/env python2
# coding: utf-8
u"""Mixer. Requires pyglet and numpy."""
import sys
import Tkinter as tk
import pyglet
import numpy
def lerp(a, b, x):
u"""Linear interpolation."""
return a + ((b - a) * x)
media = pyglet.media.load(sys.argv[1])
raw_data = media.get_audio_data(1).data
last_data = raw_data
while last_data:
last_data = media.get_audio_data(255)
if last_data:
raw_data += last_data.data
if media.audio_format.sample_size == 16:
data = numpy.fromstring(raw_data, 'Int16')
MAX_Y_VALUE = 32767.0
else:
raise ValueError(u'Wrong wave file')
class FrequencyView(object, tk.Canvas):
def __init__(self, *args, **kwargs):
self.data = kwargs.pop('data')
self.color = kwargs.pop('color', '#00aacc')
self.draw_index = None
tk.Canvas.__init__(self, *args, **kwargs)
self.bind('<Configure>', self.__configure, '+')
def __configure(self, event=None):
self.generate_graph()
def generate_graph(self):
width = int(self.winfo_width())
height = int(self.winfo_height())
len_samples = len(self.data)
y_offset = height / 2
# traveling the widget's width
points = []
for x in range(0, width):
normalized = x / float(width)
index = int(lerp(0, len_samples, normalized))
y = ((self.data[index] * height) / MAX_Y_VALUE) + y_offset
points.extend([x, y])
if self.draw_index:
self.coords(self.draw_index, *points)
else:
self.draw_index = self.create_line(*points, fill=self.color)
if __name__ == '__main__':
top = tk.Tk()
top['bg'] = '#333'
ca = FrequencyView(top, bd=0, highlightthickness=0, bg='#333', data=data)
ca.pack(expand='yes', fill='both')
top.title(u'Mixer')
top.bind('<Escape>', lambda e: top.destroy(), '+')
top.mainloop()