~ubuntu-branches/ubuntu/saucy/jack-audio-connection-kit/saucy

« back to all changes in this revision

Viewing changes to drivers/alsa/alsa_driver.c

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-12-06 11:05:15 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20081206110515-xa9v9pajr9jqvfvg
Tags: 0.115.6-1ubuntu1
* Merge from Debian unstable, remaining Ubuntu changes:
  - Redirect stderr in bash completion (Debian #504488).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c; c-file-style: "linux"; -*- */
 
2
/*
 
3
    Copyright (C) 2001 Paul Davis 
 
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 2 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, write to the Free Software
 
17
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 
 
19
*/
 
20
 
 
21
#include <math.h>
 
22
#include <stdio.h>
 
23
#include <memory.h>
 
24
#include <unistd.h>
 
25
#include <stdlib.h>
 
26
#include <errno.h>
 
27
#include <stdarg.h>
 
28
#include <signal.h>
 
29
#include <sys/types.h>
 
30
#include <regex.h>
 
31
#include <string.h>
 
32
 
 
33
#include <jack/internal.h>
 
34
#include <jack/engine.h>
 
35
#include <jack/messagebuffer.h>
 
36
 
 
37
#include <sysdeps/time.h>
 
38
 
 
39
#include "alsa_driver.h"
 
40
#include "hammerfall.h"
 
41
#include "hdsp.h"
 
42
#include "ice1712.h"
 
43
#include "usx2y.h"
 
44
#include "generic.h"
 
45
 
 
46
extern void store_work_time (int);
 
47
extern void store_wait_time (int);
 
48
extern void show_wait_times ();
 
49
extern void show_work_times ();
 
50
 
 
51
#undef DEBUG_WAKEUP
 
52
 
 
53
/* Delay (in process calls) before jackd will report an xrun */
 
54
#define XRUN_REPORT_DELAY 0
 
55
 
 
56
static void
 
57
alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver)
 
58
{
 
59
        bitset_destroy (&driver->channels_done);
 
60
        bitset_destroy (&driver->channels_not_done);
 
61
 
 
62
        if (driver->playback_addr) {
 
63
                free (driver->playback_addr);
 
64
                driver->playback_addr = 0;
 
65
        }
 
66
 
 
67
        if (driver->capture_addr) {
 
68
                free (driver->capture_addr);
 
69
                driver->capture_addr = 0;
 
70
        }
 
71
 
 
72
        if (driver->playback_interleave_skip) {
 
73
                free (driver->playback_interleave_skip);
 
74
                driver->playback_interleave_skip = NULL;
 
75
        }
 
76
 
 
77
        if (driver->capture_interleave_skip) {
 
78
                free (driver->capture_interleave_skip);
 
79
                driver->capture_interleave_skip = NULL;
 
80
        }
 
81
 
 
82
        if (driver->silent) {
 
83
                free (driver->silent);
 
84
                driver->silent = 0;
 
85
        }
 
86
 
 
87
        if (driver->dither_state) {
 
88
                free (driver->dither_state);
 
89
                driver->dither_state = 0;
 
90
        }
 
91
}
 
92
 
 
93
static int
 
94
alsa_driver_check_capabilities (alsa_driver_t *driver)
 
95
{
 
96
        return 0;
 
97
}
 
98
 
 
99
static int
 
100
alsa_driver_check_card_type (alsa_driver_t *driver)
 
101
{
 
102
        int err;
 
103
        snd_ctl_card_info_t *card_info;
 
104
        char * ctl_name;
 
105
        regex_t expression;
 
106
 
 
107
        snd_ctl_card_info_alloca (&card_info);
 
108
 
 
109
        regcomp(&expression,"(plug)?hw:[0-9](,[0-9])?",REG_ICASE|REG_EXTENDED);
 
110
        
 
111
        if (!regexec(&expression,driver->alsa_name_playback,0,NULL,0)) {
 
112
                /* the user wants a hw or plughw device, the ctl name
 
113
                 * should be hw:x where x is the card number */
 
114
        
 
115
                char tmp[5];
 
116
                strncpy(tmp,strstr(driver->alsa_name_playback,"hw"),4);
 
117
                tmp[4]='\0';
 
118
                jack_info("control device %s",tmp);
 
119
                ctl_name = strdup(tmp);
 
120
        } else {
 
121
                ctl_name = strdup(driver->alsa_name_playback);
 
122
        }
 
123
 
 
124
        // XXX: I don't know the "right" way to do this. Which to use
 
125
        // driver->alsa_name_playback or driver->alsa_name_capture.
 
126
        if ((err = snd_ctl_open (&driver->ctl_handle, ctl_name, 0)) < 0) {
 
127
                jack_error ("control open \"%s\" (%s)", ctl_name,
 
128
                            snd_strerror(err));
 
129
        } else if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
 
130
                jack_error ("control hardware info \"%s\" (%s)",
 
131
                            driver->alsa_name_playback, snd_strerror (err));
 
132
                snd_ctl_close (driver->ctl_handle);
 
133
        }
 
134
 
 
135
        driver->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
 
136
 
 
137
        regfree(&expression);
 
138
        free(ctl_name);
 
139
 
 
140
        return alsa_driver_check_capabilities (driver);
 
141
}
 
142
 
 
143
static int
 
144
alsa_driver_hammerfall_hardware (alsa_driver_t *driver)
 
145
{
 
146
        driver->hw = jack_alsa_hammerfall_hw_new (driver);
 
147
        return 0;
 
148
}
 
149
 
 
150
static int
 
151
alsa_driver_hdsp_hardware (alsa_driver_t *driver)
 
152
{
 
153
        driver->hw = jack_alsa_hdsp_hw_new (driver);
 
154
        return 0;
 
155
}
 
156
 
 
157
static int
 
158
alsa_driver_ice1712_hardware (alsa_driver_t *driver)
 
159
{
 
160
        driver->hw = jack_alsa_ice1712_hw_new (driver);
 
161
        return 0;
 
162
}
 
163
 
 
164
static int
 
165
alsa_driver_usx2y_hardware (alsa_driver_t *driver)
 
166
{
 
167
        driver->hw = jack_alsa_usx2y_hw_new (driver);
 
168
        return 0;
 
169
}
 
170
 
 
171
static int
 
172
alsa_driver_generic_hardware (alsa_driver_t *driver)
 
173
{
 
174
        driver->hw = jack_alsa_generic_hw_new (driver);
 
175
        return 0;
 
176
}
 
177
 
 
178
static int
 
179
alsa_driver_hw_specific (alsa_driver_t *driver, int hw_monitoring,
 
180
                         int hw_metering)
 
181
{
 
182
        int err;
 
183
 
 
184
        if (!strcmp(driver->alsa_driver, "RME9652")) {
 
185
                if ((err = alsa_driver_hammerfall_hardware (driver)) != 0) {
 
186
                        return err;
 
187
                }
 
188
        } else if (!strcmp(driver->alsa_driver, "H-DSP")) {
 
189
                if ((err = alsa_driver_hdsp_hardware (driver)) !=0) {
 
190
                        return err;
 
191
                }
 
192
        } else if (!strcmp(driver->alsa_driver, "ICE1712")) {
 
193
                if ((err = alsa_driver_ice1712_hardware (driver)) !=0) {
 
194
                        return err;
 
195
                }
 
196
        } else if (!strcmp(driver->alsa_driver, "USB US-X2Y")) {
 
197
                if ((err = alsa_driver_usx2y_hardware (driver)) !=0) {
 
198
                                return err;
 
199
                }
 
200
        } else {
 
201
                if ((err = alsa_driver_generic_hardware (driver)) != 0) {
 
202
                        return err;
 
203
                }
 
204
        }
 
205
 
 
206
        if (driver->hw->capabilities & Cap_HardwareMonitoring) {
 
207
                driver->has_hw_monitoring = TRUE;
 
208
                /* XXX need to ensure that this is really FALSE or
 
209
                 * TRUE or whatever*/
 
210
                driver->hw_monitoring = hw_monitoring;
 
211
        } else {
 
212
                driver->has_hw_monitoring = FALSE;
 
213
                driver->hw_monitoring = FALSE;
 
214
        }
 
215
 
 
216
        if (driver->hw->capabilities & Cap_ClockLockReporting) {
 
217
                driver->has_clock_sync_reporting = TRUE;
 
218
        } else {
 
219
                driver->has_clock_sync_reporting = FALSE;
 
220
        }
 
221
 
 
222
        if (driver->hw->capabilities & Cap_HardwareMetering) {
 
223
                driver->has_hw_metering = TRUE;
 
224
                driver->hw_metering = hw_metering;
 
225
        } else {
 
226
                driver->has_hw_metering = FALSE;
 
227
                driver->hw_metering = FALSE;
 
228
        }
 
229
 
 
230
        return 0;
 
231
}
 
232
 
 
233
static void
 
234
alsa_driver_setup_io_function_pointers (alsa_driver_t *driver)
 
235
{
 
236
        if (SND_PCM_FORMAT_FLOAT_LE == driver->playback_sample_format) {
 
237
                if (driver->playback_interleaved) {
 
238
                        driver->channel_copy = memcpy_interleave_d32_s32;
 
239
                } else {
 
240
                        driver->channel_copy = memcpy_fake;
 
241
                }
 
242
                driver->read_via_copy = sample_move_floatLE_sSs;
 
243
                driver->write_via_copy = sample_move_dS_floatLE;
 
244
        } else {
 
245
 
 
246
                switch (driver->playback_sample_bytes) {
 
247
                case 2:
 
248
                        if (driver->playback_interleaved) {
 
249
                                driver->channel_copy = memcpy_interleave_d16_s16;
 
250
                        } else {
 
251
                                driver->channel_copy = memcpy_fake;
 
252
                        }
 
253
                        
 
254
                        switch (driver->dither) {
 
255
                        case Rectangular:
 
256
                                jack_info("Rectangular dithering at 16 bits");
 
257
                                driver->write_via_copy = driver->quirk_bswap?
 
258
                                        sample_move_dither_rect_d16_sSs:
 
259
                                        sample_move_dither_rect_d16_sS;
 
260
                                break;
 
261
                                
 
262
                        case Triangular:
 
263
                                jack_info("Triangular dithering at 16 bits");
 
264
                                driver->write_via_copy = driver->quirk_bswap?
 
265
                                        sample_move_dither_tri_d16_sSs:
 
266
                                        sample_move_dither_tri_d16_sS;
 
267
                                break;
 
268
                                
 
269
                        case Shaped:
 
270
                                jack_info("Noise-shaped dithering at 16 bits");
 
271
                                driver->write_via_copy = driver->quirk_bswap?
 
272
                                        sample_move_dither_shaped_d16_sSs:
 
273
                                        sample_move_dither_shaped_d16_sS;
 
274
                                break;
 
275
                                
 
276
                        default:
 
277
                                driver->write_via_copy = driver->quirk_bswap?
 
278
                                        sample_move_d16_sSs : 
 
279
                                        sample_move_d16_sS;
 
280
                                break;
 
281
                        }
 
282
                        break;
 
283
                        
 
284
                case 3: /* NO DITHER */
 
285
                        if (driver->playback_interleaved) {
 
286
                                driver->channel_copy = memcpy_interleave_d24_s24;
 
287
                        } else {
 
288
                                driver->channel_copy = memcpy_fake;
 
289
                        }
 
290
                        
 
291
                        driver->write_via_copy = driver->quirk_bswap?
 
292
                                sample_move_d24_sSs: 
 
293
                                sample_move_d24_sS;
 
294
 
 
295
                        break;
 
296
                                                                        
 
297
                case 4: /* NO DITHER */
 
298
                        if (driver->playback_interleaved) {
 
299
                                driver->channel_copy = memcpy_interleave_d32_s32;
 
300
                        } else {
 
301
                                driver->channel_copy = memcpy_fake;
 
302
                        }
 
303
 
 
304
                        driver->write_via_copy = driver->quirk_bswap?
 
305
                                sample_move_d32u24_sSs: 
 
306
                                sample_move_d32u24_sS;
 
307
                    break;
 
308
 
 
309
                default:
 
310
                        jack_error ("impossible sample width (%d) discovered!",
 
311
                                    driver->playback_sample_bytes);
 
312
                        exit (1);
 
313
                }
 
314
        }
 
315
        
 
316
        switch (driver->capture_sample_bytes) {
 
317
        case 2:
 
318
                driver->read_via_copy = driver->quirk_bswap?
 
319
                        sample_move_dS_s16s: 
 
320
                        sample_move_dS_s16;
 
321
                break;
 
322
        case 3:
 
323
                driver->read_via_copy = driver->quirk_bswap?
 
324
                        sample_move_dS_s24s: 
 
325
                        sample_move_dS_s24;
 
326
                break;
 
327
        case 4:
 
328
                driver->read_via_copy = driver->quirk_bswap?
 
329
                        sample_move_dS_s32u24s: 
 
330
                        sample_move_dS_s32u24;
 
331
                break;
 
332
        }
 
333
}
 
334
 
 
335
static int
 
336
alsa_driver_configure_stream (alsa_driver_t *driver, char *device_name,
 
337
                              const char *stream_name,
 
338
                              snd_pcm_t *handle, 
 
339
                              snd_pcm_hw_params_t *hw_params, 
 
340
                              snd_pcm_sw_params_t *sw_params, 
 
341
                              unsigned int *nperiodsp,
 
342
                              channel_t *nchns,
 
343
                              unsigned long sample_width)
 
