~amsn-daily/amsn/amsn-packaging

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
::Version::setSubversionId {$Id$}

# This needs to be done this way because it seems Mac shows an error about conflicting versions if you have 2.2 and 2.2.10 installed...
# but on linux (and windows) it takes the latest version (2.2.10) without complaining...
# Since Mac and Windows have the snack 2.2.10 binaries shipped with amsn,we can require the exact version safely.. linux is trickier because
# the user can install whatever version he wants (and all 2.2.x versions have a 'package provide 2.2')
proc require_snack { } {
	if {[package provide snack] != ""} {
		return
	}  elseif {[OnWin] } {
		if { [catch {
			load [file join utils windows snack2.2 libsnack.dll]
			source [file join utils windows snack2.2 snack.tcl]
		} ] } {
			package require snack 
		}
	} elseif {[OnMac] } {
		if { [catch {
			package require snack 
		} ] } {
			if {[file exists [file join utils macosx snack2.2 [info tclversion] libsnack.dylib]]} {
				load [file join utils macosx snack2.2 [info tclversion] libsnack.dylib]
			} else {
				load [file join utils macosx snack2.2 libsnack.dylib]
			}
			source [file join utils macosx snack2.2 snack.tcl]
		}
	} else {
		package require snack
	}
	
	# If snack didn't get loaded, then an error would have been thrown out and we wouldn't be here setting SnackSettings...
	::audio::init 
}



namespace eval ::audio {
	variable inputDevice
	variable outputDevice
	variable mixerDevice

	################################################################
	#	::audio::init()
	#	This procedure initialises aMSN's audio settings.
	#	Arguments:
	#		- inputDevice => (bool) [NOT REQUIRED] Initialise the inputDevice.
	#		- outputDevice => (bool) [NOT REQUIRED] Initialise the outputDevice.	
	#		- mixerDevice => (bool) [NOT REQUIRED] Initialise the mixerDevice.
	#		- volume => (bool) [NOT REQUIRED] Initialise the volume.
	#		- inputGain => (bool) [NOT REQUIRED] Initialise the inputGain.
	#		- outputGain => (bool) [NOT REQUIRED] Initialise the outputGain.
	#	Return:
	#		Normal => (bool) True.
	proc init { {inputDevice_b "1"} {outputDevice_b "1"} {mixerDevice_b "1"} } {
		variable inputDevice
		variable outputDevice
		variable mixerDevice

		# just to make sure snack is loaded.. if it was already loaded then the first check of require_snack will return immediatly 
		# (avoiding a recursive loop). If snack is not loaded, we return.
		if { [catch {require_snack}] } {
			return
		}

		if { $inputDevice_b } {
			if { [::config::getKey snackInputDevice ""] == "" } {
				::config::setKey snackInputDevice [lindex [getInputDevices] 0]
			}
			setInputDevice [::config::getKey snackInputDevice]
		}
	
		if { $outputDevice_b } {
			if { [::config::getKey snackOutputDevice ""] == "" } {
				::config::setKey snackOutputDevice [lindex [getOutputDevices] 0]
			}
			setOutputDevice [::config::getKey snackOutputDevice]
		}
		
		if { $mixerDevice_b } {
			if { [::config::getKey snackMixerDevice ""] == "" } {
				::config::setKey snackMixerDevice [lindex [getMixerDevices] 0]
			}
			setMixerDevice [::config::getKey snackMixerDevice]
		}
	
		# legacy, it seems like it's needed to avoid gaps in the sound... 
		::snack::audio playLatency 750

		return
	}
	
	################################################################
	#	::audio::getInputDevices()
	# 	Return:
	#		Normal => (list) Avaliable input devices.
	proc getInputDevices { } {
		return [snack::audio inputDevices]
	}
	
	################################################################
	#	::audio::getInputDevice([fallback ""])
	#	This procedure returns the name of the current input device that is in use.
	#	Arguments:
	#		- fallback => (string) [NOT REQUIRED] The fallback value to return if there is no device set.	
	#	Return:
	#		Normal => (string) Input device name.
	proc getInputDevice {{fallback ""}} {
		variable inputDevice
		if {[info exists inputDevice]} {
			return $inputDevice
		} else {
		
			if { $fallback == "" } {
				set fallback [lindex [getInputDevices] 0]
			}
			return [setInputDevice [config::getKey snackInputDevice $fallback]]
		}
	}

