1#!/bin/sh
2# the next line restarts using wish \
3exec wish8.4 "$0" "$@"
4
5package require -exact snack 2.2
6
7file delete _tmprec.wav
8snack::sound t -debug 0
9t write _tmprec.wav
10snack::sound s -file _tmprec.wav -debug 0
11
12set m [menu .menu]
13$m add cascade -label File -menu $m.file -underline 0
14menu $m.file -tearoff 0
15$m.file add command -label "Open..." -command [list OpenSound]
16$m.file add command -label "Save As..." -command [list SaveSound]
17$m.file add command -label "Exit" -command exit
18$m add cascade -label Audio -menu $m.audio -underline 0
19menu $m.audio -tearoff 0
20$m.audio add command -label "Settings..." -command Settings
21$m.audio add command -label "Mixer..." -command snack::mixerDialog
22. config -menu $m
23
24snack::createIcons
25pack [frame .f1] -pady 5
26button .f1.bp -bitmap snackPlay -command Play
27button .f1.bu -bitmap snackPause -command Pause
28button .f1.bs -bitmap snackStop -command Stop
29button .f1.br -bitmap snackRecord -command Record -fg red
30pack .f1.bp .f1.bu .f1.bs .f1.br -side left
31pack [frame .f2] -pady 5
32label .f2.time -text "00:00.0" -width 10
33snack::levelMeter .f2.lm
34pack .f2.time .f2.lm -side left
35
36wm protocol . WM_DELETE_WINDOW exit
37
38proc OpenSound {} {
39    set filename [snack::getOpenFile]
40    s configure -file $filename
41    SetTime [s length -unit sec]
42}
43
44proc SaveSound {} {
45    set filename [snack::getSaveFile]
46    s write $filename
47}
48
49proc Settings {} {
50 set ::s(rate) [s cget -rate]
51 set ::s(enc)  [s cget -encoding]
52 set ::s(chan) [s cget -channels]
53
54 set w .conv
55 catch {destroy $w}
56 toplevel $w
57 wm title $w Settings
58
59 frame $w.q
60 pack $w.q -expand 1 -fill both -side top
61 pack [frame $w.q.f1] -side left -anchor nw -padx 3m -pady 2m
62 pack [frame $w.q.f2] -side left -anchor nw -padx 3m -pady 2m
63 pack [frame $w.q.f3] -side left -anchor nw -padx 3m -pady 2m
64 pack [frame $w.q.f4] -side left -anchor nw -padx 3m -pady 2m
65 pack [label $w.q.f1.l -text "Sample Rate"]
66 foreach e [snack::audio rates] {
67  pack [radiobutton $w.q.f1.r$e -text $e -value $e -variable ::s(rate)] \
68	  -anchor w
69 }
70 pack [entry $w.q.f1.e -textvariable ::s(rate) -width 6] -anchor w
71 pack [label $w.q.f2.l -text "Sample Encoding"]
72 foreach e [snack::audio encodings] {
73  pack [radiobutton $w.q.f2.r$e -text $e -value $e -variable ::s(enc)] \
74	  -anchor w
75 }
76 pack [label $w.q.f3.l -text Channels]
77 pack [radiobutton $w.q.f3.1 -text Mono -value 1 -variable ::s(chan)] -anchor w
78 pack [radiobutton $w.q.f3.2 -text Stereo -value 2 -variable ::s(chan)] \
79	 -anchor w
80 pack [entry $w.q.f3.e -textvariable ::s(chan) -width 3] -anchor w
81
82 pack [ frame $w.f3]
83 pack [ button $w.f3.b1 -text OK -width 6 \
84	 -command "ApplySettings;destroy $w"] -side left
85 pack [ button $w.f3.b2 -text Cancel -command "destroy $w"] -side left
86}
87
88proc ApplySettings {} {
89 s configure -file ""
90 s configure -rate $::s(rate) -channels $::s(chan) -encoding $::s(enc)
91 t configure -rate $::s(rate) -channels $::s(chan) -encoding $::s(enc)
92 t write _tmprec.wav
93 s configure -file _tmprec.wav
94}
95
96proc SetTime {t} {
97 set mmss [clock format [expr int($t)] -format "%M:%S"]
98 .f2.time config -text $mmss.[format "%d" [expr int(10*($t-int($t)))]]
99}
100
101proc Update {} {
102 if {$::op == "p"} {
103  set t [audio elapsed]
104  set end   [expr int([s cget -rate] * $t)]
105  set start [expr $end - [s cget -rate] / 10]
106  if {$start < 0} { set start 0}
107  if {$end >= [s length]} { set end -1 }
108  set l [s max -start $start -end $end]
109 } else {
110  set l [t max]
111  t length 0
112  set t [s length -unit sec]
113 }
114 SetTime $t
115 .f2.lm configure -level $l
116
117 after 100 Update
118}
119
120proc Record {} {
121 s stop
122 s configure -file _tmprec.wav
123 s record
124 t record
125 set ::op r
126 .f1.bp configure -relief raised
127 .f1.br configure -relief groove
128}
129
130proc Play {} {
131 t stop
132 s stop
133 s play -command Stop
134 set ::op p
135 .f1.bp configure -relief groove
136 .f1.br configure -relief raised
137 .f1.bu configure -relief raised
138}
139
140proc Stop {} {
141 s stop
142 t record
143 set ::op s
144 .f1.bp configure -relief raised
145 .f1.br configure -relief raised
146 .f1.bu configure -relief raised
147}
148
149proc Pause {} {
150 s pause
151 if {$::op != "s"} {
152     if {[.f1.bu cget -relief] == "raised"} {
153	 .f1.bu configure -relief groove
154     } else {
155	 .f1.bu configure -relief raised
156     }
157 }
158}
159
160t record
161set op s
162Update
163