File: Sounds.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (736 lines) | stat: -rw-r--r-- 19,568 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#  Sounds.tcl ---
#  
#      This file is part of The Coccinella application. 
#      It implements alert sounds.
#      
#  Copyright (c) 2002-2008  Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
# $Id: Sounds.tcl,v 1.48 2008-05-30 07:25:04 matben Exp $

namespace eval ::Sounds {
	    
    # We undefine ourselves if we have no audio support.
    component::define Sounds \
      "Provides alert sounds through QuickTime or the Snack audio package."
}

proc ::Sounds::Init {} {

    # We are doing all inits late since loading audio can be slow.
    ::hooks::register afterFinalHook ::Sounds::AfterFinalHook
}

# Sounds::AfterFinalHook --
# 
#       Tries to load the sounds component.

proc ::Sounds::AfterFinalHook {} {
    variable priv
    
    ::Debug 2 "::Sounds::AfterFinalHook"
    
    set priv(canPlay)      0
    set priv(inited)       0
    set priv(QuickTimeTcl) [expr {![catch {package require QuickTimeTcl}]}]
    set priv(snack)        [expr {![catch {package require snack}]}]

    # Skip if both 0.
    if {!$priv(QuickTimeTcl) && !$priv(snack)} {
	component::undefine Sounds
	return
    }
    set priv(canPlay) 1
    Mappings
    
    # This one is needed since we have missed it during the launch process.
    InitPrefsHook
   
    # We should register ourselves.
    component::register Sounds

    # Make sure we get called when certain events happen.
    InitEventHooks
    InitHook
}

proc ::Sounds::Mappings {} {
    
    ::Debug 2 "::Sounds::Mappings"
    
    variable nameToText 
    set nameToText [dict create]
	# TRANSLATORS; Sound settings in preferences
	dict set nameToText online          [mc "Contact is online"]
	dict set nameToText offline         [mc "Contact is offline"]
	dict set nameToText newmsg          [mc "Incoming message"]
	dict set nameToText newchatmsg      [mc "Incoming chat"]
	dict set nameToText newchatthread   [mc "New chat thread"]
	dict set nameToText statchange      [mc "Contact presence change"]
	dict set nameToText connected       [mc "Logged in"]
	dict set nameToText groupchatpres   [mc "Chatroom presence change"]

    # Map between sound name and file name for default sound set.
    variable soundIndex
    array set soundIndex {
	online          online.wav
	offline         offline.wav
	newmsg          newmsg.wav
	newchatmsg      newchatmsg.wav
	newchatthread   newchatthread.wav
	statchange      statchange.wav
	connected       connected.wav
	groupchatpres   clicked.wav
    }
    
    variable wqtframe .quicktime::audio

    variable allSounds
    set allSounds [array names soundIndex]
}

proc ::Sounds::GetTextForName {name} {
    variable nameToText 
    return [dict get $nameToText $name]
}

proc  ::Sounds::InitPrefsHook {} {
    global prefs
    variable sprefs
    variable allSounds
    variable priv
    
    set names [::Theme::GetAllWithFilter sound]
    if {$prefs(rootTheme) in $names} {
	set defaultSet $prefs(rootTheme)
    } else {
	set defaultSet [lindex $names 0]
    }
    set priv(defaultSet) $defaultSet
    
    set sprefs(soundSet) $defaultSet
    set sprefs(volume)   100
    set sprefs(midiCmd)  ""
    set sprefs(outputDevice)  ""

    ::PrefUtils::Add [list  \
      [list ::Sounds::sprefs(soundSet) sound_set     $sprefs(soundSet)] \
      [list ::Sounds::sprefs(volume)   sound_volume  $sprefs(volume)]   \
      [list ::Sounds::sprefs(midiCmd)  sound_midiCmd $sprefs(midiCmd)]  \
      [list ::Sounds::sprefs(outputDevice)  sound_outputDevice $sprefs(outputDevice)]  \
      ]
    
    set optL [list]
    foreach name $allSounds {
	set sprefs($name) 1
	lappend optL [list ::Sounds::sprefs($name) sound_${name} $sprefs($name)]
    }
    ::PrefUtils::Add $optL
    
    if {$priv(snack)} {
       # Volume seems to be set globally on snack.
	set sprefs(volume) [snack::audio play_gain]
	# initialize snack audio device
        if {![string equal $sprefs(outputDevice) ""]} {
            snack::audio selectOutput $sprefs(outputDevice)
	}
    }
}