	################################################################
	#	::audio::setInputDevice(device, [save])
	#	This procedure sets the input device used by snack.
	#	Arguements:
	#		- device => (string) The name of the device to set.
	#		- save => (boolean) [NOT REQUIRED] save the setting in config
	#	Return:
	#		Normal => (sting) Input device name.
	#		Error	 => (string) ""
	proc setInputDevice {device {save 1}} {
		variable inputDevice

		if { [lsearch -exact [getInputDevices] $device] != -1  } {
			# The device is avaliable.
			if { ![catch {snack::audio selectInput $device} ] } {
				set inputDevice $device
				if {$save} {
					::config::setKey snackInputDevice $device
				}
				return $device
			}
		} 

		# The selected device is not avaliable.
		if {[llength [getInputDevices]] > 0} {
			# If we have devices avaliable default to the first one.
			snack::audio selectInput [lindex [getInputDevices] 0]
			if {$save} {
				return [::config::setKey snackInputDevice [lindex [getInputDevices] 0]]
			} else { 
				return "" 
			}
		} else {
			# We have no devices avaliable, return an empty string.
			return ""
		}
	}
	
	################################################################
	#	::audio::getInputGain()
	#	This procedure returns the gain on the input device.
	#	Return:
	#		Normal => (int) The gain on the input device. (Range 0-100).
	proc getInputGain { } {
		return [snack::audio record_gain]
	}
	
	################################################################
	# ::audio::setInputGain(gain, [save])
	#	Parameters:
	#		- gain => (int) The gain factor to set on the input device. (Range 0-100).
	#		- save => (boolean) [NOT REQUIRED] save the setting in config
	#	Return
	#		Normal => (int) The gain set on the input device.
	proc setInputGain { gain {save 1} } {
		if { $gain < 0 } {
			set gain 0
		} elseif { $gain > 100 } {
			set gain 100
		}
		
		snack::audio record_gain $gain
		
		return [getInputGain]
	}
	
	################################################################
	#	::audio::getOutputDevices()
	# 	Return:
	#		Normal => (list) Avaliable output devices.
	proc getOutputDevices { } {
		return [snack::audio outputDevices]
	}
	
	################################################################
	#	::audio::outputDevice([fallback])
	#	This procedure returns the name of the current output device that is in use.
	#	Arguments:
	#		- fallback => (string) [NOT REQUIRED] The fallback value to return if there is no device set.	
	#	Return:
	#		Normal => (string) Output device name.
	proc getOutputDevice {{fallback ""}} {
		variable outputDevice
		if {[info exists outputDevice]} {
			return $outputDevice
		} else {
			if { $fallback == "" } {
				set fallback [lindex [getOutputDevices] 0]
			}
			return [setOutputDevice [config::getKey snackOutputDevice $fallback]]
		}
	}
	
	################################################################
	#	::audio::setOutputDevice(device, [save])
	#	This procedure sets the output device used by snack.
	#	Arguments:
	#		- device => (string) The name of the device to change to.
	#		- save => (boolean) [NOT REQUIRED] save the setting in config
	#	Return:
	#		Normal => (sting) Output device name.
	#		Error	 => (string) ""
	proc setOutputDevice {device {save 1 }} {
		variable outputDevice

		if { [lsearch -exact [getOutputDevices] $device] != -1  } {
			# The device is avaliable.
			if { ![catch {snack::audio selectOutput $device} ] } {
				set outputDevice $device
				if {$save} {
					::config::setKey snackOutputDevice $device
				} 
				return $device
			}
		} 

		# The selected device is not avaliable.
		if {[llength [getOutputDevices]] > 0} {
			# If we have devices avaliable default to the first one.
			snack::audio selectOutput [lindex [getOutputDevices] 0]
			if {$save} {
				return [::config::setKey snackOutputDevice [lindex [getOutputDevices] 0]]
			} else { 
				return "" 
			}
		} else {
			# Otherwise return an empty string.
			return ""
		}
	}
	
	################################################################
	#	::audio::outputGain()
	#	This procedure returns the gain set on the output device.
	#	Return:
	#		Normal => (int) The gain on the output device. (Range 0-100).
	proc getOutputGain { } {
		return [snack::audio play_gain]
	}
	
	################################################################
	#	::audio::setOutputGain(gain, [save])
	#	Parameters:
	#		- gain => (int) The gain factor to set on the input device. (Range 0-100).
	#		- save => (boolean) [NOT REQUIRED] save the setting in config
	#	Return
	#		Normal => (int) The gain set on the device.
	proc setOutputGain { gain {save 1}} {
		if { $gain < 0 } {
			set gain 0
		} elseif { $gain > 100 } {
			set gain 100
		} 
			
		snack::audio play_gain $gain
		
		return [getOutputGain]
		
	}
	
