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