Debian Codegraph @

source/calcthr.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 <stdio.h>
23 #include <string.h>
24 #include <math.h>
25 #include "calcthr.h"
26
27
28 Calcthr::Calcthr (void) :
29 A_thread ("calc")
30 {
31 }
32
33
34 Calcthr::~Calcthr (void)
35 {
36 }
37
38
39 void Calcthr::stop (void)
40 {
41 put_event (EV_EXIT, 1);
42 }
43
44
45 void Calcthr::thr_main (void)
46 {
47 int E;
48
49 while (1)
50 {
51 E = get_event ();
52 switch (E)
53 {
54 case TO_CALC:
55 handle_messsage (get_message ());
56 break;
57
58 case EV_EXIT:
59 return;
60 }
61 }
62 }
63
64
65 void Calcthr::handle_messsage (ITC_mesg *M)
66 {
67 switch (M->type ())
68 {
69 case MT_CONV_ONE:
70 conv_one ((M_calc_conv *) M);
71 send_event (TO_CTRL, M);
72 break;
73
74 case MT_CONV_ALL:
75 conv_all ((M_calc_conv *) M);
76 send_event (TO_CTRL, M);
77 break;
78
79 case MT_EDIT_ONE:
80 edit_one ((M_calc_edit *) M);
81 send_event (TO_CTRL, M);
82 break;
83
84 case MT_EDIT_ALL:
85 edit_all ((M_calc_edit *) M);
86 send_event (TO_CTRL, M);
87 break;
88
89 case MT_EXPORT:
90 exp_impdata ((M_export *) M);
91 send_event (TO_CTRL, M);
92 break;
93
94 default:
95 M->recover ();
96 }
97 }
98
99
100
101 void Calcthr::conv_one (M_calc_conv *M)
102 {
103 int c, i, j, k, n1, n2, r, w;
104 float *p, *q;
105
106 M->_conv->reset ();
107 k = M->_conv->part ();
108 for (r = 0; r < M->_k1; r += k)
109 {
110 n1 = M->_data1->n_fram () - r;
111 if (n1 < 0) n1 = 0;
112 if (n1 > k) n1 = k;
113 n2 = k - n1;
114 c = M->_data1->n_chan ();
115 for (j = 0; j < c; j++)
116 {
117 p = M->_data1->data (j) + c * r;
118 q = M->_conv->wr_ptr (j);
119 for (i = 0; i < n1; i++)
120 {
121 *q++ = *p;
122 p += c;
123 }
124 memset (q, 0, n2 * sizeof (float));
125 }
126 M->_conv->process ();
127 send_event (EV_PROG, 1);
128 w = r - M->_k0;
129 n1 = 0;
130 n2 = M->_data2->n_fram () - w;
131 if (n2 > k) n2 = k;
132 if (w < 0)
133 {
134 n1 -= w;
135 n2 += w;
136 w = 0;
137 }
138 if (n2 > 0)
139 {
140 c = M->_data2->n_chan ();
141 for (j = 0; j < c; j++)
142 {
143 p = M->_conv->rd_ptr (j) + n1;
144 q = M->_data2->data (j) + c * w;
145 for (i = 0; i < n2; i++)
146 {
147 *q = *p++;
148 q += c;
149 }
150 }
151 }
152 }
153 if (M->_sect >= 0) M->_data2->write_sect (M->_sect);
154 }
155
156
157 void Calcthr::conv_all (M_calc_conv *M)
158 {
159 int i, n;
160
161 n = M->_data1->n_sect ();
162 for (i = 0; i < n; i++)
163 {
164 M->_data1->read_sect (i);
165 M->_sect = i;
166 conv_one (M);
167 }
168 }
169
170
171
172 void Calcthr::edit_one (M_calc_edit *M)
173 {
174 int c, d, i, j, k, n;
175 float *p, *q, g, w;
176
177 c = M->_data1->n_chan ();
178 d = M->_trm01 - M->_trm00;
179 if (d)
180 {
181 p = M->_data1->data (0);
182 k = M->_ind0 * c;
183 while (k--) *p++ = 0;
184 w = M_PI / d;
185 for (i = M->_ind0; (i < M->_trm01) && (i <= M->_ind1); i++)
186 {
187 g = 0.5f * (1.0f - cosf (w * (i - M->_trm00)));
188 for (j = 0; j < c; j++) *p++ *= g;
189 }
190 }
191 d = M->_trm11 - M->_trm10;
192 if (d)
193 {
194 n = M->_data1->n_fram ();
195 p = M->_data1->data (0) + n * c;
196 n -= 1;
197 k = (n - M->_ind1) * c;
198 while (k--) *--p = 0;
199 w = M_PI / d;
200 for (i = M->_ind1; (i > M->_trm10) && (i >= M->_ind0); i--)
201 {
202 g = 0.5f * (1.0f - cosf (w * (i - M->_trm11)));
203 for (j = 0; j < c; j++) *--p *= g;
204 }
205 }
206 if (M->_slope < 0)
207 {
208 p = M->_data1->data (0) + M->_ind0 * c;
209 w = M->_slope / (10.0f * M->_data1->drate ());
210 for (i = M->_ind0; i <= M->_ind1; i++)
211 {
212 g = 1.0f / sqrtf (1.0f + powf (10.0f, (M->_tfade - i) * w));
213 for (j = 0; j < c; j++) *p++ *= g;
214 }
215 }
216 if (M->_sect >= 0)
217 {
218 p = M->_data1->data (0) + c * M->_ind0;
219 q = M->_data2->data (0);
220 for (i = 0; i < c * (int)(M->_data2->n_fram ()); i++) *q++ = M->_gain * *p++;
221 M->_data2->write_sect (M->_sect);
222 send_event (EV_PROG, 1);
223 }
224 }
225
226
227 void Calcthr::edit_all (M_calc_edit *M)
228 {
229 int i, n;
230
231 n = M->_data1->n_sect ();
232 for (i = 0; i < n; i++)
233 {
234 M->_data1->read_sect (i);
235 M->_sect = i;
236 edit_one (M);
237 }
238 }
239
240
241
242 void Calcthr::exp_impdata (M_export *M)
243 {
244 SNDFILE *S;
245 U32 i, c, m, n, s, s0, s1;
246 float *d;
247
248 i = M->_impd->i_sect ();
249 d = 0;
250 for (n = 0, m = M->_mask; m; m >>= 1) n += m & 1;
251 if (M->_sect == EXP_SECT_CURR) { s0 = i; s1 = i + 1; }
252 else { s0 = 0; s1 = M->_impd->n_sect (); }
253
254 if (M->_type == EXP_TYPE_WAVEX || M->_type == EXP_TYPE_AMBIS)
255 {
256 if (M->_impd->n_chan () > n) d = new float [n * M->_impd->n_fram ()];
257 if (M->_sect == EXP_SECT_COMB)
258 {
259 if (!(S = exp_open_multi (M->_impd, M->_name, M->_type, M->_form, -1, n)))
260 {
261 M->_impd->read_sect (i);
262 M->_rval = 1;
263 delete[] d;
264 }
265 for (s = s0; s < s1; s++)
266 {
267 M->_impd->read_sect (s);
268 exp_writesect (S, M->_impd, m, n, d);
269 }
270 sf_close (S);
271 }
272 else
273 {
274 for (s = s0; s < s1; s++)
275 {
276 if (!(S = exp_open_multi (M->_impd, M->_name, M->_type, M->_form, s, n)))
277 {
278 M->_impd->read_sect (i);
279 M->_rval = 1;
280 delete[] d;
281 }
282 M->_impd->read_sect (s);
283 exp_writesect (S, M->_impd, m, n, d);
284 sf_close (S);
285 }
286 }
287 }
288 else
289 {
290 if (M->_impd->n_chan () > 1) d = new float [M->_impd->n_fram ()];
291 for (c = 0, m = 1; c < M->_impd->n_chan (); c++, m <<= 1)
292 {
293 if (m & M->_mask)
294 {
295 if (M->_sect == EXP_SECT_COMB)
296 {
297 if (!(S = exp_open_single (M->_impd, M->_name, M->_type, M->_form, -1, c)))
298 {
299 M->_impd->read_sect (i);
300 M->_rval = 1;
301 delete[] d;
302 }
303 for (s = s0; s < s1; s++)
304 {
305 M->_impd->read_sect (s);
306 exp_writesect (S, M->_impd, m, 1, d);
307 }
308 sf_close (S);
309 }
310 else
311 {
312 for (s = s0; s < s1; s++)
313 {
314 if (!(S = exp_open_single (M->_impd, M->_name, M->_type, M->_form, s, c)))
315 {
316 M->_impd->read_sect (i);
317 M->_rval = 1;
318 delete[] d;
319 }
320 M->_impd->read_sect (s);
321 exp_writesect (S, M->_impd, m, 1, d);
322 sf_close (S);
323 }
324 }
325 }
326 }
327 }
328
329 M->_impd->read_sect (i);
330 M->_rval = 0;
331 delete[] d;
332 }
333
334
335 SNDFILE *Calcthr::exp_open_multi (Impdata *D, const char *name, U32 type, U32 form, S32 sect, U32 chan)
336 {
337 SF_INFO I;
338 SNDFILE *S;
339 const char *fext;
340 char path [1024];
341
342 fext = (type == EXP_TYPE_AMBIS) ? "amb" : "wav";
343 if (sect < 0) snprintf (path, 1024, "%s.%s", name, fext);
344 else snprintf (path, 1024, "%s-%03d.%s", name, sect + 1, fext);
345
346 I.samplerate = (int)(D->drate ());
347 I.channels = chan;
348 I.sections = 1;
349 I.format = SF_FORMAT_WAVEX;
350 switch (form)
351 {
352 case EXP_FORM_16B:
353 I.format |= SF_FORMAT_PCM_16;
354 break;
355 case EXP_FORM_24B:
356 I.format |= SF_FORMAT_PCM_24;
357 break;
358 default:
359 I.format |= SF_FORMAT_FLOAT;
360 }
361
362 S = sf_open (path, SFM_WRITE, &I);
363 if (!S)
364 {
365 fprintf (stderr, "Export: can't create '%s'.\n", path);
366 return 0;
367 }
368 if (type == EXP_TYPE_AMBIS)
369 {
370 // set the Ambisonics flag;
371 }
372
373 return S;
374 }
375
376
377 SNDFILE *Calcthr::exp_open_single (Impdata *D, const char *name, U32 type, U32 form, S32 sect, U32 chan)
378 {
379 SF_INFO I;
380 SNDFILE *S;
381 const char *fext, *suff;
382 char path [1024];
383
384 suff = Impdata::channame (D->type (), chan);
385 fext = (type == EXP_TYPE_RAW) ? "pcm" : "wav";
386 if (suff && *suff)
387 {
388 if (sect < 0) snprintf (path, 1024, "%s-%s.%s", name, suff, fext);
389 else snprintf (path, 1024, "%s-%03d-%s.%s", name, sect + 1, suff, fext);
390 }
391 else
392 {
393 if (sect < 0) snprintf (path, 1024, "%s.%s", name, fext);
394 else snprintf (path, 1024, "%s-%03d.%s", name, sect + 1, fext);
395 }
396
397 I.samplerate = (int)(D->drate ());
398 I.channels = 1;
399 I.sections = 1;
400 if (type == EXP_TYPE_RAW)
401 {
402 I.format = SF_FORMAT_RAW;
403 fext = ".pcm";
404 }
405 else
406 {
407 I.format = SF_FORMAT_WAV;
408 fext = ".wav";
409 }
410 switch (form)
411 {
412 case EXP_FORM_16B:
413 I.format |= SF_FORMAT_PCM_16;
414 break;
415 case EXP_FORM_24B:
416 I.format |= SF_FORMAT_PCM_24;
417 break;
418 default:
419 I.format |= SF_FORMAT_FLOAT;
420 }
421
422 S = sf_open (path, SFM_WRITE, &I);
423 if (!S)
424 {
425 fprintf (stderr, "Export: can't create '%s'.\n", path);
426 return 0;
427 }
428
429 return S;
430 }
431
432
433 int Calcthr::exp_writesect (SNDFILE *S, Impdata *D, U32 mask, U32 chan, float *data)
434 {
435 U32 i, j, k, n;
436 float *p, *q;
437
438 if (data)
439 {
440 for (j = k = 0; mask; j++, mask >>= 1)
441 {
442 if (mask & 1)
443 {
444 p = D->data (j);
445 q = data + k++;
446 for (i = 0; i < D->n_fram (); i++)
447 {
448 *q = *p;
449 p += D->n_chan ();
450 q += chan;
451 }
452 }
453 }
454 n = sf_writef_float (S, data, D->n_fram ());
455 }
456 else
457 {
458 n = sf_writef_float (S, D->data (0), D->n_fram ());
459 }
460
461 send_event (EV_PROG, 1);
462 return (n == D->n_fram ()) ? 0 : 1;
463 }
464
465