344
{
 
345
        int err, format;
 
346
        unsigned int frame_rate;
 
347
        snd_pcm_uframes_t stop_th;
 
348
        static struct {
 
349
                char Name[32];
 
350
                snd_pcm_format_t format;
 
351
                int swapped;
 
352
        } formats[] = {
 
353
            {"32bit float little-endian", SND_PCM_FORMAT_FLOAT_LE},
 
354
                {"32bit integer little-endian", SND_PCM_FORMAT_S32_LE, IS_LE},
 
355
                {"32bit integer big-endian", SND_PCM_FORMAT_S32_BE, IS_BE},
 
356
                {"24bit little-endian", SND_PCM_FORMAT_S24_3LE, IS_LE},
 
357
                {"24bit big-endian", SND_PCM_FORMAT_S24_3BE, IS_BE},
 
358
                {"16bit little-endian", SND_PCM_FORMAT_S16_LE, IS_LE},
 
359
                {"16bit big-endian", SND_PCM_FORMAT_S16_BE, IS_BE},
 
360
        };
 
361
#define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
 
362
#define FIRST_16BIT_FORMAT 5
 
363
 
 
364
        if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)  {
 
365
                jack_error ("ALSA: no playback configurations available (%s)",
 
366
                            snd_strerror (err));
 
367
                return -1;
 
368
        }
 
369
 
 
370
        if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params))
 
371
            < 0) {
 
372
                jack_error ("ALSA: cannot restrict period size to integral"
 
373
                            " value.");
 
374
                return -1;
 
375
        }
 
376
 
 
377
        if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
 
378
                if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
 
379
                        if ((err = snd_pcm_hw_params_set_access (
 
380
                                     handle, hw_params,
 
381
                                     SND_PCM_ACCESS_MMAP_COMPLEX)) < 0) {
 
382
                                jack_error ("ALSA: mmap-based access is not possible"
 
383
                                            " for the %s "
 
384
                                            "stream of this audio interface",
 
385
                                            stream_name);
 
386
                                return -1;
 
387
                        }
 
388
                }
 
389
        }
 
390
        
 
391
        format = (sample_width == 4) ? 0 : NUMFORMATS - 1;
 
392
 
 
393
        while (1) {
 
394
                if ((err = snd_pcm_hw_params_set_format (
 
395
                             handle, hw_params, formats[format].format)) < 0) {
 
396
 
 
397
                        if ((sample_width == 4
 
398
                             ? format++ >= NUMFORMATS - 1
 
399
                             : format-- <= 0)) {
 
400
                                jack_error ("Sorry. The audio interface \"%s\""
 
401
                                            " doesn't support any of the"
 
402
                                            " hardware sample formats that"
 
403
                                            " JACK's alsa-driver can use.",
 
404
                                            device_name);
 
405
                                return -1;
 
406
                        }
 
407
                } else {
 
408
                        if (formats[format].swapped) {
 
409
                                driver->quirk_bswap = 1;
 
410
                        } else {
 
411
                                driver->quirk_bswap = 0;
 
412
                        }
 
413
                        jack_info ("ALSA: final selected sample format for %s: %s", stream_name, formats[format].Name);
 
414
                        break;
 
415
                }
 
416
        } 
 
417
 
 
418
        frame_rate = driver->frame_rate ;
 
419
        err = snd_pcm_hw_params_set_rate_near (handle, hw_params,
 
420
                                               &frame_rate, NULL) ;
 
421
        driver->frame_rate = frame_rate ;
 
422
        if (err < 0) {
 
423
                jack_error ("ALSA: cannot set sample/frame rate to %"
 
424
                            PRIu32 " for %s", driver->frame_rate,
 
425
                            stream_name);
 
426
                return -1;
 
427
        }
 
428
        if (!*nchns) {
 
429
                /*if not user-specified, try to find the maximum
 
430
                 * number of channels */
 
431
                unsigned int channels_max ;
 
432
                err = snd_pcm_hw_params_get_channels_max (hw_params,
 
433
                                                          &channels_max);
 
434
                *nchns = channels_max ;
 
435
 
 
436
                if (*nchns > 1024) { 
 
437
 
 
438
                        /* the hapless user is an unwitting victim of
 
439
                           the "default" ALSA PCM device, which can
 
440
                           support up to 16 million channels. since
 
441
                           they can't be bothered to set up a proper
 
442
                           default device, limit the number of
 
443
                           channels for them to a sane default.
 
444
                        */
 
445
 
 
446
                        jack_error (
 
447
"You appear to be using the ALSA software \"plug\" layer, probably\n"
 
448
"a result of using the \"default\" ALSA device. This is less\n"
 
449
"efficient than it could be. Consider using a hardware device\n"
 
450
"instead rather than using the plug layer. Usually the name of the\n"
 
451
"hardware device that corresponds to the first sound card is hw:0\n"
 
452
                                );
 
453
                        *nchns = 2;  
 
454
                }
 
455
        }                               
 
456
 
 
457
        if ((err = snd_pcm_hw_params_set_channels (handle, hw_params,
 
458
                                                   *nchns)) < 0) {
 
459
                jack_error ("ALSA: cannot set channel count to %u for %s",
 
460
                            *nchns, stream_name);
 
461
                return -1;
 
462
        }
 
463
        
 
464
        if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params,
 
465
                                                      driver->frames_per_cycle,
 
466
                                                      0))
 
467
            < 0) {
 
468
                jack_error ("ALSA: cannot set period size to %" PRIu32
 
469
                            " frames for %s", driver->frames_per_cycle,
 
470
                            stream_name);
 
471
                return -1;
 
472
        }
 
473
 
 
474
        *nperiodsp = driver->user_nperiods;
 
475
        snd_pcm_hw_params_set_periods_min (handle, hw_params, nperiodsp, NULL);
 
476
        if (*nperiodsp < driver->user_nperiods)
 
477
                *nperiodsp = driver->user_nperiods;
 
478
        if (snd_pcm_hw_params_set_periods_near (handle, hw_params,
 
479
                                                nperiodsp, NULL) < 0) {
 
480
                jack_error ("ALSA: cannot set number of periods to %u for %s",
 
481
                            *nperiodsp, stream_name);
 
482
                return -1;
 
483
        }
 
484
 
 
485
        if (*nperiodsp < driver->user_nperiods) {
 
486
                jack_error ("ALSA: got smaller periods %u than %u for %s",
 
487
                            *nperiodsp, (unsigned int) driver->user_nperiods,
 
488
                            stream_name);
 
489
                return -1;
 
490
        }
 
491
        jack_info ("ALSA: use %d periods for %s", *nperiodsp, stream_name);
 
492
#if 0   
 
493
        if (!jack_power_of_two(driver->frames_per_cycle)) {
 
494
                jack_error("JACK: frames must be a power of two "
 
495
                           "(64, 512, 1024, ...)\n");
 
496
                return -1;
 
497
        }
 
498
#endif
 
499
 
 
500
        if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params,
 
501
                                                      *nperiodsp *
 
502
                                                      driver->frames_per_cycle))
 
503
            < 0) {
 
504
                jack_error ("ALSA: cannot set buffer length to %" PRIu32
 
505
                            " for %s",
 
506
                            *nperiodsp * driver->frames_per_cycle,
 
507
                            stream_name);
 
508
                return -1;
 
509
        }
 
510
 
 
511
        if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
 
512
                jack_error ("ALSA: cannot set hardware parameters for %s",
 
513
                            stream_name);
 
514
                return -1;
 
515
        }
 
516
 
 
517
        snd_pcm_sw_params_current (handle, sw_params);
 
518
 
 
519
        if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params,
 
520
                                                          0U)) < 0) {
 
521
                jack_error ("ALSA: cannot set start mode for %s", stream_name);
 
522
                return -1;
 
523
        }
 
524
 
 
525
        stop_th = *nperiodsp * driver->frames_per_cycle;
 
526
        if (driver->soft_mode) {
 
527
                stop_th = (snd_pcm_uframes_t)-1;
 
528
        }
 
529
        
 
530
        if ((err = snd_pcm_sw_params_set_stop_threshold (
 
531
                     handle, sw_params, stop_th)) < 0) {
 
532
                jack_error ("ALSA: cannot set stop mode for %s",
 
533
                            stream_name);
 
534
                return -1;
 
535
        }
 
536
 
 
537
        if ((err = snd_pcm_sw_params_set_silence_threshold (
 
538
                     handle, sw_params, 0)) < 0) {
 
539
                jack_error ("ALSA: cannot set silence threshold for %s",
 
540
                            stream_name);
 
541
                return -1;
 
542
        }
 
543
 
 
544
#if 0
 
545
        jack_info ("set silence size to %lu * %lu = %lu",
 
546
                 driver->frames_per_cycle, *nperiodsp,
 
547
                 driver->frames_per_cycle * *nperiodsp);
 
548
 
 
549
        if ((err = snd_pcm_sw_params_set_silence_size (
 
550
                     handle, sw_params,
 
551
                     driver->frames_per_cycle * *nperiodsp)) < 0) {
 
552
                jack_error ("ALSA: cannot set silence size for %s",
 
553
                            stream_name);
 
554
                return -1;
 
555
        }
 
556
#endif
 
557
 
 
558
        if (handle == driver->playback_handle)
 
559
                err = snd_pcm_sw_params_set_avail_min (
 
560
                        handle, sw_params,
 
561
                        driver->frames_per_cycle
 
562
                        * (*nperiodsp - driver->user_nperiods + 1));
 
563
        else
 
564
                err = snd_pcm_sw_params_set_avail_min (
 
565
                        handle, sw_params, driver->frames_per_cycle);
 
566
                        
 
567
        if (err < 0) {
 
568
                jack_error ("ALSA: cannot set avail min for %s", stream_name);
 
569
                return -1;
 
570
        }
 
571
 
 
572
        if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
 
573
                jack_error ("ALSA: cannot set software parameters for %s\n",
 
574
                            stream_name);
 
575
                return -1;
 
576
        }
 
577
 
 
578
        return 0;
 
579
}
 
580
 
 
581
static int 
 
582
alsa_driver_set_parameters (alsa_driver_t *driver,
 
583
                            jack_nframes_t frames_per_cycle,
 
584
                            jack_nframes_t user_nperiods,
 
585
                            jack_nframes_t rate)
 
