~ubuntu-branches/ubuntu/vivid/aeolus/vivid

« back to all changes in this revision

Viewing changes to source/tiface.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-04-19 19:12:51 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100419191251-hgarjfcdfl7c0ryl
Tags: 0.8.4-3
debian/patches/01-makefile.patch: Drop -march=native flag, it isn't valid
for Debian packages as the results are unpredictable, thanks to
Bastian Blank for reporting this (Closes: #578278).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2003-2008 Fons Adriaensen <fons@kokkinizita.net>
 
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
 
 
21
#include <stdlib.h>
 
22
#include <stdio.h>
 
23
#include <ctype.h>
 
24
#include <readline/readline.h>
 
25
#include <readline/history.h>
 
26
#include "tiface.h"
 
27
 
 
28
 
 
29
 
 
30
extern "C" Iface *create_iface (int ac, char *av [])
 
31
{
 
32
    return new Tiface (ac, av);
 
33
}
 
34
 
 
35
 
 
36
 
 
37
 
 
38
Reader::Reader (Edest *edest, int ipind) :
 
39
    H_thread (edest, ipind)
 
40
{
 
41
}
 
42
 
 
43
 
 
44
Reader::~Reader (void)
 
45
{
 
46
}
 
47
 
 
48
 
 
49
void Reader::read (void)
 
50
{
 
51
    put_event (0, new M_ifc_txtip);
 
52
}
 
53
 
 
54
 
 
55
void Reader::thr_main (void)
 
56
{
 
57
    M_ifc_txtip  *M;
 
58
 
 
59
    using_history ();
 
60
    while (1)
 
61
    {
 
62
        get_event (1);
 
63
        M = (M_ifc_txtip *) get_message ();
 
64
        M->_line = readline ("Aeolus> ");
 
65
        if (M->_line) add_history (M->_line);
 
66
        reply  (M);
 
67
    }
 
68
}
 
69
 
 
70
 
 
71
 
 
72
 
 
73
Tiface::Tiface (int ac, char *av []) :
 
74
    _reader (this, FM_TXTIP),
 
75
    _stop (false),
 
76
    _init (true),
 
77
    _initdata (0),
 
78
    _mididata (0)
 
79
{
 
80
    int i;
 
81
 
 
82
    for (i = 0; i < NGROUP; i++) _ifelms [i] = 0;
 
83
}
 
84
 
 
85
 
 
86
Tiface::~Tiface (void)
 
87
{
 
88
}
 
89
 
 
90
 
 
91
void Tiface::stop (void)
 
92
{
 
93
}
 
94
 
 
95
 
 
96
void Tiface::thr_main (void)
 
97
{
 
98
    set_time (0);
 
99
    inc_time (125000);
 
100
 
 
101
    while (! _stop)
 
102
    {
 
103
        switch (get_event ())
 
104
        {
 
105
        case FM_MODEL:
 
106
        case FM_TXTIP:
 
107
            handle_mesg (get_message ()); 
 
108
            break;
 
109
 
 
110
        case EV_EXIT:
 
111
            return;
 
112
        }
 
113
    }
 
114
    send_event (EV_EXIT, 1);
 
115
}
 
116
 
 
117
 
 
118
void Tiface::handle_mesg (ITC_mesg *M)
 
119
{
 
120
    switch (M->type ())
 
121
    {
 
122
    case MT_IFC_INIT:
 
123
        handle_ifc_init ((M_ifc_init *) M);
 
124
        M = 0;
 
125
        break;
 
126
 
 
127
    case MT_IFC_READY:
 
128
        handle_ifc_ready ();
 
129
        break;
 
130
 
 
131
    case MT_IFC_ELCLR:
 
132
        handle_ifc_elclr ((M_ifc_ifelm *) M);
 
133
        break;
 
134
 
 
135
    case MT_IFC_ELSET:
 
136
        handle_ifc_elset ((M_ifc_ifelm *) M);
 
137
        break;
 
138
 
 
139
    case MT_IFC_ELATT:
 
140
        handle_ifc_elatt ((M_ifc_ifelm *) M);
 
141
        break;
 
142
 
 
143
    case MT_IFC_GRCLR:
 
144
        handle_ifc_grclr ((M_ifc_ifelm *) M);
 
145
        break;
 
146
 
 
147
    case MT_IFC_AUPAR:
 
148
        break;
 
149
 
 
150
    case MT_IFC_DIPAR:
 
151
        break;
 
152
 
 
153
    case MT_IFC_RETUNE:
 
154
        handle_ifc_retune ((M_ifc_retune *) M);
 
155
        break;
 
156
 
 
157
    case MT_IFC_MCSET:
 
158
        handle_ifc_mcset ((M_ifc_chconf *) M);
 
159
        M = 0;
 
160
        break;
 
161
 
 
162
    case MT_IFC_TXTIP:
 
163
        handle_ifc_txtip ((M_ifc_txtip *) M);
 
164
        break;
 
165
 
 
166
    case MT_IFC_PRRCL:
 
167
        break;
 
168
 
 
169
    default:
 
170
        printf ("Received message of unknown type %5ld\n", M->type ());
 
171
    }
 
172
    if (M) M->recover ();
 
173
}
 
174
 
 
175
 
 
176
void Tiface::handle_ifc_ready (void)
 
177
{
 
178
    if (_init)
 
179
    {
 
180
        printf ("Aeolus is ready.\n");
 
181
        print_info ();
 
182
        _reader.thr_start (SCHED_OTHER, 0, 0x10000);
 
183
        _reader.read ();
 
184
    }
 
185
    _init = false;
 
186
}
 
187
 
 
188
 
 
189
void Tiface::handle_ifc_init (M_ifc_init *M)
 
190
{
 
191
    if (_initdata) _initdata ->recover ();
 
192
    _initdata = M;
 
193
}
 
194
 
 
195
 
 
196
void Tiface::handle_ifc_mcset (M_ifc_chconf *M)
 
197
{
 
198
    if (_mididata) _mididata ->recover ();
 
199
    _mididata = M;
 
200
    if (!_init) print_midimap ();
 
201
}
 
202
 
 
203
 
 
204
void Tiface::handle_ifc_retune (M_ifc_retune *M)
 
205
{
 
206
    printf ("Retuning Aeolus, A = %3.1lf Hz, %s (%s)\n",
 
207
            M->_freq,  
 
208
            _initdata->_temped [M->_temp]._label, 
 
209
            _initdata->_temped [M->_temp]._mnemo); 
 
210
}
 
211
 
 
212
 
 
213
void Tiface::handle_ifc_grclr (M_ifc_ifelm *M)
 
214
{
 
215
    _ifelms [M->_group] = 0;
 
216
}
 
217
 
 
218
 
 
219
void Tiface::handle_ifc_elclr (M_ifc_ifelm *M)
 
220
{
 
221
    _ifelms [M->_group] &= ~(1 << M->_ifelm);
 
222
}
 
223
 
 
224
 
 
225
void Tiface::handle_ifc_elset (M_ifc_ifelm *M)
 
226
{
 
227
    _ifelms [M->_group] |= (1 << M->_ifelm);
 
228
}
 
229
 
 
230
 
 
231
void Tiface::handle_ifc_elatt (M_ifc_ifelm *M)
 
232
{
 
233
    rewrite_label (_initdata->_groupd [M->_group]._ifelmd [M->_ifelm]._label);
 
234
    printf ("Retuning %7s %-1s (%s)\n",
 
235
            _initdata->_groupd [M->_group]._label,
 
236
            _tempstr,
 
237
            _initdata->_groupd [M->_group]._ifelmd [M->_ifelm]._mnemo);
 
238
}
 
239
 
 
240
 
 
241
void Tiface::handle_ifc_txtip (M_ifc_txtip *M)
 
242
{
 
243
    if (M->_line == 0)
 
244
    {
 
245
        send_event (EV_EXIT, 1);
 
246
        return;
 
247
    }
 
248
    parse_command (M->_line);
 
249
    _reader.read ();
 
250
}
 
251
 
 
252
 
 
253
void Tiface::print_info (void)
 
254
{
 
255
    printf ("Application id:  %s\n", _initdata->_appid);
 
256
    printf ("Stops directory: %s\n", _initdata->_stops);
 
257
    printf ("Instrument:      %s\n", _initdata->_instr);
 
258
    printf ("ALSA Midi port:  %d:%d\n", _initdata->_client, _initdata->_ipport);
 
259
    print_keybdd ();
 
260
    print_divisd ();
 
261
    print_midimap ();
 
262
}
 
263
 
 
264
 
 
265
void Tiface::print_keybdd (void)
 
266
{
 
267
    int i, b, k, n;
 
268
 
 
269
    printf ("Keyboards:\n");
 
270
    for (k = 0; k < NKEYBD; k++)
 
271
    {
 
272
        n = 0;
 
273
        if (_initdata->_keybdd [k]._label [0])
 
274
        {
 
275
            printf (" %-7s  midi", _initdata->_keybdd [k]._label);
 
276
            for (i = 0; i < 16; i++)
 
277
            {
 
278
                b = _mididata->_bits [i];
 
279
                if ((b & 0x1000) && ((b & 7) == k))
 
280
                {
 
281
                    printf (" %2d", i + 1);
 
282
                    n++;
 
283
                }
 
284
            }
 
285
            if (!n) printf ("  -");
 
286
            printf ("\n");
 
287
        }
 
288
    }
 
289
}
 
290
 
 
291
 
 
292
void Tiface::print_divisd (void)
 
293
{
 
294
    int i, b, k, n;
 
295
 
 
296
    printf ("Divisions:\n");
 
297
    for (k = 0; k < NDIVIS; k++)
 
298
    {
 
299
        n = 0;
 
300
        if (_initdata->_divisd [k]._label [0])
 
301
        {
 
302
            printf (" %-7s  midi", _initdata->_divisd [k]._label);
 
303
            for (i = 0; i < 16; i++)
 
304
            {
 
305
                b = _mididata->_bits [i];
 
306
                if ((b & 0x2000) && (((b >> 8) & 7) == k))
 
307
                {
 
308
                    printf (" %2d", i + 1);
 
309
                    n++;
 
310
                }
 
311
            }
 
312
            if (!n) printf ("  -");
 
313
            printf ("\n");
 
314
        }
 
315
    }
 
316
}
 
317
 
 
318
 
 
319
void Tiface::print_midimap (void)
 
320
{
 
321
    int c, d, f, k, n;
 
322
 
 
323
    printf ("Midi routing:\n");
 
324
    n = 0;
 
325
    for (c = 0; c < 16; c++)
 
326
    {
 
327
        f = _mididata->_bits [c];
 
328
        k = f & 7;
 
329
        f >>= 8;
 
330
        d = f & 7;
 
331
        f >>= 4;
 
332
        if (f)
 
333
        {
 
334
            printf (" %2d  ", c + 1);
 
335
            if (f & 1) printf ("keybd %-7s", _initdata->_keybdd [k]._label);
 
336
            if (f & 2) printf ("divis %-7s", _initdata->_divisd [k]._label);
 
337
            if (f & 4) printf ("instr");
 
338
            printf ("\n");
 
339
            n++;
 
340
        }
 
341
    }
 
342
    if (n == 0) printf (" No channels are assigned.\n");
 
343
}
 
344
 
 
345
 
 
346
void Tiface::print_stops_short (int group)
 
347
{
 
348
    int       i, n;
 
349
    uint32_t  m;
 
350
 
 
351
    rewrite_label (_initdata->_groupd [group]._label);
 
352
    printf ("Stops in group %s\n", _tempstr);
 
353
    m = _ifelms [group];
 
354
    n = _initdata->_groupd [group]._nifelm;
 
355
    for (i = 0; i < n; i++)
 
356
    {
 
357
        printf ("  %c %-8s", (m & 1) ? '+' : '-',
 
358
                 _initdata->_groupd [group]._ifelmd [i]._mnemo); 
 
359
        if ((i % 5) == 4) printf ("\n");
 
360
        m >>= 1;
 
361
    }
 
362
    if (n % 5) printf ("\n");
 
363
}
 
364
 
 
365
 
 
366
void Tiface::print_stops_long (int group)
 
367
{
 
368
    int       i, n;
 
369
    uint32_t  m;
 
370
 
 
371
    rewrite_label (_initdata->_groupd [group]._label);
 
372
    printf ("Stops in group %s\n", _tempstr);
 
373
    m = _ifelms [group];
 
374
    n = _initdata->_groupd [group]._nifelm;
 
375
    for (i = 0; i < n; i++)
 
376
    {
 
377
        rewrite_label (_initdata->_groupd [group]._ifelmd [i]._label);
 
378
        printf ("  %c %-7s %-1s\n", (m & 1) ? '+' : '-', 
 
379
                _initdata->_groupd [group]._ifelmd [i]._mnemo, _tempstr);
 
380
        m >>= 1;
 
381
    }
 
382
}              
 
383
 
 
384
 
 
385
void Tiface::rewrite_label (const char *p)
 
386
{
 
387
    char *t;
 
388
 
 
389
    strcpy (_tempstr, p);
 
390
    t = strstr (_tempstr, "-$");
 
391
    if (t) strcpy (t, t + 2);
 
392
    else
 
393
    {
 
394
        t = strchr (_tempstr, '$');
 
395
        if (t) *t = ' ';
 
396
    }
 
397
}
 
398
 
 
399
 
 
400
void Tiface::parse_command (const char *p)
 
401
{
 
402
    int c1, c2;
 
403
 
 
404
    while (isspace (*p)) p++;
 
405
    c1 = *p++;
 
406
    if (c1 == 0) return;
 
407
    c2 = *p++;
 
408
    if (c2 && !isspace (c2)) 
 
409
    {
 
410
        printf ("Bad command\n");
 
411
        return;
 
412
    }
 
413
    if (c1 == 0) return;
 
414
    switch (c1)
 
415
    {
 
416
    case 'S':
 
417
    case 's':
 
418
        command_s (p);
 
419
        break;
 
420
 
 
421
    case 'Q':
 
422
    case 'q':
 
423
        fclose (stdin);
 
424
        break;
 
425
 
 
426
    case '!':
 
427
        send_event (TO_MODEL, new ITC_mesg (MT_IFC_SAVE));
 
428
        break;
 
429
 
 
430
    default:
 
431
        printf ("Unknown command '%c'\n", c1); 
 
432
    }
 
433
}
 
434
 
 
435
 
 
436
void Tiface::command_s (const char *p)
 
437
{
 
438
    int  g, i, k, n;
 
439
    char s [64];
 
440
 
 
441
    if (   (sscanf (p, "%s%n", s, &n) != 1)
 
442
        || ((g = find_group (s)) < 0))
 
443
    {
 
444
        printf ("Expected a group name, ? or ??\n");
 
445
        return;
 
446
    }
 
447
    p += n;
 
448
    if (g == NGROUP + 1)
 
449
    {
 
450
        for (i = 0; i < _initdata->_ngroup; i++) print_stops_short (i);
 
451
        return;
 
452
    }
 
453
    if (g == NGROUP + 2)
 
454
    {
 
455
        for (i = 0; i < _initdata->_ngroup; i++) print_stops_long (i);
 
456
        return;
 
457
    }
 
458
    if (   (sscanf (p, "%s%n", s, &n) != 1)
 
459
        || ((k = comm1 (s)) < 0))
 
460
    {
 
461
        printf ("Expected one of ? ?? + - =\n");
 
462
        return;
 
463
    }
 
464
    p += n;
 
465
    if (k == 0)
 
466
    {
 
467
        print_stops_short (g);
 
468
        return;
 
469
    }
 
470
    if (k == 1)
 
471
    {
 
472
        print_stops_long (g);
 
473
        return;
 
474
    }
 
475
    if (k == 4)
 
476
    {
 
477
        send_event (TO_MODEL, new M_ifc_ifelm (MT_IFC_GRCLR, g, 0));
 
478
        k = 2;
 
479
    }
 
480
    if (k == 2) k = MT_IFC_ELSET;
 
481
    else        k = MT_IFC_ELCLR;
 
482
    while (sscanf (p, "%s%n", s, &n) == 1)
 
483
    {
 
484
        i = find_ifelm (s, g);
 
485
        if (i < 0) printf ("No stop '%s' in this group\n", s);
 
486
        else send_event (TO_MODEL, new M_ifc_ifelm (k, g, i));
 
487
        p += n;
 
488
    }
 
489
}
 
490
 
 
491
 
 
492
int Tiface::find_group (const char *p)
 
493
{
 
494
    int g;
 
495
 
 
496
    if (! strcmp (p, "?"))  return NGROUP + 1;
 
497
    if (! strcmp (p, "??")) return NGROUP + 2;
 
498
    for (g = 0; g < _initdata->_ngroup; g++)
 
499
    {
 
500
        if (! strcmp (p, _initdata->_groupd [g]._label)) return g;
 
501
    }
 
502
    return -1;
 
503
}
 
504
 
 
505
 
 
506
int Tiface::find_ifelm (const char *p, int g)
 
507
{
 
508
    int i, n;
 
509
 
 
510
    n = _initdata->_groupd [g]._nifelm;
 
511
    for (i = 0; i < n; i++)
 
512
    {
 
513
        if (! strcmp (p, _initdata->_groupd [g]._ifelmd [i]._mnemo)) return i;
 
514
    }
 
515
    return -1;
 
516
}
 
517
 
 
518
 
 
519
int Tiface::comm1 (const char *p)
 
520
{
 
521
    if (! strcmp (p, "?"))  return 0;
 
522
    if (! strcmp (p, "??")) return 1;
 
523
    if (! strcmp (p, "+"))  return 2;
 
524
    if (! strcmp (p, "-"))  return 3;
 
525
    if (! strcmp (p, "="))  return 4;
 
526
    return -1;
 
527
}