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

« back to all changes in this revision

Viewing changes to main.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
 
#include <unistd.h>
21
 
#include <stdlib.h>
22
 
#include <stdio.h>
23
 
#include <ctype.h>
24
 
#include <string.h>
25
 
#include <sys/mman.h>
26
 
#include <signal.h>
27
 
#include <clthreads.h>
28
 
#include <dlfcn.h>
29
 
#include "audio.h"
30
 
#include "imidi.h"
31
 
#include "model.h"
32
 
#include "slave.h"
33
 
#include "iface.h"
34
 
 
35
 
 
36
 
#ifdef __linux__
37
 
static char *options = "htuAJBM:N:S:I:W:d:r:p:n:";
38
 
#else
39
 
static char *options = "htuJBM:N:S:I:W:";
40
 
#endif
41
 
static char  optline [1024];
42
 
static bool  t_opt = false;
43
 
static bool  u_opt = false;
44
 
static bool  A_opt = false;
45
 
static bool  B_opt = false;
46
 
static int   r_val = 48000;
47
 
static int   p_val = 1024;
48
 
static int   n_val = 2;
49
 
static const char *N_val = "aeolus";
50
 
static const char *S_val = "stops";
51
 
static const char *I_val = "Aeolus";
52
 
static const char *W_val = "waves";
53
 
static const char *d_val = "default";
54
 
static Lfq_u32  note_queue (256);
55
 
static Lfq_u32  comm_queue (256);
56
 
static Lfq_u8   midi_queue (1024);
57
 
static Iface   *iface;
58
 
 
59
 
 
60
 
static void help (void)
61
 
{
62
 
    fprintf (stderr, "\nAeolus %s\n\n",VERSION);
63
 
    fprintf (stderr, "  (C) 2003-2008 Fons Adriaensen  <fons@kokkinizita.net>\n");
64
 
    fprintf (stderr, "Options:\n");
65
 
    fprintf (stderr, "  -h                 Display this text\n");
66
 
    fprintf (stderr, "  -t                 Text mode user interface\n");
67
 
    fprintf (stderr, "  -u                 Use presets file in user's home dir\n");
68
 
#ifdef __linux__
69
 
    fprintf (stderr, "  -N <name>          Name to use as JACK and ALSA client [aeolus]\n");   
70
 
#else
71
 
    fprintf (stderr, "  -N <name>          Name to use as JACK and CoreMIDI client [aeolus]\n");   
72
 
#endif
73
 
    fprintf (stderr, "  -S <stops>         Name of stops directory [stops]\n");   
74
 
    fprintf (stderr, "  -I <instr>         Name of instrument directory [Aeolus]\n");   
75
 
    fprintf (stderr, "  -W <waves>         Name of waves directory [waves]\n");   
76
 
    fprintf (stderr, "  -B                 Ambisonics B format output (JACK only)\n");
77
 
    fprintf (stderr, "  -J                 Use JACK (default)\n");
78
 
#ifdef __linux__
79
 
    fprintf (stderr, "  -A                 Use ALSA, with options:\n");
80
 
    fprintf (stderr, "    -d <device>        Alsa device [default]\n");
81
 
    fprintf (stderr, "    -r <rate>          Sample frequency [48000]\n");
82
 
    fprintf (stderr, "    -p <period>        Period size [1024]\n");
83
 
    fprintf (stderr, "    -n <nfrags>        Number of fragments [2]\n\n");
84
 
#endif
85
 
    exit (1);
86
 
}
87
 
 
88
 
 
89
 
static void procoptions (int ac, char *av [], const char *where)
90
 
