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