File: Soundogram.py

package info (click to toggle)
python 1.5.1-7
  • links: PTS
  • area: main
  • in suites: slink
  • size: 11,616 kB
  • ctags: 32,701
  • sloc: ansic: 90,293; python: 74,171; makefile: 2,449; lisp: 2,097; sh: 702
file content (36 lines) | stat: -rwxr-xr-x 867 bytes parent folder | download | duplicates (4)
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
# Module 'Soundogram'

import audio
from Histogram import Histogram

class Soundogram(Histogram):
	#
	def define(self, win, chunk):
		width, height = corner = win.getwinsize()
		bounds = (0, 0), corner
		self.chunk = chunk
		self.step = (len(chunk)-1)/(width/2+1) + 1
		ydata = _make_ydata(chunk, self.step)
		return Histogram.define(self, (win, bounds, ydata, (0, 128)))
	#
	def setchunk(self, chunk):
		self.chunk = chunk
		self.recompute()
	#
	def recompute(self):
		(left, top), (right, bottom) = self.bounds
		width = right - left
		self.step = (len(chunk)-1)/width + 1
		ydata = _make_ydata(chunk, self.step)
		self.setdata(ydata, (0, 128))
	#


def _make_ydata(chunk, step):
	ydata = []
	for i in range(0, len(chunk), step):
		piece = audio.chr2num(chunk[i:i+step])
		mi, ma = min(piece), max(piece)
		y = max(abs(mi), abs(ma))
		ydata.append(y)
	return ydata