586
{
 
587
        int dir;
 
588
        snd_pcm_uframes_t p_period_size = 0;
 
589
        snd_pcm_uframes_t c_period_size = 0;
 
590
        channel_t chn;
 
591
        unsigned int pr = 0;
 
592
        unsigned int cr = 0;
 
593
        int err;
 
594
 
 
595
        driver->frame_rate = rate;
 
596
        driver->frames_per_cycle = frames_per_cycle;
 
597
        driver->user_nperiods = user_nperiods;
 
598
 
 
599
        jack_info ("configuring for %" PRIu32 "Hz, period = %"
 
600
                 PRIu32 " frames (%.1f ms), buffer = %" PRIu32 " periods",
 
601
                 rate, frames_per_cycle, (((float)frames_per_cycle / (float) rate) * 1000.0f), user_nperiods);
 
602
        
 
603
        if (driver->capture_handle) {
 
604
                if (alsa_driver_configure_stream (
 
605
                            driver,
 
606
                            driver->alsa_name_capture,
 
607
                            "capture", 
 
608
                            driver->capture_handle,
 
609
                            driver->capture_hw_params,
 
610
                            driver->capture_sw_params,
 
611
                            &driver->capture_nperiods,
 
612
                            &driver->capture_nchannels,
 
613
                            driver->capture_sample_bytes)) {
 
614
                        jack_error ("ALSA: cannot configure capture channel");
 
615
                        return -1;
 
616
                }
 
617
        }
 
618
 
 
619
        if (driver->playback_handle) {
 
620
                if (alsa_driver_configure_stream (
 
621
                            driver,
 
622
                            driver->alsa_name_playback,
 
623
                            "playback",
 
624
                            driver->playback_handle,
 
625
                            driver->playback_hw_params,
 
626
                            driver->playback_sw_params,
 
627
                            &driver->playback_nperiods,
 
628
                            &driver->playback_nchannels,
 
629
                            driver->playback_sample_bytes)) {
 
630
                        jack_error ("ALSA: cannot configure playback channel");
 
631
                        return -1;
 
632
                }
 
633
        }
 
634
        
 
635
        /* check the rate, since thats rather important */
 
636
 
 
637
        if (driver->playback_handle) {
 
638
                snd_pcm_hw_params_get_rate (driver->playback_hw_params,
 
639
                                            &pr, &dir);
 
640
        }
 
641
 
 
642
        if (driver->capture_handle) {
 
643
                snd_pcm_hw_params_get_rate (driver->capture_hw_params,
 
644
                                            &cr, &dir);
 
645
        }
 
646
 
 
647
        if (driver->capture_handle && driver->playback_handle) {
 
648
                if (cr != pr) {
 
649
                        jack_error ("playback and capture sample rates do "
 
650
                                    "not match (%d vs. %d)", pr, cr);
 
651
                }
 
652
 
 
653
                /* only change if *both* capture and playback rates
 
654
                 * don't match requested certain hardware actually
 
655
                 * still works properly in full-duplex with slightly
 
656
                 * different rate values between adc and dac
 
657
                 */
 
658
                if (cr != driver->frame_rate && pr != driver->frame_rate) {
 
659
                        jack_error ("sample rate in use (%d Hz) does not "
 
660
                                    "match requested rate (%d Hz)",
 
661
                                    cr, driver->frame_rate);
 
662
                        driver->frame_rate = cr;
 
663
                }
 
664
                
 
665
        }
 
666
        else if (driver->capture_handle && cr != driver->frame_rate) {
 
667
                jack_error ("capture sample rate in use (%d Hz) does not "
 
668
                            "match requested rate (%d Hz)",
 
669
                            cr, driver->frame_rate);
 
670
                driver->frame_rate = cr;
 
671
        }
 
672
        else if (driver->playback_handle && pr != driver->frame_rate) {
 
673
                jack_error ("playback sample rate in use (%d Hz) does not "
 
674
                            "match requested rate (%d Hz)",
 
675
                            pr, driver->frame_rate);
 
676
                driver->frame_rate = pr;
 
677
        }
 
678
 
 
679
 
 
680
        /* check the fragment size, since thats non-negotiable */
 
681
        
 
682
        if (driver->playback_handle) {
 
683
                snd_pcm_access_t access;
 
684
 
 
685
                err = snd_pcm_hw_params_get_period_size (
 
686
                        driver->playback_hw_params, &p_period_size, &dir);
 
687
                err = snd_pcm_hw_params_get_format (
 
688
                        driver->playback_hw_params,
 
689
                        &(driver->playback_sample_format));
 
690
                err = snd_pcm_hw_params_get_access (driver->playback_hw_params,
 
691
                                                    &access);
 
692
                driver->playback_interleaved =
 
693
                        (access == SND_PCM_ACCESS_MMAP_INTERLEAVED) 
 
694
                        || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
 
695
 
 
696
                if (p_period_size != driver->frames_per_cycle) {
 
697
                        jack_error ("alsa_pcm: requested an interrupt every %"
 
698
                                    PRIu32
 
699
                                    " frames but got %u frames for playback",
 
700
                                    driver->frames_per_cycle, p_period_size);
 
701
                        return -1;
 
702
                }
 
703
        }
 
704
 
 
705
        if (driver->capture_handle) {
 
706
                snd_pcm_access_t access;
 
707
 
 
708
                err = snd_pcm_hw_params_get_period_size (
 
709
                        driver->capture_hw_params, &c_period_size, &dir);
 
710
                err = snd_pcm_hw_params_get_format (
 
711
                        driver->capture_hw_params,
 
712
                        &(driver->capture_sample_format));
 
713
                err = snd_pcm_hw_params_get_access (driver->capture_hw_params,
 
714
                                                    &access);
 
715
                driver->capture_interleaved =
 
716
                        (access == SND_PCM_ACCESS_MMAP_INTERLEAVED) 
 
717
                        || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
 
718
        
 
719
                if (c_period_size != driver->frames_per_cycle) {
 
720
                        jack_error ("alsa_pcm: requested an interrupt every %"
 
721
                                    PRIu32
 
722
                                    " frames but got %uc frames for capture",
 
723
                                    driver->frames_per_cycle, p_period_size);
 
724
                        return -1;
 
725
                }
 
726
        }
 
727
 
 
728
        driver->playback_sample_bytes =
 
729
                snd_pcm_format_physical_width (driver->playback_sample_format)
 
730
                / 8;
 
731
        driver->capture_sample_bytes =
 
732
                snd_pcm_format_physical_width (driver->capture_sample_format)
 
733
                / 8;
 
734
 
 
735
        if (driver->playback_handle) {
 
736
                switch (driver->playback_sample_format) {
 
737
        case SND_PCM_FORMAT_FLOAT_LE:
 
738
                case SND_PCM_FORMAT_S32_LE:
 
739
                case SND_PCM_FORMAT_S24_3LE:
 
740
                case SND_PCM_FORMAT_S24_3BE:
 
741
                case SND_PCM_FORMAT_S16_LE:
 
742
                case SND_PCM_FORMAT_S32_BE:
 
743
                case SND_PCM_FORMAT_S16_BE:
 
744
                        break;
 
745
 
 
746
                default:
 
747
                        jack_error ("programming error: unhandled format "
 
748
                                    "type for playback");
 
749
                        exit (1);
 
750
                }
 
751
        }
 
752
 
 
753
        if (driver->capture_handle) {
 
754
                switch (driver->capture_sample_format) {
 
755
        case SND_PCM_FORMAT_FLOAT_LE:
 
756
                case SND_PCM_FORMAT_S32_LE:
 
757
                case SND_PCM_FORMAT_S24_3LE:
 
758
                case SND_PCM_FORMAT_S24_3BE:
 
759
                case SND_PCM_FORMAT_S16_LE:
 
760
                case SND_PCM_FORMAT_S32_BE:
 
761
                case SND_PCM_FORMAT_S16_BE:
 
762
                        break;
 
763
 
 
764
                default:
 
765
                        jack_error ("programming error: unhandled format "
 
766
                                    "type for capture");
 
767
                        exit (1);
 
768
                }
 
769
        }
 
770
 
 
771
        if (driver->playback_interleaved) {
 
772
                const snd_pcm_channel_area_t *my_areas;
 
773
                snd_pcm_uframes_t offset, frames;
 
774
                if (snd_pcm_mmap_begin(driver->playback_handle,
 
775
                                       &my_areas, &offset, &frames) < 0) {
 
776
                        jack_error ("ALSA: %s: mmap areas info error",
 
777
                                    driver->alsa_name_playback);
 
778
                        return -1;
 
779
                }
 
780
                driver->interleave_unit =
 
781
                        snd_pcm_format_physical_width (
 
782
                                driver->playback_sample_format) / 8;
 
783
        } else {
 
784
                driver->interleave_unit = 0;  /* NOT USED */
 
785
        }
 
786
 
 
787
        if (driver->capture_interleaved) {
 
788
                const snd_pcm_channel_area_t *my_areas;
 
789
                snd_pcm_uframes_t offset, frames;
 
790
                if (snd_pcm_mmap_begin(driver->capture_handle,
 
791
                                       &my_areas, &offset, &frames) < 0) {
 
792
                        jack_error ("ALSA: %s: mmap areas info error",
 
793
                                    driver->alsa_name_capture);
 
794
                        return -1;
 
795
                }
 
796
        }
 
797
 
 
798
        if (driver->playback_nchannels > driver->capture_nchannels) {
 
799
                driver->max_nchannels = driver->playback_nchannels;
 
800
                driver->user_nchannels = driver->capture_nchannels;
 
801
        } else {
 
802
                driver->max_nchannels = driver->capture_nchannels;
 
803
                driver->user_nchannels = driver->playback_nchannels;
 
804
        }
 
805
 
 
806
        alsa_driver_setup_io_function_pointers (driver);
 
807
 
 
808
        /* Allocate and initialize structures that rely on the
 
809
           channels counts.
 
810
 
 
811
           Set up the bit pattern that is used to record which
 
812
           channels require action on every cycle. any bits that are
 
813
           not set after the engine's process() call indicate channels
 
814
           that potentially need to be silenced.
 
815
        */
 
816
 
 
817
        bitset_create (&driver->channels_done, driver->max_nchannels);
 
818
        bitset_create (&driver->channels_not_done, driver->max_nchannels);
 
819
 
 
820
        if (driver->playback_handle) {
 
821
                driver->playback_addr = (char **)
 
822
                        malloc (sizeof (char *) * driver->playback_nchannels);
 
823
                memset (driver->playback_addr, 0,
 
824
                        sizeof (char *) * driver->playback_nchannels);
 
825
                driver->playback_interleave_skip = (unsigned long *)
 
826
                        malloc (sizeof (unsigned long *) * driver->playback_nchannels);
 
827
                memset (driver->playback_interleave_skip, 0,
 
828
                        sizeof (unsigned long *) * driver->playback_nchannels);
 
829
                driver->silent = (unsigned long *)
 
830
                        malloc (sizeof (unsigned long)
 
831
                                * driver->playback_nchannels);
 
832
                
 
833
                for (chn = 0; chn < driver->playback_nchannels; chn++) {
 
834
                        driver->silent[chn] = 0;
 
835
                }
 
836
 
 
837
                for (chn = 0; chn < driver->playback_nchannels; chn++) {
 
838
                        bitset_add (driver->channels_done, chn);
 
839
                }
 
840
 
 
841
                driver->dither_state = (dither_state_t *)
 
842
                        calloc ( driver->playback_nchannels,
 
843
                                 sizeof (dither_state_t));
 
844
        }
 
845
 
 
846
        if (driver->capture_handle) {
 
847
                driver->capture_addr = (char **)
 
848
                        malloc (sizeof (char *) * driver->capture_nchannels);
 
849
                memset (driver->capture_addr, 0,
 
850
                        sizeof (char *) * driver->capture_nchannels);
 
851
                driver->capture_interleave_skip = (unsigned long *)
 
852
                        malloc (sizeof (unsigned long *) * driver->capture_nchannels);
 
853
                memset (driver->capture_interleave_skip, 0,
 
854
                        sizeof (unsigned long *) * driver->capture_nchannels);
 
855
        }
 
856
 
 
857
        driver->clock_sync_data = (ClockSyncStatus *)
 
858
                malloc (sizeof (ClockSyncStatus) * driver->max_nchannels);
 
859
 
 
860
        driver->period_usecs =
 
861
                (jack_time_t) floor ((((float) driver->frames_per_cycle) /
 
862
                                      driver->frame_rate) * 1000000.0f);
 
863
        driver->poll_timeout = (int) floor (1.5f * driver->period_usecs);
 
864
 
 
865
        if (driver->engine) {
 
866
                driver->engine->set_buffer_size (driver->engine,
 
867
                                                 driver->frames_per_cycle);
 
868
        }
 
869
 
 
870
        return 0;
 
871
}       
 
872
 
 
873
static int
 
874
alsa_driver_reset_parameters (alsa_driver_t *driver,
 
875
                              jack_nframes_t frames_per_cycle,
 
876
                              jack_nframes_t user_nperiods,
 
877
                              jack_nframes_t rate)
 
878
{
 
879
        /* XXX unregister old ports ? */
 
880
        alsa_driver_release_channel_dependent_memory (driver);
 
881
        return alsa_driver_set_parameters (driver,
 
882
                                           frames_per_cycle,
 
883
                                           user_nperiods, rate);
 
884
}
 
885
 
 
886
static int
 
887
alsa_driver_get_channel_addresses (alsa_driver_t *driver,
 
888
                                   snd_pcm_uframes_t *capture_avail,
 
889
                                   snd_pcm_uframes_t *playback_avail,
 
890
                                   snd_pcm_uframes_t *capture_offset,
 
891
                                   snd_pcm_uframes_t *playback_offset)
 
892
{
 
893
        unsigned long err;
 
894
        channel_t chn;
 
895
 
 
896
        if (capture_avail) {
 
897
                if ((err = snd_pcm_mmap_begin (
 
898
                             driver->capture_handle, &driver->capture_areas,
 
899
                             (snd_pcm_uframes_t *) capture_offset, 
 
900
                             (snd_pcm_uframes_t *) capture_avail)) < 0) {
 
901
                        jack_error ("ALSA: %s: mmap areas info error",
 
902
                                    driver->alsa_name_capture);
 
903
                        return -1;
 
904
                }
 
905
                
 
906
                for (chn = 0; chn < driver->capture_nchannels; chn++) {
 
907
                        const snd_pcm_channel_area_t *a =
 
908
                                &driver->capture_areas[chn];
 
909
                        driver->capture_addr[chn] = (char *) a->addr
 
910
                                + ((a->first + a->step * *capture_offset) / 8);
 
911
                        driver->capture_interleave_skip[chn] = (unsigned long ) (a->step / 8);
 
912
                }
 
913
        } 
 
914
 
 
915
        if (playback_avail) {
 
916
                if ((err = snd_pcm_mmap_begin (
 
917
                             driver->playback_handle, &driver->playback_areas, 
 
918
                             (snd_pcm_uframes_t *) playback_offset, 
 
919
                             (snd_pcm_uframes_t *) playback_avail)) < 0) {
 
920
                        jack_error ("ALSA: %s: mmap areas info error ",
 
921
                                    driver->alsa_name_playback);
 
922
                        return -1;
 
923
                }
 
924
                
 
925
                for (chn = 0; chn < driver->playback_nchannels; chn++) {
 
926
                        const snd_pcm_channel_area_t *a =
 
927
                                &driver->playback_areas[chn];
 
928
                        driver->playback_addr[chn] = (char *) a->addr
 
929
                                + ((a->first + a->step * *playback_offset) / 8);
 
930
                        driver->playback_interleave_skip[chn] = (unsigned long ) (a->step / 8);
 
931
                }
 
932
        } 
 
933
        
 
934
        return 0;
 
935
}
 
936
 
 
937
static int
 
938
alsa_driver_start (alsa_driver_t *driver)
 
939
{
 
940
        int err;
 
941
        snd_pcm_uframes_t poffset, pavail;
 
942
        channel_t chn;
 
943
 
 
944
        driver->poll_last = 0;
 
945
        driver->poll_next = 0;
 
946
 
 
947
        if (driver->playback_handle) {
 
948
                if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
 
949
                        jack_error ("ALSA: prepare error for playback on "
 
950
                                    "\"%s\" (%s)", driver->alsa_name_playback,
 
951
                                    snd_strerror(err));
 
952
                        return -1;
 
953
                }
 
954
        }
 
955
 
 
956
        if ((driver->capture_handle && driver->capture_and_playback_not_synced)
 
957
            || !driver->playback_handle) {
 
958
                if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
 
959
                        jack_error ("ALSA: prepare error for capture on \"%s\""
 
960
                                    " (%s)", driver->alsa_name_capture,
 
961
                                    snd_strerror(err));
 
962
                        return -1;
 
963
                }
 
964
        }
 
965
 
 
966
        if (driver->hw_monitoring) {
 
967
                if (driver->input_monitor_mask || driver->all_monitor_in) {
 
968
                        if (driver->all_monitor_in) {
 
969
                                driver->hw->set_input_monitor_mask (driver->hw, ~0U);
 
970
                        } else {
 
971
                                driver->hw->set_input_monitor_mask (
 
972
                                        driver->hw, driver->input_monitor_mask);
 
973
                        }
 
974
                } else {
 
975
                        driver->hw->set_input_monitor_mask (driver->hw,
 
976
                                                            driver->input_monitor_mask);
 
977
                }
 
978
        }
 
979
 
 
980
        if (driver->playback_handle) {
 
981
                driver->playback_nfds =
 
982
                        snd_pcm_poll_descriptors_count (driver->playback_handle);
 
983
        } else {
 
984
                driver->playback_nfds = 0;
 
985
        }
 
986
 
 
987
        if (driver->capture_handle) {
 
988
                driver->capture_nfds =
 
989
                        snd_pcm_poll_descriptors_count (driver->capture_handle);
 
990
        } else {
 
991
                driver->capture_nfds = 0;
 
992
        }
 
