Jukebox based on webserver with Tcl cgi scripts

TV

I played an hour with a server and a cgi tcl script to create a Jukebox accessable from the web (A Nokia Communicator with always on internet or some portable computer) to have a big list of songs on a page to click-and-play.

The server where this image is on, too:

Image TV Wiki jukebox.png

has a lot of music files stored on it (about 2k songs from legally rented CDs, and never shared with anyone, and it stays that way) and it would be handy to sit somewhere (like in the garden) and click a song to play it on my big audio system, isn't it?

So I made these two Tcl scripts, one in the /var/www/cgi directory called mus.tcl:

 #!/bin/sh
 # \
 exec tclsh "$0" "$@"
 #
   puts stdout "Content-type: text/html\n"
   puts stdout "<html>"
   puts stdout "<a href=\"http://www.theover.org/cgi-bin/mus.tcl\">This page</a> lets you play a music file on my server."
   puts stdout "<P>"
   flush stdout

   if {[catch {set s [socket localhost 4328]}] != 0} {
      puts stdout "<b>The jukebox is inactive!</b>"
      puts stdout "<P><a href=\"http://www.theover.org\">www.theover.org</a>"
      puts stdout "</html>"
      flush stdout
      exit
   }
   set n -1
   if {[string compare $env(QUERY_STRING) ""] != 0}  {
    set n [lindex [split $env(QUERY_STRING) \#] 0]
    puts $s $n
    flush $s
   } {
      puts $s -1
      flush $s
   }
   set j 0
   foreach i [glob /somepath/Music/*.wav] {
     if {$n != $j} {
      puts stdout "$j <a href=\"/cgi-bin/mus.tcl?$j#prev\">[string range [lindex [split $i .] 0] [string length "/somepath/Music/"] end]</a><br>"
     } {
      puts stdout "<a name=prev> </a>$j <a href=\"/cgi-bin/mus.tcl?$j#prev\"><i>[string range [lindex [split $i .] 0] [string length "/somepath/Music/"] end]</i></a><br>"
     }
      flush stdout
      incr j
   }
   puts stdout "done"
   puts stdout "<P><a href=\"http://www.theover.org\">www.theover.org</a>"
   puts stdout "</html>"
   flush stdout
   exit

Using the Apache 2 server this seems to run fine, and without the next script running simply returns "Jukebox not active", without 
doing anything, otherwise it lists the audio files, and communicates the number of the clicked file to the server script.

In a normal user (the one with access to the audio hardware, being the logged in one) bash shell I start this server script which 
plays the requested audio file using mplayer (which must be installed of course):

 set ma [glob /somepath/Music/*.wav]

 set ss [socket -server servmus 4328]

  proc servmus { s i p } {
   global ma
   after 2000 close $s
   set n [read $s 10]
   puts $n
   flush stdout
   if {$n == -1} return
   catch {exec killall mplayer}
   puts [lindex $ma $n] 
   flush stdout
   exec mplayer  -quiet -ao alsa:noblock:device=hw=0 -cache 4096 [lindex $ma $n] &
 }

 vwait forever

Presto!

Of course I am aware of the need to change something about the names being put on the list page, such that all kinds of html problems are prevented. I guess at least quotes should be replaced. But hey, this is a editable wiki, have a go at it!