1#!/bin/sh
2# the next line restarts using wish \
3exec wish8.4 "$0" "$@"
4
5package require -exact snack 2.2
6
7set width 300
8set height 200
9set pps 300
10set bright 0.0
11set contrast 0.0
12set winlen 128
13set fftlen 256
14set gridfspacing 0
15set gridtspacing 0.0
16set filename spectrogram.ps
17set colors {#000 #006 #00B #00F #03F #07F #0BF #0FF #0FB #0F7 \
18	    #0F0 #3F0 #7F0 #BF0 #FF0 #FB0 #F70 #F30 #F00}
19set color Red
20set type Hamming
21option add *font {Helvetica 10 bold}
22
23pack [ canvas .c -width 600 -height 300]
24pack [ label .l -text "Drag spectrogram with left mouse button"]
25pack [ frame .f1] -pady 2
26pack [ scale .f1.s1 -variable width -label Width -from 10 -to 600 -orient hori\
27	-length 100 -command {.c itemconf speg -width }] -side left
28pack [ scale .f1.s2 -variable height -label Height -from 10 -to 300 -orient\
29	hori -length 100 -command {.c itemconf speg -height }] -side left
30pack [ scale .f1.s3 -variable pps -label Pix/sec -from 10 -to 600 -orient hori\
31	-length 100 -command {.c itemconf speg -pixelspersec }] -side left
32pack [ scale .f1.s4 -variable bright -label Brightness -from -100 -to 100\
33	-res 0.1 -orient hori -length 100 -command {.c itemconf speg -brightness }] -side left
34pack [ scale .f1.s5 -variable contrast -label Contrast -from -100 -to 100 -res 0.1 -orient hori -length 100 -command {.c itemconf speg -contrast }] -side left
35
36set topfr 8000
37pack [ scale .f1.s7 -variable topfr -label Top -from 1000 -to 8000 -orient hori -length 100 -command {.c itemconf speg -topfr }] -side left
38
39pack [ frame .f2] -pady 2
40tk_optionMenu .f2.cm type Hamming Hanning Bartlett Blackman Rectangle
41for {set i 0} {$i < 5} {incr i} {
42  .f2.cm.menu entryconfigure $i -command {.c itemconf speg -windowtype $type}
43}
44pack .f2.cm -side left
45pack [ label .f2.lw -text "window:"] -side left
46foreach n {32 64 128 256 512 1024 2048} {
47    pack [ radiobutton .f2.w$n -text $n -variable winlen -value $n\
48	    -command {.c itemconf speg -winlength $winlen}] -side left
49}
50
51pack [ frame .f3] -pady 2
52pack [ label .f3.lf -text "FFT points:"] -side left
53foreach n {64 128 256 512 1024 2048 4096} {
54    pack [ radiobutton .f3.f$n -text $n -variable fftlen -value $n\
55	    -command {.c itemconf speg -fft $fftlen}] -side left
56}
57
58pack [ frame .f4] -pady 2
59pack [ label .f4.lf -text "Grid f-spacing:"] -side left
60foreach n {0 500 1000 2000} {
61    pack [ radiobutton .f4.f$n -text $n -variable gridfspacing -value $n\
62	    -command {.c itemconf speg -gridfspacing $gridfspacing}] -side left
63}
64pack [ label .f4.lf2 -text "Grid t-spacing:"] -side left
65foreach n {0 1 25 5} {
66    pack [ radiobutton .f4.t$n -text 0.$n -variable gridtspacing -value 0.$n\
67	    -command {.c itemconf speg -gridtspacing $gridtspacing}] -side left
68}
69
70pack [ frame .f42] -pady 2
71pack [ label .f42.lf3 -text "Grid color:"] -side left
72foreach f {Black Red Blue White Cyan} {
73    pack [ radiobutton .f42.c$f -text $f -variable color -value $f \
74	    -command {.c itemconf speg -gridcolor $color}] -side left
75}
76
77pack [ frame .f5] -pady 2
78pack [ button .f5.br -bitmap snackRecord -command Record -fg red] -side left
79pack [ button .f5.bs -bitmap snackStop -command {s stop}] -side left
80pack [ label .f5.l -text "Load sound file:"] -side left
81pack [ button .f5.b1 -text ex1.wav -command {s read ex1.wav}] -side left
82pack [ button .f5.b2 -text ex2.wav -command {s read ex2.wav}] -side left
83
84proc Record {} {
85    global width pps
86
87    s flush
88    .c itemconf speg -pixelspersecond $pps -width $width
89    s record
90    after cancel [list catch {.f5.bs invoke}]
91    after 10000  [list catch {.f5.bs invoke}]
92}
93
94set col ""
95pack [ frame .f6] -pady 2
96pack [ label .f6.l1 -text "Colors:"] -side left
97pack [ radiobutton .f6.r1 -text B/W -var col -val "" -command {.c itemconf speg -colormap $col}] -side left
98pack [ radiobutton .f6.r2 -text Rainbow -var col -val $colors -command {.c itemconf speg -colormap $col}] -side left
99pack [ label .f6.l2 -text "Generate postscript file:"] -side left
100pack [ entry .f6.e -textvariable filename] -side left
101pack [ button .f6.b -text Save -command {.c postscript -file $filename}] -side left
102
103pack [ button .bClose -text Close -command exit]
104
105bind .c <1> [list initDrag %x %y]
106bind .c <B1-Motion> [list Drag %x %y]
107
108proc initDrag {x y} {
109  set ::ox [.c canvasx $x]
110  set ::oy [.c canvasy $y]
111}
112
113proc Drag {x y} {
114  set x [.c canvasx $x]
115  set y [.c canvasy $y]
116  .c move current [expr $x - $::ox] [expr $y - $::oy]
117  set ::ox $x
118  set ::oy $y
119}
120
121snack::sound s -load ex1.wav
122
123update
124
125.c create spectrogram 300 150 -anchor c -sound s -height $height -width $width -tags speg -pixelsp $pps
126