993
 
 
994
        if (driver->pfd) {
 
995
                free (driver->pfd);
 
996
        }
 
997
 
 
998
        driver->pfd = (struct pollfd *)
 
999
                malloc (sizeof (struct pollfd) * 
 
1000
                        (driver->playback_nfds + driver->capture_nfds + 2));
 
1001
 
 
1002
        if (driver->midi && !driver->xrun_recovery)
 
1003
                (driver->midi->start)(driver->midi);
 
1004
 
 
1005
        if (driver->playback_handle) {
 
1006
                /* fill playback buffer with zeroes, and mark 
 
1007
                   all fragments as having data.
 
1008
                */
 
1009
                
 
1010
                pavail = snd_pcm_avail_update (driver->playback_handle);
 
1011
 
 
1012
                if (pavail !=
 
1013
                    driver->frames_per_cycle * driver->playback_nperiods) {
 
1014
                        jack_error ("ALSA: full buffer not available at start");
 
1015
                        return -1;
 
1016
                }
 
1017
        
 
1018
                if (alsa_driver_get_channel_addresses (driver,
 
1019
                                        0, &pavail, 0, &poffset)) {
 
1020
                        return -1;
 
1021
                }
 
1022
 
 
1023
                /* XXX this is cheating. ALSA offers no guarantee that
 
1024
                   we can access the entire buffer at any one time. It
 
1025
                   works on most hardware tested so far, however, buts
 
1026
                   its a liability in the long run. I think that
 
1027
                   alsa-lib may have a better function for doing this
 
1028
                   here, where the goal is to silence the entire
 
1029
                   buffer.
 
1030
                */
 
1031
                
 
1032
                for (chn = 0; chn < driver->playback_nchannels; chn++) {
 
1033
                        alsa_driver_silence_on_channel (
 
1034
                                driver, chn,
 
1035
                                driver->user_nperiods
 
1036
                                * driver->frames_per_cycle);
 
1037
                }
 
1038
                
 
1039
                snd_pcm_mmap_commit (driver->playback_handle, poffset,
 
1040
                                     driver->user_nperiods
 
1041
                                     * driver->frames_per_cycle);
 
1042
                
 
1043
                if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
 
1044
                        jack_error ("ALSA: could not start playback (%s)",
 
1045
                                    snd_strerror (err));
 
1046
                        return -1;
 
1047
                }
 
1048
        }
 
1049
 
 
1050
        if ((driver->capture_handle && driver->capture_and_playback_not_synced)
 
1051
            || !driver->playback_handle) {
 
1052
                if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
 
1053
                        jack_error ("ALSA: could not start capture (%s)",
 
1054
                                    snd_strerror (err));
 
1055
                        return -1;
 
1056
                }
 
1057
        }
 
1058
                        
 
1059
        return 0;
 
1060
}
 
1061
 
 
1062
static int
 
1063
alsa_driver_stop (alsa_driver_t *driver)
 
1064
{
 
1065
        int err;
 
1066
        JSList* node;
 
1067
        int chn;
 
1068
 
 
1069
        /* silence all capture port buffers, because we might
 
1070
           be entering offline mode.
 
1071
        */
 
1072
 
 
1073
        for (chn = 0, node = driver->capture_ports; node;
 
1074
             node = jack_slist_next (node), chn++) {
 
1075
 
 
1076
                jack_port_t* port;
 
1077
                char* buf;
 
1078
                jack_nframes_t nframes = driver->engine->control->buffer_size;
 
1079
 
 
1080
                port = (jack_port_t *) node->data;
 
1081
                buf = jack_port_get_buffer (port, nframes);
 
1082
                memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
 
1083
        }
 
1084
                
 
1085
        if (driver->playback_handle) {
 
1086
                if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
 
1087
                        jack_error ("ALSA: channel flush for playback "
 
1088
                                    "failed (%s)", snd_strerror (err));
 
1089
                        return -1;
 
1090
                }
 
1091
        }
 
1092
 
 
1093
        if (!driver->playback_handle
 
1094
            || driver->capture_and_playback_not_synced) {
 
1095
                if (driver->capture_handle) {
 
1096
                        if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
 
1097
                                jack_error ("ALSA: channel flush for "
 
1098
                                            "capture failed (%s)",
 
1099
                                            snd_strerror (err));
 
1100
                                return -1;
 
1101
                        }
 
1102
                }
 
1103
        }
 
1104
        
 
1105
        if (driver->hw_monitoring) {
 
1106
                driver->hw->set_input_monitor_mask (driver->hw, 0);
 
1107
        }
 
1108
 
 
1109
        if (driver->midi && !driver->xrun_recovery)
 
1110
                (driver->midi->stop)(driver->midi);
 
1111
 
 
1112
        return 0;
 
1113
}
 
1114
 
 
1115
static int
 
1116
alsa_driver_restart (alsa_driver_t *driver)
 
1117
{
 
1118
        int res;
 
1119
 
 
1120
        driver->xrun_recovery = 1;
 
1121
        if ((res = driver->nt_stop((struct _jack_driver_nt *) driver))==0)
 
1122
                res = driver->nt_start((struct _jack_driver_nt *) driver);
 
1123
        driver->xrun_recovery = 0;
 
1124
 
 
1125
        if (res && driver->midi)
 
1126
                (driver->midi->stop)(driver->midi);
 
1127
 
 
1128
        return res;
 
1129
}
 
1130
 
 
1131
static int
 
1132
alsa_driver_xrun_recovery (alsa_driver_t *driver, float *delayed_usecs)
 
1133
{
 
1134
        snd_pcm_status_t *status;
 
1135
        int res;
 
1136
 
 
1137
        snd_pcm_status_alloca(&status);
 
1138
 
 
1139
        if (driver->capture_handle) {
 
1140
                if ((res = snd_pcm_status(driver->capture_handle, status))
 
1141
                    < 0) {
 
1142
                        jack_error("status error: %s", snd_strerror(res));
 
1143
                }
 
1144
        } else {
 
1145
                if ((res = snd_pcm_status(driver->playback_handle, status))
 
1146
                    < 0) {
 
1147
                        jack_error("status error: %s", snd_strerror(res));
 
1148
                }
 
1149
        }
 
1150
 
 
1151
        if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN
 
1152
            && driver->process_count > XRUN_REPORT_DELAY) {
 
1153
                struct timeval now, diff, tstamp;
 
1154
                driver->xrun_count++;
 
1155
                snd_pcm_status_get_tstamp(status,&now);
 
1156
                snd_pcm_status_get_trigger_tstamp(status, &tstamp);
 
1157
                timersub(&now, &tstamp, &diff);
 
1158
                *delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
 
1159
                MESSAGE("\n\n**** alsa_pcm: xrun of at least %.3f "
 
1160
                        "msecs\n\n",
 
1161
                        *delayed_usecs / 1000.0);
 
1162
        }
 
1163
 
 
1164
        if (alsa_driver_restart (driver)) {
 
1165
                return -1;
 
1166
        }
 
1167
        return 0;
 
1168
}       
 
1169
 
 
1170
void
 
1171
alsa_driver_silence_untouched_channels (alsa_driver_t *driver,
 
1172
                                        jack_nframes_t nframes)
 
1173
{
 
1174
        channel_t chn;
 
1175
        jack_nframes_t buffer_frames =
 
1176
                driver->frames_per_cycle * driver->playback_nperiods;
 
1177
 
 
1178
        for (chn = 0; chn < driver->playback_nchannels; chn++) {
 
1179
                if (bitset_contains (driver->channels_not_done, chn)) { 
 
1180
                        if (driver->silent[chn] < buffer_frames) {
 
1181
                                alsa_driver_silence_on_channel_no_mark (
 
1182
                                        driver, chn, nframes);
 
1183
                                driver->silent[chn] += nframes;
 
1184
                        }
 
1185
                }
 
1186
        }
 
1187
}
 
1188
 
 
1189
void 
 
1190
alsa_driver_set_clock_sync_status (alsa_driver_t *driver, channel_t chn,
 
1191
                                   ClockSyncStatus status)
 
1192
{
 
1193
        driver->clock_sync_data[chn] = status;
 
1194
        alsa_driver_clock_sync_notify (driver, chn, status);
 
1195
}
 
1196
 
 
1197
static int under_gdb = FALSE;
 
1198
 
 
1199
static jack_nframes_t 
 
1200
alsa_driver_wait (alsa_driver_t *driver, int extra_fd, int *status, float
 
1201
                  *delayed_usecs)
 
1202
{
 
1203
        snd_pcm_sframes_t avail = 0;
 
1204
        snd_pcm_sframes_t capture_avail = 0;
 
1205
        snd_pcm_sframes_t playback_avail = 0;
 
1206
        int xrun_detected = FALSE;
 
1207
        int need_capture;
 
1208
        int need_playback;
 
1209
        unsigned int i;
 
1210
        jack_time_t poll_enter;
 
1211
        jack_time_t poll_ret = 0;
 
1212
 
 
1213
        *status = -1;
 
1214
        *delayed_usecs = 0;
 
1215
 
 
1216
        need_capture = driver->capture_handle ? 1 : 0;
 
1217
 
 
1218
        if (extra_fd >= 0) {
 
1219
                need_playback = 0;
 
1220
        } else {
 
1221
                need_playback = driver->playback_handle ? 1 : 0;
 
1222
        }
 
1223
 
 
1224
  again:
 
1225
        
 
1226
        while (need_playback || need_capture) {
 
1227
 
 
1228
                int poll_result;
 
1229
                unsigned int ci = 0;
 
1230
                unsigned int nfds;
 
1231
                unsigned short revents;
 
1232
 
 
1233
                nfds = 0;
 
1234
 
 
1235
                if (need_playback) {
 
1236
                        snd_pcm_poll_descriptors (driver->playback_handle,
 
1237
                                                  &driver->pfd[0],
 
1238
                                                  driver->playback_nfds);
 
1239
                        nfds += driver->playback_nfds;
 
1240
                }
 
1241
                
 
1242
                if (need_capture) {
 
1243
                        snd_pcm_poll_descriptors (driver->capture_handle,
 
1244
                                                  &driver->pfd[nfds],
 
1245
                                                  driver->capture_nfds);
 
1246
                        ci = nfds;
 
1247
                        nfds += driver->capture_nfds;
 
1248
                }
 
1249
 
 
1250
                /* ALSA doesn't set POLLERR in some versions of 0.9.X */
 
1251
                
 
1252
                for (i = 0; i < nfds; i++) {
 
1253
                        driver->pfd[i].events |= POLLERR;
 
1254
                }
 
1255
 
 
1256
                if (extra_fd >= 0) {
 
1257
                        driver->pfd[nfds].fd = extra_fd;
 
1258
                        driver->pfd[nfds].events =
 
1259
                                POLLIN|POLLERR|POLLHUP|POLLNVAL;
 
1260
                        nfds++;
 
1261
                }
 
1262
 
 
1263
                poll_enter = jack_get_microseconds ();
 
1264
 
 
1265
                if (poll_enter > driver->poll_next) {
 
1266
                        /*
 
1267
                         * This processing cycle was delayed past the
 
1268
                         * next due interrupt!  Do not account this as
 
1269
                         * a wakeup delay:
 
1270
                         */
 
1271
                        driver->poll_next = 0;
 
1272
                        driver->poll_late++;
 
1273
                }
 
1274
 
 
1275
                poll_result = poll (driver->pfd, nfds, driver->poll_timeout);
 
1276
                if (poll_result < 0) {
 
1277
 
 
1278
                        if (errno == EINTR) {
 
1279
                                jack_info ("poll interrupt");
 
1280
                                // this happens mostly when run
 
1281
                                // under gdb, or when exiting due to a signal
 
1282
                                if (under_gdb) {
 
1283
                                        goto again;
 
1284
                                }
 
1285
                                *status = -2;
 
1286
                                return 0;
 
1287
                        }
 
1288
                        
 
1289
                        jack_error ("ALSA: poll call failed (%s)",
 
1290
                                    strerror (errno));
 
1291
                        *status = -3;
 
1292
                        return 0;
 
1293
                        
 
1294
                }
 
1295
 
 
1296
                poll_ret = jack_get_microseconds ();
 
1297
 
 
1298
                if (extra_fd < 0) {
 
1299
                        if (driver->poll_next && poll_ret > driver->poll_next) {
 
1300
                                *delayed_usecs = poll_ret - driver->poll_next;
 
1301
                        } 
 
1302
                        driver->poll_last = poll_ret;
 
1303
                        driver->poll_next = poll_ret + driver->period_usecs;
 
1304
                        driver->engine->transport_cycle_start (driver->engine, 
 
1305
                                                               poll_ret);
 
1306
                }
 
1307
 
 
1308
#ifdef DEBUG_WAKEUP
 
1309
                jack_info ("%" PRIu64 ": checked %d fds, %" PRIu64
 
1310
                         " usecs since poll entered", poll_ret, nfds,
 
1311
                         poll_ret - poll_enter);
 
1312
#endif
 
1313
 
 
1314
                /* check to see if it was the extra FD that caused us
 
1315
                 * to return from poll */
 
1316
 
 
1317
                if (extra_fd >= 0) {
 
1318
 
 
1319
                        if (driver->pfd[nfds-1].revents == 0) {
 
1320
                                /* we timed out on the extra fd */
 
1321
 
 
1322
                                *status = -4;
 
1323
                                return -1;
 
1324
                        } 
 
1325
 
 
1326
                        /* if POLLIN was the only bit set, we're OK */
 
1327
 
 
1328
                        *status = 0;
 
1329
                        return (driver->pfd[nfds-1].revents == POLLIN) ? 0 : -1;
 
1330
                }
 
1331
 
 
1332
                if (need_playback) {
 
1333
                        if (snd_pcm_poll_descriptors_revents
 
1334
                            (driver->playback_handle, &driver->pfd[0],
 
1335
                             driver->playback_nfds, &revents) < 0) {
 
1336
                                jack_error ("ALSA: playback revents failed");
 
1337
                                *status = -6;
 
1338
                                return 0;
 
1339
                        }
 
1340
 
 
1341
                        if (revents & POLLERR) {
 
1342
                                xrun_detected = TRUE;
 
1343
                        }
 
1344
 
 
1345
                        if (revents & POLLOUT) {
 
1346
                                need_playback = 0;
 
1347
#ifdef DEBUG_WAKEUP
 
1348
                                jack_info ("%" PRIu64
 
1349
                                         " playback stream ready",
 
1350
                                         poll_ret);
 
1351
#endif
 
1352
                        }
 
1353
                }
 
1354
 
 
1355
                if (need_capture) {
 
1356
                        if (snd_pcm_poll_descriptors_revents
 
1357
                            (driver->capture_handle, &driver->pfd[ci],
 
1358
                             driver->capture_nfds, &revents) < 0) {
 
1359
                                jack_error ("ALSA: capture revents failed");
 
1360
                                *status = -6;
 
1361
                                return 0;
 
1362
                        }
 
1363
 
 
1364
                        if (revents & POLLERR) {
 
1365
                                xrun_detected = TRUE;
 
1366
                        }
 
1367
 
 
1368
                        if (revents & POLLIN) {
 
1369
                                need_capture = 0;
 
1370
#ifdef DEBUG_WAKEUP
 
1371
                                jack_info ("%" PRIu64
 
1372
                                         " capture stream ready",
 
1373
                                         poll_ret);
 
1374
#endif
 
1375
                        }
 
1376
                }
 
1377
                
 
1378
                if (poll_result == 0) {
 
1379
                        jack_error ("ALSA: poll time out, polled for %" PRIu64
 
1380
                                    " usecs",
 
1381
                                    poll_ret - poll_enter);
 
1382
                        *status = -5;
 
1383
                        return 0;
 
1384
                }               
 
1385
 
 
1386
        }
 
1387
 
 
1388
        if (driver->capture_handle) {
 
1389
                if ((capture_avail = snd_pcm_avail_update (
 
1390
                             driver->capture_handle)) < 0) {
 
1391
                        if (capture_avail == -EPIPE) {
 
1392
                                xrun_detected = TRUE;
 
1393
                        } else {
 
1394
                                jack_error ("unknown ALSA avail_update return"
 
1395
                                            " value (%u)", capture_avail);
 
1396
                        }
 
1397
                }
 
1398
        } else {
 
1399
                /* odd, but see min() computation below */
 
1400
                capture_avail = INT_MAX; 
 
1401
        }
 
1402
 
 
1403
        if (driver->playback_handle) {
 
1404
                if ((playback_avail = snd_pcm_avail_update (
 
1405
                             driver->playback_handle)) < 0) {
 
1406
                        if (playback_avail == -EPIPE) {
 
1407
                                xrun_detected = TRUE;
 
1408
                        } else {
 
1409
                                jack_error ("unknown ALSA avail_update return"
 
1410
                                            " value (%u)", playback_avail);
 
1411
                        }
 
1412
                }
 
1413
        } else {
 
1414
                /* odd, but see min() computation below */
 
1415
                playback_avail = INT_MAX; 
 
1416
        }
 
1417
 
 
1418
        if (xrun_detected) {
 
1419
                *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
 
1420
                return 0;
 
1421
        }
 
1422
 
 
1423
        *status = 0;
 
1424
        driver->last_wait_ust = poll_ret;
 
1425
 
 
1426
        avail = capture_avail < playback_avail ? capture_avail : playback_avail;
 
1427
 
 
1428
#ifdef DEBUG_WAKEUP
 
1429
        jack_info ("wakeup complete, avail = %lu, pavail = %lu "
 
1430
                 "cavail = %lu",
 
1431
                 avail, playback_avail, capture_avail);
 
1432
#endif
 
1433
 
 
1434
        /* mark all channels not done for now. read/write will change this */
 
1435
 
 
1436
        bitset_copy (driver->channels_not_done, driver->channels_done);
 
1437
 
 
1438
        /* constrain the available count to the nearest (round down) number of
 
1439
           periods.
 
1440
        */
 
1441
 
 
1442
        return avail - (avail % driver->frames_per_cycle);
 
1443
}
 