proc ::Sounds::InitEventHooks {} {
    
    # Add all event hooks.
    ::hooks::register quitAppHook             ::Sounds::Free 80
    ::hooks::register newMessageHook          [list ::Sounds::Msg normal newmsg]
    ::hooks::register newChatMessageHook      [list ::Sounds::Msg chat newchatmsg]
    ::hooks::register newGroupChatMessageHook [list ::Sounds::Msg groupchat newchatmsg]
    ::hooks::register newChatThreadHook       [list ::Sounds::Event newchatthread]
    ::hooks::register loginHook               [list ::Sounds::Event connected]
    ::hooks::register presenceNewHook         ::Sounds::Presence

    # Define all hooks for preference settings.
    ::hooks::register prefsBuildHook         ::Sounds::BuildPrefsHook
    ::hooks::register prefsSaveHook          ::Sounds::SavePrefsHook
    ::hooks::register prefsCancelHook        ::Sounds::CancelPrefsHook
    ::hooks::register prefsUserDefaultsHook  ::Sounds::UserDefaultsHook
}

# Sounds::InitHook --
#
#       Make all the necessary initializations, create audio objects.
#       
# Arguments:
#       
# Results:
#       none.

proc ::Sounds::InitHook {} {
    global  this    
    variable allSounds
    variable priv
    variable sprefs
	
    ::Debug 2 "::Sounds::InitHook"

    if {$priv(inited)} {
	return
    }
    
    # Create all sounds from current sound set (which is "" as default).
    if {$priv(canPlay)} {
	
	# Verify that sound set exists.
	if {$sprefs(soundSet) in [GetAllSets]} {
	    set sprefs(soundSet) $priv(defaultSet)
	}
	LoadSoundSet $sprefs(soundSet)
    }
    set priv(inited) 1
}

proc ::Sounds::GetAllSets {} {
    set names [list]
    foreach path [GetAllSoundSetPaths] {
	lappend names [lindex [file split $path] end-1]
    }
    return $names
}

proc ::Sounds::GetAllSoundSetPaths {} {
    
    set soundPaths [list]
    set paths [::Theme::GetAllPathsWithFilter sound]
    foreach path $paths {
	foreach dir [glob -nocomplain -types d -directory $path -- *] {
	    set indFile [file join $dir soundIndex.tcl]
	    if {[file exists $indFile]} {
		lappend soundPaths $dir
	    }
	}
    }
    return $soundPaths
}

proc ::Sounds::GetAllSoundsPresentSet {} {
    variable allSounds
    return $allSounds
}

proc ::Sounds::LoadSoundSet {soundSet} {
    global  this
    variable allSounds
    variable soundIndex
    variable priv
    variable wqtframe

    ::Debug 2 "::Sounds::LoadSoundSet: soundSet=$soundSet"
    
    Free
    unset -nocomplain nameToPath	
    array set sound [array get soundIndex]
    if {$soundSet ni [GetAllSets]} {
	return
    }
    if {$priv(QuickTimeTcl)} {
	frame $wqtframe
    }
    set dir [GetPathForSet $soundSet]
    foreach s $allSounds {
	Create $s [file join $dir $sound($s)]
    }
}

proc ::Sounds::GetPathForSet {soundSet} {
    global this
    return [file join [::Theme::GetPath $soundSet] $this(sounds)]
}

proc ::Sounds::Create {name path} {
    global  this    
    variable priv
    variable nameToPath
    variable wqtframe
    
    # QuickTime doesn't understand vfs; need to copy out to tmp dir.
    if {$priv(QuickTimeTcl)} {
	if {[info exists ::starkit::topdir]} {
	    set path [CopyToTemp $path]
	}
	if {[catch {
	    movie $wqtframe.$name -file $path -controller 0
	}]} {
	    # ?
	}
    } elseif {[file extension $path] eq ".mid"} {
	if {[info exists ::starkit::topdir]} {
	    set path [CopyToTemp $path]
	}
    } elseif {$priv(snack)} {
	
	# Snack seems not to complain about midi files; just no sound.
	if {[catch {
	    snack::sound $name -load $path
	}]} {
	    # ?
	}
    }
    set nameToPath($name) $path
    return $path
}

proc ::Sounds::Play {snd} {
    variable sprefs
    variable priv
    variable afterid

    if {!$priv(inited)} {
	Init
    }
    
    # Check the jabber prefs if sound should be played.
    if {[info exists sprefs($snd)] && !$sprefs($snd)} {
	return
    }
    
    unset -nocomplain afterid($snd)
    DoPlay $snd
}

