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
|
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh8.5 "$0" "$@"
if {[llength $argv] == 0} {
puts {Usage: rplay.tcl file [host1:port] [host2:port] ...}
exit
}
# Hostname of where an aserver.tcl is running
# Please edit here to set default host and port number
if {[llength $argv] == 1} {
lappend argv localhost:23654
}
proc PlayFile {file hostport} {
# Open the sound file as a binary file
if [catch {set fd [open $file]} res] {
puts $res
exit
}
fconfigure $fd -translation binary
if {$::tcl_version > 8.0} {
fconfigure $fd -encoding binary
}
# Quick way to set both host and port
foreach {host port} [split $hostport :] break
# Open binary socket connection to aserver.tcl
if [catch {set sock [socket $host $port]} res] {
puts "Error: no aserver.tcl at $host,$port"
exit
}
fconfigure $sock -translation binary -blocking 1
if {$::tcl_version > 8.0} {
fconfigure $sock -encoding binary
}
# Notify the server that a play operation is due
puts -nonewline $sock play
flush $sock
# Get handle for this operation from the server.
# Can be used to stop the operation later. (Not shown here.)
set handle [gets $sock]
# Write audio data as long as the server can accept more
fileevent $sock writable "PlayHandler $fd $sock"
}
proc PlayHandler {fd sock} {
set data [read $fd 10000]
puts -nonewline $sock $data
if [eof $fd] {
close $fd
close $sock
exit
}
}
# Use specified hosts
foreach host [lrange $argv 1 end] {
PlayFile [lindex $argv 0] $host
}
vwait forever
|