1#!/bin/sh
2# the next line restarts using tclsh \
3exec tclsh8.4 "$0" "$@"
4
5if {[llength $argv] == 0} {
6  puts {Usage: rplay.tcl file [host1:port] [host2:port] ...}
7  exit
8}
9
10# Hostname of where an aserver.tcl is running
11# Please edit here to set default host and port number
12
13if {[llength $argv] == 1} {
14  lappend argv localhost:23654
15}
16
17proc PlayFile {file hostport} {
18  # Open the sound file as a binary file
19
20  if [catch {set fd [open $file]} res] {
21    puts $res
22    exit
23  }
24  fconfigure $fd -translation binary
25  if {$::tcl_version > 8.0} {
26    fconfigure $fd -encoding binary
27  }
28
29  # Quick way to set both host and port
30
31  foreach {host port} [split $hostport :] break
32
33  # Open binary socket connection to aserver.tcl
34
35  if [catch {set sock [socket $host $port]} res] {
36    puts "Error: no aserver.tcl at $host,$port"
37    exit
38  }
39  fconfigure $sock -translation binary -blocking 1
40  if {$::tcl_version > 8.0} {
41    fconfigure $sock -encoding binary
42  }
43
44  # Notify the server that a play operation is due
45
46  puts -nonewline $sock play
47  flush $sock
48
49  # Get handle for this operation from the server.
50  # Can be used to stop the operation later. (Not shown here.)
51
52  set handle [gets $sock]
53
54  # Write audio data as long as the server can accept more
55
56  fileevent $sock writable "PlayHandler $fd $sock"
57}
58
59proc PlayHandler {fd sock} {
60  set data [read $fd 10000]
61  puts -nonewline $sock $data
62  if [eof $fd] {
63    close $fd
64    close $sock
65    exit
66  }
67}
68
69# Use specified hosts
70
71foreach host [lrange $argv 1 end] {
72    PlayFile [lindex $argv 0] $host
73}
74
75vwait forever
76