Debian Codegraph @

source/mainthr.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 <unistd.h>
24 #include <string.h>
25 #include <sndfile.h>
26 #include <math.h>
27 #include "mainthr.h"
28
29
30 Mainthr::Mainthr (Shdata *shdata) :
31 A_thread ("main"),
32 _state (Shdata::X_INIT),
33 _count (0),
34 _shdata (shdata)
35 {
36 _shdata->_mode = Shdata::M_MEAS;
37 _shdata->_opsel = 0;
38 _shdata->_opdat._stat = Shdata::S_NONE;
39 _shdata->_opdat._queue = 0;
40 for (int i = 0; i < 8; i++)
41 {
42 _shdata->_ipdat [i]._stat = Shdata::S_NONE;
43 _shdata->_ipdat [i]._type = 0;
44 _shdata->_ipdat [i]._chan0 = 0;
45 _shdata->_ipdat [i]._queue = 0;
46 }
47 }
48
49
50 Mainthr::~Mainthr (void)
51 {
52 }
53
54
55 void Mainthr::stop (void)
56 {
57 _state = Shdata::X_EXIT;
58 }
59
60
61 void Mainthr::thr_main (void)
62 {
63 int c, r;
64
65 set_time (0);
66 inc_time (100000);
67 while (1)
68 {
69 get_event_timed ();
70 inc_time (50000);
71 c = _shdata->_par2cld.eget ();
72 do
73 {
74 if (c == Shdata::X_EXIT)
75 {
76 _shdata->_mode = 0;
77 stopcapt (true);
78 _shdata->_cld2par.eput (_state = Shdata::X_EXIT);
79 return;
80 }
81 else if (c == Shdata::X_TICK) _count = 0;
82 else if (++_count == 40) return;
83
84 switch (_state)
85 {
86 case Shdata::X_INIT:
87 if (c == Shdata::X_STOP)
88 {
89 _shdata->_cld2par.eput (Shdata::X_STOP);
90 _shdata->_mode = Shdata::M_MEAS;
91 _state = Shdata::X_STOP;
92 }
93 break;
94
95 case Shdata::X_STOP:
96 if (c == Shdata::X_TEST)
97 {
98 if (inittest ())
99 {
100 _shdata->_mode |= Shdata::M_PLAY;
101 _shdata->_cld2par.eput (_state = Shdata::X_TEST);
102 }
103 }
104 else if (c == Shdata::X_CAPT)
105 {
106 if (initcapt ())
107 {
108 _shdata->_mode |= Shdata::M_PLAY | Shdata::M_CAPT;
109 _shdata->_cld2par.eput (_state = Shdata::X_CAPT);
110 }
111 }
112 break;
113
114 case Shdata::X_TEST:
115 if (c == Shdata::X_STOP)
116 {
117 stopplay ();
118 _shdata->_mode = Shdata::M_MEAS;
119 _shdata->_cld2par.eput (_state = Shdata::X_STOP);
120 }
121 break;
122
123 case Shdata::X_CAPT:
124 if (c == Shdata::X_STOP)
125 {
126 _shdata->_mode = Shdata::M_MEAS;
127 stopcapt (true);
128 _shdata->_cld2par.eput (_state = Shdata::X_STOP);
129 }
130 else
131 {
132 r = iohandle ();
133 if (r & TRIG) _shdata->_cld2par.eput (Shdata::X_TRIG);
134 if (r & DONE)
135 {
136 _shdata->_mode = Shdata::M_MEAS;
137 stopcapt (false);
138 _shdata->_cld2par.eput (_state = Shdata::X_STOP);
139 }
140 }
141 break;
142 }
143
144 c = _shdata->_par2cld.eget ();
145 }
146 while (c);
147 }
148 }
149
150
151 int Mainthr::inittest (void)
152 {
153 int i, n;
154 float *p, w, m, g;
155
156 n = 2 * _shdata->_fsamp;
157 n -= n % _shdata->_fsize;
158 g = _shdata->_level;
159 _shdata->_opdat._queue = new Fltlfq (n, 1);
160 p = _shdata->_opdat._queue->wr_ptr ();
161 w = 2 * M_PI * 880.0 / n;
162 m = 2 * M_PI * 15.0 / n;
163 for (i = 0; i < n; i++)
164 {
165 *p++ = g * sinf (i * w + 20.0f * sinf (i * m));
166 }
167 return 1;
168 }
169
170
171 int Mainthr::initcapt (void)
172 {
173 int i, n;
174 char s [1024];
175 Ipdata *D;
176
177 _shdata->_error [0] = 0;
178 n = 2 * _shdata->_fsamp;
179 n -= n % _shdata->_fsize;
180
181 if (_opfile.open_read (_shdata->_opdat._name))
182 {
183 _shdata->_opdat._stat = Shdata::S_ERROR;
184 strcpy (_shdata->_error, "Can't open sweep file");
185 return 0;
186 }
187 if (_opfile.type () != Impdata::TYPE_SWEEP)
188 {
189 _opfile.close ();
190 _shdata->_opdat._stat = Shdata::S_ERROR;
191 strcpy (_shdata->_error, "Sweep file has wrong type");
192 return 0;
193 }
194 _shdata->_opdat._stat = Shdata::S_OPEN;
195 _shdata->_opdat._queue = new Fltlfq (n, 1);
196 if (_shdata->_oplen > (int)(_opfile.n_fram ())) _shdata->_oplen = _opfile.n_fram ();
197
198 for (i = 0; i < 8; i++)
199 {
200 D = _shdata->_ipdat + i;
201 if (D->_type)
202 {
203 sprintf (s, "%s/capture/%s", _shdata->_sessdir, D->_name);
204 Impdata::checkext (s);
205 _ipfile [i].set_type (D->_type);
206 _ipfile [i].set_rate (_shdata->_fsamp, 1);
207 _ipfile [i].set_n_sect (_shdata->_niter);
208 _ipfile [i].set_n_fram (_shdata->_iplen);
209 if (_ipfile [i].open_write (s))
210 {
211 D->_stat = Shdata::S_ERROR;
212 sprintf (_shdata->_error, "Can't open capture file '%s'", s);
213 stopcapt (true);
214 return 0;
215 }
216 D->_stat = Shdata::S_OPEN;
217 D->_queue = new Fltlfq (n, D->_type & 255);
218 }
219 }
220
221 _op_it = 0;
222 _op_si = 0;
223 _ip_it = 0;
224 _ip_si = 0;
225 rdopdata (n);
226 return 1;
227 }
228
229
230 int Mainthr::iohandle (void)
231 {
232 int n, r;
233 Fltlfq *Q = _shdata->_opdat._queue;
234
235 n = Q->wr_frames ();
236 r = 0;
237 if (n > Q->size () / 4) return rdopdata (n) | wripdata (n);
238 return 0;
239 }
240
241
242 int Mainthr::rdopdata (int nfrm)
243 {
244 int i, k, n, r;
245 float g;
246 float *p;
247 Fltlfq *Q = _shdata->_opdat._queue;
248
249 g = _shdata->_level;
250 r = 0;
251 while (nfrm)
252 {
253 n = Q->wr_nowrap (nfrm);
254 if (n > 4096) n = 4096;
255 p = Q->wr_ptr ();
256 nfrm -= n;
257 while (n)
258 {
259 if (_op_it < _shdata->_niter)
260 {
261 k = _shdata->_oplen - _op_si;
262 if (k > 0)
263 {
264 if ( k > n) k = n;
265 if (_op_si == 0) _opfile.locate (0);
266 _opfile.read_ext (k, _buff);
267 for (i = 0; i < k; i++) _buff [i] = g * _buff [2 * i];
268 memcpy (p, _buff, k * sizeof (float));
269 }
270 else
271 {
272 k = _shdata->_itlen - _op_si;
273 if (k > n) k = n;
274 memset (p, 0, k * sizeof (float));
275 }
276 _op_si += k;
277 if (_op_si == _shdata->_itlen)
278 {
279 _op_si = 0;
280 _op_it++;
281 }
282 }
283 else
284 {
285 k = n;
286 memset (p, 0, k * sizeof (float));
287 }
288 Q->wr_commit (k);
289 p += k;
290 n -= k;
291 }
292 }
293 return r;
294 }
295
296
297 int Mainthr::wripdata (int nfrm)
298 {
299 int i, k, n, r;
300 Ipdata *D;
301 Fltlfq *Q;
302
303 r = 0;
304 while (nfrm)
305 {
306 k = _shdata->_iplen - _ip_si;
307 if (k > 0)
308 {
309 if (k > nfrm) k = nfrm;
310 for (i = 0, D = _shdata->_ipdat; i < 8; i++, D++)
311 {
312 Q = D->_queue;
313 if (Q)
314 {
315 if (_ip_si == 0) _ipfile [i].locate (_ip_it);
316 n = Q->rd_nowrap (k);
317 _ipfile [i].write_ext (n, Q->rd_ptr ());
318 Q->rd_commit (n);
319 if (n < k)
320 {
321 _ipfile [i].write_ext (k - n, Q->rd_ptr ());
322 Q->rd_commit (k - n);
323 }
324 }
325 }
326 nfrm -= k;
327 _ip_si += k;
328 if (_ip_si == _shdata->_iplen)
329 {
330 r |= TRIG;
331 if (_ip_it == _shdata->_niter - 1) return r | DONE;
332 }
333 }
334 else
335 {
336 k = _shdata->_itlen - _ip_si;
337 if (k > nfrm) k = nfrm;
338 for (i = 0, D = _shdata->_ipdat; i < 8; i++, D++)
339 {
340 Q = D->_queue;
341 if ( Q) Q->rd_commit (k);
342 }
343 nfrm -= k;
344 _ip_si += k;
345 if (_ip_si == _shdata->_itlen)
346 {
347 _ip_si = 0;
348 _ip_it++;
349 }
350 }
351 }
352 return r;
353 }
354
355
356 void Mainthr::stopplay (void)
357 {
358 set_time ();
359 inc_time (100000);
360 get_event_timed ();
361 _opfile.close ();
362 delete _shdata->_opdat._queue;
363 _shdata->_opdat._queue = 0;
364 }
365
366
367 void Mainthr::stopcapt (bool rm)
368 {
369 int i;
370 char s [1024];
371 Ipdata *D;
372
373 stopplay ();
374 for (i = 0; i < 8; i++)
375 {
376 D = _shdata->_ipdat + i;
377 if (D->_stat == Shdata::S_OPEN)
378 {
379 _ipfile [i].close ();
380 D->_stat = Shdata::S_NONE;
381 delete D->_queue;
382 D->_queue = 0;
383 if (rm)
384 {
385 sprintf (s, "%s/capture/%s", _shdata->_sessdir, D->_name);
386 unlink (s);
387 }
388 }
389 }
390 }
391
392