Skip to main content

home entertainment part3

talking to winamp

All interaction with winamp is via a control script that will respond to ajax calls.
The script also handles the playlist. There is only one script for all the winamp instances, the configuration is in a 'ini' file.

the ini file looks like this:


[player1]
location = "10.0.0.1"
pass = 1234
port = 4800
name = test

[player2]
location = "10.0.0.2"
pass = 1234
port = 4800
name = studio

[player3]
location = "10.0.0.3"
pass = 1234
port = 4800
name = lab

[playlist]
location = "c:\xampp\htdocs\collection.txt"

[players]
max = 3


The playlist contains all available tracks, more about that later.

control script

Basicly the script combines some httpQ commands like when you send a 'play' command the response is the time remaining.
I will not dive into the details, however there are a couple of problems I had to solve.

Prevent caching, I used the solution below, this goes at the very beginning of the script.


<?php
// prevent caching (php)
header('Cache-Control: no-cache');
header('Pragma: no-cache');
header('Expires: ' . gmdate(DATE_RFC1123, time()-1));

?>



At first I tried to use one of the more advanced functions that php5 provides to talk to httpQ. This worked but unfortunately not all of the time, so I switched to the code provided on the httpQ website. This code however is not up to date, the function accepts only one argument and for a number of commands there are 2 or more arguments. In my fix the complete httpQ_argument string is the argument for the function.

example: httpQ('getid3tag','tags=t,a,l&delim=|')

I changed a few things:
  • left out the size limit for the fgets
  • utf8 encoded the response to obtain some meaningfull playlist titles
  • added a timeout of 3 seconds in the fscockopen command

$_GET['p'] is 'player[x]' like in the ini file.


function httpQ($command, $arg)
{
$fp;
(isset($_GET['p'])) ? $p = $_GET['p'] : $p = '';
$setup = parse_ini_file("winamp.ini",true);
if($p != '')
{
$server=$setup[$p]['location'];
$pass = $setup[$p]['pass'];
$port = $setup[$p]['port'];
$fp = fsockopen($server, $port, &$errno, &$errstr,3);
}
if(!$fp)
{
echo "($errno)\n";
}
else
{
if($arg == "")
{
$msg = "GET /$command?p=$pass HTTP/1.0\r\n\r\n";
}
else
{
$msg = "GET /$command?p=$pass&$arg HTTP/1.0\r\n\r\n";
}

fputs($fp, $msg);
while(!feof($fp))
{
$response = utf8_encode(fgets($fp));
}
fclose($fp);
}


return $response;
}



handling the playlist

The playlist is maintained by a a separate script. You could have all the musicfiles in a database but I use a 'playlist'file that contains all the tracks. I use this 15000 lines file to compose the playlist.
Again there were two things needed to make it work:
  • utf8_encode the titles in the script output
  • rawurlencode the titles when passing them to httpQ

internet radio

The configuration for the radio streams is in a separate ini file.

Like this:

[1]
name = groove
url = http://somafm.com/groovesalad56.pls
[2]
name = wumb
url = "http://www.live365.com/play/307208"
[3]
name = kcrw
url = http://64.12.61.3:80/stream/1046


Next: putting it all together.

Comments