1#!/bin/sh
2# the next line restarts using wish \
3exec wish8.4 "$0" "$@"
4
5package require -exact snack 2.2
6
7set rate 16000
8snack::sound s -rate $rate
9
10# Length of FFT
11set n 1024
12set type FFT
13
14# Start recording, create polygon, and schedule a draw in 100 ms
15
16proc Start {} {
17  Stop
18  set ::pos 0
19  .c delete all
20  .c create polygon -1 -1 -1 -1 -tags polar -fill green
21  s record
22  after 100 Draw
23}
24
25# Stop recording and updating the plot
26
27proc Stop {} {
28  s stop
29  after cancel Draw
30}
31
32# Calculate spectrum and plot it
33
34proc Draw {} {
35  if {[s length] > $::n} {
36    set ::pos [expr [s length] - $::n]
37    set spec [s dBPowerSpectrum -start $::pos -fftlen $::n -winlen $::n \
38	-analysistype $::type]
39    set coords {}
40    set f 0.0001
41    foreach val $spec {
42      set v [expr {6.282985 * log($f)/log(2.0)}]
43      set a [expr {[winfo height .c]/214.0*($val+100)}]
44      set x [expr {[winfo width .c]/2+$a*cos($v)}]
45      set y [expr {[winfo height .c]/2+$a*sin($v)}]
46      lappend coords $x $y
47      set f [expr {$f + 16000.0 / $::n}]
48    }
49    eval .c coords polar $coords
50  }
51  after 10 Draw
52  if {[s length -unit sec] > 20} Stop
53}
54
55# Create simple GUI
56
57pack [ frame .f] -side bottom
58pack [ button .f.b1 -bitmap snackRecord -command Start -fg red -width 40] \
59    -side left
60pack [ button .f.b2 -bitmap snackStop   -command Stop -width 40] -side left
61pack [ radiobutton .f.b3 -text FFT -variable type -value FFT] -side left
62pack [ radiobutton .f.b4 -text LPC -variable type -value LPC] -side left
63pack [ canvas .c -width 300 -height 300 -bg black] -side top -expand true \
64    -fill both
65.c create text 150 150 -text "Polar spectrum plot of microphone signal" \
66    -fill red
67