module.audio.au.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.au.php //
  11. // module for analyzing AU files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  16. exit;
  17. }
  18. class getid3_au extends getid3_handler
  19. {
  20. /**
  21. * @return bool
  22. */
  23. public function Analyze() {
  24. $info = &$this->getid3->info;
  25. $this->fseek($info['avdataoffset']);
  26. $AUheader = $this->fread(8);
  27. $magic = '.snd';
  28. if (substr($AUheader, 0, 4) != $magic) {
  29. $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" (".snd") at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($AUheader, 0, 4)).'"');
  30. return false;
  31. }
  32. // shortcut
  33. $info['au'] = array();
  34. $thisfile_au = &$info['au'];
  35. $info['fileformat'] = 'au';
  36. $info['audio']['dataformat'] = 'au';
  37. $info['audio']['bitrate_mode'] = 'cbr';
  38. $thisfile_au['encoding'] = 'ISO-8859-1';
  39. $thisfile_au['header_length'] = getid3_lib::BigEndian2Int(substr($AUheader, 4, 4));
  40. $AUheader .= $this->fread($thisfile_au['header_length'] - 8);
  41. $info['avdataoffset'] += $thisfile_au['header_length'];
  42. $thisfile_au['data_size'] = getid3_lib::BigEndian2Int(substr($AUheader, 8, 4));
  43. $thisfile_au['data_format_id'] = getid3_lib::BigEndian2Int(substr($AUheader, 12, 4));
  44. $thisfile_au['sample_rate'] = getid3_lib::BigEndian2Int(substr($AUheader, 16, 4));
  45. $thisfile_au['channels'] = getid3_lib::BigEndian2Int(substr($AUheader, 20, 4));
  46. $thisfile_au['comments']['comment'][] = trim(substr($AUheader, 24));
  47. $thisfile_au['data_format'] = $this->AUdataFormatNameLookup($thisfile_au['data_format_id']);
  48. $thisfile_au['used_bits_per_sample'] = $this->AUdataFormatUsedBitsPerSampleLookup($thisfile_au['data_format_id']);
  49. if ($thisfile_au['bits_per_sample'] = $this->AUdataFormatBitsPerSampleLookup($thisfile_au['data_format_id'])) {
  50. $info['audio']['bits_per_sample'] = $thisfile_au['bits_per_sample'];
  51. } else {
  52. unset($thisfile_au['bits_per_sample']);
  53. }
  54. $info['audio']['sample_rate'] = $thisfile_au['sample_rate'];
  55. $info['audio']['channels'] = $thisfile_au['channels'];
  56. if (($info['avdataoffset'] + $thisfile_au['data_size']) > $info['avdataend']) {
  57. $this->warning('Possible truncated file - expecting "'.$thisfile_au['data_size'].'" bytes of audio data, only found '.($info['avdataend'] - $info['avdataoffset']).' bytes"');
  58. }
  59. $info['playtime_seconds'] = $thisfile_au['data_size'] / ($thisfile_au['sample_rate'] * $thisfile_au['channels'] * ($thisfile_au['used_bits_per_sample'] / 8));
  60. $info['audio']['bitrate'] = ($thisfile_au['data_size'] * 8) / $info['playtime_seconds'];
  61. return true;
  62. }
  63. /**
  64. * @param int $id
  65. *
  66. * @return string|false
  67. */
  68. public function AUdataFormatNameLookup($id) {
  69. static $AUdataFormatNameLookup = array(
  70. 0 => 'unspecified format',
  71. 1 => '8-bit mu-law',
  72. 2 => '8-bit linear',
  73. 3 => '16-bit linear',
  74. 4 => '24-bit linear',
  75. 5 => '32-bit linear',
  76. 6 => 'floating-point',
  77. 7 => 'double-precision float',
  78. 8 => 'fragmented sampled data',
  79. 9 => 'SUN_FORMAT_NESTED',
  80. 10 => 'DSP program',
  81. 11 => '8-bit fixed-point',
  82. 12 => '16-bit fixed-point',
  83. 13 => '24-bit fixed-point',
  84. 14 => '32-bit fixed-point',
  85. 16 => 'non-audio display data',
  86. 17 => 'SND_FORMAT_MULAW_SQUELCH',
  87. 18 => '16-bit linear with emphasis',
  88. 19 => '16-bit linear with compression',
  89. 20 => '16-bit linear with emphasis + compression',
  90. 21 => 'Music Kit DSP commands',
  91. 22 => 'SND_FORMAT_DSP_COMMANDS_SAMPLES',
  92. 23 => 'CCITT g.721 4-bit ADPCM',
  93. 24 => 'CCITT g.722 ADPCM',
  94. 25 => 'CCITT g.723 3-bit ADPCM',
  95. 26 => 'CCITT g.723 5-bit ADPCM',
  96. 27 => 'A-Law 8-bit'
  97. );
  98. return (isset($AUdataFormatNameLookup[$id]) ? $AUdataFormatNameLookup[$id] : false);
  99. }
  100. /**
  101. * @param int $id
  102. *
  103. * @return int|false
  104. */
  105. public function AUdataFormatBitsPerSampleLookup($id) {
  106. static $AUdataFormatBitsPerSampleLookup = array(
  107. 1 => 8,
  108. 2 => 8,
  109. 3 => 16,
  110. 4 => 24,
  111. 5 => 32,
  112. 6 => 32,
  113. 7 => 64,
  114. 11 => 8,
  115. 12 => 16,
  116. 13 => 24,
  117. 14 => 32,
  118. 18 => 16,
  119. 19 => 16,
  120. 20 => 16,
  121. 23 => 16,
  122. 25 => 16,
  123. 26 => 16,
  124. 27 => 8
  125. );
  126. return (isset($AUdataFormatBitsPerSampleLookup[$id]) ? $AUdataFormatBitsPerSampleLookup[$id] : false);
  127. }
  128. /**
  129. * @param int $id
  130. *
  131. * @return int|false
  132. */
  133. public function AUdataFormatUsedBitsPerSampleLookup($id) {
  134. static $AUdataFormatUsedBitsPerSampleLookup = array(
  135. 1 => 8,
  136. 2 => 8,
  137. 3 => 16,
  138. 4 => 24,
  139. 5 => 32,
  140. 6 => 32,
  141. 7 => 64,
  142. 11 => 8,
  143. 12 => 16,
  144. 13 => 24,
  145. 14 => 32,
  146. 18 => 16,
  147. 19 => 16,
  148. 20 => 16,
  149. 23 => 4,
  150. 25 => 3,
  151. 26 => 5,
  152. 27 => 8,
  153. );
  154. return (isset($AUdataFormatUsedBitsPerSampleLookup[$id]) ? $AUdataFormatUsedBitsPerSampleLookup[$id] : false);
  155. }
  156. }