~ubuntu-branches/ubuntu/intrepid/aeolus/intrepid

« back to all changes in this revision

Viewing changes to main.cc

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2007-05-14 22:18:54 UTC
  • Revision ID: james.westby@ubuntu.com-20070514221854-274rj6fqs5tegu7q
Tags: upstream-0.6.6+2
ImportĀ upstreamĀ versionĀ 0.6.6+2

Show diffs side-by-side

added added

removed removed

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