1444
 
 
1445
static int
 
1446
alsa_driver_null_cycle (alsa_driver_t* driver, jack_nframes_t nframes)
 
1447
{
 
1448
        jack_nframes_t nf;
 
1449
        snd_pcm_uframes_t offset;
 
1450
        snd_pcm_uframes_t contiguous;
 
1451
        int chn;
 
1452
 
 
1453
        if (nframes > driver->frames_per_cycle) {
 
1454
                return -1;
 
1455
        }
 
1456
        
 
1457
        if (driver->capture_handle) {
 
1458
                nf = nframes;
 
1459
                offset = 0;
 
1460
                while (nf) {
 
1461
                        contiguous = nf;
 
1462
                        
 
1463
                        if (snd_pcm_mmap_begin (
 
1464
                                    driver->capture_handle,
 
1465
                                    &driver->capture_areas,
 
1466
                                    (snd_pcm_uframes_t *) &offset, 
 
1467
                                    (snd_pcm_uframes_t *) &contiguous)) {
 
1468
                                return -1;
 
1469
                        }
 
1470
                
 
1471
                        if (snd_pcm_mmap_commit (driver->capture_handle,
 
1472
                                                 offset, contiguous) < 0) {
 
1473
                                return -1;
 
1474
                        }
 
1475
 
 
1476
                        nf -= contiguous;
 
1477
                }
 
1478
        }
 
1479
 
 
1480
        if (driver->playback_handle) {
 
1481
                nf = nframes;
 
1482
                offset = 0;
 
1483
                while (nf) {
 
1484
                        contiguous = nf;
 
1485
                        
 
1486
                        if (snd_pcm_mmap_begin (
 
1487
                                    driver->playback_handle,
 
1488
                                    &driver->playback_areas,
 
1489
                                    (snd_pcm_uframes_t *) &offset, 
 
1490
                                    (snd_pcm_uframes_t *) &contiguous)) {
 
1491
                                return -1;
 
1492
                        }
 
1493
                        
 
1494
                        for (chn = 0; chn < driver->playback_nchannels; chn++) {
 
1495
                                alsa_driver_silence_on_channel (driver, chn,
 
1496
                                                                contiguous);
 
1497
                        }
 
1498
                
 
1499
                        if (snd_pcm_mmap_commit (driver->playback_handle,
 
1500
                                                 offset, contiguous) < 0) {
 
1501
                                return -1;
 
1502
                        }
 
1503
 
 
1504
                        nf -= contiguous;
 
1505
                }
 
1506
        }
 
1507
 
 
1508
        return 0;
 
1509
}
 
1510
 
 
1511
static int
 
1512
alsa_driver_bufsize (alsa_driver_t* driver, jack_nframes_t nframes)
 
1513
{
 
1514
        return alsa_driver_reset_parameters (driver, nframes,
 
1515
                                             driver->user_nperiods,
 
1516
                                             driver->frame_rate);
 
1517
}
 
1518
 
 
1519
static int
 
1520
alsa_driver_read (alsa_driver_t *driver, jack_nframes_t nframes)
 
1521
{
 
1522
        snd_pcm_sframes_t contiguous;
 
1523
        snd_pcm_sframes_t nread;
 
1524
        snd_pcm_uframes_t offset;
 
1525
        jack_nframes_t  orig_nframes;
 
1526
        jack_default_audio_sample_t* buf;
 
1527
        channel_t chn;
 
1528
        JSList *node;
 
1529
        jack_port_t* port;
 
1530
        int err;
 
1531
 
 
1532
        if (nframes > driver->frames_per_cycle) {
 
1533
                return -1;
 
1534
        }
 
1535
        
 
1536
        if (driver->engine->freewheeling) {
 
1537
                return 0;
 
1538
        }
 
1539
 
 
1540
        if (driver->midi)
 
1541
                (driver->midi->read)(driver->midi, nframes);
 
1542
        
 
1543
        if (!driver->capture_handle) {
 
1544
                return 0;
 
1545
        }
 
1546
 
 
1547
        nread = 0;
 
1548
        contiguous = 0;
 
1549
        orig_nframes = nframes;
 
1550
        
 
1551
        while (nframes) {
 
1552
                
 
1553
                contiguous = nframes;
 
1554
                
 
1555
                if (alsa_driver_get_channel_addresses (
 
1556
                            driver, 
 
1557
                            (snd_pcm_uframes_t *) &contiguous, 
 
1558
                            (snd_pcm_uframes_t *) 0,
 
1559
                            &offset, 0) < 0) {
 
1560
                        return -1;
 
1561
                }
 
1562
                        
 
1563
                for (chn = 0, node = driver->capture_ports; node;
 
1564
                     node = jack_slist_next (node), chn++) {
 
1565
                        
 
1566
                        port = (jack_port_t *) node->data;
 
1567
                        
 
1568
                        if (!jack_port_connected (port)) {
 
1569
                                /* no-copy optimization */
 
1570
                                continue;
 
1571
                        }
 
1572
                        buf = jack_port_get_buffer (port, orig_nframes);
 
1573
                        alsa_driver_read_from_channel (driver, chn,
 
1574
                                buf + nread, contiguous);
 
1575
                }
 
1576
                
 
1577
                if ((err = snd_pcm_mmap_commit (driver->capture_handle,
 
1578
                                offset, contiguous)) < 0) {
 
1579
                        jack_error ("ALSA: could not complete read of %"
 
1580
                                PRIu32 " frames: error = %d", contiguous, err);
 
1581
                        return -1;
 
1582
                }
 
1583
 
 
1584
                nframes -= contiguous;
 
1585
                nread += contiguous;
 
1586
        }
 
1587
 
 
1588
        return 0;
 
1589
}
 
1590
 
 
1591
static int
 
1592
alsa_driver_write (alsa_driver_t* driver, jack_nframes_t nframes)
 
1593
{
 
1594
        channel_t chn;
 
1595
        JSList *node;
 
1596
        JSList *mon_node;
 
1597
        jack_default_audio_sample_t* buf;
 
1598
        jack_default_audio_sample_t* monbuf;
 
1599
        jack_nframes_t orig_nframes;
 
1600
        snd_pcm_sframes_t nwritten;
 
1601
        snd_pcm_sframes_t contiguous;
 
1602
        snd_pcm_uframes_t offset;
 
1603
        jack_port_t *port;
 
1604
        int err;
 
1605
 
 
1606
        driver->process_count++;
 
1607
 
 
1608
        if (!driver->playback_handle || driver->engine->freewheeling) {
 
1609
                return 0;
 
1610
        }
 
1611
        if (nframes > driver->frames_per_cycle) {
 
1612
                return -1;
 
1613
        }
 
1614
 
 
1615
        if (driver->midi)
 
1616
                (driver->midi->write)(driver->midi, nframes);
 
1617
        
 
1618
        nwritten = 0;
 
1619
        contiguous = 0;
 
1620
        orig_nframes = nframes;
 
1621
        
 
1622
        /* check current input monitor request status */
 
1623
        
 
1624
        driver->input_monitor_mask = 0;
 
1625
        
 
1626
        for (chn = 0, node = driver->capture_ports; node;
 
1627
             node = jack_slist_next (node), chn++) {
 
1628
                if (((jack_port_t *) node->data)->shared->monitor_requests) {
 
1629
                        driver->input_monitor_mask |= (1<<chn);
 
1630
                }
 
1631
        }
 
1632
 
 
1633
        if (driver->hw_monitoring) {
 
1634
                if ((driver->hw->input_monitor_mask
 
1635
                     != driver->input_monitor_mask)
 
1636
                    && !driver->all_monitor_in) {
 
1637
                        driver->hw->set_input_monitor_mask (
 
1638
                                driver->hw, driver->input_monitor_mask);
 
1639
                }
 
1640
        }
 
1641
        
 
1642
        while (nframes) {
 
1643
                
 
1644
                contiguous = nframes;
 
1645
                
 
1646
                if (alsa_driver_get_channel_addresses (
 
1647
                            driver, 
 
1648
                            (snd_pcm_uframes_t *) 0,
 
1649
                            (snd_pcm_uframes_t *) &contiguous, 
 
1650
                            0, &offset) < 0) {
 
1651
                        return -1;
 
1652
                }
 
1653
                
 
1654
                for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
 
1655
                     node;
 
1656
                     node = jack_slist_next (node), chn++) {
 
1657
 
 
1658
                        port = (jack_port_t *) node->data;
 
1659
 
 
1660
                        if (!jack_port_connected (port)) {
 
1661
                                continue;
 
1662
                        }
 
1663
                        buf = jack_port_get_buffer (port, orig_nframes);
 
1664
                        alsa_driver_write_to_channel (driver, chn,
 
1665
                                buf + nwritten, contiguous);
 
1666
 
 
1667
                        if (mon_node) {
 
1668
                                port = (jack_port_t *) mon_node->data;
 
1669
                                if (!jack_port_connected (port)) {
 
1670
                                        continue;
 
1671
                                }
 
1672
                                monbuf = jack_port_get_buffer (port, orig_nframes);
 
1673
                                memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
 
1674
                                mon_node = jack_slist_next (mon_node);                          
 
1675
                        }
 
1676
                }
 
1677
 
 
1678
                
 
1679
                if (!bitset_empty (driver->channels_not_done)) {
 
1680
                        alsa_driver_silence_untouched_channels (driver,
 
1681
                                                                contiguous);
 
1682
                }
 
1683
                
 
1684
                if ((err = snd_pcm_mmap_commit (driver->playback_handle,
 
1685
                                offset, contiguous)) < 0) {
 
1686
                        jack_error ("ALSA: could not complete playback of %"
 
1687
                                PRIu32 " frames: error = %d", contiguous, err);
 
1688
                        if (err != EPIPE && err != ESTRPIPE)
 
1689
                                return -1;
 
1690
                }
 
1691
 
 
1692
                nframes -= contiguous;
 
1693
                nwritten += contiguous;
 
1694
        }
 
