~ubuntu-branches/ubuntu/quantal/tetraproc/quantal

« back to all changes in this revision

Viewing changes to source/jclient.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-03-24 14:56:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110324145648-r9qi83fs3wej4801
Tags: upstream-0.8.2
ImportĀ upstreamĀ versionĀ 0.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2006-2010 Fons Adriaensen <fons@linuxaudio.org>
 
3
    
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
*/
 
18
 
 
19
 
 
20
#include <string.h>
 
21
#include <sndfile.h>
 
22
#include "jclient.h"
 
23
 
 
24
 
 
25
//-------------------------------------------------------------------------------------------------------
 
26
 
 
27
 
 
28
Jclient::Jclient (const char *jname) :
 
29
    A_thread ("Jclient"),
 
30
    _jack_client (0),
 
31
    _jname (jname),
 
32
    _state (S_IDLE)
 
33
{
 
34
    init_jack ();   
 
35
    init_proc ();
 
36
}
 
37
 
 
38
 
 
39
Jclient::~Jclient (void)
 
40
{
 
41
    if (_jack_client) close_jack ();
 
42
}
 
43
 
 
44
 
 
45
void Jclient::init_jack (void)
 
46
{
 
47
    int            i;
 
48
    jack_status_t  s;
 
49
 
 
50
    if ((_jack_client = jack_client_open (_jname, (jack_options_t) 0, &s)) == 0)
 
51
    {
 
52
        fprintf (stderr, "Can't connect to JACK\n");
 
53
        exit (1);
 
54
    }
 
55
 
 
56
    jack_set_process_callback (_jack_client, jack_static_process, (void *) this);
 
57
    jack_on_shutdown (_jack_client, jack_static_shutdown, (void *) this);
 
58
 
 
59
    for (i = 0; i <  8; i++)
 
60
    {
 
61
        _jack_ipports [i] = jack_port_register (_jack_client, ipp_names [i], JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
 
62
    }
 
63
    for (i = 0; i < 10; i++)
 
64
    {
 
65
        _jack_opports [i] = jack_port_register (_jack_client, opp_names [i], JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
 
66
    }
 
67
 
 
68
    if (jack_activate (_jack_client))
 
69
    {
 
70
        fprintf(stderr, "Can't activate JACK.\n");
 
71
        exit (1);
 
72
    }
 
73
 
 
74
    _jname = jack_get_client_name (_jack_client);
 
75
    _fsamp = jack_get_sample_rate (_jack_client);
 
76
    _fsize = jack_get_buffer_size (_jack_client);
 
77
    _psize = _fsize;
 
78
 
 
79
}
 
80
 
 
81
 
 
82
void Jclient::close_jack ()
 
83
{
 
84
    jack_deactivate (_jack_client);
 
85
    jack_client_close (_jack_client);
 
86
}
 
87
 
 
88
 
 
89
void Jclient::jack_static_shutdown (void *arg)
 
90
{
 
91
    return ((Jclient *) arg)->jack_shutdown ();
 
92
}
 
93
 
 
94
 
 
95
void Jclient::jack_shutdown (void)
 
96
{
 
97
    send_event (EV_EXIT, 1);
 
98
}
 
99
 
 
100
 
 
101
int Jclient::jack_static_process (jack_nframes_t nframes, void *arg)
 
102
{
 
103
    return ((Jclient *) arg)->jack_process (nframes);
 
104
}
 
105
 
 
106
 
 
107
void Jclient::init_proc ()
 
108
{
 
109
    _meter = METER_INP;
 
110
    _monit = MONIT_REC;
 
111
    _vgain = 0.0f;
 
112
    _voldb0 = -100.0f;
 
113
    _voldb1 = -100.0f;
 
114
    _dvoldb = 200.0f * _fsize / _fsamp;
 
115
    _abproc.init (_fsamp, _fsize, _psize);
 
116
    _virtmic.set_fsam (_fsamp);
 
117
    Kmeterdsp::init (_fsamp, _fsize, 0.5f, 15.0f);
 
118
}
 
119
 
 
120
 
 
121
int Jclient::jack_process (jack_nframes_t nframes)
 
122
{
 
123
    unsigned int  e, j;
 
124
    float         *p [4], *q [4];
 
125
 
 
126
    switch (e = get_event_nowait ())
 
127
    {
 
128
    case EV_GO_IDLE:
 
129
        _state = S_IDLE;
 
130
        send_event (e, 1);
 
131
        break;
 
132
    case EV_GO_PROC:
 
133
        _state = S_PROC;
 
134
        send_event (e, 1);
 
135
        break;
 
136
    }
 
137
 
 
138
    if (nframes != _fsize) return 0;
 
139
 
 
140
    for (j = 0; j < 4; j++)
 
141
    {
 
142
        p [j] = (float *) jack_port_get_buffer (_jack_ipports [j], nframes);
 
143
        q [j] = (float *) jack_port_get_buffer (_jack_opports [j], nframes);
 
144
    }
 
145
 
 
146
    if (_state == S_IDLE || _abproc.cerror ())
 
147
    {
 
148
        for (j = 0; j < 4; j++)
 
149
        {
 
150
            if (_meter == METER_INP) _kmdsp [j].reset ();
 
151
            memset (q [j], 0, nframes * sizeof (float));
 
152
        }
 
153
    }
 
154
    else
 
155
    {
 
156
        if (_meter == METER_INP) measure (nframes, p);
 
157
        _abproc.process (nframes, p, q);
 
158
    }
 
159
    monitor (nframes, q);
 
160
 
 
161
    return 0;
 
162
}
 
163
 
 
164
 
 
165
void Jclient::monitor (unsigned int n, float *p [4])
 
166
{
 
167
    unsigned int  i, j;
 
168
    float         g, d, t, *q [4];
 
169
 
 
170
    // Select monitor source.
 
171
    for (j = 0; j < 4; j++) 
 
172
    {
 
173
        if (_monit == MONIT_EXT) p [j] = (float *) jack_port_get_buffer (_jack_ipports [4 + j], n);
 
174
        q [j] = (float *) jack_port_get_buffer (_jack_opports [4 + j], n);
 
175
    }
 
176
 
 
177
    // Update level meters.
 
178
    if (_meter == METER_MON) measure (n, p);
 
179
 
 
180
    // Check volume change.
 
181
    g = _vgain;
 
182
    d = _voldb1 - _voldb0;
 
183
    if (fabsf (d) > 0.1f)
 
184
    {
 
185
        if      (d >  _dvoldb) d =  _dvoldb;
 
186
        else if (d < -_dvoldb) d = -_dvoldb; 
 
187
        _voldb0 += d;
 
188
        t = (_voldb0 < -80.0f) ? 0 : powf (10.0f, 0.05f * _voldb0);
 
189
        d = (t - g) / _fsize + 1e-30f;
 
190
        _vgain = t;
 
191
    }   
 
192
    else d = 0;
 
193
   
 
194
    // Apply volume.
 
195
    for (i = 0; i < n; i++)
 
196
    {
 
197
        g += d;
 
198
        q [0][i] = p [0][i] * g;
 
199
        q [1][i] = p [1][i] * g;
 
200
        q [2][i] = p [2][i] * g;
 
201
        q [3][i] = p [3][i] * g;
 
202
    }
 
203
 
 
204
    // Create stereo output.
 
205
    _virtmic.process (n, q [0], q [1], q [2], q [3],
 
206
                      (float *) jack_port_get_buffer (_jack_opports [8], n),
 
207
                      (float *) jack_port_get_buffer (_jack_opports [9], n));
 
208
}
 
209
 
 
210
 
 
211
void Jclient::measure (unsigned int n, float *p [4])
 
212
{
 
213
    int i;
 
214
 
 
215
    for (i = 0; i < 4; i++)
 
216
    {
 
217
        _kmdsp [i].process (p [i], n);
 
218
    }
 
219
}
 
220
 
 
221
 
 
222
//-------------------------------------------------------------------------------------------------------
 
223
 
 
224
 
 
225
const char *Jclient::ipp_names [8] =
 
226
{
 
227
    "A-form.1(LF)",
 
228
    "A-form.2(RF)",
 
229
    "A-form.3(LB)",
 
230
    "A-form.4(RB)",
 
231
    "Mon-in.W",
 
232
    "Mon-in.X",
 
233
    "Mon-in.Y",
 
234
    "Mon-in.Z"
 
235
};
 
236
 
 
237
 
 
238
const char *Jclient::opp_names [10] =
 
239
{
 
240
    "B-form.W",
 
241
    "B-form.X",
 
242
    "B-form.Y",
 
243
    "B-form.Z",
 
244
    "Mon-out.W",
 
245
    "Mon-out.X",
 
246
    "Mon-out.Y",
 
247
    "Mon-out.Z",
 
248
    "Stereo.L",
 
249
    "Stereo.R"
 
250
};
 
251
 
 
252
 
 
253
//-------------------------------------------------------------------------------------------------------