module.audio.avr.php 6.8 KB

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