<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at                              |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Author: David Jonathan Grant <david@davidjonathangrant.info>         |
// +----------------------------------------------------------------------+

require_once("PEAR.php");

/**
* Class for accessing various information about Ogg Vorbis Files.
*
* @access public
* @package File
* @author David Jonathan Grant <david@davidjonathangrant.info>
*/
class File_Vorbis extends PEAR
{
	var $mComments = array();
	var $mFilePointer;
	var $mVendor;
	var $mVersion;
	var $mChannels;
	var $mSampleRate;
	var $mMinRate;
	var $mMaxRate;
	var $mNominalRate;
	
    function File_Vorbis($filePath)
    {
		define("VORBIS_CAPTURE_PATTERN", "OggS");
        $this->mFilePointer = fopen($filePath, "rb") or exit ("FOO");
        $this->parseInformation();
        $this->parseComments();
        fclose($this->mFilePointer);
    }
    
    function parseInformation()
    {
    	$this->seekHeader();
    	$version = unpack("V1data", fread($this->mFilePointer, 4));
    	$this->mVersion = $version['data'];
    	$channels = unpack("C1data", fread($this->mFilePointer, 1));
    	$this->mChannels = $channels['data'];
    	$sample_rate = unpack("V1data", fread($this->mFilePointer, 4));
    	$this->mSampleRate = $sample_rate['data'];
    	$bitrate['max'] = unpack("V1data", fread($this->mFilePointer, 4));
    	$this->mMaxRate = $bitrate['max']['data'];
    	$bitrate['nom'] = unpack("V1data", fread($this->mFilePointer, 4));
    	$this->mNominalRate = $bitrate['nom']['data'];
    	$bitrate['min'] = unpack("V1data", fread($this->mFilePointer, 4));
    	$this->mMinRate = $bitrate['min']['data'];
    }
    
    function parseComments()
    {
    	// Advance to the second page.
    	$this->seekHeader();
    	// Find the length of the Vendor string.
        $data = unpack("V1vendor_length", fread($this->mFilePointer, 4));
        $this->mVendor = fread($this->mFilePointer, $data['vendor_length']);
        // How many comments are in the Vorbis file?
        $data = unpack("V1comment_list_length", fread($this->mFilePointer, 4));
        // Loop through the comments list and write them to the internal array.
        for ($i = 0; $i < $data['comment_list_length']; $i++)
        {
        	$comment_length = unpack("V1data", fread($this->mFilePointer, 4));
        	$comment = explode("=", fread($this->mFilePointer, $comment_length['data']));
        	$comment_title = (string) $comment[0];
        	$comment_value = (string) $comment[1];
        	if (isset($this->mComments[$comment_title]))
        	{
        		if (is_array($this->mComments[$comment_title]))
        		{
        			$this->mComments[$comment_title][] = $comment_value;
        		}
        		else
        		{
        			$this->mComments[$comment_title] = array($this->mComments[$comment_title], $comment_value);
        		}
        	}
        	else
        	{
        		$this->mComments[$comment_title] = $comment_value;
        	}
        }
    }
    
    function getCommentList()
    {
      	return (array_keys($this->mComments));
	}
    
    function getComment($commentTitle)
    {
    	if (isset($this->mComments[$commentTitle]))
    	{
    		if (is_array($this->mComments[$commentTitle]))
    		{
    			return (explode(", ", $this->mComments[$commentTitle]));
    		}
    		else
    		{
    			return ($this->mComments[$commentTitle]);
    		}
    	}
    	else
    	{
    		echo ("ERROR");
    	}
    }

    function seekHeader()
    {
        $stream = fread($this->mFilePointer, 4);
        while ($stream != VORBIS_CAPTURE_PATTERN) {
            $stream = substr($stream, 1) . fread($this->mFilePointer, 1);
        }
        // Skip out all the crap.
        fseek($this->mFilePointer, 22, SEEK_CUR);
        $data = unpack("c1segments", fread($this->mFilePointer, 1));
        fseek($this->mFilePointer, $data['segments'], SEEK_CUR);
    	fseek($this->mFilePointer, 7, SEEK_CUR);
    }
}
?>
