File: Speech.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 (402 lines) | stat: -rw-r--r-- 10,683 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
#  Speech.tcl ---
#  
#      Implements platform independent synthetic speech.
#      
#  Copyright (c) 2003-2007  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: Speech.tcl,v 1.19 2008-03-30 10:00:41 matben Exp $

namespace eval ::Speech {
    variable sprefs

    set sprefs(package) ""
    set sprefs(haveSpeech) 0
    
    switch -- $::this(platform) {
	macosx {
	    if {[catch {package require TclSpeech}]} {
		return
	    }
	    set sprefs(haveSpeech) 1
	    set sprefs(package) TclSpeech
	}
	windows {
	    if {[catch {package require MSSpeech}]} {
		return
	    }
	    set sprefs(haveSpeech) 1
	    set sprefs(package) MSSpeech
	}
	default {
	    return
	}
    }

    component::define Speech \
      "Text-to-speech on Macs using TclSpeech and on Windows using MSSpeech"
}

# Speech::Load --
# 
#       Tries to load the speech component.

proc ::Speech::Load {} {
    
    ::Debug 2 "::Speech::Load"    
    
    # Set up all hooks.
    Init
    
    # We should register ourselves.
    component::register Speech
}

proc ::Speech::Init {} {
        
    ::Debug 2 "::Speech::Init"

    # Hooks to run when message displayed to user.
    ::hooks::register displayMessageHook          [list ::Speech::SpeakMessage normal]
    ::hooks::register displayChatMessageHook      ::Speech::SpeakMessage2
    ::hooks::register displayGroupChatMessageHook ::Speech::SpeakMessage2
    ::hooks::register whiteboardTextInsertHook    ::Speech::SpeakWBText

    # Define all hooks for preference settings.
    ::hooks::register prefsInitHook          ::Speech::InitPrefsHook
    ::hooks::register prefsBuildHook         ::Speech::BuildPrefsHook
    ::hooks::register prefsSaveHook          ::Speech::SavePrefsHook
    ::hooks::register prefsCancelHook        ::Speech::CancelPrefsHook
    ::hooks::register prefsUserDefaultsHook  ::Speech::UserDefaultsPrefsHook
}

# Speech::Verify --
# 
#       Verifies that we actually have a speech package.
#       Also checks voices available.

proc ::Speech::Verify {} {
    global  this
    variable sprefs
  
    set plat $this(platform)
    set voices [SpeakGetVoices]

    # Voices consistency check.
    if {([lsearch $voices $sprefs(voiceUs-$plat)] < 0) || \
      ($sprefs(voiceUs-$plat) eq "")} {
	set sprefs(voiceUs-$plat) [lindex $voices 0]
    }
    if {([lsearch $voices $sprefs(voiceOther-$plat)] < 0) || \
      ($sprefs(voiceOther-$plat) eq "")} {
	set sprefs(voiceOther-$plat) [lindex $voices 1]
    }
    
    # Always keep shortcut names.
    set sprefs(voiceUs)    $sprefs(voiceUs-$plat)
    set sprefs(voiceOther) $sprefs(voiceOther-$plat)
}

proc ::Speech::SpeakMessage2 {xmldata} {
    variable sprefs

    set from [wrapper::getattribute $xmldata from]
    set type [wrapper::getattribute $xmldata type]
    set subject [wrapper::getcdata [wrapper::getfirstchildwithtag $xmldata subject]]
    set body    [wrapper::getcdata [wrapper::getfirstchildwithtag $xmldata body]]

    switch -- $type {
	chat {
	    if {$sprefs(speakChat)} {
		set myjid [::Jabber::GetMyJid]
		set jid2 [jlib::barejid $myjid]
		if {[string match ${jid2}* $from]} {
		    set voice $sprefs(voiceUs)
		    set txt "I say, "
		} else {
		    set voice $sprefs(voiceOther)
		    set txt " , "
		}
		append txt $body
		Speak $txt $voice
	    }
	}
	groupchat {
	    if {$sprefs(speakChat)} {
		jlib::splitjid $from roomjid res
		set myjid [::Jabber::GetMyJid $roomjid]
		if {[string equal $myjid $from]} {
		    Speak $body $sprefs(voiceUs)
		} else {
		    Speak $body $sprefs(voiceOther)
		}
	    }
	}
    }
    return
}