1695
 
 
1696
        return 0;
 
1697
}
 
1698
 
 
1699
static inline int
 
1700
alsa_driver_run_cycle (alsa_driver_t *driver)
 
1701
{
 
1702
        jack_engine_t *engine = driver->engine;
 
1703
        int wait_status;
 
1704
        float delayed_usecs;
 
1705
        jack_nframes_t nframes;
 
1706
 
 
1707
        DEBUG ("alsa run cycle wait\n");
 
1708
 
 
1709
        nframes = alsa_driver_wait (driver, -1, &wait_status, &delayed_usecs);
 
1710
 
 
1711
        DEBUG ("alsaback from wait, nframes = %lu", nframes);
 
1712
 
 
1713
        if (unlikely(wait_status < 0))
 
1714
                return -1;              /* driver failed */
 
1715
 
 
1716
        if (unlikely(nframes == 0)) {
 
1717
 
 
1718
                /* we detected an xrun and restarted: notify
 
1719
                 * clients about the delay. 
 
1720
                 */
 
1721
                engine->delay (engine, delayed_usecs);
 
1722
                return 0;
 
1723
        } 
 
1724
 
 
1725
        return engine->run_cycle (engine, nframes, delayed_usecs);
 
1726
}
 
1727
 
 
1728
static int
 
1729
alsa_driver_attach (alsa_driver_t *driver)
 
1730
{
 
1731
        char buf[32];
 
1732
        channel_t chn;
 
1733
        jack_port_t *port;
 
1734
        int port_flags;
 
1735
 
 
1736
        driver->engine->set_buffer_size (driver->engine, driver->frames_per_cycle);
 
1737
        driver->engine->set_sample_rate (driver->engine, driver->frame_rate);
 
1738
 
 
1739
        port_flags = JackPortIsOutput|JackPortIsPhysical|JackPortIsTerminal;
 
1740
 
 
1741
        if (driver->has_hw_monitoring) {
 
1742
                port_flags |= JackPortCanMonitor;
 
1743
        }
 
1744
 
 
1745
        for (chn = 0; chn < driver->capture_nchannels; chn++) {
 
1746
 
 
1747
                snprintf (buf, sizeof(buf), "capture_%lu", chn+1);
 
1748
 
 
1749
                if ((port = jack_port_register (driver->client, buf,
 
1750
                                                JACK_DEFAULT_AUDIO_TYPE,
 
1751
                                                port_flags, 0)) == NULL) {
 
1752
                        jack_error ("ALSA: cannot register port for %s", buf);
 
1753
                        break;
 
1754
                }
 
1755
 
 
1756
                jack_port_set_latency (port, driver->frames_per_cycle + driver->capture_frame_latency);
 
1757
 
 
1758
                driver->capture_ports =
 
1759
                        jack_slist_append (driver->capture_ports, port);
 
1760
        }
 
1761
        
 
1762
        port_flags = JackPortIsInput|JackPortIsPhysical|JackPortIsTerminal;
 
1763
 
 
1764
        for (chn = 0; chn < driver->playback_nchannels; chn++) {
 
1765
                jack_port_t *monitor_port;
 
1766
 
 
1767
                snprintf (buf, sizeof(buf) - 1, "playback_%lu", chn+1);
 
1768
 
 
1769
                if ((port = jack_port_register (driver->client, buf,
 
1770
                                                JACK_DEFAULT_AUDIO_TYPE,
 
1771
                                                port_flags, 0)) == NULL) {
 
1772
                        jack_error ("ALSA: cannot register port for %s", buf);
 
1773
                        break;
 
1774
                }
 
1775
                
 
1776
                jack_port_set_latency (port, (driver->frames_per_cycle * (driver->user_nperiods - 1)) + driver->playback_frame_latency);
 
1777
 
 
1778
                driver->playback_ports =
 
1779
                        jack_slist_append (driver->playback_ports, port);
 
1780
 
 
1781
                if (driver->with_monitor_ports) {
 
1782
                        snprintf (buf, sizeof(buf) - 1, "monitor_%lu", chn+1);
 
1783
                        
 
1784
                        if ((monitor_port = jack_port_register (
 
1785
                                     driver->client, buf,
 
1786
                                     JACK_DEFAULT_AUDIO_TYPE,
 
1787
                                     JackPortIsOutput, 0)) == NULL) {
 
1788
                                jack_error ("ALSA: cannot register monitor "
 
1789
                                            "port for %s", buf);
 
1790
                        } else {
 
1791
 
 
1792
                                jack_port_set_latency (monitor_port, driver->frames_per_cycle);
 
1793
                                
 
1794
                                driver->monitor_ports =
 
1795
                                        jack_slist_append (driver->monitor_ports, monitor_port);
 
1796
                        }
 
1797
                        
 
1798
                }
 
1799
        }
 
1800
 
 
1801
        if (driver->midi) {
 
1802
                int err = (driver->midi->attach)(driver->midi);
 
1803
                if (err)
 
1804
                        jack_error("ALSA: cannot attach midi: %d", err);
 
1805
        }
 
1806
        
 
1807
 
 
1808
        return jack_activate (driver->client);
 
1809
}
 
1810
 
 
1811
static int
 
1812
alsa_driver_detach (alsa_driver_t *driver)
 
1813
{
 
1814
        JSList *node;
 
1815
 
 
1816
        if (driver->engine == NULL) {
 
1817
                return 0;
 
1818
        }
 
1819
 
 
1820
        if (driver->midi)
 
1821
                (driver->midi->detach)(driver->midi);
 
1822
        
 
1823
        for (node = driver->capture_ports; node;
 
1824
             node = jack_slist_next (node)) {
 
1825
                jack_port_unregister (driver->client,
 
1826
                                      ((jack_port_t *) node->data));
 
1827
        }
 
1828
 
 
1829
        jack_slist_free (driver->capture_ports);
 
1830
        driver->capture_ports = 0;
 
1831
                
 
1832
        for (node = driver->playback_ports; node;
 
1833
             node = jack_slist_next (node)) {
 
1834
                jack_port_unregister (driver->client,
 
1835
                                      ((jack_port_t *) node->data));
 
1836
        }
 
1837
 
 
1838
        jack_slist_free (driver->playback_ports);
 
1839
        driver->playback_ports = 0;
 
1840
 
 
1841
        if (driver->monitor_ports) {
 
1842
                for (node = driver->monitor_ports; node;
 
1843
                     node = jack_slist_next (node)) {
 
1844
                        jack_port_unregister (driver->client,
 
1845
                                              ((jack_port_t *) node->data));
 
1846
                }
 
1847
                
 
1848
                jack_slist_free (driver->monitor_ports);
 
1849
                driver->monitor_ports = 0;
 
1850
        }
 
1851
        
 
1852
        return 0;
 
1853
}
 
1854
 
 
1855
#if 0
 
1856
static int  /* UNUSED */
 
1857
alsa_driver_change_sample_clock (alsa_driver_t *driver, SampleClockMode mode)
 
1858
 
 
1859
{
 
1860
        return driver->hw->change_sample_clock (driver->hw, mode);
 
1861
}
 
1862
 
 
1863
static void  /* UNUSED */
 
1864
alsa_driver_request_all_monitor_input (alsa_driver_t *driver, int yn)
 
1865
 
 
1866
{
 
1867
        if (driver->hw_monitoring) {
 
1868
                if (yn) {
 
1869
                        driver->hw->set_input_monitor_mask (driver->hw, ~0U);
 
1870
                } else {
 
1871
                        driver->hw->set_input_monitor_mask (
 
1872
                                driver->hw, driver->input_monitor_mask);
 
1873
                }
 
1874
        }
 
1875
 
 
1876
        driver->all_monitor_in = yn;
 
1877
}
 
1878
 
 
1879
static void  /* UNUSED */
 
1880
alsa_driver_set_hw_monitoring (alsa_driver_t *driver, int yn)
 
1881
{
 
1882
        if (yn) {
 
1883
                driver->hw_monitoring = TRUE;
 
1884
                
 
1885
                if (driver->all_monitor_in) {
 
1886
                        driver->hw->set_input_monitor_mask (driver->hw, ~0U);
 
1887
                } else {
 
1888
                        driver->hw->set_input_monitor_mask (
 
1889
                                driver->hw, driver->input_monitor_mask);
 
1890
                }
 
1891
        } else {
 
1892
                driver->hw_monitoring = FALSE;
 
1893
                driver->hw->set_input_monitor_mask (driver->hw, 0);
 
1894
        }
 
1895
}
 
1896
 
 
1897
static ClockSyncStatus  /* UNUSED */
 
1898
alsa_driver_clock_sync_status (channel_t chn)
 
1899
{
 
1900
        return Lock;
 
1901
}
 
1902
#endif
 
1903
 
 
1904
static void
 
1905
alsa_driver_delete (alsa_driver_t *driver)
 
1906
{
 
1907
        JSList *node;
 
1908
 
 
1909
        if (driver->midi)
 
1910
                (driver->midi->destroy)(driver->midi);
 
1911
 
 
1912
        for (node = driver->clock_sync_listeners; node;
 
1913
             node = jack_slist_next (node)) {
 
1914
                free (node->data);
 
1915
        }
 
1916
        jack_slist_free (driver->clock_sync_listeners);
 
1917
 
 
1918
        if (driver->ctl_handle) {
 
1919
                snd_ctl_close (driver->ctl_handle);
 
1920
                driver->ctl_handle = 0;
 
1921
        } 
 
1922
 
 
1923
        if (driver->capture_handle) {
 
1924
                snd_pcm_close (driver->capture_handle);
 
1925
                driver->capture_handle = 0;
 
1926
        } 
 
1927
 
 
1928
        if (driver->playback_handle) {
 
1929
                snd_pcm_close (driver->playback_handle);
 
1930
                driver->capture_handle = 0;
 
1931
        }
 
1932
        
 
1933
        if (driver->capture_hw_params) {
 
1934
                snd_pcm_hw_params_free (driver->capture_hw_params);
 
1935
                driver->capture_hw_params = 0;
 
1936
        }
 
1937
 
 
1938
        if (driver->playback_hw_params) {
 
1939
                snd_pcm_hw_params_free (driver->playback_hw_params);
 
1940
                driver->playback_hw_params = 0;
 
1941
        }
 
1942
        
 
1943
        if (driver->capture_sw_params) {
 
1944
                snd_pcm_sw_params_free (driver->capture_sw_params);
 
1945
                driver->capture_sw_params = 0;
 
1946
        }
 
1947
        
 
1948
        if (driver->playback_sw_params) {
 
1949
                snd_pcm_sw_params_free (driver->playback_sw_params);
 
1950
                driver->playback_sw_params = 0;
 
1951
        }
 
1952
 
 
1953
        if (driver->pfd) {
 
1954
                free (driver->pfd);
 
1955
        }
 
1956
        
 
1957
        if (driver->hw) {
 
1958
                driver->hw->release (driver->hw);
 
1959
                driver->hw = 0;
 
1960
        }
 
1961
        free(driver->alsa_name_playback);
 
1962
        free(driver->alsa_name_capture);
 
1963
        free(driver->alsa_driver);
 
1964
 
 
1965
        alsa_driver_release_channel_dependent_memory (driver);
 
1966
        jack_driver_nt_finish ((jack_driver_nt_t *) driver);
 
1967
        free (driver);
 
1968
}
 
1969
 
 
1970
static jack_driver_t *
 
1971
alsa_driver_new (char *name, char *playback_alsa_device,
 
1972
                 char *capture_alsa_device,
 
1973
                 jack_client_t *client, 
 
1974
                 jack_nframes_t frames_per_cycle,
 
1975
                 jack_nframes_t user_nperiods,
 
1976
                 jack_nframes_t rate,
 
1977
                 int hw_monitoring,
 
1978
                 int hw_metering,
 
1979
                 int capturing,
 
1980
                 int playing,
 
1981
                 DitherAlgorithm dither,
 
1982
                 int soft_mode, 
 
1983
                 int monitor,
 
1984
                 int user_capture_nchnls,
 
1985
                 int user_playback_nchnls,
 
1986
                 int shorts_first,
 
1987
                 jack_nframes_t capture_latency,
 
1988
                 jack_nframes_t playback_latency,
 
1989
                 alsa_midi_t *midi_driver
 
1990
                 )
 