proc ::Sounds::DoPlay {snd} {
    variable priv
    variable wqtframe
    variable nameToPath
    
    if {$priv(QuickTimeTcl)} {
	if {[catch {$wqtframe.$snd play}]} {
	    # ?
	}
    } elseif {[file extension $nameToPath($snd)] eq ".mid"} {
	PlayMIDI $nameToPath($snd)
    } elseif {$priv(snack)} {
	if {[catch {$snd play}]} {
	    # ?
	}
    }
}

proc ::Sounds::PlayWhenIdle {snd} {
    variable afterid
    variable sprefs
	
    if {![info exists sprefs($snd)] || !$sprefs($snd)} {
	return
    }
    if {![info exists afterid($snd)]} {
	set afterid($snd) 1
	after idle [list ::Sounds::Play $snd]
    }    
}

proc ::Sounds::DoPlayWhenIdle {snd} {
    variable afterid
	
    if {![info exists afterid($snd)]} {
	set afterid($snd) 1
	after idle [list ::Sounds::Play $snd]
    }    
}

proc ::Sounds::PlaySoundTmp {path} {
    global  this
    variable priv
    variable wqtframe
    
    if {$priv(QuickTimeTcl)} {
	if {[info exists ::starkit::topdir]} {
	    set path [CopyToTemp $path]
	}
	destroy $wqtframe._tmp
	catch {
	    movie $wqtframe._tmp -file $path -controller 0
	    $wqtframe._tmp play
	}
    } elseif {[file extension $path] eq ".mid"} {
	if {[info exists ::starkit::topdir]} {
	    set path [CopyToTemp $path]
	}
	PlayMIDI $path 
    } elseif {$priv(snack)} {
	catch {_tmp destroy}
	catch {snack::sound _tmp -load $path}
	catch {_tmp play}
    }
}

proc ::Sounds::CopyToTemp {path} {
    global  this
    
    set tmp [::tfileutils::tempfile $this(tmpPath) sound]
    append tmp [file extension $path]
    file copy -force $path $tmp
    return $tmp
}

proc ::Sounds::PlayMIDI {fileName} {
    variable sprefs
    
    # This is unix only.
    set cmd  [lindex $sprefs(midiCmd) 0]
    set opts [lrange $sprefs(midiCmd) 1 end]
    set mcmd [auto_execok $cmd]
    if {$mcmd ne ""} {
	catch {exec $mcmd $opts $fileName &}
    }
}

proc ::Sounds::Msg {type snd xmldata {uuid ""}} {

    set body [wrapper::getcdata [wrapper::getfirstchildwithtag $xmldata body]]

    # We sometimes get non text stuff messages, like jabber:x:event etc.
    if {![string length $body]} {
	return
    }
    set from [wrapper::getattribute $xmldata from]

    # We shouldn't make noise for our own messages.
    switch -- $type {
	normal {

	}
	chat {
	    set myjid [::Jabber::GetMyJid]
	    set jid2 [jlib::barejid $myjid]
	    if {[string match ${jid2}* $from]} {
		return
	    }
	}
	groupchat {
	    set roomjid [jlib::barejid $from]
	    set myjid [::Jabber::GetMyJid $roomjid]
	    if {[jlib::jidequal $myjid $from]} {
		return
	    }
	}
    }
    PlayWhenIdle $snd
    return
}

proc ::Sounds::Event {snd args} {    
    PlayWhenIdle $snd
}

# Sounds::Presence --
#
#       Makes an alert sound corresponding to the jid's presence status.
#
# Arguments:
#       jid         bare JID
#       presence    "available", "unavailable", or "unsubscribed"
#       args        list of '-key value' pairs of presence attributes.
#       
# Results:
#       roster tree updated.

proc ::Sounds::Presence {jid presence args} {
    
    array set argsA $args
    
    set xmldata $argsA(-xmldata)
    set from [wrapper::getattribute $xmldata from]
    set jid2 [jlib::barejid $from]
    
    set wasAvail [::Jabber::Jlib roster wasavailable $jid]
    
    # Alert sounds.
    if {[::Jabber::Jlib service isroom $jid2]} {
	PlayWhenIdle groupchatpres
    } elseif {[string equal $presence "unavailable"]} {
	PlayWhenIdle offline
    } elseif {$wasAvail} {
	
	# Pling only when also show changed.
	set show ""
	if {[info exists argsA(-show)]} {
	    set show $argsA(-show)
	}
	set oldShow ""
	array set oldPresA [::Jabber::Jlib roster getoldpresence $from]
	if {[info exists oldPresA(-show)]} {
	    set oldShow $oldPresA(-show)
	}
	if {$show ne $oldShow} {
	    PlayWhenIdle statchange
	}
    } elseif {[string equal $presence "available"]} {
	PlayWhenIdle online
    }  
}