proc ::Speech::SpeakMessage {type body args} {
    variable sprefs
    
    array set argsArr $args
    set from ""
    if {[info exists argsArr(-from)]} {
	set from $argsArr(-from)
    }
    
    switch -- $type {
	normal {
	    if {$sprefs(speakMsg)} {
		set txt " "
		if {[info exists argsArr(-subject)] && \
		  ($argsArr(-subject) ne "")} {
		    append txt "Subject is $argsArr(-subject). "
		}
		append txt $body
		Speak $txt $sprefs(voiceOther)
	    }
	}
	chat {
	    if {$sprefs(speakChat)} {
		set myjid [::Jabber::GetMyJid]
		jlib::splitjid $myjid jid2 res
		if {[string match ${jid2}* $from]} {
		    set voice $sprefs(voiceUs)
		    set txt "I say, "
		} else {
		    set voice $sprefs(voiceOther)
		    set txt " , "
		}
		append txt $body
		Speak $txt $voice
	    }
	}
	groupchat {
	    if {$sprefs(speakChat)} {
		jlib::splitjid $from roomjid res
		set myjid [::Jabber::GetMyJid $roomjid]
		if {[string equal $myjid $from]} {
		    Speak $body $sprefs(voiceUs)
		} else {
		    Speak $body $sprefs(voiceOther)
		}
	    }
	}
    }
}

proc ::Speech::SpeakWBText {who str} {
    variable sprefs
        
    set punct {[.,;?!]}
    if {$sprefs(speakWBText) && [string match *${punct}* $str] && ($str ne "")} {
	set key [string map {me Us other Other} $who]
	set voice $sprefs(voice$key)
	Speak $str $voice
    }
}

proc ::Speech::Speak {msg {voice {}}} {
    global  this
    
    switch -- $this(platform) {
	macosx {
	    ::Mac::Speech::Speak $msg $voice
	}
	windows {
	    ::MSSpeech::Speak $msg $voice
	}
	unix {
	    # empty.
	}
    }
}

proc ::Speech::SpeakGetVoices {} {
    global  this
    
    switch -- $this(platform) {
	macosx {
	    return [speech::speakers]
	}
	windows {
	    return [::MSSpeech::GetVoices]
	}
	unix {
	    return
	}
    }
}

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

proc ::Speech::InitPrefsHook {} {
    
    variable sprefs
       
    ::Debug 2 "::Speech::InitPrefsHook sprefs(haveSpeech)=$sprefs(haveSpeech)"
    
    # Default in/out voices.
    set sprefs(voiceUs-macosx)     ""
    set sprefs(voiceOther-macosx)  ""
    set sprefs(voiceUs-windows)    ""
    set sprefs(voiceOther-windows) ""

    set sprefs(speakMsg)      0
    set sprefs(speakChat)     0
    set sprefs(speakWBText)   0

    ::PrefUtils::Add [list  \
      [list ::Speech::sprefs(speakMsg)    speakMsg        $sprefs(speakMsg)]    \
      [list ::Speech::sprefs(speakChat)   speakChat       $sprefs(speakChat)]   \
      [list ::Speech::sprefs(speakWBText) speakWBText     $sprefs(speakWBText)] \
      [list ::Speech::sprefs(voiceUs-macosx)     speakVoiceUs-macosx    $sprefs(voiceUs-macosx)]     \
      [list ::Speech::sprefs(voiceOther-macosx)  speakVoiceOther-macosx $sprefs(voiceOther-macosx)] \
      [list ::Speech::sprefs(voiceUs-windows)     speakVoiceUs-windows    $sprefs(voiceUs-windows)]     \
      [list ::Speech::sprefs(voiceOther-windows)  speakVoiceOther-windows $sprefs(voiceOther-windows)] \
      ]

    #
    Verify
}

proc ::Speech::BuildPrefsHook {wtree nbframe} {
    variable sprefs
    
    if {$sprefs(haveSpeech)} {
	# TRANSLATORS; Only on Mac OS X and Windows in preferences.
	::Preferences::NewTableItem {General {Speech}} [mc "Text-to-Speech"]

	set wpage [$nbframe page {Speech}]    
	BuildPrefsPage $wpage
    }
}