	################################################################
	#	::audio::getMixerDevices()
	#	This proc lists all the mixer devcices avaliable to snack.
	# 	Return:
	#		Normal => (list) Avaliable mixers.
	proc getMixerDevices { } {
		return [snack::mixer devices]
	}
	
	################################################################
	#	::audio::getMixerDevice([fallback ""])
	#	This procedure returns the name of the current mixer that is in use.
	#	If no mixer is set it sets the mixer to $fallback.
	#	Arguments:
	#		- fallback => (string) [NOT REQUIRED] The fallback value to return if there is no device set.	
	#	Return:
	#		Normal => (string) Mixer name.
	proc getMixerDevice {{fallback ""}} {
		variable mixerDevice
		if {[info exists mixerDevice]} {
			return $mixerDevice
		} else {
			if { $fallback == "" } {
				set fallback [lindex [getMixerDevices] 0]
			}
			return [setMixerDevice [config::getKey snackMixerDevice $fallback]]
		}
	}
	
	################################################################
	#	::audio::setMixerDevice(device, [save])
	#	This procedure sets the mixer used by snack.
	#	Arguements:
	#		- device => (string) The name of the mixer device to use.
	#		- save => (boolean) [NOT REQUIRED] save the setting in config
	#	Return:
	#		Normal => (sting) Mixer name.
	#		Error	 => (string) ""
	proc setMixerDevice { device {save 1} } {
		variable mixerDevice

		if { [lsearch -exact [getMixerDevices] $device] != "-1"  } {
			# The mixer is avaliable.
			if { ![catch { snack::mixer select $device } ] } {
				set mixerDevice $device
				if {$save} {
					::config::setKey snackMixerDevice $device
				}
				return $device
			}
		} 

		# The selected mixer is not avaliable.
		if {[llength [getMixerDevices]] > 0} {
			# If we have mixers avaliable default to the first one.
			snack::mixer select [lindex [getMixerDevices] 0]
			if {$save} {
				return [::config::setKey snackMixerDevice [lindex [getMixerDevices] 0]]
			} else { 
				return "" 
			}
		} else {
			# Otherwise return an empty string.
			return ""
		}
	}
	
	################################################################
	#	::audio::getVolume()
	#	This procedure returns the volume of the mixer we are currently using.
	#	This equalises the volume for stereo devices to ensure both are at the same level.
	#	Arguments:
	#		- line => (string) [NOT REQUIRED] The output line for the device.
	#	Return:
	#		Normal	=> (int) Volume.
	proc getVolume {{line ""}} {
		# the following line is just to make sure the appropriate mixer was selected
		::audio::getMixerDevice
		
		if {$line == "" } {
			set line [lindex [snack::mixer lines] 0]
		}
		
		set ret [snack::mixer volume $line]
		#snack returns sometimes weird results
		if {![string is integer -strict $ret]} {
			set ret 0
		}
		return $ret
	}
	
	################################################################
	#	::audio::setVolume(volume, [save], [line])
	#	This procedure sets snack's internal volume.
	#	Arguments:
	#		- volume => (int) The playback volume to set on the mixer. (Range 0-100)
	#		- save => (boolean) [NOT REQUIRED] save the setting in config
	#		- line => (string) [NOT REQUIRED] The output line (default=[lindex [snack::mixer lines] 0]).
	#	Return:
	#		Normal => (int) Volume
	#		Error	 => (int) Volume
	proc setVolume { volume { line "" } } {

		# the following line is just to make sure the appropriate mixer was selected
		::audio::getMixerDevice
		
		if { [llength [snack::mixer lines]] == "0" } {
			# We have no input lines. (Bad setup).
			return ""
		}
		
		if { $line == "" || [lsearch -exact [snack::mixer lines] $line] == "-1" } {
			# The line chosen doesn't exist, so use the first one avaliable.
			set line [lindex [snack::mixer lines] 0]
		}
		

		# This damn crappy thing is crap! you can't 'set' the volume, you can just link it to a variable, then you change that variable's value to change the volume!! interesting though...
		set ::audio::volume_$line $volume
		if {[snack::mixer channels $line] == "Mono"} {
			snack::mixer volume $line ::audio::volume_$line
		} else {
			snack::mixer volume $line ::audio::volume_$line ::audio::volume_$line
		}
		
		
		return [getVolume $line]
		
	}
	
	################################################################
	#	::audio::clearConfig()
	#	This procedure clears the config that we've set.
	#	Return:
	#		Normal => (bool) True
	proc clearConfig {} {
		::config::setKey snackInputDevice ""
		::config::setKey snackOutputDevice ""
		::config::setKey snackMixerDevice ""
	}
}


catch {require_snack}