1#! /usr/bin/env python
2
3from Tkinter import *
4from tkSnack import *
5from math import *
6
7root = Tkinter.Tk()
8
9initializeSnack(root)
10snd = Sound()
11
12w = 300
13h = 300
14s = 100
15n = 1024
16type = StringVar()
17type.set("FFT")
18
19def stop():
20    snd.stop()
21    root.after_cancel(id)
22
23def draw():
24    if (snd.length() > n) :
25        pos = snd.length() - n
26        spec = snd.dBPowerSpectrum(start=pos,fftlen=n,winlen=n,analysistype=type.get())
27        coords=[]
28        f=0.0001
29        for val in spec :
30            v = 6.282985 * log(f)/log(2.0)
31            a = 1.4*(val+s)
32            x = w/2+a*cos(v)
33            y = h/2+a*sin(v)
34            coords.append(x)
35            coords.append(y)
36            f = f + 16000.0 / n
37        c.delete('polar')
38        c.create_polygon(coords, tags='polar', fill='green')
39    if (snd.length(unit='sec') > 20) :
40        stop()
41    id = root.after(100,draw)
42
43def start():
44    pos = 0
45    snd.record()
46    c.update_idletasks()
47    id = root.after(100,draw)
48
49c = SnackCanvas(height=h, width=w, bg='black')
50c.pack()
51f = Frame()
52f.pack()
53Button(f, bitmap='snackRecord', fg='red', command=start).pack(side='left')
54Button(f, bitmap='snackStop', command=stop).pack(side='left')
55Radiobutton(f, text='FFT', variable=type, value='FFT').pack(side='left')
56Radiobutton(f, text='LPC', variable=type, value='LPC').pack(side='left')
57Button(f, text='Exit', command=root.quit).pack(side='left')
58
59root.mainloop()
60