proc ::Speech::SavePrefsHook {} {
    variable sprefs
    variable tmpPrefs

    if {$sprefs(haveSpeech)} {
	array set sprefs [array get tmpPrefs]
	foreach {key value} [array get tmpPrefs] {
	    set sprefs($key) $tmpPrefs($key)
	}
	Verify
    }
}

proc ::Speech::CancelPrefsHook {} {
    variable sprefs
    variable tmpPrefs
    
    if {$sprefs(haveSpeech)} {

	# Detect any changes.
	foreach {key value} [array get tmpPrefs] {
	    if {![string equal $sprefs($key) $tmpPrefs($key)]} {
		::Preferences::HasChanged
		return
	    }
	}
    }
}

proc ::Speech::UserDefaultsPrefsHook {} {
    variable sprefs
    variable tmpPrefs
    
    array set tmpPrefs [array get sprefs]
}

proc ::Speech::BuildPrefsPage {page} {
    global  this
    variable sprefs
    variable tmpPrefs
        
    array set tmpPrefs [array get sprefs]
    set plat $this(platform)
    
    set wc $page.c
    ttk::frame $wc -padding [option get . notebookPageSmallPadding {}]
    pack $wc -side top -anchor [option get . dialogAnchor {}]

    ttk::frame $wc.head -padding {0 0 0 6}
    ttk::label $wc.head.l -text [mc "Text-to-Speech"]
    ttk::separator $wc.head.s -orient horizontal

    grid  $wc.head.l  $wc.head.s
    grid $wc.head.s -sticky ew
    grid columnconfigure $wc.head 1 -weight 1
    pack  $wc.head  -side top -fill x

    set wfr $wc.f
    ttk::frame $wfr
    pack  $wfr  -side top
    
    ttk::checkbutton $wfr.speak     -text [mc "Text-to-speech for whiteboard text"]  \
      -variable [namespace current]::tmpPrefs(speakWBText)
    ttk::checkbutton $wfr.speakmsg  -text [mc "Text-to-speech for messages"]  \
      -variable [namespace current]::tmpPrefs(speakMsg)
    ttk::checkbutton $wfr.speakchat -text [mc "Text-to-speech for chat"]  \
      -variable [namespace current]::tmpPrefs(speakChat)

    grid $wfr.speak      -sticky w
    grid $wfr.speakmsg   -sticky w
    grid $wfr.speakchat  -sticky w
    
    if {$sprefs(haveSpeech)} {
	
	# Get a list of voices
	set voicelist [concat None [SpeakGetVoices]]
    } else {
	set voicelist {None}
	$wfr.speak     state {disabled}
	$wfr.speakmsg  state {disabled}
	$wfr.speakchat state {disabled}
	set tmpPrefs(SpeechOn) 0
    }
    
    set wfvo $wfr.fvo
    ttk::frame $wfvo
    grid  $wfvo  -pady 4
    
    ttk::label $wfvo.in  -text [mc "Select voice for incoming text"]:
    ttk::label $wfvo.out -text [mc "Select voice for outgoing write"]:
    ui::combobutton $wfvo.pin \
       -variable [namespace current]::tmpPrefs(voiceOther-$plat) \
       -menulist [ui::optionmenu::menuList $voicelist]
    ui::combobutton $wfvo.pout \
       -variable [namespace current]::tmpPrefs(voiceUs-$plat) \
       -menulist [ui::optionmenu::menuList $voicelist]
    
    grid  $wfvo.in   $wfvo.pin   -sticky e -padx 2 -pady 1
    grid  $wfvo.out  $wfvo.pout  -sticky e -padx 2 -pady 1
    grid  $wfvo.pin  $wfvo.pout  -sticky ew

    if {!$sprefs(haveSpeech)} {
	$wfvo.pin  state {disabled}
	$wfvo.pout state {disabled}
    }    
    
    bind $page <Destroy> +::Speech::Free
}

proc ::Speech::Free {} {
    variable tmpPrefs
    unset -nocomplain tmpPrefs
}

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