module.audio.avr.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.avr.php //
  11. // module for analyzing AVR Audio 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_avr extends getid3_handler
  19. {
  20. /**
  21. * @return bool
  22. */
  23. public function Analyze() {
  24. $info = &$this->getid3->info;
  25. // http://cui.unige.ch/OSG/info/AudioFormats/ap11.html
  26. // http://www.btinternet.com/~AnthonyJ/Atari/programming/avr_format.html
  27. // offset type length name comments
  28. // ---------------------------------------------------------------------
  29. // 0 char 4 ID format ID == "2BIT"
  30. // 4 char 8 name sample name (unused space filled with 0)
  31. // 12 short 1 mono/stereo 0=mono, -1 (0xFFFF)=stereo
  32. // With stereo, samples are alternated,
  33. // the first voice is the left :
  34. // (LRLRLRLRLRLRLRLRLR...)
  35. // 14 short 1 resolution 8, 12 or 16 (bits)
  36. // 16 short 1 signed or not 0=unsigned, -1 (0xFFFF)=signed
  37. // 18 short 1 loop or not 0=no loop, -1 (0xFFFF)=loop on
  38. // 20 short 1 MIDI note 0xFFnn, where 0 <= nn <= 127
  39. // 0xFFFF means "no MIDI note defined"
  40. // 22 byte 1 Replay speed Frequence in the Replay software
  41. // 0=5.485 Khz, 1=8.084 Khz, 2=10.971 Khz,
  42. // 3=16.168 Khz, 4=21.942 Khz, 5=32.336 Khz
  43. // 6=43.885 Khz, 7=47.261 Khz
  44. // -1 (0xFF)=no defined Frequence
  45. // 23 byte 3 sample rate in Hertz
  46. // 26 long 1 size in bytes (2 * bytes in stereo)
  47. // 30 long 1 loop begin 0 for no loop
  48. // 34 long 1 loop size equal to 'size' for no loop
  49. // 38 short 2 Reserved, MIDI keyboard split */
  50. // 40 short 2 Reserved, sample compression */
  51. // 42 short 2 Reserved */
  52. // 44 char 20; Additional filename space, used if (name[7] != 0)
  53. // 64 byte 64 user data
  54. // 128 bytes ? sample data (12 bits samples are coded on 16 bits:
  55. // 0000 xxxx xxxx xxxx)
  56. // ---------------------------------------------------------------------
  57. // Note that all values are in motorola (big-endian) format, and that long is
  58. // assumed to be 4 bytes, and short 2 bytes.
  59. // When reading the samples, you should handle both signed and unsigned data,
  60. // and be prepared to convert 16->8 bit, or mono->stereo if needed. To convert
  61. // 8-bit data between signed/unsigned just add 127 to the sample values.
  62. // Simularly for 16-bit data you should add 32769
  63. $info['fileformat'] = 'avr';
  64. $this->fseek($info['avdataoffset']);
  65. $AVRheader = $this->fread(128);
  66. $info['avr']['raw']['magic'] = substr($AVRheader, 0, 4);
  67. $magic = '2BIT';
  68. if ($info['avr']['raw']['magic'] != $magic) {
  69. $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['avr']['raw']['magic']).'"');
  70. unset($info['fileformat']);
  71. unset($info['avr']);
  72. return false;
  73. }
  74. $info['avdataoffset'] += 128;
  75. $info['avr']['sample_name'] = rtrim(substr($AVRheader, 4, 8));
  76. $info['avr']['raw']['mono'] = getid3_lib::BigEndian2Int(substr($AVRheader, 12, 2));
  77. $info['avr']['bits_per_sample'] = getid3_lib::BigEndian2Int(substr($AVRheader, 14, 2));
  78. $info['avr']['raw']['signed'] = getid3_lib::BigEndian2Int(substr($AVRheader, 16, 2));
  79. $info['avr']['raw']['loop'] = getid3_lib::BigEndian2Int(substr($AVRheader, 18, 2));
  80. $info['avr']['raw']['midi'] = getid3_lib::BigEndian2Int(substr($AVRheader, 20, 2));
  81. $info['avr']['raw']['replay_freq'] = getid3_lib::BigEndian2Int(substr($AVRheader, 22, 1));
  82. $info['avr']['sample_rate'] = getid3_lib::BigEndian2Int(substr($AVRheader, 23, 3));
  83. $info['avr']['sample_length'] = getid3_lib::BigEndian2Int(substr($AVRheader, 26, 4));
  84. $info['avr']['loop_start'] = getid3_lib::BigEndian2Int(substr($AVRheader, 30, 4));
  85. $info['avr']['loop_end'] = getid3_lib::BigEndian2Int(substr($AVRheader, 34, 4));
  86. $info['avr']['midi_split'] = getid3_lib::BigEndian2Int(substr($AVRheader, 38, 2));
  87. $info['avr']['sample_compression'] = getid3_lib::BigEndian2Int(substr($AVRheader, 40, 2));
  88. $info['avr']['reserved'] = getid3_lib::BigEndian2Int(substr($AVRheader, 42, 2));
  89. $info['avr']['sample_name_extra'] = rtrim(substr($AVRheader, 44, 20));
  90. $info['avr']['comment'] = rtrim(substr($AVRheader, 64, 64));
  91. $info['avr']['flags']['stereo'] = (($info['avr']['raw']['mono'] == 0) ? false : true);
  92. $info['avr']['flags']['signed'] = (($info['avr']['raw']['signed'] == 0) ? false : true);
  93. $info['avr']['flags']['loop'] = (($info['avr']['raw']['loop'] == 0) ? false : true);
  94. $info['avr']['midi_notes'] = array();
  95. if (($info['avr']['raw']['midi'] & 0xFF00) != 0xFF00) {
  96. $info['avr']['midi_notes'][] = ($info['avr']['raw']['midi'] & 0xFF00) >> 8;
  97. }
  98. if (($info['avr']['raw']['midi'] & 0x00FF) != 0x00FF) {
  99. $info['avr']['midi_notes'][] = ($info['avr']['raw']['midi'] & 0x00FF);
  100. }
  101. if (($info['avdataend'] - $info['avdataoffset']) != ($info['avr']['sample_length'] * (($info['avr']['bits_per_sample'] == 8) ? 1 : 2))) {
  102. $this->warning('Probable truncated file: expecting '.($info['avr']['sample_length'] * (($info['avr']['bits_per_sample'] == 8) ? 1 : 2)).' bytes of audio data, found '.($info['avdataend'] - $info['avdataoffset']));
  103. }
  104. $info['audio']['dataformat'] = 'avr';
  105. $info['audio']['lossless'] = true;
  106. $info['audio']['bitrate_mode'] = 'cbr';
  107. $info['audio']['bits_per_sample'] = $info['avr']['bits_per_sample'];
  108. $info['audio']['sample_rate'] = $info['avr']['sample_rate'];
  109. $info['audio']['channels'] = ($info['avr']['flags']['stereo'] ? 2 : 1);
  110. $info['playtime_seconds'] = ($info['avr']['sample_length'] / $info['audio']['channels']) / $info['avr']['sample_rate'];
  111. $info['audio']['bitrate'] = ($info['avr']['sample_length'] * (($info['avr']['bits_per_sample'] == 8) ? 8 : 16)) / $info['playtime_seconds'];
  112. return true;
  113. }
  114. }