Debian Codegraph @

source/impdata.cc

raw
1 // ----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2003-2013 Fons Adriaensen <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // ----------------------------------------------------------------------------
19
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include "impdata.h"
24
25
26 #define HDRSIZE 256
27
28
29 //------------------------------------------------------------------------------------------------------------
30
31
32 Impdata::Impdata (void) :
33 _vers (0),
34 _type (0),
35 _rate_n (0),
36 _rate_d (1),
37 _n_fram (0),
38 _n_sect (0),
39 _tref_i (0),
40 _tref_n (0),
41 _tref_d (1),
42 _bits (0),
43 _i_fram (0),
44 _i_sect (0),
45 _mode (0),
46 _size (0),
47 _data (0),
48 _aldfile (0),
49 _sndfile (0)
50 {
51 }
52
53
54 Impdata::~Impdata (void)
55 {
56 close ();
57 delete[] _data;
58 }
59
60
61 int Impdata::set_type (U32 type)
62 {
63 if (_mode) return ERR_MODE;
64 _type = type;
65 deall ();
66 return 0;
67 }
68
69
70 int Impdata::set_n_fram (U32 n_fram)
71 {
72 if (_aldfile) return ERR_MODE;
73 _n_fram = n_fram;
74 deall ();
75 return 0;
76 }
77
78
79 int Impdata::set_n_sect (U32 n_sect)
80 {
81 if (_aldfile) return ERR_MODE;
82 _n_sect = n_sect;
83 return 0;
84 }
85
86
87 int Impdata::alloc (void)
88 {
89 U32 k;
90
91 k = (_type & TYPE__CHAN) * _n_fram;
92 if (k != _size)
93 {
94 delete[] _data;
95 if (k)
96 {
97 _size = k;
98 _data = new float [k];
99 memset (_data, 0, _size * sizeof (float));
100 }
101 else
102 {
103 _size = 0;
104 _data = 0;
105 }
106 }
107 return 0;
108 }
109
110
111 int Impdata::deall (void)
112 {
113 delete[] _data;
114 _data = 0;
115 _size = 0;
116 return 0;
117 }
118
119
120 int Impdata::clear (void)
121 {
122 if (_data) memset (_data, 0, _size * sizeof (float));
123 return 0;
124 }
125
126
127 //------------------------------------------------------------------------------------------------------------
128
129
130 int Impdata::open_read (const char *name)
131 {
132 char p [HDRSIZE];
133
134 if (_mode) return ERR_MODE;
135 deall ();
136 _type = 0;
137 _n_fram = 0;
138 _n_sect = 0;
139
140 if ((_aldfile = fopen (name, "r")) == 0) return ERR_OPEN;
141
142 if (fread (p, 1, HDRSIZE, _aldfile) != HDRSIZE)
143 {
144 fclose (_aldfile);
145 _aldfile = 0;
146 return ERR_READ;
147 }
148
149 if (strcmp (p, "aliki") || p [6] || p [7])
150 {
151 fclose (_aldfile);
152 _aldfile = 0;
153 return ERR_FORM;
154 }
155
156 _vers = *(U32 *)(p + 8);
157 if (_vers == 1)
158 {
159 _type = mapvers1 (*(U32 *)(p + 12));
160 _rate_n = *(U32 *)(p + 16);
161 _rate_d = 1;
162 _n_sect = *(U32 *)(p + 20);
163 _n_fram = *(U32 *)(p + 24);
164 _tref_i = *(U32 *)(p + 28);
165 _tref_n = 0;
166 _tref_d = 1;
167 _bits = 0;
168 }
169 else if (_vers == 2)
170 {
171 _type = *(U32 *)(p + 12);
172 _rate_n = *(U32 *)(p + 16);
173 _rate_d = *(U32 *)(p + 20);
174 _n_fram = *(U32 *)(p + 24);
175 _n_sect = *(U32 *)(p + 28);
176 _tref_i = *(U32 *)(p + 32);
177 _tref_n = *(U32 *)(p + 36);
178 _tref_d = *(U32 *)(p + 40);
179 _bits = *(U32 *)(p + 44);
180 }
181 else
182 {
183 fclose (_aldfile);
184 _aldfile = 0;
185 return ERR_FORM;
186 }
187
188 if (!_type || !_n_fram || !_n_sect)
189 {
190 fclose (_aldfile);
191 _aldfile = 0;
192 return ERR_FORM;
193 }
194
195 _mode = MODE_READ;
196 _i_sect = 0;
197 _i_fram = 0;
198
199 return 0;
200 }
201
202
203 int Impdata::open_write (const char *name)
204 {
205 char p [HDRSIZE];
206
207 if (_mode || !_type || !_n_fram || !_n_sect) return ERR_MODE;
208
209 if ((_aldfile = fopen (name, "w")) == 0) return ERR_OPEN;
210
211 strcpy (p, "aliki");
212 p [6] = p [7] = 0;
213 *(U32 *)(p + 8) = _vers = 2;
214 *(U32 *)(p + 12) = _type;
215 *(U32 *)(p + 16) = _rate_n;
216 *(U32 *)(p + 20) = _rate_d;
217 *(U32 *)(p + 24) = _n_fram;
218 *(U32 *)(p + 28) = _n_sect;
219 *(U32 *)(p + 32) = _tref_i;
220 *(U32 *)(p + 36) = _tref_n;
221 *(U32 *)(p + 40) = _tref_d;
222 *(U32 *)(p + 44) = _bits;
223
224 memset (p + 48, 0, HDRSIZE - 48);
225 if (fwrite (p, 1, HDRSIZE, _aldfile) != HDRSIZE)
226 {
227 fclose (_aldfile);
228 _aldfile = 0;
229 return ERR_WRITE;
230 }
231
232 _mode = MODE_WRITE;
233 _i_sect = 0;
234 _i_fram = 0;
235
236 return 0;
237 }
238
239
240 int Impdata::sf_open_read (const char *name)
241 {
242 SF_INFO I;
243
244 if (_mode) return ERR_MODE;
245 deall ();
246 _type = 0;
247 _n_fram = 0;
248 _n_sect = 0;
249
250 if ((_sndfile = sf_open (name, SFM_READ, &I)) == 0) return ERR_OPEN;
251
252 _type = TYPE_RAW + I.channels;
253 _rate_n = I.samplerate;
254 _rate_d = 1;
255 _n_sect = 1;
256 _n_fram = I.frames;
257 _tref_i = 1;
258 _tref_n = 0;
259 _tref_d = 1;
260 _bits = 0;
261
262 #ifndef NOAMBIS
263 if (sf_command (_sndfile, SFC_WAVEX_GET_AMBISONIC, 0, 0) == SF_AMBISONIC_B_FORMAT)
264 {
265 switch (I.channels)
266 {
267 case 3: _type = TYPE_AMB_B3; break;
268 case 4: _type = TYPE_AMB_B4; break;
269 }
270 }
271 #endif
272
273 if (!_n_fram)
274 {
275 sf_close (_sndfile);
276 _sndfile = 0;
277 return ERR_FORM;
278 }
279
280 _mode = MODE_READ;
281
282 return 0;
283 }
284
285
286 int Impdata::sf_open_write (const char *name)
287 {
288 SF_INFO I;
289
290 if (_mode || !_type || !_n_fram || !_n_sect) return ERR_MODE;
291
292 I.channels = _type & TYPE__CHAN;
293 I.format = (I.channels > 2) ? SF_FORMAT_WAVEX : SF_FORMAT_WAV;
294 I.format |= SF_FORMAT_FLOAT;
295 I.samplerate = _rate_n / _rate_d;
296 I.sections = 1;
297
298 if ((_sndfile = sf_open (name, SFM_WRITE, &I)) == 0) return ERR_OPEN;
299
300 #ifndef NOAMBIS
301 switch (_type)
302 {
303 case TYPE_AMB_B3:
304 case TYPE_AMB_B4:
305 sf_command (_sndfile, SFC_WAVEX_SET_AMBISONIC, 0, SF_AMBISONIC_B_FORMAT);
306 break;
307 }
308 #endif
309
310 _mode = MODE_WRITE;
311 _i_sect = 0;
312 _i_fram = 0;
313
314 return 0;
315 }
316
317
318 int Impdata::close (void)
319 {
320 if (_aldfile)
321 {
322 fclose (_aldfile);
323 _aldfile = 0;
324 }
325 else if (_sndfile)
326 {
327 sf_close (_sndfile);
328 _sndfile = 0;
329 }
330 _mode = 0;
331 return 0;
332 }
333
334
335 //------------------------------------------------------------------------------------------------------------
336
337
338 int Impdata::read_sect (U32 i_sect)
339 {
340 int r;
341
342 r = locate (i_sect);
343 if (r) return r;
344 return _data ? read_ext (_n_fram, _data) : ERR_DATA;
345 }
346
347
348 int Impdata::write_sect (U32 i_sect)
349 {
350 int r;
351
352 r = locate (i_sect);
353 if (r) return r;
354 return _data ? write_ext (_n_fram, _data) : ERR_DATA;
355 }
356
357
358 int Impdata::locate (U32 i_sect)
359 {
360 if (_aldfile)
361 {
362 if (fseek (_aldfile, HDRSIZE + (_type & TYPE__CHAN) * _n_fram * i_sect * sizeof (float), SEEK_SET)) return ERR_SEEK;
363 _i_sect = i_sect;
364 _i_fram = 0;
365 return 0;
366 }
367 else if (_sndfile)
368 {
369 if (sf_seek (_sndfile, _n_fram * i_sect, SEEK_SET) != _n_fram * i_sect) return ERR_SEEK;
370 _i_sect = i_sect;
371 _i_fram = 0;
372 return 0;
373 }
374 else return ERR_MODE;
375 }
376
377
378 int Impdata::read_ext (U32 k, float *p)
379 {
380 if (_mode != MODE_READ) return ERR_MODE;
381 if (k > _n_fram - _i_fram) k = _n_fram - _i_fram;
382 if (_aldfile)
383 {
384 k = fread (p, (_type & TYPE__CHAN) * sizeof (float), k, _aldfile);
385 }
386 else if (_sndfile)
387 {
388 k = sf_readf_float (_sndfile, p, k);
389 }
390 else return ERR_MODE;
391 _i_fram += k;
392 return k;
393 }
394
395
396 int Impdata::write_ext (U32 k, float *p)
397 {
398 if (_mode != MODE_WRITE) return ERR_MODE;
399 if (k > _n_fram - _i_fram) k = _n_fram - _i_fram;
400 if (_aldfile)
401 {
402 k = fwrite (p, (_type & TYPE__CHAN) * sizeof (float), k, _aldfile);
403 }
404 else if (_sndfile)
405 {
406 k = sf_writef_float (_sndfile, p, k);
407 }
408 else return ERR_MODE;
409 _i_fram += k;
410 return k;
411 }
412
413
414 //------------------------------------------------------------------------------------------------------------
415
416
417 U32 Impdata::mapvers1 (U32 type)
418 {
419 if (type < 256) return TYPE_RAW | type;
420 switch (type)
421 {
422 case 0x0102: return TYPE_MS;
423 case 0x0202: return TYPE_SWEEP;
424 case 0x0103: return TYPE_AMB_B3;
425 case 0x0104: return TYPE_AMB_B4;
426 }
427 return TYPE_RAW | type;
428 }
429
430
431 void Impdata::checkext (char *name)
432 {
433 char *p;
434
435 p = strrchr (name, '.');
436 if (p && ! strcmp (p, ".ald")) return;
437 strcat (name, ".ald");
438 }
439
440
441 char Impdata::_suffix [12];
442 const char *Impdata::_suffix2 [] = { "F", "I" };
443 const char *Impdata::_suffix3 [] = { "L", "R" };
444 const char *Impdata::_suffix4 [] = { "M", "S" };
445 const char *Impdata::_suffix5 [] = { "LF", "RF", "LB", "RB" };
446 const char *Impdata::_suffix7 [] = { "W", "X", "Y", "Z" };
447
448
449 const char *Impdata::channame (U32 type, U32 chan)
450 {
451 const char **p;
452
453 if (chan >= (type & TYPE__CHAN)) return 0;
454
455 switch (type & TYPE__TYPE)
456 {
457 case 0x00020000: p = _suffix2; break;
458 case 0x00030000: p = _suffix3; break;
459 case 0x00040000: p = _suffix4; break;
460 case 0x00050000: p = _suffix5; break;
461 case 0x00060000:
462 case 0x00070000: p = _suffix7; break;
463 default: p = 0;
464 }
465
466 if (p) return p [chan];
467 sprintf (_suffix, "%d", chan + 1);
468 return _suffix;
469 }
470
471
472 //------------------------------------------------------------------------------------------------------------