1991
{
 
1992
        int err;
 
1993
 
 
1994
        alsa_driver_t *driver;
 
1995
 
 
1996
        jack_info ("creating alsa driver ... %s|%s|%" PRIu32 "|%" PRIu32
 
1997
                "|%" PRIu32"|%" PRIu32"|%" PRIu32 "|%s|%s|%s|%s",
 
1998
                playing ? playback_alsa_device : "-",
 
1999
                capturing ? capture_alsa_device : "-", 
 
2000
                frames_per_cycle, user_nperiods, rate,
 
2001
                user_capture_nchnls,user_playback_nchnls,
 
2002
                hw_monitoring ? "hwmon": "nomon",
 
2003
                hw_metering ? "hwmeter":"swmeter",
 
2004
                soft_mode ? "soft-mode":"-",
 
2005
                shorts_first ? "16bit":"32bit");
 
2006
 
 
2007
        driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
 
2008
 
 
2009
        jack_driver_nt_init ((jack_driver_nt_t *) driver);
 
2010
 
 
2011
        driver->nt_attach = (JackDriverNTAttachFunction) alsa_driver_attach;
 
2012
        driver->nt_detach = (JackDriverNTDetachFunction) alsa_driver_detach;
 
2013
        driver->read = (JackDriverReadFunction) alsa_driver_read;
 
2014
        driver->write = (JackDriverReadFunction) alsa_driver_write;
 
2015
        driver->null_cycle =
 
2016
                (JackDriverNullCycleFunction) alsa_driver_null_cycle;
 
2017
        driver->nt_bufsize = (JackDriverNTBufSizeFunction) alsa_driver_bufsize;
 
2018
        driver->nt_start = (JackDriverNTStartFunction) alsa_driver_start;
 
2019
        driver->nt_stop = (JackDriverNTStopFunction) alsa_driver_stop;
 
2020
        driver->nt_run_cycle = (JackDriverNTRunCycleFunction) alsa_driver_run_cycle;
 
2021
 
 
2022
        driver->playback_handle = NULL;
 
2023
        driver->capture_handle = NULL;
 
2024
        driver->ctl_handle = 0;
 
2025
        driver->hw = 0;
 
2026
        driver->capture_and_playback_not_synced = FALSE;
 
2027
        driver->max_nchannels = 0;
 
2028
        driver->user_nchannels = 0;
 
2029
        driver->playback_nchannels = user_playback_nchnls;
 
2030
        driver->capture_nchannels = user_capture_nchnls;
 
2031
        driver->playback_sample_bytes = (shorts_first ? 2:4);
 
2032
        driver->capture_sample_bytes = (shorts_first ? 2:4);
 
2033
        driver->capture_frame_latency = capture_latency;
 
2034
        driver->playback_frame_latency = playback_latency;
 
2035
 
 
2036
        driver->playback_addr = 0;
 
2037
        driver->capture_addr = 0;
 
2038
        driver->playback_interleave_skip = NULL;
 
2039
        driver->capture_interleave_skip = NULL;
 
2040
 
 
2041
 
 
2042
        driver->silent = 0;
 
2043
        driver->all_monitor_in = FALSE;
 
2044
        driver->with_monitor_ports = monitor;
 
2045
 
 
2046
        driver->clock_mode = ClockMaster; /* XXX is it? */
 
2047
        driver->input_monitor_mask = 0;   /* XXX is it? */
 
2048
 
 
2049
        driver->capture_ports = 0;
 
2050
        driver->playback_ports = 0;
 
2051
        driver->monitor_ports = 0;
 
2052
        
 
2053
        driver->pfd = 0;
 
2054
        driver->playback_nfds = 0;
 
2055
        driver->capture_nfds = 0;
 
2056
 
 
2057
        driver->dither = dither;
 
2058
        driver->soft_mode = soft_mode;
 
2059
 
 
2060
        driver->quirk_bswap = 0;
 
2061
 
 
2062
        pthread_mutex_init (&driver->clock_sync_lock, 0);
 
2063
        driver->clock_sync_listeners = 0;
 
2064
 
 
2065
        driver->poll_late = 0;
 
2066
        driver->xrun_count = 0;
 
2067
        driver->process_count = 0;
 
2068
 
 
2069
        driver->alsa_name_playback = strdup (playback_alsa_device);
 
2070
        driver->alsa_name_capture = strdup (capture_alsa_device);
 
2071
 
 
2072
        driver->midi = midi_driver;
 
2073
        driver->xrun_recovery = 0;
 
2074
 
 
2075
        if (alsa_driver_check_card_type (driver)) {
 
2076
                alsa_driver_delete (driver);
 
2077
                return NULL;
 
2078
        }
 
2079
 
 
2080
        alsa_driver_hw_specific (driver, hw_monitoring, hw_metering);
 
2081
 
 
2082
        if (playing) {
 
2083
                if (snd_pcm_open (&driver->playback_handle,
 
2084
                                  playback_alsa_device,
 
2085
                                  SND_PCM_STREAM_PLAYBACK,
 
2086
                                  SND_PCM_NONBLOCK) < 0) {
 
2087
                        switch (errno) {
 
2088
                        case EBUSY:
 
2089
                                jack_error ("the playback device \"%s\" is "
 
2090
                                            "already in use. Please stop the"
 
2091
                                            " application using it and "
 
2092
                                            "run JACK again",
 
2093
                                            playback_alsa_device);
 
2094
                                alsa_driver_delete (driver);
 
2095
                                return NULL;
 
2096
                                break;
 
2097
 
 
2098
                        case EPERM:
 
2099
                                jack_error ("you do not have permission to open "
 
2100
                                            "the audio device \"%s\" for playback",
 
2101
                                            playback_alsa_device);
 
2102
                                alsa_driver_delete (driver);
 
2103
                                return NULL;
 
2104
                                break;
 
2105
                        } 
 
2106
                        
 
2107
                        driver->playback_handle = NULL;
 
2108
                } 
 
2109
 
 
2110
                if (driver->playback_handle) {
 
2111
                        snd_pcm_nonblock (driver->playback_handle, 0);
 
2112
                }
 
2113
        } 
 
2114
 
 
2115
        if (capturing) {
 
2116
                if (snd_pcm_open (&driver->capture_handle,
 
2117
                                  capture_alsa_device,
 
2118
                                  SND_PCM_STREAM_CAPTURE,
 
2119
                                  SND_PCM_NONBLOCK) < 0) {
 
2120
                        switch (errno) {
 
2121
                        case EBUSY:
 
2122
                                jack_error ("the capture device \"%s\" is "
 
2123
                                            "already in use. Please stop the"
 
2124
                                            " application using it and "
 
2125
                                            "run JACK again",
 
2126
                                            capture_alsa_device);
 
2127
                                alsa_driver_delete (driver);
 
2128
                                return NULL;
 
2129
                                break;
 
2130
 
 
2131
                        case EPERM:
 
2132
                                jack_error ("you do not have permission to open "
 
2133
                                            "the audio device \"%s\" for capture",
 
2134
                                            capture_alsa_device);
 
2135
                                alsa_driver_delete (driver);
 
2136
                                return NULL;
 
2137
                                break;
 
2138
                        } 
 
2139
 
 
2140
                        driver->capture_handle = NULL;
 
2141
                }
 
2142
 
 
2143
                if (driver->capture_handle) {
 
2144
                        snd_pcm_nonblock (driver->capture_handle, 0);
 
2145
                }
 
2146
        }
 
2147
 
 
2148
        if (driver->playback_handle == NULL) {
 
2149
                if (playing) {
 
2150
 
 
2151
                        /* they asked for playback, but we can't do it */
 
2152
 
 
2153
                        jack_error ("ALSA: Cannot open PCM device %s for "
 
2154
                                    "playback. Falling back to capture-only"
 
2155
                                    " mode", name);
 
2156
 
 
2157
                        if (driver->capture_handle == NULL) {
 
2158
                                /* can't do anything */
 
2159
                                alsa_driver_delete (driver);
 
2160
                                return NULL;
 
2161
                        }
 
2162
                        
 
2163
                        playing = FALSE;
 
2164
                }
 
2165
        }
 
2166
 
 
2167
        if (driver->capture_handle == NULL) {
 
2168
                if (capturing) {
 
2169
 
 
2170
                        /* they asked for capture, but we can't do it */
 
2171
                        
 
2172
                        jack_error ("ALSA: Cannot open PCM device %s for "
 
2173
                                    "capture. Falling back to playback-only"
 
2174
                                    " mode", name);
 
2175
                        
 
2176
                        if (driver->playback_handle == NULL) {
 
2177
                                /* can't do anything */
 
2178
                                alsa_driver_delete (driver);
 
2179
                                return NULL;
 
2180
                        }
 
2181
 
 
2182
                        capturing = FALSE;
 
2183
                }
 
2184
        }
 
2185
 
 
2186
        driver->playback_hw_params = 0;
 
2187
        driver->capture_hw_params = 0;
 
2188
        driver->playback_sw_params = 0;
 
2189
        driver->capture_sw_params = 0;
 
2190
 
 
2191
        if (driver->playback_handle) {
 
2192
                if ((err = snd_pcm_hw_params_malloc (
 
2193
                             &driver->playback_hw_params)) < 0) {
 
2194
                        jack_error ("ALSA: could not allocate playback hw"
 
2195
                                    " params structure");
 
2196
                        alsa_driver_delete (driver);
 
2197
                        return NULL;
 
2198
                }
 
2199
 
 
2200
                if ((err = snd_pcm_sw_params_malloc (
 
2201
                             &driver->playback_sw_params)) < 0) {
 
2202
                        jack_error ("ALSA: could not allocate playback sw"
 
2203
                                    " params structure");
 
2204
                        alsa_driver_delete (driver);
 
2205
                        return NULL;
 
2206
                }
 
2207
        }
 
2208
 
 
2209
        if (driver->capture_handle) {
 
2210
                if ((err = snd_pcm_hw_params_malloc (
 
2211
                             &driver->capture_hw_params)) < 0) {
 
2212
                        jack_error ("ALSA: could not allocate capture hw"
 
2213
                                    " params structure");
 
2214
                        alsa_driver_delete (driver);
 
2215
                        return NULL;
 
2216
                }
 
2217
 
 
2218
                if ((err = snd_pcm_sw_params_malloc (
 
2219
                             &driver->capture_sw_params)) < 0) {
 
2220
                        jack_error ("ALSA: could not allocate capture sw"
 
2221
                                    " params structure");
 
2222
                        alsa_driver_delete (driver);
 
2223
                        return NULL;
 
2224
                }
 
2225
        }
 
2226
 
 
2227
        if (alsa_driver_set_parameters (driver, frames_per_cycle,
 
2228
                                        user_nperiods, rate)) {
 
2229
                alsa_driver_delete (driver);
 
2230
                return NULL;
 
2231
        }
 
2232
 
 
2233
        driver->capture_and_playback_not_synced = FALSE;
 
2234
 
 
2235
        if (driver->capture_handle && driver->playback_handle) {
 
2236
                if (snd_pcm_link (driver->playback_handle,
 
2237
                                  driver->capture_handle) != 0) {
 
2238
                        driver->capture_and_playback_not_synced = TRUE;
 
2239
                } 
 
2240
        }
 
2241
 
 
2242
        driver->client = client;
 
2243
 
 
2244
        return (jack_driver_t *) driver;
 
2245
}
 
2246
 
 
2247
int
 
2248
alsa_driver_listen_for_clock_sync_status (alsa_driver_t *driver, 
 
2249
                                          ClockSyncListenerFunction func,
 
2250
                                          void *arg)
 
2251
{
 
2252
        ClockSyncListener *csl;
 
2253
 
 
2254
        csl = (ClockSyncListener *) malloc (sizeof (ClockSyncListener));
 
2255
        csl->function = func;
 
2256
        csl->arg = arg;
 
2257
        csl->id = driver->next_clock_sync_listener_id++;
 
2258
        
 
2259
        pthread_mutex_lock (&driver->clock_sync_lock);
 
2260
        driver->clock_sync_listeners =
 
2261
                jack_slist_prepend (driver->clock_sync_listeners, csl);
 
2262
        pthread_mutex_unlock (&driver->clock_sync_lock);
 
2263
        return csl->id;
 
2264
}
 
2265
 
 
2266
int
 
2267
alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t *driver,
 
2268
                                                 unsigned int which)
 
2269
 
 
2270
{
 
2271
        JSList *node;
 
2272
        int ret = -1;
 
2273
        pthread_mutex_lock (&driver->clock_sync_lock);
 
2274
        for (node = driver->clock_sync_listeners; node;
 
2275
             node = jack_slist_next (node)) {
 
2276
                if (((ClockSyncListener *) node->data)->id == which) {
 
2277
                        driver->clock_sync_listeners =
 
2278
                                jack_slist_remove_link (
 
2279
                                        driver->clock_sync_listeners, node);
 
2280
                        free (node->data);
 
2281
                        jack_slist_free_1 (node);
 
2282
                        ret = 0;
 
2283
                        break;
 
2284
                }
 
2285
        }
 
2286
        pthread_mutex_unlock (&driver->clock_sync_lock);
 
2287
        return ret;
 
2288
}
 
2289
 
 
2290
void 
 
2291
alsa_driver_clock_sync_notify (alsa_driver_t *driver, channel_t chn,
 
2292
                               ClockSyncStatus status)
 
2293
{
 
2294
        JSList *node;
 
2295
 
 
2296
        pthread_mutex_lock (&driver->clock_sync_lock);
 
2297
        for (node = driver->clock_sync_listeners; node;
 
2298
             node = jack_slist_next (node)) {
 
2299
                ClockSyncListener *csl = (ClockSyncListener *) node->data;
 
2300
                csl->function (chn, status, csl->arg);
 
2301
        }
 
2302
        pthread_mutex_unlock (&driver->clock_sync_lock);
 
2303
 
 
2304
}
 
2305
 
 
2306
static int
 
2307
dither_opt (char c, DitherAlgorithm* dither)
 
2308
{
 
2309
        switch (c) {
 
2310
        case '-':
 
2311
        case 'n':
 
2312
                *dither = None;
 
2313
                break;
 
2314
                
 
2315
        case 'r':
 
2316
                *dither = Rectangular;
 
2317
                break;
 
2318
                
 
2319
        case 's':
 
2320
                *dither = Shaped;
 
2321
                break;
 
2322
                
 
2323
        case 't':
 
2324
                *dither = Triangular;
 
2325
                break;
 
2326
                
 
2327
        default:
 
2328
                jack_error ("ALSA driver: illegal dithering mode %c", c);
 
2329
                return -1;
 
2330
        }
 
2331
        return 0;
 
2332
}
 
2333
 
 
2334
 
 
2335
/* DRIVER "PLUGIN" INTERFACE */
 
2336
 
 
2337
const char driver_client_name[] = "alsa_pcm";
 
2338
 
 
2339
const jack_driver_desc_t *
 
2340
driver_get_descriptor ()
 