{
91
 
    int k;
92
 
    
93
 
    optind = 1;
94
 
    opterr = 0;
95
 
    while ((k = getopt (ac, av, options)) != -1)
96
 
    {
97
 
        if (optarg && (*optarg == '-'))
98
 
        {
99
 
            fprintf (stderr, "\n%s\n", where);
100
 
            fprintf (stderr, "  Missing argument for '-%c' option.\n", k); 
101
 
            fprintf (stderr, "  Use '-h' to see all options.\n");
102
 
            exit (1);
103
 
        }
104
 
        switch (k)
105
 
        {
106
 
        case 'h' : help (); exit (0);
107
 
        case 't' : t_opt = true;  break;
108
 
        case 'u' : u_opt = true;  break;
109
 
        case 'A' : A_opt = true;  break;
110
 
        case 'J' : A_opt = false; break;
111
 
        case 'B' : B_opt = true; break;
112
 
        case 'r' : r_val = atoi (optarg); break;
113
 
        case 'p' : p_val = atoi (optarg); break;
114
 
        case 'n' : n_val = atoi (optarg); break;
115
 
        case 'N' : N_val = optarg; break; 
116
 
        case 'S' : S_val = optarg; break; 
117
 
        case 'I' : I_val = optarg; break; 
118
 
        case 'W' : W_val = optarg; break; 
119
 
        case 'd' : d_val = optarg; break; 
120
 
        case '?':
121
 
            fprintf (stderr, "\n%s\n", where);
122
 
            if (optopt != ':' && strchr (options, optopt)) fprintf (stderr, "  Missing argument for '-%c' option.\n", optopt); 
123
 
            else if (isprint (optopt)) fprintf (stderr, "  Unknown option '-%c'.\n", optopt);
124
 
            else fprintf (stderr, "  Unknown option character '0x%02x'.\n", optopt & 255);
125
 
            fprintf (stderr, "  Use '-h' to see all options.\n");
126
 
            exit (1);
127
 
        default:
128
 
            abort ();
129
 
        }
130
 
    }
131
 
}
132
 
 
133
 
 
134
 
static int readconfig (const char *path)
135
 
{
136
 
    FILE *F;
137
 
    char  s [1024];
138
 
    char *p;
139
 
    char *av [30];
140
 
    int   ac;
141
 
 
142
 
    av [0] = 0;
143
 
    ac = 1;
144
 
    if ((F = fopen (path, "r")))
145
 
    {
146
 
        while (fgets (optline, 1024, F) && (ac == 1))
147
 
        {
148
 
            p = strtok (optline, " \t\n");
149
 
            if (*p == '#') continue;
150
 
            while (p && (ac < 30))
151
 
            {
152
 
                av [ac++] = p;
153
 
                p = strtok (0, " \t\n");
154
 
            }
155
 
        }
156
 
        fclose (F);
157
 
        sprintf (s, "In file '%s':", path);
158
 
        procoptions (ac, av, s);
159
 
        return 0;
160
 
    }
161
 
    return 1;
162
 
}
163
 
 
164
 
 
165
 
static void sigint_handler (int)
166
 
{
167
 
    signal (SIGINT, SIG_IGN);
168
 
    iface->stop ();
169
 
}
170
 
 
171
 
 
172
 
int main (int ac, char *av [])
173
 
