Debian Codegraph @

source/audiothr.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 <math.h>
22 #include "audiothr.h"
23
24
25 Audiothr::Audiothr (Shdata *shdata) :
26 _shdata (shdata),
27 _run_alsa (0),
28 _run_jack (0)
29 {
30 for (int i = 0; i < N_IP; i++) _shdata->_meter [i] = 0.0f;
31 if (_shdata->_state == Shdata::A_JACK) init_jack ();
32 else if (_shdata->_state == Shdata::A_ALSA) init_alsa ();
33 }
34
35
36 Audiothr::~Audiothr (void)
37 {
38 if (_run_alsa) close_alsa ();
39 if (_run_jack) close_jack ();
40 }
41
42
43 void Audiothr::init_alsa (void)
44 {
45 _fsamp = _shdata->_fsamp;
46 _fsize = _shdata->_fsize;
47 _nplay = _shdata->_nplay;
48 _ncapt = _shdata->_ncapt;
49
50 _alsa_handle = new Alsa_pcmi (_shdata->_ident,
51 _shdata->_ident,
52 0,
53 _shdata->_fsamp,
54 _shdata->_fsize,
55 _shdata->_nfrag,
56 0);
57 if (_alsa_handle->state () < 0)
58 {
59 fprintf (stderr, "Can't connect to ALSA\n");
60 _shdata->_state |= Shdata::A_FAIL;
61 _shdata->_cld2par.eput (Shdata::X_EXIT);
62 return;
63 }
64
65 _run_alsa = true;
66 if (_alsa_handle->nplay () < _nplay) _shdata->_nplay = _nplay = _alsa_handle->nplay ();
67 if (_alsa_handle->ncapt () < _ncapt) _shdata->_ncapt = _ncapt = _alsa_handle->ncapt ();
68
69 for (int i = 0; i < _nplay; i++) _op [i] = new float [_fsize];
70 for (int i = 0; i < _ncapt; i++) _ip [i] = new float [_fsize];
71
72 if (_shdata->_class == SCHED_FIFO)
73 {
74 if (thr_start (SCHED_FIFO, _shdata->_prior, 0x00010000))
75 {
76 fprintf (stderr, "Can't create ALSA thread with RT priority\n");
77 _shdata->_state |= Shdata::A_NORT;
78 _shdata->_class = SCHED_OTHER;
79 }
80 }
81 if (_shdata->_class == SCHED_OTHER)
82 {
83 if (thr_start (SCHED_OTHER, 0, 0x00010000))
84 {
85 fprintf (stderr, "Can't create ALSA thread\n");
86 _shdata->_state |= Shdata::A_FAIL;
87 _shdata->_cld2par.eput (Shdata::X_EXIT);
88 return;
89 }
90 }
91
92 init ();
93 }
94
95
96 void Audiothr::close_alsa ()
97 {
98 _run_alsa = false;
99 _alsa_sync.lock ();
100 _alsa_sync.eget ();
101 _alsa_sync.unlock ();
102
103 for (int i = 0; i < _nplay; i++) delete[] _op [i];
104 for (int i = 0; i < _ncapt; i++) delete[] _ip [i];
105 }
106
107
108 void Audiothr::thr_main (void)
109 {
110 unsigned long k;
111
112 _alsa_handle->pcm_start ();
113
114 while (_run_alsa)
115 {
116 k = _alsa_handle->pcm_wait ();
117 while (k >= _fsize)
118 {
119 if (_ncapt)
120 {
121 _alsa_handle->capt_init (_fsize);
122 for (int i = 0; i < _ncapt; i++) _alsa_handle->capt_chan (i, _ip [i], _fsize);
123 _alsa_handle->capt_done (_fsize);
124 }
125 process ();
126 if (_nplay)
127 {
128 _alsa_handle->play_init (_fsize);
129 for (int i = 0; i < _nplay; i++) _alsa_handle->play_chan (i, _op [i], _fsize);
130 _alsa_handle->play_done (_fsize);
131 }
132 k -= _fsize;
133 }
134 }
135
136 _alsa_handle->pcm_stop ();
137 _alsa_sync.lock ();
138 _alsa_sync.eput (0);
139 _alsa_sync.unlock ();
140 }
141
142
143 void Audiothr::init_jack (const char *server)
144 {
145 char s [16];
146 int opts;
147 jack_status_t stat;
148 struct sched_param spar;
149
150 opts = JackNoStartServer;
151 if (server) opts |= JackServerName;
152
153 _jack_handle = jack_client_open (_shdata->_ident, (jack_options_t)opts, &stat, server);
154 if (!_jack_handle)
155 {
156 fprintf (stderr, "Can't connect to JACK\n");
157 _shdata->_state |= Shdata::A_FAIL;
158 _shdata->_cld2par.eput (Shdata::X_EXIT);
159 return;
160 }
161
162 jack_set_process_callback (_jack_handle, jack_static_callback, (void *)this);
163 jack_on_shutdown (_jack_handle, jack_static_shutdown, (void *)this);
164
165 _nplay = _shdata->_nplay;
166 _ncapt = _shdata->_ncapt;
167 for (int i = 0; i < _nplay; i++)
168 {
169 sprintf(s, "out_%d", i + 1);
170 _jack_op [i] = jack_port_register (_jack_handle, s, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
171 }
172 for (int i = 0; i < _ncapt; i++)
173 {
174 sprintf(s, "in_%d", i + 1);
175 _jack_ip [i] = jack_port_register (_jack_handle, s, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
176 }
177
178 _shdata->_fsamp = _fsamp = jack_get_sample_rate (_jack_handle);
179 _shdata->_fsize = _fsize = jack_get_buffer_size (_jack_handle);
180 _run_jack = true;
181
182 if (jack_activate (_jack_handle))
183 {
184 fprintf(stderr, "Can't activate JACK");
185 _shdata->_state |= Shdata::A_FAIL;
186 _shdata->_cld2par.eput (Shdata::X_EXIT);
187 return;
188 }
189
190 pthread_getschedparam (jack_client_thread_id (_jack_handle), &_shdata->_class, &spar);
191 _shdata->_prior = spar.sched_priority - sched_get_priority_max (_shdata->_class);
192
193 init ();
194 }
195
196
197 void Audiothr::close_jack ()
198 {
199 jack_deactivate (_jack_handle);
200 for (int i = 0; i < _nplay; i++) jack_port_unregister(_jack_handle, _jack_op [i]);
201 for (int i = 0; i < _ncapt; i++) jack_port_unregister(_jack_handle, _jack_ip [i]);
202 jack_client_close (_jack_handle);
203 }
204
205
206 void Audiothr::jack_static_shutdown (void *arg)
207 {
208 ((Audiothr *) arg)->jack_shutdown ();
209 }
210
211
212 void Audiothr::jack_shutdown (void)
213 {
214 _shdata->_state |= Shdata::A_KILL;
215 _shdata->_mode = 0;
216 _shdata->_cld2par.eput (Shdata::X_EXIT);
217 }
218
219
220 int Audiothr::jack_static_callback (jack_nframes_t nframes, void *arg)
221 {
222 return ((Audiothr *) arg)->jack_callback (nframes);
223 }
224
225
226
227 int Audiothr::jack_callback (jack_nframes_t nframes)
228 {
229 if (nframes != _fsize)
230 {
231 _shdata->_state |= Shdata::A_SIZE;
232 _shdata->_mode = 0;
233 _shdata->_cld2par.eput (Shdata::X_EXIT);
234 }
235
236 for (int i = 0; i < _ncapt; i++) _ip [i] = (float *)(jack_port_get_buffer (_jack_ip [i], nframes));
237 for (int i = 0; i < _nplay; i++) _op [i] = (float *)(jack_port_get_buffer (_jack_op [i], nframes));
238 process ();
239
240 return 0;
241 }
242
243
244 void Audiothr::init (void)
245 {
246 _decay = powf (10.0f, -0.05f * DECAY * _fsize / _fsamp);
247 _shdata->_cld2par.eput (Shdata::X_INIT);
248 }
249
250
251 void Audiothr::process (void)
252 {
253 unsigned int i, j, k, n;
254 float *p, *q, t, m;
255 Fltlfq *Q;
256 Ipdata *D;
257
258 if (_shdata->_mode & Shdata::M_MEAS)
259 {
260 for (j = 0; (int) j < _ncapt; j++)
261 {
262 p = _ip [j];
263 m = fabsf (*p++);
264 for (i = 1; i < _fsize; i++)
265 {
266 t = fabsf (*p++);
267 if (t > m) m = t;
268 }
269 t = _decay * _shdata->_meter [j] + 1e-10f;
270 if (t > m) m = t;
271 _shdata->_meter [j] = m;
272 }
273 }
274 else
275 {
276 for (j = 0; (int) j < _ncapt; j++) _shdata->_meter [j] = 0.0f;
277 }
278
279 if (_shdata->_mode & Shdata::M_CAPT)
280 {
281 for (k = 0; k < 8; k++)
282 {
283 D = _shdata->_ipdat + k;
284 Q = D->_queue;
285 if (Q)
286 {
287 n = D->_type & 255;
288 for (j = 0; j < n; j++)
289 {
290 p = Q->wr_ptr () + j;
291 q = _ip [D->_chan0 + j];
292 for (i = 0; i < _fsize; i++) p [i * n] = q [i];
293 }
294 Q->wr_commit (_fsize);
295 }
296 }
297 }
298
299 if (_shdata->_mode & Shdata::M_PLAY)
300 {
301 Q = _shdata->_opdat._queue;
302 p = Q->rd_ptr ();
303 k = _shdata->_opsel;
304 for (j = 0; (int) j < _nplay; j++)
305 {
306 if (k & 1) memcpy (_op [j], p, _fsize * sizeof (float));
307 else memset (_op [j], 0, _fsize * sizeof (float));
308 k >>= 1;
309 }
310 Q->rd_commit (_fsize);
311 }
312 else
313 {
314 for (j = 0; (int) j < _nplay; j++) memset (_op [j], 0, _fsize * sizeof (float));
315 }
316 }