2341
{
 
2342
        jack_driver_desc_t * desc;
 
2343
        jack_driver_param_desc_t * params;
 
2344
        unsigned int i;
 
2345
 
 
2346
        desc = calloc (1, sizeof (jack_driver_desc_t));
 
2347
 
 
2348
        strcpy (desc->name,"alsa");
 
2349
        desc->nparams = 18;
 
2350
  
 
2351
        params = calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
 
2352
 
 
2353
        i = 0;
 
2354
        strcpy (params[i].name, "capture");
 
2355
        params[i].character  = 'C';
 
2356
        params[i].type       = JackDriverParamString;
 
2357
        strcpy (params[i].value.str, "none");
 
2358
        strcpy (params[i].short_desc,
 
2359
                "Provide capture ports.  Optionally set device");
 
2360
        strcpy (params[i].long_desc, params[i].short_desc);
 
2361
 
 
2362
        i++;
 
2363
        strcpy (params[i].name, "playback");
 
2364
        params[i].character  = 'P';
 
2365
        params[i].type       = JackDriverParamString;
 
2366
        strcpy (params[i].value.str, "none");
 
2367
        strcpy (params[i].short_desc,
 
2368
                "Provide playback ports.  Optionally set device");
 
2369
        strcpy (params[i].long_desc, params[i].short_desc);
 
2370
 
 
2371
        i++;
 
2372
        strcpy (params[i].name, "device");
 
2373
        params[i].character  = 'd';
 
2374
        params[i].type       = JackDriverParamString;
 
2375
        strcpy (params[i].value.str,  "hw:0");
 
2376
        strcpy (params[i].short_desc, "ALSA device name");
 
2377
        strcpy (params[i].long_desc,  params[i].short_desc);
 
2378
 
 
2379
        i++;
 
2380
        strcpy (params[i].name, "rate");
 
2381
        params[i].character  = 'r';
 
2382
        params[i].type       = JackDriverParamUInt;
 
2383
        params[i].value.ui   = 48000U;
 
2384
        strcpy (params[i].short_desc, "Sample rate");
 
2385
        strcpy (params[i].long_desc, params[i].short_desc);
 
2386
 
 
2387
        i++;
 
2388
        strcpy (params[i].name, "period");
 
2389
        params[i].character  = 'p';
 
2390
        params[i].type       = JackDriverParamUInt;
 
2391
        params[i].value.ui   = 1024U;
 
2392
        strcpy (params[i].short_desc, "Frames per period");
 
2393
        strcpy (params[i].long_desc, params[i].short_desc);
 
2394
 
 
2395
        i++;
 
2396
        strcpy (params[i].name, "nperiods");
 
2397
        params[i].character  = 'n';
 
2398
        params[i].type       = JackDriverParamUInt;
 
2399
        params[i].value.ui   = 2U;
 
2400
        strcpy (params[i].short_desc, "Number of periods of playback latency");
 
2401
        strcpy (params[i].long_desc, params[i].short_desc);
 
2402
 
 
2403
        i++;
 
2404
        strcpy (params[i].name, "hwmon");
 
2405
        params[i].character  = 'H';
 
2406
        params[i].type       = JackDriverParamBool;
 
2407
        params[i].value.i    = 0;
 
2408
        strcpy (params[i].short_desc,"Hardware monitoring, if available");
 
2409
        strcpy (params[i].long_desc, params[i].short_desc);
 
2410
 
 
2411
        i++;
 
2412
        strcpy (params[i].name, "hwmeter");
 
2413
        params[i].character  = 'M';
 
2414
        params[i].type       = JackDriverParamBool;
 
2415
        params[i].value.i    = 0;
 
2416
        strcpy (params[i].short_desc, "Hardware metering, if available");
 
2417
        strcpy (params[i].long_desc, params[i].short_desc);
 
2418
 
 
2419
        i++;
 
2420
        strcpy (params[i].name, "duplex");
 
2421
        params[i].character  = 'D';
 
2422
        params[i].type       = JackDriverParamBool;
 
2423
        params[i].value.i    = 1;
 
2424
        strcpy (params[i].short_desc,
 
2425
                "Provide both capture and playback ports");
 
2426
        strcpy (params[i].long_desc, params[i].short_desc);
 
2427
 
 
2428
        i++;
 
2429
        strcpy (params[i].name, "softmode");
 
2430
        params[i].character  = 's';
 
2431
        params[i].type       = JackDriverParamBool;
 
2432
        params[i].value.i    = 0;
 
2433
        strcpy (params[i].short_desc, "Soft-mode, no xrun handling");
 
2434
        strcpy (params[i].long_desc,  params[i].short_desc);
 
2435
 
 
2436
        i++;
 
2437
        strcpy (params[i].name, "monitor");
 
2438
        params[i].character  = 'm';
 
2439
        params[i].type       = JackDriverParamBool;
 
2440
        params[i].value.i    = 0;
 
2441
        strcpy (params[i].short_desc, "Provide monitor ports for the output");
 
2442
        strcpy (params[i].long_desc, params[i].short_desc);
 
2443
 
 
2444
        i++;
 
2445
        strcpy (params[i].name, "dither");
 
2446
        params[i].character  = 'z';
 
2447
        params[i].type       = JackDriverParamChar;
 
2448
        params[i].value.c    = 'n';
 
2449
        strcpy (params[i].short_desc, "Dithering mode");
 
2450
        strcpy (params[i].long_desc,
 
2451
                "Dithering mode:\n"
 
2452
                "  n - none\n"
 
2453
                "  r - rectangular\n"
 
2454
                "  s - shaped\n"
 
2455
                "  t - triangular");
 
2456
 
 
2457
        i++;
 
2458
        strcpy (params[i].name, "inchannels");
 
2459
        params[i].character  = 'i';
 
2460
        params[i].type       = JackDriverParamUInt;
 
2461
        params[i].value.i    = 0;
 
2462
        strcpy (params[i].short_desc,
 
2463
                "Number of capture channels (defaults to hardware max)");
 
2464
        strcpy (params[i].long_desc, params[i].short_desc);
 
2465
 
 
2466
        i++;
 
2467
        strcpy (params[i].name, "outchannels");
 
2468
        params[i].character  = 'o';
 
2469
        params[i].type       = JackDriverParamUInt;
 
2470
        params[i].value.i    = 0;
 
2471
        strcpy (params[i].short_desc,
 
2472
                "Number of playback channels (defaults to hardware max)");
 
2473
        strcpy (params[i].long_desc, params[i].short_desc);
 
2474
 
 
2475
        i++;
 
2476
        strcpy (params[i].name, "shorts");
 
2477
        params[i].character  = 'S';
 
2478
        params[i].type       = JackDriverParamBool;
 
2479
        params[i].value.i    = FALSE;
 
2480
        strcpy (params[i].short_desc, "Try 16-bit samples before 32-bit");
 
2481
        strcpy (params[i].long_desc, params[i].short_desc);
 
2482
 
 
2483
 
 
2484
        i++;
 
2485
        strcpy (params[i].name, "input-latency");
 
2486
        params[i].character  = 'I';
 
2487
        params[i].type       = JackDriverParamUInt;
 
2488
        params[i].value.i    = 0;
 
2489
        strcpy (params[i].short_desc, "Extra input latency (frames)");
 
2490
        strcpy (params[i].long_desc, params[i].short_desc);
 
2491
 
 
2492
        i++;
 
2493
        strcpy (params[i].name, "output-latency");
 
2494
        params[i].character  = 'O';
 
2495
        params[i].type       = JackDriverParamUInt;
 
2496
        params[i].value.i    = 0;
 
2497
        strcpy (params[i].short_desc, "Extra output latency (frames)");
 
2498
        strcpy (params[i].long_desc, params[i].short_desc);
 
2499
 
 
2500
        i++;
 
2501
        strcpy (params[i].name, "midi");
 
2502
        params[i].character  = 'X';
 
2503
        params[i].type       = JackDriverParamString;
 
2504
        strcpy (params[i].value.str,  "none");
 
2505
        strcpy (params[i].short_desc, "ALSA MIDI driver (seq|raw)");
 
2506
        strcpy (params[i].long_desc,
 
2507
                "ALSA MIDI driver:\n"
 
2508
                " none - no MIDI driver\n"
 
2509
                " seq - ALSA Sequencer driver\n"
 
2510
                " raw - ALSA RawMIDI driver\n");
 
2511
 
 
2512
        desc->params = params;
 
2513
 
 
2514
        return desc;
 
2515
}
 
2516
 
 
2517
jack_driver_t *
 
2518
driver_initialize (jack_client_t *client, const JSList * params)
 
2519
{
 
2520
        jack_nframes_t srate = 48000;
 
2521
        jack_nframes_t frames_per_interrupt = 1024;
 
2522
        unsigned long user_nperiods = 2;
 
2523
        char *playback_pcm_name = "hw:0";
 
2524
        char *capture_pcm_name = "hw:0";
 
2525
        int hw_monitoring = FALSE;
 
2526
        int hw_metering = FALSE;
 
2527
        int capture = FALSE;
 
2528
        int playback = FALSE;
 
2529
        int soft_mode = FALSE;
 
2530
        int monitor = FALSE;
 
2531
        DitherAlgorithm dither = None;
 
2532
        int user_capture_nchnls = 0;
 
2533
        int user_playback_nchnls = 0;
 
2534
        int shorts_first = FALSE;
 
2535
        jack_nframes_t systemic_input_latency = 0;
 
2536
        jack_nframes_t systemic_output_latency = 0;
 
2537
        char *midi_driver_name = "none";
 
2538
        alsa_midi_t *midi = NULL;
 
2539
        const JSList * node;
 
2540
        const jack_driver_param_t * param;
 
2541
 
 
2542
        for (node = params; node; node = jack_slist_next (node)) {
 
2543
                param = (const jack_driver_param_t *) node->data;
 
2544
 
 
2545
                switch (param->character) {
 
2546
 
 
2547
                case 'C':
 
2548
                        capture = TRUE;
 
2549
                        if (strcmp (param->value.str, "none") != 0) {
 
2550
                                capture_pcm_name = strdup (param->value.str);
 
2551
                        }
 
2552
                        break;
 
2553
 
 
2554
                case 'P':
 
2555
                        playback = TRUE;
 
2556
                        if (strcmp (param->value.str, "none") != 0) {
 
2557
                                playback_pcm_name = strdup (param->value.str);
 
2558
                        }
 
2559
                        break;
 
2560
 
 
2561
                case 'D':
 
2562
                        playback = TRUE;
 
2563
                        capture = TRUE;
 
2564
                        break;
 
2565
 
 
2566
                case 'd':
 
2567
                        playback_pcm_name = strdup (param->value.str);
 
2568
                        capture_pcm_name  = strdup (param->value.str);
 
2569
                        break;
 
2570
 
 
2571
                case 'H':
 
2572
                        hw_monitoring = param->value.i;
 
2573
                        break;
 
2574
 
 
2575
                case 'm':
 
2576
                        monitor = param->value.i;
 
2577
                        break;
 
2578
 
 
2579
                case 'M':
 
2580
                        hw_metering = param->value.i;
 
2581
                        break;
 
2582
 
 
2583
                case 'r':
 
2584
                        srate = param->value.ui;
 
2585
                        jack_info ("apparent rate = %d", srate);
 
2586
                        break;
 
2587
                        
 
2588
                case 'p':
 
2589
                        frames_per_interrupt = param->value.ui;
 
2590
                        break;
 
2591
                                
 
2592
                case 'n':
 
2593
                        user_nperiods = param->value.ui;
 
2594
                        if (user_nperiods < 2)  /* enforce minimum value */
 
2595
                                user_nperiods = 2;
 
2596
                        break;
 
2597
                                
 
2598
                case 's':
 
2599
                        soft_mode = param->value.i;
 
2600
                        break;
 
2601
 
 
2602
                case 'z':
 
2603
                        if (dither_opt (param->value.c, &dither)) {
 
2604
                          return NULL;
 
2605
                        }
 
2606
                        break;
 
2607
 
 
2608
                case 'i':
 
2609
                        user_capture_nchnls = param->value.ui;
 
2610
                        break;
 
2611
                case 'o':
 
2612
                        user_playback_nchnls = param->value.ui;
 
2613
                        break;
 
2614
 
 
2615
                case 'S':
 
2616
                        shorts_first = param->value.i;
 
2617
                        break;
 
2618
 
 
2619
                case 'I':
 
2620
                        systemic_input_latency = param->value.ui;
 
2621
                        break;
 
2622
 
 
2623
                case 'O':
 
2624
                        systemic_output_latency = param->value.ui;
 
2625
                        break;
 
2626
 
 
2627
                case 'X':
 
2628
                        midi_driver_name = strdup (param->value.str);
 
2629
                        break;
 
2630
 
 
2631
                }
 
2632
        }
 
2633
                        
 
2634
        /* duplex is the default */
 
2635
        if (!capture && !playback) {
 
2636
                capture = TRUE;
 
2637
                playback = TRUE;
 
2638
        }
 
2639
 
 
2640
        if (strcmp(midi_driver_name, "seq")==0) {
 
2641
                midi = alsa_seqmidi_new(client, NULL);
 
2642
        } else if (strcmp(midi_driver_name, "raw")==0) {
 
2643
                midi = alsa_rawmidi_new(client);
 
2644
        }
 
2645
 
 
2646
        return alsa_driver_new ("alsa_pcm", playback_pcm_name,
 
2647
                                capture_pcm_name, client,
 
2648
                                frames_per_interrupt, 
 
2649
                                user_nperiods, srate, hw_monitoring,
 
2650
                                hw_metering, capture, playback, dither,
 
2651
                                soft_mode, monitor, 
 
2652
                                user_capture_nchnls, user_playback_nchnls,
 
2653
                                shorts_first, 
 
2654
                                systemic_input_latency,
 
2655
                                systemic_output_latency, midi);
 
2656
}
 
2657
 
 
2658
void
 
2659
driver_finish (jack_driver_t *driver)
 
2660
{
 
2661
        alsa_driver_delete ((alsa_driver_t *) driver);
 
2662
}