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