Add support for MPEG-1/2 Audio (ISO/IEC 11172-3 and ISO/IEC 13818-3 respectively)
git-svn-id: http://php-reader.googlecode.com/svn/trunk@114 51a70ab9-7547-0410-9469-37e369ee0574
This commit is contained in:
507
src/MPEG/Audio/Frame.php
Normal file
507
src/MPEG/Audio/Frame.php
Normal file
@@ -0,0 +1,507 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP Reader Library
|
||||
*
|
||||
* Copyright (c) 2008 The PHP Reader Project Workgroup. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* - Neither the name of the project workgroup nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @package php-reader
|
||||
* @subpackage MPEG
|
||||
* @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
|
||||
* @license http://code.google.com/p/php-reader/wiki/License New BSD License
|
||||
* @version $Id: Frame.php 1 2008-07-06 10:43:41Z rbutterfield $
|
||||
*/
|
||||
|
||||
/**#@+ @ignore */
|
||||
require_once("Reader.php");
|
||||
require_once("Twiddling.php");
|
||||
require_once("MPEG/Audio/Object.php");
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* This class represents an MPEG Audio frame as described in ISO/IEC 11172-3 and
|
||||
* ISO/IEC 13818-3 standards.
|
||||
*
|
||||
* To accommodate fast header processing the error checking data and the audio
|
||||
* data are lazy fetch by default. You can change this behaviour by giving a
|
||||
* proper option to the {@link MPEG_Audio} class.
|
||||
*
|
||||
* @package php-reader
|
||||
* @subpackage MPEG
|
||||
* @author Ryan Butterfield <buttza@gmail.com>
|
||||
* @author Sven Vollbehr <svollbehr@gmail.com>
|
||||
* @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
|
||||
* @license http://code.google.com/p/php-reader/wiki/License New BSD License
|
||||
* @version $Rev: 1 $
|
||||
*/
|
||||
class MPEG_Audio_Frame extends MPEG_Audio_Object
|
||||
{
|
||||
/**
|
||||
* The bitrate lookup table. The table has the following format.
|
||||
*
|
||||
* <code>
|
||||
* array (
|
||||
* SAMPLING_FREQUENCY_HIGH | SAMPLING_FREQUENCY_LOW => array (
|
||||
* LAYER_ONE | LAYER_TWO | LAYER_TREE => array ( <bitrates> )
|
||||
* )
|
||||
* )
|
||||
* </code>
|
||||
*
|
||||
* @var Array
|
||||
*/
|
||||
private static $bitrates = array (
|
||||
self::SAMPLING_FREQUENCY_HIGH => array (
|
||||
self::LAYER_ONE => array (
|
||||
1 => 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
|
||||
),
|
||||
self::LAYER_TWO => array (
|
||||
1 => 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384
|
||||
),
|
||||
self::LAYER_THREE => array (
|
||||
1 => 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320
|
||||
)
|
||||
),
|
||||
self::SAMPLING_FREQUENCY_LOW => array (
|
||||
self::LAYER_ONE => array (
|
||||
1 => 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256
|
||||
),
|
||||
self::LAYER_TWO => array (
|
||||
1 => 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160
|
||||
),
|
||||
self::LAYER_THREE => array (
|
||||
1 => 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* Sample rate lookup table. The table has the following format.
|
||||
*
|
||||
* <code>
|
||||
* array (
|
||||
* LAYER_ONE | LAYER_TWO | LAYER_TREE => array ( <sample rates> )
|
||||
* )
|
||||
* </code>
|
||||
*
|
||||
* @var Array
|
||||
*/
|
||||
private static $samplingFrequencies = array (
|
||||
self::VERSION_ONE => array (44100, 48000, 32000),
|
||||
self::VERSION_TWO => array (22050, 24000, 16000),
|
||||
self::VERSION_TWO_FIVE => array (11025, 12000, 8000)
|
||||
);
|
||||
|
||||
/**
|
||||
* Samples per frame lookup table. The table has the following format.
|
||||
*
|
||||
* <code>
|
||||
* array (
|
||||
* SAMPLING_FREQUENCY_HIGH | SAMPLING_FREQUENCY_LOW => array (
|
||||
* LAYER_ONE | LAYER_TWO | LAYER_TREE => <sample count>
|
||||
* )
|
||||
* )
|
||||
* </code>
|
||||
*
|
||||
* @var Array
|
||||
*/
|
||||
private static $samples = array (
|
||||
self::SAMPLING_FREQUENCY_HIGH => array (
|
||||
self::LAYER_ONE => 384,
|
||||
self::LAYER_TWO => 1152,
|
||||
self::LAYER_THREE => 1152),
|
||||
self::SAMPLING_FREQUENCY_LOW => array (
|
||||
self::LAYER_ONE => 384,
|
||||
self::LAYER_TWO => 1152,
|
||||
self::LAYER_THREE => 576));
|
||||
|
||||
/**
|
||||
* Coefficient lookup table. The table has the following format.
|
||||
*
|
||||
* <code>
|
||||
* array (
|
||||
* SAMPLING_FREQUENCY_HIGH | SAMPLING_FREQUENCY_LOW => array (
|
||||
* LAYER_ONE | LAYER_TWO | LAYER_TREE => array ( <coefficient> )
|
||||
* )
|
||||
* )
|
||||
* </code>
|
||||
*
|
||||
* @var Array
|
||||
*/
|
||||
private static $coefficients = array (
|
||||
self::SAMPLING_FREQUENCY_HIGH => array (
|
||||
self::LAYER_ONE => 12, self::LAYER_TWO => 144, self::LAYER_THREE => 144
|
||||
),
|
||||
self::SAMPLING_FREQUENCY_LOW => array (
|
||||
self::LAYER_ONE => 12, self::LAYER_TWO => 144, self::LAYER_THREE => 72
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* Slot size per layer lookup table. The table has the following format.
|
||||
*
|
||||
* <code>
|
||||
* array (
|
||||
* LAYER_ONE | LAYER_TWO | LAYER_TREE => <size>
|
||||
* )
|
||||
* </code>
|
||||
*
|
||||
*
|
||||
* @var Array
|
||||
*/
|
||||
private static $slotsizes = array (
|
||||
self::LAYER_ONE => 4, self::LAYER_TWO => 1, self::LAYER_THREE => 1
|
||||
);
|
||||
|
||||
|
||||
/** @var integer */
|
||||
private $_offset;
|
||||
|
||||
/** @var integer */
|
||||
private $_version;
|
||||
|
||||
/** @var integer */
|
||||
private $_frequencyType;
|
||||
|
||||
/** @var integer */
|
||||
private $_layer;
|
||||
|
||||
/** @var integer */
|
||||
private $_redundancy;
|
||||
|
||||
/** @var integer */
|
||||
private $_bitrate;
|
||||
|
||||
/** @var integer */
|
||||
private $_samplingFrequency;
|
||||
|
||||
/** @var integer */
|
||||
private $_padding;
|
||||
|
||||
/** @var integer */
|
||||
private $_mode;
|
||||
|
||||
/** @var integer */
|
||||
private $_modeExtension;
|
||||
|
||||
/** @var integer */
|
||||
private $_copyright;
|
||||
|
||||
/** @var integer */
|
||||
private $_original;
|
||||
|
||||
/** @var integer */
|
||||
private $_emphasis;
|
||||
|
||||
/** @var integer */
|
||||
private $_length;
|
||||
|
||||
/** @var integer */
|
||||
private $_samples;
|
||||
|
||||
/** @var integer */
|
||||
private $_crc = false;
|
||||
|
||||
/** @var string */
|
||||
private $_data = false;
|
||||
|
||||
/**
|
||||
* Constructs the class with given parameters and reads object related data
|
||||
* from the MPEG frame.
|
||||
*
|
||||
* @param Reader $reader The reader object.
|
||||
* @param Array $options Array of options.
|
||||
*/
|
||||
public function __construct($reader, &$options = array())
|
||||
{
|
||||
parent::__construct($reader, $options);
|
||||
|
||||
$this->_offset = $this->_reader->getOffset();
|
||||
|
||||
$header = Transform::fromUInt32BE($this->_reader->read(4));
|
||||
$this->_version = Twiddling::getValue($header, 19, 20);
|
||||
$this->_frequencyType = Twiddling::testBit($header, 19);
|
||||
$this->_layer = Twiddling::getValue($header, 17, 18);
|
||||
$this->_redundancy = !Twiddling::testBit($header, 16);
|
||||
$this->_bitrate = isset
|
||||
(self::$bitrates[$this->_frequencyType][$this->_layer]
|
||||
[$index = Twiddling::getValue($header, 12, 15)]) ?
|
||||
self::$bitrates[$this->_frequencyType][$this->_layer][$index] : false;
|
||||
$this->_samplingFrequency = isset
|
||||
(self::$samplingFrequencies[$this->_version]
|
||||
[$index = Twiddling::getValue($header, 10, 11)]) ?
|
||||
self::$samplingFrequencies[$this->_version][$index] : false;
|
||||
$this->_padding = Twiddling::testBit($header, 9);
|
||||
$this->_mode = Twiddling::getValue($header, 6, 7);
|
||||
$this->_modeExtension = Twiddling::getValue($header, 4, 5);
|
||||
$this->_copyright = Twiddling::testBit($header, 3);
|
||||
$this->_original = Twiddling::testBit($header, 2);
|
||||
$this->_emphasis = Twiddling::getValue($header, 0, 1);
|
||||
|
||||
$this->_length = (int)
|
||||
((self::$coefficients[$this->_frequencyType][$this->_layer] *
|
||||
($this->_bitrate * 1000) / $this->_samplingFrequency) +
|
||||
($this->_padding ? 1 : 0)) * self::$slotsizes[$this->_layer];
|
||||
$this->_samples = self::$samples[$this->_frequencyType][$this->_layer];
|
||||
|
||||
if ($this->getOption("readmode", "lazy") == "lazy")
|
||||
$this->_reader->skip($this->_length - 4);
|
||||
else { // full
|
||||
$this->_readCrc();
|
||||
$this->_readData();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the version identifier of the algorithm.
|
||||
*
|
||||
* @see VERSION_ONE, VERSION_TWO, VERSION_TWO_FIVE
|
||||
* @return integer
|
||||
*/
|
||||
public function getVersion() { return $this->_version; }
|
||||
|
||||
/**
|
||||
* Returns the sampling frequency type. This can be one of the following
|
||||
* values.
|
||||
*
|
||||
* o <b>{@link SAMPLING_FREQUENCY_HIGH}</b> -- Higher Sampling Frequency
|
||||
* (Version 1)
|
||||
* o <b>{@link SAMPLING_FREQUENCY_LOW}</b> -- Lower Sampling Frequency
|
||||
* (Version 2 and 2.5)
|
||||
*
|
||||
* @see SAMPLING_FREQUENCY_LOW, SAMPLING_FREQUENCY_HIGH
|
||||
* @return integer
|
||||
*/
|
||||
public function getFrequencyType() { return $this->_frequencyType; }
|
||||
|
||||
/**
|
||||
* Returns the type of layer used.
|
||||
*
|
||||
* @see LAYER_ONE, LAYER_TWO, LAYER_THREE
|
||||
* @return integer
|
||||
*/
|
||||
public function getLayer() { return $this->_layer; }
|
||||
|
||||
/**
|
||||
* An alias to getRedundancy().
|
||||
*
|
||||
* @see getRedundancy
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasRedundancy() { return $this->getRedundancy(); }
|
||||
|
||||
/**
|
||||
* Returns boolean corresponding to whether redundancy has been added in the
|
||||
* audio bitstream to facilitate error detection and concealment. Equals
|
||||
* <var>false</var> if no redundancy has been added, <var>true</var> if
|
||||
* redundancy has been added.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getRedundancy() { return $this->_redundancy; }
|
||||
|
||||
/**
|
||||
* Returns the bitrate in kbps. The returned value indicates the total bitrate
|
||||
* irrespective of the mode (stereo, joint_stereo, dual_channel,
|
||||
* single_channel).
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getBitrate() { return $this->_bitrate; }
|
||||
|
||||
/**
|
||||
* Returns the sampling frequency in Hz.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getSamplingFrequency() { return $this->_samplingFrequency; }
|
||||
|
||||
/**
|
||||
* An alias to getPadding().
|
||||
*
|
||||
* @see getPadding
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasPadding() { return $this->getPadding(); }
|
||||
|
||||
/**
|
||||
* Returns boolean corresponding the frame contains an additional slot to
|
||||
* adjust the mean bitrate to the sampling frequency. Equals to
|
||||
* <var>true</var> if padding has been added, <var>false</var> otherwise.
|
||||
*
|
||||
* Padding is only necessary with a sampling frequency of 44.1kHz.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getPadding() { return $this->_padding; }
|
||||
|
||||
/**
|
||||
* Returns the mode. In Layer I and II the CHANNEL_JOINT_STEREO mode is
|
||||
* intensity_stereo, in Layer III it is intensity_stereo and/or ms_stereo.
|
||||
*
|
||||
* @see CHANNEL_STEREO, CHANNEL_JOINT_STEREO, CHANNEL_DUAL_CHANNEL,
|
||||
* CHANNEL_SINGLE_CHANNEL
|
||||
* @return integer
|
||||
*/
|
||||
public function getMode() { return $this->_mode; }
|
||||
|
||||
/**
|
||||
* Returns the mode extension used in CHANNEL_JOINT_STEREO mode.
|
||||
*
|
||||
* In Layer I and II the return type indicates which subbands are in
|
||||
* intensity_stereo. All other subbands are coded in stereo.
|
||||
*
|
||||
* o <b>{@link MODE_SUBBAND_4_TO_31}</b> -- subbands 4-31 in
|
||||
* intensity_stereo, bound==4
|
||||
* o <b>{@link MODE_SUBBAND_8_TO_31}</b> -- subbands 8-31 in
|
||||
* intensity_stereo, bound==8
|
||||
* o <b>{@link MODE_SUBBAND_12_TO_31}</b> -- subbands 12-31 in
|
||||
* intensity_stereo, bound==12
|
||||
* o <b>{@link MODE_SUBBAND_16_TO_31}</b> -- subbands 16-31 in
|
||||
* intensity_stereo, bound==16
|
||||
*
|
||||
* In Layer III they indicate which type of joint stereo coding method is
|
||||
* applied. The frequency ranges over which the intensity_stereo and ms_stereo
|
||||
* modes are applied are implicit in the algorithm. Please see
|
||||
* {@link MODE_ISOFF_MSSOFF}, {@link MODE_ISON_MSSOFF},
|
||||
* {@link MODE_ISOFF_MSSON}, and {@link MODE_ISON_MSSON}.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getModeExtension() { return $this->_modeExtension; }
|
||||
|
||||
/**
|
||||
* An alias to getCopyright().
|
||||
*
|
||||
* @see getCopyright
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasCopyright() { return $this->getCopyright(); }
|
||||
|
||||
/**
|
||||
* Returns <var>true</var> if the coded bitstream is copyright protected,
|
||||
* <var>false</var> otherwise.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getCopyright() { return $this->_copyright; }
|
||||
|
||||
/**
|
||||
* An alias to getOriginal().
|
||||
*
|
||||
* @see getOriginal
|
||||
* @return boolean
|
||||
*/
|
||||
public function isOriginal() { return $this->getOriginal(); }
|
||||
|
||||
/**
|
||||
* Returns whether the bitstream is original or home made.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getOriginal() { return $this->_original; }
|
||||
|
||||
/**
|
||||
* Returns the type of de-emphasis that shall be used. The value is one of the
|
||||
* following.
|
||||
*
|
||||
* o <b>{@link EMPHASIS_NONE}</b> -- No emphasis
|
||||
* o <b>{@link EMPHASIS_50_15}</b> -- 50/15 microsec. emphasis
|
||||
* o <b>{@link EMPHASIS_CCIT_J17}</b> -- CCITT J.17
|
||||
*
|
||||
* @see EMPHASIS_NONE, EMPHASIS_50_15, EMPHASIS_CCIT_J17
|
||||
* @return integer
|
||||
*/
|
||||
public function getEmphasis() { return $this->_emphasis; }
|
||||
|
||||
/**
|
||||
* Returns the length of the frame based on the current layer, bit rate,
|
||||
* sampling frequency and padding, in bytes.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getLength() { return $this->_length; }
|
||||
|
||||
/**
|
||||
* Returns the number of samples contained in the frame.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getSamples() { return $this->_samples; }
|
||||
|
||||
/**
|
||||
* Returns the 16-bit CRC of the frame or <var>false</var> if not present.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getCrc()
|
||||
{
|
||||
if ($this->getOption("readmode", "lazy") == "lazy" &&
|
||||
$this->hasRedundancy() && $this->_crc === false) {
|
||||
$this->_readCrc();
|
||||
}
|
||||
return $this->_crc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the CRC data.
|
||||
*/
|
||||
private function _readCrc()
|
||||
{
|
||||
if ($this->hasRedundancy()) {
|
||||
$offset = $this->_reader->getOffset();
|
||||
$this->_reader->setOffset($this->_offset + 4);
|
||||
$this->_crc = $reader->readUInt16BE();
|
||||
$this->_reader->setOffset($offset);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the audio data.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
if ($this->getOption("readmode", "lazy") == "lazy" &&
|
||||
$this->_data === false) {
|
||||
$this->_readData();
|
||||
}
|
||||
return $this->_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the frame data.
|
||||
*/
|
||||
private function _readData()
|
||||
{
|
||||
$offset = $this->_reader->getOffset();
|
||||
$this->_reader->setOffset
|
||||
($this->_offset + 4 + ($this->hasRedundancy() ? 2 : 0));
|
||||
$this->_data = $this->_reader->read
|
||||
($this->getLength() - 4 - ($this->hasRedundancy() ? 2 : 0));
|
||||
$this->_reader->setOffset($offset);
|
||||
}
|
||||
}
|
||||
481
src/MPEG/Audio/LAMEHeader.php
Normal file
481
src/MPEG/Audio/LAMEHeader.php
Normal file
@@ -0,0 +1,481 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP Reader Library
|
||||
*
|
||||
* Copyright (c) 2008 The PHP Reader Project Workgroup. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* - Neither the name of the project workgroup nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @package php-reader
|
||||
* @subpackage MPEG
|
||||
* @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
|
||||
* @license http://code.google.com/p/php-reader/wiki/License New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**#@+ @ignore */
|
||||
require_once("Reader.php");
|
||||
require_once("Twiddling.php");
|
||||
require_once("MPEG/Object.php");
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* This class represents a LAME extension to the Xing VBR header. The purpose of
|
||||
* this header is to provide extra information about the MP3 bistream, encoder
|
||||
* and parameters used. This header should, as much as possible, be meaningfull
|
||||
* for as many encoders as possible, even if it is unlikely that other encoders
|
||||
* than LAME will implement it.
|
||||
*
|
||||
* This header should be backward compatible with the Xing VBR tag, providing
|
||||
* basic support for a lot of already written software. As much as possible the
|
||||
* current revision (revision 1) should provide information similar to the one
|
||||
* already provided by revision 0.
|
||||
*
|
||||
* @package php-reader
|
||||
* @subpackage MPEG
|
||||
* @author Sven Vollbehr <svollbehr@gmail.com>
|
||||
* @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
|
||||
* @license http://code.google.com/p/php-reader/wiki/License New BSD License
|
||||
* @version $Rev: 1 $
|
||||
*/
|
||||
class MPEG_Audio_LAMEHeader extends MPEG_Object
|
||||
{
|
||||
|
||||
/** @var integer */
|
||||
const VBR_METHOD_CONSTANT = 1;
|
||||
|
||||
/** @var integer */
|
||||
const VBR_METHOD_ABR = 2;
|
||||
|
||||
/** @var integer */
|
||||
const VBR_METHOD_RH = 3;
|
||||
|
||||
/** @var integer */
|
||||
const VBR_METHOD_MTRH = 4;
|
||||
|
||||
/** @var integer */
|
||||
const VBR_METHOD_MT = 5;
|
||||
|
||||
/** @var integer */
|
||||
const ENCODING_FLAG_NSPSYTUNE = 1;
|
||||
|
||||
/** @var integer */
|
||||
const ENCODING_FLAG_NSSAFEJOINT = 2;
|
||||
|
||||
/** @var integer */
|
||||
const ENCODING_FLAG_NOGAP_CONTINUED = 4;
|
||||
|
||||
/** @var integer */
|
||||
const ENCODING_FLAG_NOGAP_CONTINUATION = 8;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_MONO = 0;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_STEREO = 1;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_DUAL = 2;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_JOINT = 3;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_FORCE = 4;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_AUTO = 5;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_INTENSITY = 6;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_UNDEFINED = 7;
|
||||
|
||||
/** @var integer */
|
||||
const SOURCE_FREQUENCY_32000_OR_LOWER = 0;
|
||||
|
||||
/** @var integer */
|
||||
const SOURCE_FREQUENCY_44100 = 1;
|
||||
|
||||
/** @var integer */
|
||||
const SOURCE_FREQUENCY_48000 = 2;
|
||||
|
||||
/** @var integer */
|
||||
const SOURCE_FREQUENCY_HIGHER = 3;
|
||||
|
||||
/** @var integer */
|
||||
const SURROUND_NONE = 0;
|
||||
|
||||
/** @var integer */
|
||||
const SURROUND_DPL = 1;
|
||||
|
||||
/** @var integer */
|
||||
const SURROUND_DPL2 = 2;
|
||||
|
||||
/** @var integer */
|
||||
const SURROUND_AMBISONIC = 3;
|
||||
|
||||
/** @var string */
|
||||
private $_version;
|
||||
|
||||
/** @var integer */
|
||||
private $_revision;
|
||||
|
||||
/** @var integer */
|
||||
private $_vbrMethod;
|
||||
|
||||
/** @var integer */
|
||||
private $_lowpass;
|
||||
|
||||
/** @var integer */
|
||||
private $_peakSignalAmplitude;
|
||||
|
||||
/** @var integer */
|
||||
private $_radioReplayGain;
|
||||
|
||||
/** @var integer */
|
||||
private $_audiophileReplayGain;
|
||||
|
||||
/** @var integer */
|
||||
private $_encodingFlags;
|
||||
|
||||
/** @var integer */
|
||||
private $_athType;
|
||||
|
||||
/** @var integer */
|
||||
private $_bitrate;
|
||||
|
||||
/** @var integer */
|
||||
private $_encoderDelaySamples;
|
||||
|
||||
/** @var integer */
|
||||
private $_paddedSamples;
|
||||
|
||||
/** @var integer */
|
||||
private $_sourceSampleFrequency;
|
||||
|
||||
/** @var boolean */
|
||||
private $_unwiseSettingsUsed;
|
||||
|
||||
/** @var integer */
|
||||
private $_mode;
|
||||
|
||||
/** @var integer */
|
||||
private $_noiseShaping;
|
||||
|
||||
/** @var integer */
|
||||
private $_mp3Gain;
|
||||
|
||||
/** @var integer */
|
||||
private $_surroundInfo;
|
||||
|
||||
/** @var integer */
|
||||
private $_presetUsed;
|
||||
|
||||
/** @var integer */
|
||||
private $_musicLength;
|
||||
|
||||
/** @var integer */
|
||||
private $_musicCrc;
|
||||
|
||||
/** @var integer */
|
||||
private $_crc;
|
||||
|
||||
/**
|
||||
* Constructs the class with given parameters and reads object related data
|
||||
* from the bitstream.
|
||||
*
|
||||
* @param Reader $reader The reader object.
|
||||
* @param Array $options Array of options.
|
||||
*/
|
||||
public function __construct($reader, &$options = array())
|
||||
{
|
||||
parent::__construct($reader, $options);
|
||||
|
||||
$this->_version = $this->_reader->readString8(5);
|
||||
|
||||
$tmp = $this->_reader->readUInt8();
|
||||
$this->_revision = Twiddling::getValue($tmp, 4, 8);
|
||||
$this->_vbrMethod = Twiddling::getValue($tmp, 0, 3);
|
||||
|
||||
$this->_lowpass = $this->_reader->readUInt8() * 100;
|
||||
|
||||
$this->_peakSignalAmplitude = $this->_reader->readUInt32BE();
|
||||
|
||||
$tmp = $this->_reader->readUInt16BE();
|
||||
$this->_radioReplayGain = array(
|
||||
"name" => Twiddling::getValue($tmp, 0, 2),
|
||||
"originator" => Twiddling::getValue($tmp, 3, 5),
|
||||
"absoluteGainAdjustment" => Twiddling::getValue($tmp, 7, 15) / 10
|
||||
);
|
||||
|
||||
$tmp = $this->_reader->readUInt16BE();
|
||||
$this->_audiophileReplayGain = array(
|
||||
"name" => Twiddling::getValue($tmp, 0, 2),
|
||||
"originator" => Twiddling::getValue($tmp, 3, 5),
|
||||
"absoluteGainAdjustment" => Twiddling::getValue($tmp, 7, 15) / 10
|
||||
);
|
||||
|
||||
$tmp = $this->_reader->readUInt8();
|
||||
$this->_encodingFlags = Twiddling::getValue($tmp, 4, 8);
|
||||
$this->_athType = Twiddling::getValue($tmp, 0, 3);
|
||||
|
||||
$this->_bitrate = $this->_reader->readUInt8();
|
||||
|
||||
$tmp = $this->_reader->readUInt32BE();
|
||||
// Encoder delay fields
|
||||
$this->_encoderDelaySamples = Twiddling::getValue($tmp, 20, 31);
|
||||
$this->_paddedSamples = Twiddling::getValue($tmp, 8, 19);
|
||||
// Misc field
|
||||
$this->_sourceSampleFrequency = Twiddling::getValue($tmp, 6, 7);
|
||||
$this->_unwiseSettingsUsed = Twiddling::testBit($tmp, 5);
|
||||
$this->_mode = Twiddling::getValue($tmp, 2, 4);
|
||||
$this->_noiseShaping = Twiddling::getValue($tmp, 0, 1);
|
||||
|
||||
$this->_mp3Gain = pow(2, $this->_reader->readInt8() / 4);
|
||||
|
||||
$tmp = $this->_reader->readUInt16BE();
|
||||
$this->_surroundInfo = Twiddling::getValue($tmp, 11, 14);
|
||||
$this->_presetUsed = Twiddling::getValue($tmp, 0, 10);
|
||||
|
||||
$this->_musicLength = $this->_reader->readUInt32BE();
|
||||
|
||||
$this->_musicCrc = $this->_reader->readUInt16BE();
|
||||
$this->_crc = $this->_reader->readUInt16BE();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the version string of the header.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVersion() { return $this->_version; }
|
||||
|
||||
/**
|
||||
* Returns the info tag revision.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getRevision() { return $this->_revision; }
|
||||
|
||||
/**
|
||||
* Returns the VBR method used for encoding. See the corresponding constants
|
||||
* for possible return values.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getVbrMethod() { return $this->_vbrMethod; }
|
||||
|
||||
/**
|
||||
* Returns the lowpass filter value.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getLowpass() { return $this->_lowpass; }
|
||||
|
||||
/**
|
||||
* Returns the peak signal amplitude field of replay gain. The value of 1.0
|
||||
* (ie 100%) represents maximal signal amplitude storeable in decoding format.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getPeakSignalAmplitude()
|
||||
{
|
||||
return $this->_peakSignalAmplitude;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the radio replay gain field of replay gain, required to make all
|
||||
* tracks equal loudness, as an array that consists of the following keys.
|
||||
*
|
||||
* o name -- Specifies the name of the gain adjustment. Can be one of the
|
||||
* following values: 0 = not set, 1 = radio, or 2 = audiophile.
|
||||
*
|
||||
* o originator -- Specifies the originator of the gain adjustment. Can be
|
||||
* one of the following values: 0 = not set, 1 = set by artist, 2 = set
|
||||
* by user, 3 = set by my model, 4 = set by simple RMS average.
|
||||
*
|
||||
* o absoluteGainAdjustment -- Speficies the absolute gain adjustment.
|
||||
*
|
||||
* @return Array
|
||||
*/
|
||||
public function getRadioReplayGain() { return $this->_radioReplayGain; }
|
||||
|
||||
/**
|
||||
* Returns the audiophile replay gain field of replay gain, required to give
|
||||
* ideal listening loudness, as an array that consists of the following keys.
|
||||
*
|
||||
* o name -- Specifies the name of the gain adjustment. Can be one of the
|
||||
* following values: 0 = not set, 1 = radio, or 2 = audiophile.
|
||||
*
|
||||
* o originator -- Specifies the originator of the gain adjustment. Can be
|
||||
* one of the following values: 0 = not set, 1 = set by artist, 2 = set
|
||||
* by user, 3 = set by my model, 4 = set by simple RMS average.
|
||||
*
|
||||
* o absoluteGainAdjustment -- Speficies the absolute gain adjustment.
|
||||
*
|
||||
* @return Array
|
||||
*/
|
||||
public function getAudiophileReplayGain()
|
||||
{
|
||||
return $this->_audiophileReplayGain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the encoding flags. See the corresponding flag constants for
|
||||
* possible values.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getEncodingFlags() { return $this->_encodingFlags; }
|
||||
|
||||
/**
|
||||
* Returns the ATH type.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getAthType() { return $this->_athType; }
|
||||
|
||||
/**
|
||||
* Returns the bitrate for CBR encoded files and the minimal birate for
|
||||
* VBR encoded file. The maximum value of this field is 255 even with higher
|
||||
* actual bitrates.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getBitrate() { return $this->_bitrate; }
|
||||
|
||||
/**
|
||||
* Returns the encoder delay or number of samples added at start.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getEncoderDelaySamples()
|
||||
{
|
||||
return $this->_encoderDelaySamples;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of padded samples to complete the last frame.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getPaddedSamples() { return $this->_paddedSamples; }
|
||||
|
||||
/**
|
||||
* Returns the source sample frequency. See corresponding constants for
|
||||
* possible values.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getSourceSampleFrequency()
|
||||
{
|
||||
return $this->_sourceSampleFrequency;
|
||||
}
|
||||
|
||||
/**
|
||||
* An alias to getUnwiseSettingsUsed().
|
||||
*
|
||||
* @see getUnwiseSettingsUsed
|
||||
* @return boolean
|
||||
*/
|
||||
public function areUnwiseSettingsUsed()
|
||||
{
|
||||
return $this->getUnwiseSettingsUsed();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether unwise settings were used to encode the file.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getUnwiseSettingsUsed() { return $this->_unwiseSettingsUsed; }
|
||||
|
||||
/**
|
||||
* Returns the stereo mode. See corresponding constants for possible values.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getMode() { return $this->_mode; }
|
||||
|
||||
/**
|
||||
* Returns the noise shaping.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getNoiseShaping() { return $this->_noiseShaping; }
|
||||
|
||||
/**
|
||||
* Returns the MP3 gain change. Any MP3 can be amplified in a lossless manner.
|
||||
* If done so, this field can be used to log such transformation happened so
|
||||
* that any given time it can be undone.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getMp3Gain() { return $this->_mp3Gain; }
|
||||
|
||||
/**
|
||||
* Returns the surround info. See corresponding contants for possible values.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getSurroundInfo() { return $this->_surroundInfo; }
|
||||
|
||||
/**
|
||||
* Returns the preset used in encoding.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getPresetUsed() { return $this->_presetUsed; }
|
||||
|
||||
/**
|
||||
* Returns the exact length in bytes of the MP3 file originally made by LAME
|
||||
* excluded ID3 tag info at the end.
|
||||
*
|
||||
* The first byte it counts is the first byte of this LAME header and the last
|
||||
* byte it counts is the last byte of the last MP3 frame containing music.
|
||||
* The value should be equal to file length at the time of LAME encoding,
|
||||
* except when using ID3 tags.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getMusicLength() { return $this->_musicLength; }
|
||||
|
||||
/**
|
||||
* Returns the CRC-16 of the complete MP3 music data as made originally by
|
||||
* LAME.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getMusicCrc() { return $this->_musicCrc; }
|
||||
|
||||
/**
|
||||
* Returns the CRC-16 of the first 190 bytes of the header frame.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getCrc() { return $this->_crc; }
|
||||
}
|
||||
167
src/MPEG/Audio/Object.php
Normal file
167
src/MPEG/Audio/Object.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP Reader Library
|
||||
*
|
||||
* Copyright (c) 2008 The PHP Reader Project Workgroup. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* - Neither the name of the project workgroup nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @package php-reader
|
||||
* @subpackage MPEG
|
||||
* @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
|
||||
* @license http://code.google.com/p/php-reader/wiki/License New BSD License
|
||||
* @version $Id: Object.php 107 2008-08-03 19:09:16Z svollbehr $
|
||||
*/
|
||||
|
||||
/**#@+ @ignore */
|
||||
require_once("MPEG/Object.php");
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* The base class for all MPEG Audio objects.
|
||||
*
|
||||
* @package php-reader
|
||||
* @subpackage MPEG
|
||||
* @author Ryan Butterfield <buttza@gmail.com>
|
||||
* @author Sven Vollbehr <svollbehr@gmail.com>
|
||||
* @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
|
||||
* @license http://code.google.com/p/php-reader/wiki/License New BSD License
|
||||
* @version $Rev: 107 $
|
||||
*/
|
||||
abstract class MPEG_Audio_Object extends MPEG_Object
|
||||
{
|
||||
/** @var integer */
|
||||
const VERSION_ONE = 3;
|
||||
|
||||
/** @var integer */
|
||||
const VERSION_TWO = 2;
|
||||
|
||||
/** @var integer */
|
||||
const VERSION_TWO_FIVE = 0;
|
||||
|
||||
/** @var integer */
|
||||
const SAMPLING_FREQUENCY_LOW = 0;
|
||||
|
||||
/** @var integer */
|
||||
const SAMPLING_FREQUENCY_HIGH = 1;
|
||||
|
||||
/** @var integer */
|
||||
const LAYER_ONE = 3;
|
||||
|
||||
/** @var integer */
|
||||
const LAYER_TWO = 2;
|
||||
|
||||
/** @var integer */
|
||||
const LAYER_THREE = 1;
|
||||
|
||||
/** @var integer */
|
||||
const CHANNEL_STEREO = 0;
|
||||
|
||||
/** @var integer */
|
||||
const CHANNEL_JOINT_STEREO = 1;
|
||||
|
||||
/** @var integer */
|
||||
const CHANNEL_DUAL_CHANNEL = 2;
|
||||
|
||||
/** @var integer */
|
||||
const CHANNEL_SINGLE_CHANNEL = 3;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_SUBBAND_4_TO_31 = 0;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_SUBBAND_8_TO_31 = 1;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_SUBBAND_12_TO_31 = 2;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_SUBBAND_16_TO_31 = 3;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_ISOFF_MSSOFF = 0;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_ISON_MSSOFF = 1;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_ISOFF_MSSON = 2;
|
||||
|
||||
/** @var integer */
|
||||
const MODE_ISON_MSSON = 3;
|
||||
|
||||
/** @var integer */
|
||||
const EMPHASIS_NONE = 0;
|
||||
|
||||
/** @var integer */
|
||||
const EMPHASIS_50_15 = 1;
|
||||
|
||||
/** @var integer */
|
||||
const EMPHASIS_CCIT_J17 = 3;
|
||||
|
||||
|
||||
/**
|
||||
* Layer III side information size lookup table. The table has the following
|
||||
* format.
|
||||
*
|
||||
* <code>
|
||||
* array (
|
||||
* SAMPLING_FREQUENCY_HIGH | SAMPLING_FREQUENCY_LOW => array (
|
||||
* CHANNEL_STEREO | CHANNEL_JOINT_STEREO | CHANNEL_DUAL_CHANNEL |
|
||||
* CHANNEL_SINGLE_CHANNEL => <size>
|
||||
* )
|
||||
* )
|
||||
* </code>
|
||||
*
|
||||
*
|
||||
* @var Array
|
||||
*/
|
||||
protected static $sidesizes = array(
|
||||
self::SAMPLING_FREQUENCY_HIGH => array(
|
||||
self::CHANNEL_STEREO => 32,
|
||||
self::CHANNEL_JOINT_STEREO => 32,
|
||||
self::CHANNEL_DUAL_CHANNEL => 32,
|
||||
self::CHANNEL_SINGLE_CHANNEL => 17
|
||||
),
|
||||
self::SAMPLING_FREQUENCY_LOW => array(
|
||||
self::CHANNEL_STEREO => 17,
|
||||
self::CHANNEL_JOINT_STEREO => 17,
|
||||
self::CHANNEL_DUAL_CHANNEL => 17,
|
||||
self::CHANNEL_SINGLE_CHANNEL => 9
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Constructs the class with given parameters.
|
||||
*
|
||||
* @param Reader $reader The reader object.
|
||||
* @param Array $options The options array.
|
||||
*/
|
||||
public function __construct($reader, &$options = array())
|
||||
{
|
||||
parent::__construct($reader, $options);
|
||||
}
|
||||
}
|
||||
165
src/MPEG/Audio/VBRIHeader.php
Normal file
165
src/MPEG/Audio/VBRIHeader.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP Reader Library
|
||||
*
|
||||
* Copyright (c) 2008 The PHP Reader Project Workgroup. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* - Neither the name of the project workgroup nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @package php-reader
|
||||
* @subpackage MPEG
|
||||
* @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
|
||||
* @license http://code.google.com/p/php-reader/wiki/License New BSD License
|
||||
* @version $Id: VBRIHeader.php 1 2008-07-19 10:43:41Z rbutterfield $
|
||||
*/
|
||||
|
||||
/**#@+ @ignore */
|
||||
require_once("Reader.php");
|
||||
require_once("Twiddling.php");
|
||||
require_once("MPEG/Object.php");
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* This class represents the Fraunhofer IIS VBR header which is often found in
|
||||
* the first frame of an MPEG stream.
|
||||
*
|
||||
* @package php-reader
|
||||
* @subpackage MPEG
|
||||
* @author Ryan Butterfield <buttza@gmail.com>
|
||||
* @author Sven Vollbehr <svollbehr@gmail.com>
|
||||
* @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
|
||||
* @license http://code.google.com/p/php-reader/wiki/License New BSD License
|
||||
* @version $Rev: 1 $
|
||||
*/
|
||||
class MPEG_Audio_VBRIHeader extends MPEG_Object
|
||||
{
|
||||
/** @var integer */
|
||||
private $_version;
|
||||
|
||||
/** @var integer */
|
||||
private $_delay;
|
||||
|
||||
/** @var integer */
|
||||
private $_qualityIndicator;
|
||||
|
||||
/** @var integer */
|
||||
private $_bytes;
|
||||
|
||||
/** @var integer */
|
||||
private $_frames;
|
||||
|
||||
/** @var Array */
|
||||
private $_toc = array();
|
||||
|
||||
/** @var integer */
|
||||
private $_tocFramesPerEntry;
|
||||
|
||||
/** @var integer */
|
||||
private $_length;
|
||||
|
||||
/**
|
||||
* Constructs the class with given parameters and reads object related data
|
||||
* from the bitstream.
|
||||
*
|
||||
* @param Reader $reader The reader object.
|
||||
* @param Array $options Array of options.
|
||||
*/
|
||||
public function __construct($reader, &$options = array())
|
||||
{
|
||||
$offset = $this->_reader->getOffset();
|
||||
$this->_version = $this->_reader->readUInt16BE();
|
||||
$this->_delay = $this->_reader->readUInt16BE();
|
||||
$this->_qualityIndicator = $this->_reader->readUInt16BE();
|
||||
$this->_bytes = $this->_reader->readUInt32BE();
|
||||
$this->_frames = $this->_reader->readUInt32BE();
|
||||
$tocEntries = $this->_reader->readUInt16BE();
|
||||
$tocEntryScale = $this->_reader->readUInt16BE();
|
||||
$tocEntrySize = $this->_reader->readUInt16BE();
|
||||
$this->_tocFramesPerEntry = $this->_reader->readUInt16BE();
|
||||
$this->_toc = array_merge(unpack(($tocEntrySize == 1) ? "C*" :
|
||||
($tocEntrySize == 2) ? "n*" : "N*",
|
||||
$this->_reader->read($tocCount * $tocEntrySize)));
|
||||
foreach ($this->_toc as $key => $value)
|
||||
$this->_toc[$key] = $tocEntryScale * $value;
|
||||
$this->_length = $this->_reader->getOffset() - $offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the header version.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getVersion() { return $this->_version; }
|
||||
|
||||
/**
|
||||
* Returns the delay.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getDelay() { return $this->_delay; }
|
||||
|
||||
/**
|
||||
* Returns the quality indicator. Return value varies from 0 (best quality) to
|
||||
* 100 (worst quality).
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getQualityIndicator() { return $this->_qualityIndicator; }
|
||||
|
||||
/**
|
||||
* Returns the number of bytes in the file.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getBytes() { return $this->_bytes; }
|
||||
|
||||
/**
|
||||
* Returns the number of frames in the file.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getFrames() { return $this->_frames; }
|
||||
|
||||
/**
|
||||
* Returns the table of contents array.
|
||||
*
|
||||
* @return Array
|
||||
*/
|
||||
public function getToc() { return $this->_toc; }
|
||||
|
||||
/**
|
||||
* Returns the number of frames per TOC entry.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getTocFramesPerEntry() { return $this->_tocFramesPerEntry; }
|
||||
|
||||
/**
|
||||
* Returns the length of the header in bytes.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getLength() { return $this->_length; }
|
||||
}
|
||||
136
src/MPEG/Audio/XINGHeader.php
Normal file
136
src/MPEG/Audio/XINGHeader.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP Reader Library
|
||||
*
|
||||
* Copyright (c) 2008 The PHP Reader Project Workgroup. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* - Neither the name of the project workgroup nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @package php-reader
|
||||
* @subpackage MPEG
|
||||
* @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
|
||||
* @license http://code.google.com/p/php-reader/wiki/License New BSD License
|
||||
* @version $Id: XINGHeader.php 1 2008-07-06 10:43:41Z rbutterfield $
|
||||
*/
|
||||
|
||||
/**#@+ @ignore */
|
||||
require_once("Reader.php");
|
||||
require_once("Twiddling.php");
|
||||
require_once("MPEG/Object.php");
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* This class represents the Xing VBR header which is often found in the first
|
||||
* frame of an MPEG stream.
|
||||
*
|
||||
* @package php-reader
|
||||
* @subpackage MPEG
|
||||
* @author Ryan Butterfield <buttza@gmail.com>
|
||||
* @author Sven Vollbehr <svollbehr@gmail.com>
|
||||
* @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
|
||||
* @license http://code.google.com/p/php-reader/wiki/License New BSD License
|
||||
* @version $Rev: 1 $
|
||||
*/
|
||||
class MPEG_Audio_XINGHeader extends MPEG_Object
|
||||
{
|
||||
/** @var integer */
|
||||
private $_frames = false;
|
||||
|
||||
/** @var integer */
|
||||
private $_bytes = false;
|
||||
|
||||
/** @var Array */
|
||||
private $_toc = array();
|
||||
|
||||
/** @var integer */
|
||||
private $_qualityIndicator = false;
|
||||
|
||||
/**
|
||||
* Constructs the class with given parameters and reads object related data
|
||||
* from the bitstream.
|
||||
*
|
||||
* @param Reader $reader The reader object.
|
||||
* @param Array $options Array of options.
|
||||
*/
|
||||
public function __construct($reader, &$options = array())
|
||||
{
|
||||
parent::__construct($reader, $options);
|
||||
|
||||
$flags = $reader->readUInt32BE();
|
||||
|
||||
if (Twiddling::testAnyBits($flags, 0x1))
|
||||
$this->_frames = $this->_reader->readUInt32BE();
|
||||
if (Twiddling::testAnyBits($flags, 0x2))
|
||||
$this->_bytes = $this->_reader->readUInt32BE();
|
||||
if (Twiddling::testAnyBits($flags, 0x4))
|
||||
$this->_toc = array_merge(unpack("C*", $this->_reader->read(100)));
|
||||
if (Twiddling::testAnyBits($flags, 0x8))
|
||||
$this->_qualityIndicator = $this->_reader->readUInt32BE();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of frames in the file.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getFrames() { return $this->_frames; }
|
||||
|
||||
/**
|
||||
* Returns the number of bytes in the file.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getBytes() { return $this->_bytes; }
|
||||
|
||||
/**
|
||||
* Returns the table of contents array. The returned array has a fixed amount
|
||||
* of 100 seek points to the file.
|
||||
*
|
||||
* @return Array
|
||||
*/
|
||||
public function getToc() { return $this->_toc; }
|
||||
|
||||
/**
|
||||
* Returns the quality indicator. The indicator is from 0 (best quality) to
|
||||
* 100 (worst quality).
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getQualityIndicator() { return $this->_qualityIndicator; }
|
||||
|
||||
/**
|
||||
* Returns the length of the header in bytes.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getLength()
|
||||
{
|
||||
return 4 +
|
||||
($this->_frames !== false ? 4 : 0) +
|
||||
($this->_bytes !== false ? 4 : 0) +
|
||||
(empty($this->_toc) ? 0 : 100) +
|
||||
($this->_qualityIndicator !== false ? 4 : 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user