12 namespace eval ::utils::sound {}
14 set ::utils::sound::pipe ""
15 set ::utils::sound::hasSound 0
16 set ::utils::sound::isPlayingSound 0
17 set ::utils::sound::soundQueue {}
18 set ::utils::sound::soundFiles [list \
19 King Queen Rook Bishop Knight CastleQ CastleK Back Mate Promote Check \
20 a b c d e f g h x 1 2 3 4 5 6 7 8 move alert]
28 array set ::utils::sound::soundMap {
29 K King Q Queen R Rook B Bishop N Knight k CastleK q CastleQ
30 x x U Back # Mate = Promote + Check alert alert
31 a a b b c c d d e e f f g g h h
32 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8
40 proc ::utils::sound::Setup {} {
46 if {[
catch {
package require snack 2.0}]} {
49 set ::utils::sound::pipe [open "| scidsnd.exe" "r+"]
50 fconfigure $::utils::sound::pipe -blocking 0 -buffering line
51 fileevent $::utils::sound::pipe readable {
52 gets $::utils::sound::pipe
53 ::utils::sound::SoundFinished
57 if { $::utils::sound::pipe == "" } {
set hasSound 0}
60 foreach soundFile $soundFiles {
61 ::snack::sound sound_$soundFile
73 proc ::utils::sound::ReadFolder {{newFolder ""}} {
77 if {$newFolder != ""} {
set soundFolder ""}
80 foreach soundFile $soundFiles {
81 set f [
file join $soundFolder $soundFile.wav]
82 if {[
file readable $f]} {
83 if { $::utils::sound::pipe == "" } {
84 sound_$soundFile configure -file $f
94 proc ::utils::sound::AnnounceMove {move} {
98 if {! $hasSound} {
return}
100 if {[
string range $move 0 4] == "O-O-O"} {
set move q}
101 if {[
string range $move 0 2] == "O-O"} {
set move k}
103 set parts [
split $move ""]
105 foreach part $parts {
106 if {[
info exists soundMap($part)]} {
107 lappend soundList sound_$soundMap($part)
110 if {[
llength $soundList] > 0} {
112 foreach s $soundList {
119 proc ::utils::sound::AnnounceNewMove {move} {
124 proc ::utils::sound::AnnounceForward {move} {
125 if {$::utils::sound::announceForward} {
AnnounceMove $move}
129 proc ::utils::sound::AnnounceBack {} {
134 proc ::utils::sound::SoundFinished {} {
135 after cancel ::utils::sound::CancelSounds
136 set ::utils::sound::isPlayingSound 0
141 proc ::utils::sound::CancelSounds {} {
142 if {! $::utils::sound::hasSound} {
return}
144 if { $::utils::sound::pipe != "" } {
145 puts $::utils::sound::pipe "stop"
149 set ::utils::sound::soundQueue {}
150 set ::utils::sound::isPlayingSound 0
156 proc ::utils::sound::PlaySound {sound} {
157 if {! $::utils::sound::hasSound} {
return}
158 lappend ::utils::sound::soundQueue $sound
159 after idle ::utils::sound::CheckSoundQueue
168 proc ::utils::sound::CheckSoundQueue {} {
170 variable isPlayingSound
171 if {$isPlayingSound} {
return}
172 if {[
llength $soundQueue] == 0} {
return}
174 set next [
lindex $soundQueue 0]
175 set soundQueue [
lrange $soundQueue 1 end]
177 if { $::utils::sound::pipe != "" } {
178 set next [
string range $next 6 end]
179 set f [
file join $::utils::sound::soundFolder $next.wav]
180 puts $::utils::sound::pipe "[
file nativename $f]"
182 catch { $next play -blocking 0 -command ::utils::sound::SoundFinished}
183 after 5000 ::utils::sound::CancelSounds
194 proc ::utils::sound::OptionsDialog {} {
197 foreach v {soundFolder announceNew announceForward announceBack} {
198 set ::utils::sound::${v}_temp [
set ::utils::sound::$v]
201 toplevel $w -background [ttk::style lookup . -background]
202 wm title $w "Scid: Sound Options"
206 ttk::label $w.status -text ""
207 if {! $::utils::sound::hasSound} {
208 $w.status configure -text "Scid could not find the Snack audio package at startup; Sound is disabled."
209 pack $w.status -side bottom
211 pack [ttk::frame $w.b] -side bottom -fill x
212 pack [ttk::frame $w.f] -side top -fill x -padx 5 -pady 5 -ipadx 4 -ipady 4
217 ttk::label $f.ftitle -text $::tr(SoundsFolder) -font font_Bold
218 grid $f.ftitle -row $r -column 0 -columnspan 3 -pady 4
221 ttk::entry $f.folderEntry -width 40 -textvariable ::utils::sound::soundFolder_temp
222 grid $f.folderEntry -row $r -column 0 -columnspan 2 -sticky we
223 ttk::button $f.folderBrowse -text " $::tr(Browse)... " \
224 -command ::utils::sound::OptionsDialogChooseFolder
225 grid $f.folderBrowse -row $r -column 2
228 ttk::label $f.folderHelp -text $::tr(SoundsFolderHelp)
229 grid $f.folderHelp -row $r -column 0 -columnspan 3
232 grid [ttk::frame $f.gap$r -height 5] -row $r -column -0
incr r
234 ttk::label $f.title -text $::tr(SoundsAnnounceOptions) -font font_Bold
235 grid $f.title -row $r -column 0 -columnspan 3 -pady 4
238 ttk::checkbutton $f.announceNew -text $::tr(SoundsAnnounceNew) \
239 -variable ::utils::sound::announceNew_temp
240 grid $f.announceNew -row $r -column 0 -columnspan 2 -sticky w
243 grid [ttk::frame $f.gap$r -height 5] -row $r -column -0
incr r
245 ttk::checkbutton $f.announceForward -text $::tr(SoundsAnnounceForward) \
246 -variable ::utils::sound::announceForward_temp
247 grid $f.announceForward -row $r -column 0 -columnspan 2 -sticky w
250 grid [ttk::frame $f.gap$r -height 5] -row $r -column -0
incr r
252 ttk::checkbutton $f.announceBack -text $::tr(SoundsAnnounceBack) \
253 -variable ::utils::sound::announceBack_temp
254 grid $f.announceBack -row $r -column 0 -columnspan 2 -sticky w
256 dialogbutton $w.b.ok -text OK -command ::utils::sound::OptionsDialogOK
257 dialogbutton $w.b.cancel -text $::tr(Cancel) -command [list destroy $w]
259 bind $w <Return> [list $w.b.ok invoke]
260 bind $w <Escape> [list $w.b.cancel invoke]
265 focus $w.f.folderEntry
268 proc ::utils::sound::OptionsDialogChooseFolder {} {
269 set newFolder [
tk_chooseDirectory \
270 -initialdir $::utils::sound::soundFolder_temp \
271 -parent .soundOptions \
272 -title "Scid: $::tr(SoundsFolder)"]
273 if {$newFolder != ""} {
274 set ::utils::sound::soundFolder_temp [
file nativename $newFolder]
278 proc ::utils::sound::OptionsDialogOK {} {
283 catch {grab release $w}
286 set isNewSoundFolder 0
287 if {$soundFolder != $::utils::sound::soundFolder_temp} {
288 set isNewSoundFolder 1
292 foreach v {soundFolder announceNew announceForward announceBack} {
293 set ::utils::sound::$v [
set ::utils::sound::${v}_temp]
299 if {$isNewSoundFolder && $soundFolder != ""} {
301 tk_messageBox -title "Scid: Sound Files" -type ok -icon info \
302 -message "Found $numSoundFiles of [
llength $::utils::sound::soundFiles] sound files in $::utils::sound::soundFolder"