File_Ogg
[ class tree: File_Ogg ] [ index: File_Ogg ] [ all elements ]

Source for file Media.php

Documentation is available at Media.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------------+
  4. // | File_Ogg PEAR Package for Accessing Ogg Bitstreams                         |
  5. // | Copyright (c) 2005-2007                                                    |
  6. // | David Grant <david@grant.org.uk>                                           |
  7. // | Tim Starling <tstarling@wikimedia.org>                                     |
  8. // +----------------------------------------------------------------------------+
  9. // | This library is free software; you can redistribute it and/or              |
  10. // | modify it under the terms of the GNU Lesser General Public                 |
  11. // | License as published by the Free Software Foundation; either               |
  12. // | version 2.1 of the License, or (at your option) any later version.         |
  13. // |                                                                            |
  14. // | This library is distributed in the hope that it will be useful,            |
  15. // | but WITHOUT ANY WARRANTY; without even the implied warranty of             |
  16. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU          |
  17. // | Lesser General Public License for more details.                            |
  18. // |                                                                            |
  19. // | You should have received a copy of the GNU Lesser General Public           |
  20. // | License along with this library; if not, write to the Free Software        |
  21. // | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA |
  22. // +----------------------------------------------------------------------------+
  23.  
  24. require_once('File/Ogg/Bitstream.php');
  25.  
  26. /**
  27.  * Parent class for media bitstreams
  28.  * Contains some functions common to various media formats
  29.  */
  30. abstract class File_Ogg_Media extends File_Ogg_Bitstream
  31. {
  32.     /**
  33.      * Array to hold each of the comments.
  34.      *
  35.      * @access  private
  36.      * @var     array 
  37.      */
  38.     var $_comments = array();
  39.     /**
  40.      * Vendor string for the stream.
  41.      *
  42.      * @access  private
  43.      * @var     string 
  44.      */
  45.     var $_vendor;
  46.  
  47.     /**
  48.      * Length of the stream in seconds
  49.      */
  50.     var $_streamLength;
  51.  
  52.     /*function File_Ogg_Media($streamSerial, $streamData, $filePointer)
  53.     {
  54.         File_Ogg_Bitstream::File_Ogg_Bitstream($streamSerial, $streamData, $filePointer);
  55.     }*/
  56.  
  57.     /**
  58.      * Get a short string describing the type of the stream
  59.      * @return string 
  60.      */
  61.     abstract function getType();
  62.  
  63.     /**
  64.      * Get the 6-byte identification string expected in the common header
  65.      * @return string 
  66.      */
  67.     function getIdentificationString()
  68.     {
  69.         return '';
  70.     }
  71.  
  72.     /**
  73.      * @access  private
  74.      * @param   int     $packetType 
  75.      * @param   int     $pageOffset 
  76.      */
  77.     function _decodeCommonHeader($packetType$pageOffset)
  78.     {
  79.         fseek($this->_filePointer$this->_streamList[$pageOffset]['body_offset']SEEK_SET);
  80.         if ($packetType !== false{
  81.             // Check if this is the correct header.
  82.             $packet unpack("Cdata"fread($this->_filePointer1));
  83.             if ($packet['data'!= $packetType)
  84.                 throw new PEAR_Exception("Stream Undecodable"OGG_ERROR_UNDECODABLE);
  85.         
  86.             // The following six characters should be equal to getIdentificationString()
  87.             $id $this->getIdentificationString();
  88.             if ($id !== '' && fread($this->_filePointerstrlen($id)) !== $id)
  89.                 throw new PEAR_Exception("Stream is undecodable due to a malformed header."OGG_ERROR_UNDECODABLE);
  90.         // else seek only, no common header
  91.     }
  92.  
  93.     /**
  94.      * Parse a Vorbis-style comments header.
  95.      *
  96.      * This function parses the comments header.  The comments header contains a series of
  97.      * UTF-8 comments related to the audio encoded in the stream.  This header also contains
  98.      * a string to identify the encoding software.  More details on the comments header can
  99.      * be found at the following location: http://xiph.org/vorbis/doc/v-comment.html
  100.      *
  101.      * @access  private
  102.      */
  103.     function _decodeBareCommentsHeader()
  104.     {
  105.         // Decode the vendor string length as a 32-bit unsigned integer.
  106.         $vendor_len unpack("Vdata"fread($this->_filePointer4));
  107.         if $vendor_len['data'> 0 {
  108.             // Retrieve the vendor string from the stream.
  109.             $this->_vendor  fread($this->_filePointer$vendor_len['data']);
  110.         else {
  111.             $this->_vendor '';
  112.         }
  113.         // Decode the size of the comments list as a 32-bit unsigned integer.
  114.         $comment_list_length unpack("Vdata"fread($this->_filePointer4));
  115.         // Iterate through the comments list.
  116.         for ($i = 0; $i $comment_list_length['data']; ++$i{
  117.             // Unpack the length of this comment.
  118.             $comment_length unpack("Vdata"fread($this->_filePointer4));
  119.             // Comments are in the format 'ARTIST=Super Furry Animals', so split it on the equals character.
  120.             // NOTE: Equals characters are strictly prohibited in either the COMMENT or DATA parts.
  121.             $comment        explode("="fread($this->_filePointer$comment_length['data']));
  122.             $comment_title  = (string) $comment[0];
  123.             $comment_value  = (string) utf8_decode($comment[1]);
  124.     
  125.             // Check if the comment type (e.g. ARTIST) already exists.  If it does,
  126.             // take the new value, and the existing value (or array) and insert it
  127.             // into a new array.  This is important, since each comment type may have
  128.             // multiple instances (e.g. ARTIST for a collaboration) and we should not
  129.             // overwrite the previous value.
  130.             if (isset($this->_comments[$comment_title])) {
  131.                 if (is_array($this->_comments[$comment_title]))
  132.                     $this->_comments[$comment_title][$comment_value;
  133.                 else
  134.                     $this->_comments[$comment_title= array($this->_comments[$comment_title]$comment_value);
  135.             else
  136.                 $this->_comments[$comment_title$comment_value;
  137.         }
  138.     }
  139.     
  140.     /**
  141.      * Provides a list of the comments extracted from the Vorbis stream.
  142.      *
  143.      * It is recommended that the user fully inspect the array returned by this function
  144.      * rather than blindly requesting a comment in false belief that it will always
  145.      * be present.  Whilst the Vorbis specification dictates a number of popular
  146.      * comments (e.g. TITLE, ARTIST, etc.) for use in Vorbis streams, they are not
  147.      * guaranteed to appear.
  148.      *
  149.      * @access  public
  150.      * @return  array 
  151.      */
  152.     function getCommentList()
  153.     {
  154.         return (array_keys($this->_comments));
  155.     }
  156.  
  157.     /**
  158.      * Provides an interface to the numerous comments located with a Vorbis stream.
  159.      *
  160.      * A Vorbis stream may contain one or more instances of each comment, so the user
  161.      * should check the variable type before printing out the result of this method.
  162.      * The situation in which multiple instances of a comment occurring are not as
  163.      * rare as one might think, since they are conceivable at least for ARTIST comments
  164.      * in the situation where a track is a duet.
  165.      *
  166.      * @access  public
  167.      * @param   string  $commentTitle   Comment title to search for, e.g. TITLE.
  168.      * @param   string  $separator      String to separate multiple values.
  169.      * @return  string 
  170.      */
  171.     function getField($commentTitle$separator ", ")
  172.     {
  173.     if (isset($this->_comments[$commentTitle])) {
  174.         if (is_array($this->_comments[$commentTitle]))
  175.             return (implode($separator$this->_comments[$commentTitle]));
  176.         else
  177.             return ($this->_comments[$commentTitle]);
  178.     else
  179.         // The comment doesn't exist in this file.  The user should've called getCommentList first.
  180.         return ("");
  181.     }
  182.     
  183.     /**
  184.      * Get the entire comments array.
  185.      * May return an empty array if the bitstream does not support comments.
  186.      *
  187.      * @access  public
  188.      * @return  array 
  189.      */
  190.     function getComments({
  191.         return $this->_comments;
  192.     }
  193.  
  194.     /**
  195.      * Vendor of software used to encode this stream.
  196.      *
  197.      * Gives the vendor string for the software used to encode this stream.
  198.      * It is common to find libVorbis here.  The majority of encoders appear
  199.      * to use libvorbis from Xiph.org.
  200.      *
  201.      * @access  public
  202.      * @return  string 
  203.      */
  204.     function getVendor()
  205.     {
  206.         return ($this->_vendor);
  207.     }
  208.  
  209.     /**
  210.      * Get an associative array containing header information about the stream
  211.      * @access  public
  212.      * @return  array 
  213.      */
  214.     function getHeader(
  215.     {
  216.         return array();
  217.     }
  218.  
  219.     /**
  220.      * Get the length of the stream in seconds
  221.      * @return float 
  222.      */
  223.     function getLength()
  224.     {
  225.         return $this->_streamLength;
  226.     }
  227. }

Documentation generated on Mon, 11 Mar 2019 15:15:21 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.