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