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