proc ::Sounds::Free {} {
    variable priv
    variable allSounds
    variable wqtframe
    
    if {$priv(QuickTimeTcl)} {
	catch {destroy $wqtframe}
    } elseif {$priv(snack)} {
	foreach name $allSounds {
	    catch {$name destroy}
	}
    }
}

# Preference page --------------------------------------------------------------

proc ::Sounds::BuildPrefsHook {wtree nbframe} {
    variable priv
    
    if {$priv(canPlay)} {
	::Preferences::NewTableItem {General Sounds} [mc "Sounds"]
	set wpage [$nbframe page {Sounds}]    
	BuildPrefsPage $wpage
    }
}

proc ::Sounds::BuildPrefsPage {wpage} {
    variable nameToText 
    variable sprefs
    variable tmpPrefs
    variable allSounds
    variable priv

    # System gain can have been changed.
    if {$priv(snack)} {
	set sprefs(volume) [snack::audio play_gain]
        set outputDevices [snack::audio outputDevices]
    }
    
    foreach name $allSounds {
	set tmpPrefs($name) $sprefs($name)
    }
    set tmpPrefs(soundSet)     $sprefs(soundSet)
    set tmpPrefs(volume)       $sprefs(volume)
    set tmpPrefs(midiCmd)      $sprefs(midiCmd)
    set tmpPrefs(outputDevice)  $sprefs(outputDevice)
    set soundSets [GetAllSets]
    
    set wc $wpage.c
    ttk::frame $wc -padding [option get . notebookPageSmallPadding {}]
    pack $wc -side top -anchor [option get . dialogAnchor {}]

    ttk::frame $wc.alrt -padding {0 0 0 6}
    ttk::label $wc.alrt.l -text [mc "Alert sounds"]
    ttk::separator $wc.alrt.s -orient horizontal
    
    grid  $wc.alrt.l  $wc.alrt.s  -sticky w
    grid $wc.alrt.s -sticky ew
    grid columnconfigure $wc.alrt 1 -weight 1
    
    pack  $wc.alrt  -side top -anchor w -fill x
   
    set fss $wc.fss
    ttk::frame $fss
    ttk::label $fss.l -text [mc "Sound set"]:
    ui::combobutton $fss.p -variable [namespace current]::tmpPrefs(soundSet) \
      -menulist [ui::optionmenu::menuList $soundSets]
        
    grid  $fss.l  $fss.p  -sticky w -padx 2
    grid  $fss.p  -sticky ew
    grid columnconfigure $fss 1 -minsize [$fss.p maxwidth]

    ttk::label $wc.lbl -text [mc "Enable or disable sounds below"]:

    set wmid $wc.m
    ttk::frame $wmid

    pack  $wc.fss  -side top -anchor w
    pack  $wc.lbl  -side top -anchor w -pady 8
    pack  $wc.m    -side top
    
    foreach name $allSounds {
	ttk::checkbutton $wmid.c$name -text [dict get $nameToText $name]  \
	  -variable [namespace current]::tmpPrefs($name)
	ttk::button $wmid.b$name -text [mc "Play"] \
	  -command [list [namespace current]::PlayTmpPrefSound $name]
	grid  $wmid.c$name  $wmid.b$name  -sticky w -padx 4 -pady 1
	grid  $wmid.b$name  -sticky ew
    }

    set fvol $wc.fvol
    ttk::frame $fvol
    ttk::label $fvol.l -text [mc "Volume"]:
    ttk::scale $fvol.v -orient horizontal -from 0 -to 100 \
      -variable [namespace current]::tmpPrefs(volume) -value $tmpPrefs(volume)

    pack  $fvol.l  -side left -padx 4
    pack  $fvol.v  -side left -padx 4
    pack  $fvol  -side top -pady 4 -anchor [option get . dialogAnchor {}]

    if {$priv(snack)} {
        set odev $wc.odev
        ttk::frame $odev
        ttk::label $odev.l -text [mc "Sound device"]:
        ui::combobutton $odev.p -variable [namespace current]::tmpPrefs(outputDevice) \
          -menulist [ui::optionmenu::menuList $outputDevices]
        grid $odev.l $odev.p -sticky w -padx 2
        grid $odev.p -sticky ew
        grid columnconfigure $odev 1 -minsize [$odev.p maxwidth]
        pack  $wc.odev  -side top -anchor w
    }

    ttk::button $wc.midi -text [mc "MIDI Player"] -command ::Sounds::MidiPlayer
    pack  $wc.midi -pady 2
    
    bind $wpage <Destroy> {+::Sounds::PrefsFree}
}

