blob: a362eb2a749f4e0633ec929b75ba52b31e013487 [file] [log] [blame]
Guido van Rossume1f069e1990-10-25 18:51:42 +00001# Module 'Soundogram'
2
3import audio
4from minmax import min, max
5from Histogram import Histogram
6
7class Soundogram() = Histogram():
8 #
9 def define(self, (win, chunk)):
10 width, height = corner = win.getwinsize()
11 bounds = (0, 0), corner
12 self.chunk = chunk
13 self.step = (len(chunk)-1)/(width/2+1) + 1
14 ydata = _make_ydata(chunk, self.step)
15 return Histogram.define(self, (win, bounds, ydata, (0, 128)))
16 #
17 def setchunk(self, chunk):
18 self.chunk = chunk
19 self.recompute()
20 #
21 def recompute(self):
22 (left, top), (right, bottom) = self.bounds
23 width = right - left
24 self.step = (len(chunk)-1)/width + 1
25 ydata = _make_ydata(chunk, self.step)
26 self.setdata(ydata, (0, 128))
27 #
28
29
30def _make_ydata(chunk, step):
31 ydata = []
32 for i in range(0, len(chunk), step):
33 piece = audio.chr2num(chunk[i:i+step])
34 mi, ma = min(piece), max(piece)
35 y = max(abs(mi), abs(ma))
36 ydata.append(y)
37 return ydata