module.audio.amr.php 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. // also https://github.com/JamesHeinrich/getID3 //
  7. /////////////////////////////////////////////////////////////////
  8. // See readme.txt for more details //
  9. /////////////////////////////////////////////////////////////////
  10. // //
  11. // module.audio.aa.php //
  12. // module for analyzing Audible Audiobook files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_amr extends getid3_handler
  17. {
  18. public function Analyze() {
  19. $info = &$this->getid3->info;
  20. $this->fseek($info['avdataoffset']);
  21. $AMRheader = $this->fread(6);
  22. $magic = '#!AMR'."\x0A";
  23. if (substr($AMRheader, 0, 6) != $magic) {
  24. $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($AMRheader, 0, 6)).'"';
  25. return false;
  26. }
  27. // shortcut
  28. $info['amr'] = array();
  29. $thisfile_amr = &$info['amr'];
  30. $info['fileformat'] = 'amr';
  31. $info['audio']['dataformat'] = 'amr';
  32. $info['audio']['bitrate_mode'] = 'vbr'; // within a small predefined range: 4.75kbps to 12.2kbps
  33. $info['audio']['bits_per_sample'] = 13; // http://en.wikipedia.org/wiki/Adaptive_Multi-Rate_audio_codec: "Sampling frequency 8 kHz/13-bit (160 samples for 20 ms frames), filtered to 200–3400 Hz"
  34. $info['audio']['sample_rate'] = 8000; // http://en.wikipedia.org/wiki/Adaptive_Multi-Rate_audio_codec: "Sampling frequency 8 kHz/13-bit (160 samples for 20 ms frames), filtered to 200–3400 Hz"
  35. $info['audio']['channels'] = 1;
  36. $thisfile_amr['frame_mode_count'] = array(0=>0, 1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0);
  37. $buffer = '';
  38. do {
  39. if ((strlen($buffer) < $this->getid3->fread_buffer_size()) && !feof($this->getid3->fp)) {
  40. $buffer .= $this->fread($this->getid3->fread_buffer_size() * 2);
  41. }
  42. $AMR_frame_header = ord(substr($buffer, 0, 1));
  43. $codec_mode_request = ($AMR_frame_header & 0x78) >> 3; // The 2nd bit through 5th bit (counting the most significant bit as the first bit) comprise the CMR (Codec Mode Request), values 0-7 being valid for AMR. The top bit of the CMR can actually be ignored, though it is used when AMR forms RTP payloads. The lower 3-bits of the header are reserved and are not used. Viewing the header from most significant bit to least significant bit, the encoding is XCCCCXXX, where Xs are reserved (typically 0) and the Cs are the CMR.
  44. if ($codec_mode_request > 7) {
  45. $info['error'][] = '';
  46. break;
  47. }
  48. $thisfile_amr['frame_mode_count'][$codec_mode_request]++;
  49. $buffer = substr($buffer, $this->amr_mode_bytes_per_frame($codec_mode_request));
  50. } while (strlen($buffer) > 0);
  51. $info['playtime_seconds'] = array_sum($thisfile_amr['frame_mode_count']) * 0.020; // each frame contain 160 samples and is 20 milliseconds long
  52. $info['audio']['bitrate'] = (8 * ($info['avdataend'] - $info['avdataoffset'])) / $info['playtime_seconds']; // bitrate could be calculated from average bitrate by distributation of frame types. That would give effective audio bitrate, this gives overall file bitrate which will be a little bit higher since every frame will waste 8 bits for header, plus a few bits for octet padding
  53. $info['bitrate'] = $info['audio']['bitrate'];
  54. return true;
  55. }
  56. public function amr_mode_bitrate($key) {
  57. static $amr_mode_bitrate = array(
  58. 0 => 4750,
  59. 1 => 5150,
  60. 2 => 5900,
  61. 3 => 6700,
  62. 4 => 7400,
  63. 5 => 7950,
  64. 6 => 10200,
  65. 7 => 12200,
  66. );
  67. return (isset($amr_mode_bitrate[$key]) ? $amr_mode_bitrate[$key] : false);
  68. }
  69. public function amr_mode_bytes_per_frame($key) {
  70. static $amr_mode_bitrate = array(
  71. 0 => 13, // 1-byte frame header + 95 bits [padded to: 12 bytes] audio data
  72. 1 => 14, // 1-byte frame header + 103 bits [padded to: 13 bytes] audio data
  73. 2 => 16, // 1-byte frame header + 118 bits [padded to: 15 bytes] audio data
  74. 3 => 18, // 1-byte frame header + 134 bits [padded to: 17 bytes] audio data
  75. 4 => 20, // 1-byte frame header + 148 bits [padded to: 19 bytes] audio data
  76. 5 => 21, // 1-byte frame header + 159 bits [padded to: 20 bytes] audio data
  77. 6 => 27, // 1-byte frame header + 204 bits [padded to: 26 bytes] audio data
  78. 7 => 32, // 1-byte frame header + 244 bits [padded to: 31 bytes] audio data
  79. );
  80. return (isset($amr_mode_bitrate[$key]) ? $amr_mode_bitrate[$key] : false);
  81. }
  82. }