proc ::Sounds::MidiPlayer {} {
    variable tmpPrefs
    
    set title [mc "External Midi Player"]
    set ans [ui::megaentry -title $title -message [mc "Set command to use for playing MIDI sounds. This option is only relevant if you use a sound set with MIDI files."] \
      -label [mc "MIDI command"]: -value $tmpPrefs(midiCmd)]
    if {$ans ne ""} {
	set tmpPrefs(midiCmd) [ui::megaentrytext $ans]
    }
}

proc ::Sounds::PlayTmpPrefSound {name} {
    global  this
    variable priv
    variable soundIndex
    variable tmpPrefs
    variable wqtframe
    
    array set sound [array get soundIndex]
    set path [GetPathForSet $tmpPrefs(soundSet)]
    source [file join $path soundIndex.tcl]
    set f [file join $path $sound($name)]

    if {$priv(QuickTimeTcl)} {
	if {[info exists ::starkit::topdir]} {
	    set tmp [::tfileutils::tempfile $this(tmpPath) sound]
	    append tmp [file extension $f]
	    file copy -force $f $tmp
	    set f $tmp
	}
	destroy $wqtframe._tmp
	catch {
	    movie $wqtframe._tmp -file $f -controller 0 \
	      -volume [expr {int($tmpPrefs(volume) * 2.55)}]
	    $wqtframe._tmp play
	}
    } elseif {$priv(snack)} {
	catch {_tmp destroy}
	catch {snack::sound _tmp -load $f}
	catch {_tmp play}
    }
}

proc ::Sounds::SavePrefsHook {} {
    variable sprefs
    variable tmpPrefs
    variable allSounds
    variable priv
    variable wqtframe
    
    if {!$priv(canPlay)} {
	return
    }
    if {[string equal $tmpPrefs(soundSet) [mc "Default"]]} {
	set tmpPrefs(soundSet) ""
    }
    if {![string equal $tmpPrefs(soundSet) $sprefs(soundSet)]} {
	LoadSoundSet $tmpPrefs(soundSet)
    }
    set sprefs(soundSet)     $tmpPrefs(soundSet)
    set sprefs(volume)       $tmpPrefs(volume)
    set sprefs(outputDevice) $tmpPrefs(outputDevice)
    foreach name $allSounds {
	set sprefs($name) $tmpPrefs($name)
    }
    if {$priv(QuickTimeTcl)} {
	foreach wmovie [winfo children $wqtframe] {
	    if {[winfo class $wmovie] eq "Movie"} {
		$wmovie configure -volume [expr {int($sprefs(volume) * 2.55)}]
	    }
	}
    }
    
    # The snack play_gain seems to be set globally on the machine which is BAD!
    if {$priv(snack)} {
	snack::audio play_gain [expr {int($sprefs(volume))}]
	snack::audio selectOutput $sprefs(outputDevice)
    }
    set sprefs(midiCmd) $tmpPrefs(midiCmd)

}

proc ::Sounds::CancelPrefsHook {} {
    variable sprefs
    variable tmpPrefs
    variable allSounds
    variable priv
    
    if {!$priv(canPlay)} {
	return
    }    
    
    foreach name $allSounds {
	if {$sprefs($name) ne $tmpPrefs($name)} {
	    ::Preferences::HasChanged
	}
    }
    if {[string equal $tmpPrefs(soundSet) [mc "Default"]]} {
	set tmpPrefs(soundSet) ""
    }
    if {![string equal $sprefs(soundSet) $tmpPrefs(soundSet)]} {
	::Preferences::HasChanged
    }
    if {![string equal $sprefs(volume) $tmpPrefs(volume)]} {
	::Preferences::HasChanged
    }
    if {![string equal $sprefs(midiCmd) $tmpPrefs(midiCmd)]} {
	::Preferences::HasChanged
    }
    if {![string equal $sprefs(outputDevice) $tmpPrefs(outputDevice)]} {
	::Preferences::HasChanged
    }
}

proc ::Sounds::UserDefaultsHook {} {
    variable sprefs
    variable tmpPrefs
    variable allSounds

    foreach name $allSounds {
	set tmpPrefs($name) $sprefs($name)
    }
    if {[string equal $sprefs(soundSet) ""]} {
	set tmpPrefs(soundSet) [mc "Default"]
    } else {
	set tmpPrefs(soundSet) $sprefs(soundSet)
    }
}

proc ::Sounds::PrefsFree {} {
    variable tmpPrefs
    
    unset -nocomplain tmpPrefs
}

#-------------------------------------------------------------------------------