PageRenderTime 44ms CodeModel.GetById 13ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/src/Zend/Media/Mpeg/Abs/LameHeader.php

http://php-reader.googlecode.com/
PHP | 527 lines | 189 code | 85 blank | 253 comment | 0 complexity | 3fd6370da6ca5a79b2e0e9f7884a46f5 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Media
 17 * @subpackage MPEG
 18 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: LameHeader.php 177 2010-03-09 13:13:34Z svollbehr $
 21 */
 22
 23/**#@+ @ignore */
 24require_once 'Zend/Bit/Twiddling.php';
 25require_once 'Zend/Media/Mpeg/Abs/Object.php';
 26/**#@-*/
 27
 28/**
 29 * This class represents a LAME extension to the Xing VBR header. The purpose of
 30 * this header is to provide extra information about the audio bistream, encoder
 31 * and parameters used. This header should, as much as possible, be meaningfull
 32 * for as many encoders as possible, even if it is unlikely that other encoders
 33 * than LAME will implement it.
 34 *
 35
 36 * This header should be backward compatible with the Xing VBR tag, providing
 37 * basic support for a lot of already written software. As much as possible the
 38 * current revision (revision 1) should provide information similar to the one
 39 * already provided by revision 0.
 40 *
 41 * @category   Zend
 42 * @package    Zend_Media
 43 * @subpackage MPEG
 44 * @author     Sven Vollbehr <sven@vollbehr.eu>
 45 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 
 46 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 47 * @version    $Id: LameHeader.php 177 2010-03-09 13:13:34Z svollbehr $
 48 */
 49class Zend_Media_Mpeg_Abs_LameHeader extends Zend_Media_Mpeg_Abs_Object
 50{
 51    /** @var integer */
 52    const VBR_METHOD_CONSTANT = 1;
 53
 54    /** @var integer */
 55    const VBR_METHOD_ABR = 2;
 56
 57    /** @var integer */
 58    const VBR_METHOD_RH = 3;
 59
 60    /** @var integer */
 61    const VBR_METHOD_MTRH = 4;
 62
 63    /** @var integer */
 64    const VBR_METHOD_MT = 5;
 65
 66    /** @var integer */
 67    const ENCODING_FLAG_NSPSYTUNE = 1;
 68
 69    /** @var integer */
 70    const ENCODING_FLAG_NSSAFEJOINT = 2;
 71
 72    /** @var integer */
 73    const ENCODING_FLAG_NOGAP_CONTINUED = 4;
 74
 75    /** @var integer */
 76    const ENCODING_FLAG_NOGAP_CONTINUATION = 8;
 77
 78    /** @var integer */
 79    const MODE_MONO = 0;
 80
 81    /** @var integer */
 82    const MODE_STEREO = 1;
 83
 84    /** @var integer */
 85    const MODE_DUAL = 2;
 86
 87    /** @var integer */
 88    const MODE_JOINT = 3;
 89
 90    /** @var integer */
 91    const MODE_FORCE = 4;
 92
 93    /** @var integer */
 94    const MODE_AUTO = 5;
 95
 96    /** @var integer */
 97    const MODE_INTENSITY = 6;
 98
 99    /** @var integer */
100    const MODE_UNDEFINED = 7;
101
102    /** @var integer */
103    const SOURCE_FREQUENCY_32000_OR_LOWER = 0;
104
105    /** @var integer */
106    const SOURCE_FREQUENCY_44100 = 1;
107
108    /** @var integer */
109    const SOURCE_FREQUENCY_48000 = 2;
110
111    /** @var integer */
112    const SOURCE_FREQUENCY_HIGHER = 3;
113
114    /** @var integer */
115    const SURROUND_NONE = 0;
116
117    /** @var integer */
118    const SURROUND_DPL = 1;
119
120    /** @var integer */
121    const SURROUND_DPL2 = 2;
122
123    /** @var integer */
124    const SURROUND_AMBISONIC = 3;
125
126    /** @var string */
127    private $_version;
128
129    /** @var integer */
130    private $_revision;
131
132    /** @var integer */
133    private $_vbrMethod;
134
135    /** @var integer */
136    private $_lowpass;
137
138    /** @var integer */
139    private $_peakSignalAmplitude;
140
141    /** @var integer */
142    private $_radioReplayGain;
143
144    /** @var integer */
145    private $_audiophileReplayGain;
146
147    /** @var integer */
148    private $_encodingFlags;
149
150    /** @var integer */
151    private $_athType;
152
153    /** @var integer */
154    private $_bitrate;
155
156    /** @var integer */
157    private $_encoderDelaySamples;
158
159    /** @var integer */
160    private $_paddedSamples;
161
162    /** @var integer */
163    private $_sourceSampleFrequency;
164
165    /** @var boolean */
166    private $_unwiseSettingsUsed;
167
168    /** @var integer */
169    private $_mode;
170
171    /** @var integer */
172    private $_noiseShaping;
173
174    /** @var integer */
175    private $_mp3Gain;
176
177    /** @var integer */
178    private $_surroundInfo;
179
180    /** @var integer */
181    private $_presetUsed;
182
183    /** @var integer */
184    private $_musicLength;
185
186    /** @var integer */
187    private $_musicCrc;
188
189    /** @var integer */
190    private $_crc;
191
192    /**
193     * Constructs the class with given parameters and reads object related data
194     * from the bitstream.
195     *
196     * @param Zend_Io_Reader $reader The reader object.
197     * @param Array          $options Array of options.
198     */
199    public function __construct($reader, &$options = array())
200    {
201        parent::__construct($reader, $options);
202
203        $this->_version = $this->_reader->readString8(5);
204
205        $tmp = $this->_reader->readUInt8();
206        $this->_revision = Zend_Bit_Twiddling::getValue($tmp, 4, 8);
207        $this->_vbrMethod = Zend_Bit_Twiddling::getValue($tmp, 0, 3);
208
209        $this->_lowpass = $this->_reader->readUInt8() * 100;
210
211        $this->_peakSignalAmplitude = $this->_reader->readUInt32BE();
212
213        $tmp = $this->_reader->readUInt16BE();
214        $this->_radioReplayGain = array(
215            'name' => Zend_Bit_Twiddling::getValue($tmp, 0, 2),
216            'originator' => Zend_Bit_Twiddling::getValue($tmp, 3, 5),
217            'absoluteGainAdjustment' =>
218                Zend_Bit_Twiddling::getValue($tmp, 7, 15) / 10
219        );
220
221        $tmp = $this->_reader->readUInt16BE();
222        $this->_audiophileReplayGain = array(
223            'name' => Zend_Bit_Twiddling::getValue($tmp, 0, 2),
224            'originator' => Zend_Bit_Twiddling::getValue($tmp, 3, 5),
225            'absoluteGainAdjustment' =>
226                Zend_Bit_Twiddling::getValue($tmp, 7, 15) / 10
227        );
228
229        $tmp = $this->_reader->readUInt8();
230        $this->_encodingFlags = Zend_Bit_Twiddling::getValue($tmp, 4, 8);
231        $this->_athType = Zend_Bit_Twiddling::getValue($tmp, 0, 3);
232
233        $this->_bitrate = $this->_reader->readUInt8();
234
235        $tmp = $this->_reader->readUInt32BE();
236        // Encoder delay fields
237        $this->_encoderDelaySamples =
238            Zend_Bit_Twiddling::getValue($tmp, 20, 31);
239        $this->_paddedSamples = Zend_Bit_Twiddling::getValue($tmp, 8, 19);
240        // Misc field
241        $this->_sourceSampleFrequency =
242            Zend_Bit_Twiddling::getValue($tmp, 6, 7);
243        $this->_unwiseSettingsUsed = Zend_Bit_Twiddling::testBit($tmp, 5);
244        $this->_mode = Zend_Bit_Twiddling::getValue($tmp, 2, 4);
245        $this->_noiseShaping = Zend_Bit_Twiddling::getValue($tmp, 0, 1);
246
247        $this->_mp3Gain = pow(2, $this->_reader->readInt8() / 4);
248
249        $tmp = $this->_reader->readUInt16BE();
250        $this->_surroundInfo = Zend_Bit_Twiddling::getValue($tmp, 11, 14);
251        $this->_presetUsed = Zend_Bit_Twiddling::getValue($tmp, 0, 10);
252
253        $this->_musicLength = $this->_reader->readUInt32BE();
254
255        $this->_musicCrc = $this->_reader->readUInt16BE();
256        $this->_crc = $this->_reader->readUInt16BE();
257    }
258
259    /**
260     * Returns the version string of the header.
261     *
262     * @return string
263     */
264    public function getVersion() 
265    {
266        return $this->_version; 
267    }
268
269    /**
270     * Returns the info tag revision.
271     *
272     * @return integer
273     */
274    public function getRevision() 
275    {
276        return $this->_revision; 
277    }
278
279    /**
280     * Returns the VBR method used for encoding. See the corresponding constants
281     * for possible return values.
282     *
283     * @return integer
284     */
285    public function getVbrMethod() 
286    {
287        return $this->_vbrMethod; 
288    }
289
290    /**
291     * Returns the lowpass filter value.
292     *
293     * @return integer
294     */
295    public function getLowpass() 
296    {
297        return $this->_lowpass; 
298    }
299
300    /**
301     * Returns the peak signal amplitude field of replay gain. The value of 1.0
302     * (ie 100%) represents maximal signal amplitude storeable in decoding
303     * format.
304     *
305     * @return integer
306     */
307    public function getPeakSignalAmplitude()
308    {
309        return $this->_peakSignalAmplitude;
310    }
311
312    /**
313     * Returns the radio replay gain field of replay gain, required to make all
314     * tracks equal loudness, as an array that consists of the following keys.
315     *
316     *   o name -- Specifies the name of the gain adjustment. Can be one of the
317     *     following values: 0 = not set, 1 = radio, or 2 = audiophile.
318     *
319     *   o originator -- Specifies the originator of the gain adjustment. Can be
320     *     one of the following values: 0 = not set, 1 = set by artist, 2 = set
321     *     by user, 3 = set by my model, 4 = set by simple RMS average.
322     *
323     *   o absoluteGainAdjustment -- Speficies the absolute gain adjustment.
324     *
325     * @return Array
326     */
327    public function getRadioReplayGain() 
328    {
329        return $this->_radioReplayGain; 
330    }
331
332    /**
333     * Returns the audiophile replay gain field of replay gain, required to give
334     * ideal listening loudness, as an array that consists of the following
335     * keys.
336     *
337     *   o name -- Specifies the name of the gain adjustment. Can be one of the
338     *     following values: 0 = not set, 1 = radio, or 2 = audiophile.
339     *
340     *   o originator -- Specifies the originator of the gain adjustment. Can be
341     *     one of the following values: 0 = not set, 1 = set by artist, 2 = set
342     *     by user, 3 = set by my model, 4 = set by simple RMS average.
343     *
344     *   o absoluteGainAdjustment -- Speficies the absolute gain adjustment.
345     *
346     * @return Array
347     */
348    public function getAudiophileReplayGain()
349    {
350        return $this->_audiophileReplayGain;
351    }
352
353    /**
354     * Returns the encoding flags. See the corresponding flag constants for
355     * possible values.
356     *
357     * @return integer
358     */
359    public function getEncodingFlags() 
360    {
361        return $this->_encodingFlags; 
362    }
363
364    /**
365     * Returns the ATH type.
366     *
367     * @return integer
368     */
369    public function getAthType() 
370    {
371        return $this->_athType; 
372    }
373
374    /**
375     * Returns the bitrate for CBR encoded files and the minimal birate for
376     * VBR encoded file. The maximum value of this field is 255 even with higher
377     * actual bitrates.
378     *
379     * @return integer
380     */
381    public function getBitrate() 
382    {
383        return $this->_bitrate; 
384    }
385
386    /**
387     * Returns the encoder delay or number of samples added at start.
388     *
389     * @return integer
390     */
391    public function getEncoderDelaySamples()
392    {
393        return $this->_encoderDelaySamples;
394    }
395
396    /**
397     * Returns the number of padded samples to complete the last frame.
398     *
399     * @return integer
400     */
401    public function getPaddedSamples() 
402    {
403        return $this->_paddedSamples; 
404    }
405
406    /**
407     * Returns the source sample frequency. See corresponding constants for
408     * possible values.
409     *
410     * @return integer
411     */
412    public function getSourceSampleFrequency()
413    {
414        return $this->_sourceSampleFrequency;
415    }
416
417    /**
418     * An alias to getUnwiseSettingsUsed().
419     *
420     * @see getUnwiseSettingsUsed
421     * @return boolean
422     */
423    public function areUnwiseSettingsUsed()
424    {
425        return $this->getUnwiseSettingsUsed();
426    }
427
428    /**
429     * Returns whether unwise settings were used to encode the file.
430     *
431     * @return boolean
432     */
433    public function getUnwiseSettingsUsed() 
434    {
435        return $this->_unwiseSettingsUsed; 
436    }
437
438    /**
439     * Returns the stereo mode. See corresponding constants for possible values.
440     *
441     * @return integer
442     */
443    public function getMode() 
444    {
445        return $this->_mode; 
446    }
447
448    /**
449     * Returns the noise shaping.
450     *
451     * @return integer
452     */
453    public function getNoiseShaping() 
454    {
455        return $this->_noiseShaping; 
456    }
457
458    /**
459     * Returns the MP3 gain change. Any MP3 can be amplified in a lossless
460     * manner. If done so, this field can be used to log such transformation
461     * happened so that any given time it can be undone.
462     *
463     * @return integer
464     */
465    public function getMp3Gain() 
466    {
467        return $this->_mp3Gain;
468    }
469
470    /**
471     * Returns the surround info. See corresponding contants for possible
472     * values.
473     *
474     * @return integer
475     */
476    public function getSurroundInfo() 
477    {
478        return $this->_surroundInfo; 
479    }
480
481    /**
482     * Returns the preset used in encoding.
483     *
484     * @return integer
485     */
486    public function getPresetUsed() 
487    {
488        return $this->_presetUsed; 
489    }
490
491    /**
492     * Returns the exact length in bytes of the MP3 file originally made by LAME
493     * excluded ID3 tag info at the end.
494     *
495     * The first byte it counts is the first byte of this LAME header and the
496     * last byte it counts is the last byte of the last MP3 frame containing
497     * music. The value should be equal to file length at the time of LAME
498     * encoding, except when using ID3 tags.
499     *
500     * @return integer
501     */
502    public function getMusicLength() 
503    {
504        return $this->_musicLength; 
505    }
506
507    /**
508     * Returns the CRC-16 of the complete MP3 music data as made originally by
509     * LAME.
510     *
511     * @return integer
512     */
513    public function getMusicCrc() 
514    {
515        return $this->_musicCrc; 
516    }
517
518    /**
519     * Returns the CRC-16 of the first 190 bytes of the header frame.
520     *
521     * @return integer
522     */
523    public function getCrc() 
524    {
525        return $this->_crc; 
526    }
527}