{
174
 
    ITC_ctrl       itcc;
175
 
    Audio         *audio;
176
 
    Imidi         *imidi;
177
 
    Model         *model;
178
 
    Slave         *slave;
179
 
    void          *so_handle;
180
 
    iface_cr      *so_create;
181
 
    char           s [1024];
182
 
    char          *p;
183
 
    int            n;
184
 
     
185
 
    p = getenv ("HOME");
186
 
    if (p) sprintf (s, "%s/.aeolusrc", p);
187
 
    else strcpy (s, ".aeolusrc");
188
 
    if (readconfig (s)) readconfig ("/etc/aeolus.conf"); 
189
 
    procoptions (ac, av, "On command line:");
190
 
 
191
 
    if (mlockall (MCL_CURRENT | MCL_FUTURE)) fprintf (stderr, "Warning: memory lock failed.\n");
192
 
 
193
 
    if (t_opt) sprintf (s, "%s/aeolus_txt.so", LIBDIR);
194
 
    else       sprintf (s, "%s/aeolus_x11.so", LIBDIR);
195
 
    so_handle = dlopen (s, RTLD_NOW);
196
 
    if (! so_handle)
197
 
    {
198
 
        fprintf (stderr, "Error: can't open user interface plugin: %s.\n", dlerror ());
199
 
        return 1;
200
 
    }
201
 
    so_create = (iface_cr *) dlsym (so_handle, "create_iface");
202
 
    if (! so_create)
203
 
    {
204
 
        fprintf (stderr, "Error: can't create user interface plugin: %s.\n", dlerror ());
205
 
        dlclose (so_handle);
206
 
        return 1;
207
 
    }
208
 
 
209
 
    audio = new Audio (N_val, &note_queue, &comm_queue);
210
 
#ifdef __linux__
211
 
    if (A_opt) audio->init_alsa (d_val, r_val, p_val, n_val);
212
 
    else       audio->init_jack (B_opt, &midi_queue);
213
 
#else
214
 
    audio->init_jack (B_opt, &midi_queue);
215
 
#endif
216
 
    model = new Model (&comm_queue, &midi_queue, audio->midimap (), audio->appname (), S_val, I_val, W_val, u_opt);
217
 
    imidi = new Imidi (&note_queue, &midi_queue, audio->midimap (), audio->appname ());
218
 
    slave = new Slave ();
219
 
    iface = so_create (ac, av);
220
 
 
221
 
    ITC_ctrl::connect (audio, EV_EXIT,  &itcc, EV_EXIT);    
222
 
    ITC_ctrl::connect (audio, EV_QMIDI, model, EV_QMIDI);    
223
 
    ITC_ctrl::connect (audio, TO_MODEL, model, FM_AUDIO);    
224
 
    ITC_ctrl::connect (imidi, EV_EXIT,  &itcc, EV_EXIT);    
225
 
    ITC_ctrl::connect (imidi, EV_QMIDI, model, EV_QMIDI);    
226
 
    ITC_ctrl::connect (imidi, TO_MODEL, model, FM_IMIDI);    
227
 
    ITC_ctrl::connect (model, EV_EXIT,  &itcc, EV_EXIT);    
228
 
    ITC_ctrl::connect (model, TO_AUDIO, audio, FM_MODEL);    
229
 
    ITC_ctrl::connect (model, TO_SLAVE, slave, FM_MODEL);    
230
 
    ITC_ctrl::connect (model, TO_IFACE, iface, FM_MODEL);    
231
 
    ITC_ctrl::connect (slave, EV_EXIT,  &itcc, EV_EXIT);    
232
 
    ITC_ctrl::connect (slave, TO_AUDIO, audio, FM_SLAVE);    
233
 
    ITC_ctrl::connect (slave, TO_MODEL, model, FM_SLAVE);    
234
 
    ITC_ctrl::connect (iface, EV_EXIT,  &itcc, EV_EXIT);    
235
 
    ITC_ctrl::connect (iface, TO_MODEL, model, FM_IFACE);    
236
 
 
237
 
    audio->start ();
238
 
    if (imidi->thr_start (SCHED_FIFO, audio->relpri () - 20, 0x00010000))
239
 
    {
240
 
        fprintf (stderr, "Warning: can't run midi thread in RT mode.\n");
241
 
        imidi->thr_start (SCHED_OTHER, 0, 0x00010000);
242
 
    }
243
 
    if (model->thr_start (SCHED_FIFO, audio->relpri () - 30, 0x00010000))
244
 
    {
245
 
        fprintf (stderr, "Warning: can't run model thread in RT mode.\n");
246
 
        model->thr_start (SCHED_OTHER, 0, 0x00010000);
247
 
    }
248
 
    slave->thr_start (SCHED_OTHER, 0, 0x00010000);
249
 
    iface->thr_start (SCHED_OTHER, 0, 0x00010000);
250
 
 
251
 
    signal (SIGINT, sigint_handler); 
252
 
    n = 4;
253
 
    while (n)
254
 
    {    
255
 
        itcc.get_event (1 << EV_EXIT);
256
 
        {
257
 
            if (n-- == 4)
258
 
            {   
259
 
                imidi->terminate ();
260
 
                model->terminate ();
261
 
                slave->terminate ();
262
 
                iface->terminate ();
263
 
            }
264
 
        }
265
 
    }
266
 
 
267
 
    delete audio;
268
 
    delete imidi;
269
 
    delete model;
270
 
    delete slave;
271
 
    delete iface;
272
 
    dlclose (so_handle);
273
 
 
274
 
    return 0;
275
 
}
276