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

Source for file Flac.php

Documentation is available at Flac.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/Media.php');
  25.  
  26. /**
  27.  * @author      David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
  28.  * @category    File
  29.  * @copyright   David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
  30.  * @license     http://www.gnu.org/copyleft/lesser.html GNU LGPL
  31.  * @link        http://pear.php.net/package/File_Ogg
  32.  * @link        http://flac.sourceforge.net/documentation.html
  33.  * @package     File_Ogg
  34.  * @version     CVS: $Id: Flac.php 302834 2010-08-27 02:16:20Z tstarling $
  35.  */
  36. class File_Ogg_Flac extends File_Ogg_Media
  37. {
  38.     /**
  39.      * @access  private
  40.      */
  41.     function __construct($streamSerial$streamData$filePointer)
  42.     {
  43.         parent::__construct($streamSerial$streamData$filePointer);
  44.         $this->_decodeHeader();
  45.         $this->_decodeCommentsHeader();
  46.         $this->_streamLength    $this->_streamInfo['total_samples']
  47.             / $this->_streamInfo['sample_rate'];
  48.     }
  49.  
  50.     /**
  51.      * Get a short string describing the type of the stream
  52.      * @return string 
  53.      */
  54.     function getType({
  55.         return 'FLAC';
  56.     }
  57.  
  58.     /**
  59.      * @access  private
  60.      * @param   int     $packetType 
  61.      * @param   int     $pageOffset 
  62.      */
  63.     function _decodeHeader()
  64.     {
  65.         fseek($this->_filePointer$this->_streamData['pages'][0]['body_offset']SEEK_SET);
  66.         // Check if this is the correct header.
  67.         $packet unpack("Cdata"fread($this->_filePointer1));
  68.         if ($packet['data'!= 0x7f)
  69.             throw new PEAR_Exception("Stream Undecodable"OGG_ERROR_UNDECODABLE);
  70.     
  71.         // The following four characters should be "FLAC".
  72.         if (fread($this->_filePointer4!= 'FLAC')
  73.             throw new PEAR_Exception("Stream is undecodable due to a malformed header."OGG_ERROR_UNDECODABLE);
  74.  
  75.         $version unpack("Cmajor/Cminor"fread($this->_filePointer2));
  76.         $this->_version = "{$version['major']}.{$version['minor']}";
  77.         if ($version['major'> 1{
  78.             throw new PEAR_Exception("Cannot decode a version {$version['major']} FLAC stream"OGG_ERROR_UNDECODABLE);
  79.         }
  80.         $h File_Ogg::_readBigEndian$this->_filePointer
  81.             array(
  82.                 // Ogg-specific
  83.                 'num_headers'       => 16,
  84.                 'flac_native_sig'   => 32,
  85.                 // METADATA_BLOCK_HEADER
  86.                 'is_last'           => 1,
  87.                 'type'              => 7,
  88.                 'length'            => 24,
  89.             ));
  90.  
  91.         // METADATA_BLOCK_STREAMINFO
  92.         // The variable names are canonical, and come from the FLAC source (format.h)
  93.         $this->_streamInfo File_Ogg::_readBigEndian$this->_filePointer,
  94.             array(
  95.                 'min_blocksize'     => 16,
  96.                 'max_blocksize'     => 16,
  97.                 'min_framesize'     => 24,
  98.                 'max_framesize'     => 24,
  99.                 'sample_rate'       => 20,
  100.                 'channels'          => 3,
  101.                 'bits_per_sample'   => 5,
  102.                 'total_samples'     => 36,
  103.            ));
  104.         $this->_streamInfo['md5sum'bin2hex(fread($this->_filePointer16));
  105.     }
  106.  
  107.     /**
  108.      * Get an associative array containing header information about the stream
  109.      * @access  public
  110.      * @return  array 
  111.      */
  112.     function getHeader()
  113.     {
  114.         return $this->_streamInfo;
  115.     }
  116.  
  117.     function _decodeCommentsHeader()
  118.     {
  119.         fseek($this->_filePointer$this->_streamData['pages'][1]['body_offset']SEEK_SET);
  120.         $blockHeader File_Ogg::_readBigEndian$this->_filePointer,
  121.             array(
  122.                 'last_block' => 1,
  123.                 'block_type' => 7,
  124.                 'length' => 24
  125.             )
  126.         );
  127.         if ($blockHeader['block_type'!= 4{
  128.             throw new PEAR_Exception("Stream Undecodable"OGG_ERROR_UNDECODABLE);
  129.         }
  130.         
  131.         $this->_decodeBareCommentsHeader();
  132.     }
  133. }
  134. ?>

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