File: jack.c

package info (click to toggle)
xmms-jack 0.16-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,392 kB
  • ctags: 497
  • sloc: sh: 8,312; ansic: 2,517; makefile: 68
file content (640 lines) | stat: -rw-r--r-- 18,483 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*      xmms - jack output plugin
 *	Copyright 2002 Chris Morgan<cmorgan@alum.wpi.edu>
 *
 *	This code maps xmms calls into the jack translation library
 */

#ifdef FORBMP
#include <bmp/configfile.h>
#else
#include <xmms/configfile.h>
#endif
#include <dlfcn.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include "config.h"
#include "bio2jack.h" /* includes for the bio2jack library */
#include "jack.h"
#include "xconvert.h" /* xmms rate conversion header file */
#include <string.h>



/* set to 1 for verbose output */
#define VERBOSE_OUTPUT          0

jackconfig jack_cfg;

#define OUTFILE stderr

#define TRACE(...)                                      \
    if(jack_cfg.isTraceEnabled) {                       \
        fprintf(OUTFILE, "%s:", __FUNCTION__),          \
        fprintf(OUTFILE, __VA_ARGS__),				    \
        fflush(OUTFILE);                                \
    }

#define ERR(...)                                        \
    if(jack_cfg.isTraceEnabled) {                       \
        fprintf(OUTFILE, "ERR: %s:", __FUNCTION__),     \
        fprintf(OUTFILE, __VA_ARGS__),				    \
        fflush(OUTFILE);                                \
    }


static int driver = 0; /* handle to the jack output device */

typedef struct format_info {
  AFormat format;
  long    frequency;
  int     channels;
  long    bps;
} format_info_t;

static format_info_t input;
static format_info_t effect;
static format_info_t output;

static convert_freq_func_t freq_convert; /* rate convert function */
static struct xmms_convert_buffers *convertb; /* convert buffer */

#define MAKE_FUNCPTR(f) static typeof(f) * fp_##f = NULL;
MAKE_FUNCPTR(xmms_convert_buffers_new);
MAKE_FUNCPTR(xmms_convert_buffers_destroy);
MAKE_FUNCPTR(xmms_convert_get_frequency_func);
void *xmmslibhandle; /* handle to the dlopen'ed libxmms.so */

static int isXmmsFrequencyAvailable = 0;

#if HAVE_CTOR
void jack_destructor() __attribute__((destructor));
static gboolean output_opened; /* true if we have a connection to jack */
#endif /* #if HAVE_CTOR */

static GtkWidget *dialog, *button, *label;


#if HAVE_CTOR
void jack_destructor()
{
  int errval;
  TRACE("cleanup\n");

  if((errval = JACK_Close(driver)))
    ERR("error closing device, errval of %d\n", errval);

  /* only clean this up if we have the function to call */
  if(isXmmsFrequencyAvailable)
  {
    fp_xmms_convert_buffers_destroy(convertb); /* clean up the rate conversion buffers */
    dlclose(xmmslibhandle);
  }

  return;
}
#endif /* #if HAVE_CTOR */


void jack_sample_rate_error(void)
{
	dialog = gtk_dialog_new();
	gtk_window_set_title(GTK_WINDOW(dialog), ("Sample rate mismatch"));
	gtk_container_border_width(GTK_CONTAINER(dialog), 5);
	label = gtk_label_new((
		"Xmms is asking for a sample rate that differs from\n "
		"that of the jack server.  Xmms 1.2.8 or later\n"
		"contains resampling routines that xmms-jack will\n"
		"dynamically load and use to perform resampling.\n"
		"Or you can restart the jack server\n"
		"with a sample rate that matches the one that\n"
		"xmms desires.  -r is the option for the jack\n"
		"alsa driver so -r 44100 or -r 48000 should do\n\n"
		"Chris Morgan <cmorgan@alum.wpi.edu>\n"));

	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
	gtk_widget_show(label);

	button = gtk_button_new_with_label((" Close "));
	gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(dialog));
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
	gtk_widget_show(button);

	gtk_widget_show(dialog);
	gtk_widget_grab_focus(button);
}


/* Return the number of milliseconds of audio data that has been */
/* written out to the device */
gint jack_get_written_time(void)
{
  long return_val;
  return_val = JACK_GetPosition(driver, MILLISECONDS, WRITTEN);

  TRACE("returning %ld milliseconds\n", return_val);
  return return_val;
}


/* Return the current number of milliseconds of audio data that has */
/* been played out of the audio device, not including the buffer */
gint jack_get_output_time(void)
{
  gint return_val;
  
  /* don't try to get any values if the device is still closed */
  if(JACK_GetState(driver) == CLOSED)
    return_val = 0;
  else
    return_val = JACK_GetPosition(driver, MILLISECONDS, PLAYED); /* get played position in terms of milliseconds */

  TRACE("returning %d milliseconds\n", return_val);
  return return_val;
}


/* returns TRUE if we are currently playing */
/* NOTE: this was confusing at first BUT, if the device is open and there */
/* is no more audio to be played, then the device is NOT PLAYING */
gint jack_playing(void)
{
  gint return_val;

  /* If we are playing see if we ACTUALLY have something to play */
  if(JACK_GetState(driver) == PLAYING)
  {
    /* If we have zero bytes stored, we are done playing */
    if(JACK_GetBytesStored(driver) == 0)
      return_val = FALSE;
    else
      return_val = TRUE;
  }
  else
    return_val = FALSE;

  TRACE("returning %d\n", return_val);
  return return_val;
}


void jack_set_port_connection_mode()
{
  /* setup the port connection mode that determines how bio2jack will connect ports */
  enum JACK_PORT_CONNECTION_MODE mode;
  
  if(strcmp(jack_cfg.port_connection_mode, "CONNECT_ALL") == 0)
      mode = CONNECT_ALL;
  else if(strcmp(jack_cfg.port_connection_mode, "CONNECT_OUTPUT") == 0)
      mode = CONNECT_OUTPUT;
  else if(strcmp(jack_cfg.port_connection_mode, "CONNECT_NONE") == 0)
      mode = CONNECT_NONE;
  else
  {
      TRACE("Defaulting to CONNECT_ALL");
      mode = CONNECT_ALL;
  }
  JACK_SetPortConnectionMode(mode);
}

/* Initialize necessary things */
void jack_init(void)
{
  /* read the isTraceEnabled setting from the config file */
  ConfigFile *cfgfile;
  gchar *filename;

  #ifdef FORBMP
  filename = g_strconcat(g_get_home_dir(), "/.bmp/config", NULL);
  #else
  filename = g_strconcat(g_get_home_dir(), "/.xmms/config", NULL);
  #endif
  cfgfile = xmms_cfg_open_file(filename);
  if (!cfgfile)
  {
      jack_cfg.isTraceEnabled = FALSE;
      jack_cfg.port_connection_mode = "CONNECT_ALL"; /* default to connect all */
  } else
  {
      xmms_cfg_read_boolean(cfgfile, "jack", "isTraceEnabled", &jack_cfg.isTraceEnabled);
      if(!xmms_cfg_read_string(cfgfile, "jack", "port_connection_mode", &jack_cfg.port_connection_mode))
          jack_cfg.port_connection_mode = "CONNECT_ALL";
  }

  xmms_cfg_free(cfgfile);
  g_free(filename);


  TRACE("initializing\n");
  JACK_Init(); /* initialize the driver */

  /* set the bio2jack name so users will see xmms-jack in their */
  /* jack client list */
  JACK_SetClientName("xmms-jack");

  /* set the port connection mode */
  jack_set_port_connection_mode();

  xmmslibhandle = dlopen("libxmms.so", RTLD_NOW);
  if(xmmslibhandle)
  {
    fp_xmms_convert_buffers_new = dlsym(xmmslibhandle, "xmms_convert_buffers_new");
    fp_xmms_convert_buffers_destroy = dlsym(xmmslibhandle, "xmms_convert_buffers_destroy");
    fp_xmms_convert_get_frequency_func = dlsym(xmmslibhandle, "xmms_convert_get_frequency_func");

    if(!fp_xmms_convert_buffers_new)
    {
      TRACE("fp_xmms_convert_buffers_new couldn't be dlsym'ed\n");
      TRACE("dlerror: %s\n", dlerror());
    }

    if(!fp_xmms_convert_buffers_destroy)
    {
      TRACE("fp_xmms_convert_buffers_destroy couldn't be dlsym'ed\n");
      TRACE("dlerror: %s\n", dlerror());
    }

    if(!fp_xmms_convert_get_frequency_func)
    {
      TRACE("fp_xmms_get_frequency_func couldn't be dlsym'ed\n");
      TRACE("dlerror: %s\n", dlerror());
    }

    if(!fp_xmms_convert_buffers_new || !fp_xmms_convert_buffers_destroy ||
       !fp_xmms_convert_get_frequency_func)
    {
      dlclose(xmmslibhandle); /* close the library, no need to keep it open */
      TRACE("One or more frequency convertion functions are missing, upgrade to xmms >=1.2.8\n");
    } else
    {
      TRACE("Found frequency convertion functions, setting isXmmsFrequencyAvailable to 1\n");
      isXmmsFrequencyAvailable = 1;
    }
  } else
  {
    TRACE("unable to dlopen '%s'\n", "libxmms.so");
  }

  /* only initialize this stuff if we have the functions available */
  if(isXmmsFrequencyAvailable)
  {
    convertb = fp_xmms_convert_buffers_new ();
    freq_convert = fp_xmms_convert_get_frequency_func(FMT_S16_LE, 2);
  }

#if HAVE_CTOR
  output_opened = FALSE;
#endif /* #if HAVE_CTOR */
}


/* Return the amount of data that can be written to the device */
gint jack_free(void)
{
  unsigned long return_val = JACK_GetBytesFreeSpace(driver);
  unsigned long tmp;

  /* adjust for frequency differences, otherwise xmms could send us */
  /* as much data as we have free, then we go to convert this to */
  /* the output frequency and won't have enough space, so adjust */
  /* by the ratio of the two */
  if(effect.frequency != output.frequency)
  {
    tmp = return_val;
    return_val = (return_val * effect.frequency) / output.frequency;
    TRACE("adjusting from %ld to %ld free bytes to compensate for frequency differences\n", tmp, return_val);
  }

  if(return_val > G_MAXINT)
  {
      TRACE("Warning: return_val > G_MAXINT\n");
      return_val = G_MAXINT;
  }

  TRACE("free space of %ld bytes\n", return_val);

  return return_val;
}


/* Close the device */
void jack_close(void)
{
#if !HAVE_CTOR
  int errval;
#endif

  TRACE("\n");

#if HAVE_CTOR
  JACK_Reset(driver); /* flush buffers, reset position and set state to STOPPED */
  TRACE("resetting driver, not closing now, destructor will close for us\n");
#else
  TRACE("calling JACK_Close()\n");
  if((errval = JACK_Close(driver)))
  {
    ERR("error closing device, errval of %d\n", errval);
  }
#endif /* #if HAVE_CTOR */
}


/* Open the device up */
gint jack_open(AFormat fmt, gint sample_rate, gint num_channels)
{
  int bits_per_sample;
  int retval;
  unsigned long rate;

  TRACE("fmt == %d, sample_rate == %d, num_channels == %d\n",
	fmt, sample_rate, num_channels);

  if((fmt == FMT_U8) || (fmt == FMT_S8))
  {
    bits_per_sample = 8;
  } else
  {
    bits_per_sample = 16;
  }

  /* record some useful information */
  input.format    = fmt;
  input.frequency = sample_rate;
  input.bps       = bits_per_sample * sample_rate * num_channels;
  input.channels  = num_channels;

  /* setup the effect as matching the input format */
  effect.format    = input.format;
  effect.frequency = input.frequency;
  effect.channels  = input.channels;
  effect.bps       = input.bps;

#if HAVE_CTOR
  /* if we are already opened then don't open again */
  if(output_opened)
  {
    /* if something has changed we should close and re-open the connect to jack */
    if((output.channels != input.channels) ||
       (output.frequency != input.frequency) ||
       (output.format != input.format))
    {
      TRACE("output.channels is %d, jack_open called with %d channels\n", output.channels, input.channels);
      TRACE("output.frequency is %ld, jack_open called with %ld\n", output.frequency, input.frequency);
      TRACE("output.format is %d, jack_open called with %d\n", output.format, input.format);
      JACK_Close(driver);
    } else
    {
        TRACE("output_opened is TRUE and no options changed, not reopening\n");
        return 1;
    }
  }
#endif /* #if HAVE_CTOR */

  /* try to open the jack device with the requested rate at first */
  output.frequency = input.frequency;
  output.bps       = input.bps;
  output.channels  = input.channels;
  output.format    = input.format;

  rate = output.frequency;
  retval = JACK_Open(&driver, bits_per_sample, &rate, output.channels);
  output.frequency = rate; /* avoid compile warning as output.frequency differs in type
                              from what JACK_Open() wants for the type of the rate parameter */
  if((retval == ERR_RATE_MISMATCH) && isXmmsFrequencyAvailable)
  {
    TRACE("xmms(input) wants rate of '%ld', jacks rate(output) is '%ld', opening at jack rate\n", input.frequency, output.frequency);

    /* open the jack device with true jack's rate, return 0 upon failure */
    retval = JACK_Open(&driver, bits_per_sample, &rate, output.channels);
    output.frequency = rate; /* avoid compile warning as output.frequency differs in type
                                from what JACK_Open() wants for the type of the rate parameter */
    if(retval)
    {
      TRACE("failed to open jack with JACK_Open(), error %d\n", retval);
      return 0;
    }
    TRACE("success!!\n");
  } else if((retval == ERR_RATE_MISMATCH) && !isXmmsFrequencyAvailable)
  {
    TRACE("JACK_Open(), sample rate mismatch with no resampling routines available\n");

    jack_sample_rate_error(); /* notify the user that we can't resample */

    return 0;
  } else if(retval != ERR_SUCCESS)
  {
    TRACE("failed to open jack with JACK_Open(), error %d\n", retval);
    return 0;
  }

#if HAVE_CTOR
  output_opened = TRUE;
#endif

  return 1;
}


/* write some audio out to the device */
void jack_write(gpointer ptr, gint length)
{
  long written;
  EffectPlugin *ep;
  AFormat new_format;
  int new_frequency, new_channels;
  long positionMS;

  TRACE("starting length of %d\n", length);

  /* copy the current values into temporary values */
  new_format = input.format;
  new_frequency = input.frequency;
  new_channels = input.channels;

  /* query xmms for the current plugin */
  ep = get_current_effect_plugin();
  if(effects_enabled() && ep && ep->query_format)
  {
    ep->query_format(&new_format, &new_frequency, &new_channels);
  }

  /* if the format has changed take this into account by modifying */
  /* the time offset and reopening the device with the new format settings */
  if (new_format != effect.format ||
      new_frequency != effect.frequency ||
      new_channels != effect.channels)
  {
    TRACE("format changed, storing new values and opening/closing jack\n");
    TRACE("effect.format == %d, new_format == %d, effect.frequency == %ld, new_frequency == %d, effect.channels == %d, new_channels = %d\n",
	  effect.format, new_format, effect.frequency, new_frequency, effect.channels, new_channels);
 
    positionMS = JACK_GetPosition(driver, MILLISECONDS, PLAYED);

    jack_close();
    jack_open(new_format, new_frequency, new_channels);
 
    /* restore the position after the open and close */
    JACK_SetState(driver, PAUSED);
    JACK_SetPosition(driver, MILLISECONDS, positionMS);
    JACK_SetState(driver, PLAYING);
  }

  /* if effects are enabled and we have a plugin, run the current */
  /* samples through the plugin */
  if (effects_enabled() && ep && ep->mod_samples)
  {
    length = ep->mod_samples(&ptr, length,
                             input.format,
                             input.frequency,
                             input.channels);
    TRACE("effects_enabled(), length is now %d\n", length);
  }

  TRACE("effect.frequency == %ld, input.frequency == %ld, output.frequency == %ld\n",
        effect.frequency, input.frequency, output.frequency);

  /* if we need rate conversion, perform it here */
  if((effect.frequency != output.frequency) && isXmmsFrequencyAvailable)
  {
    TRACE("performing rate conversion from '%ld'(effect) to '%ld'(output)\n", effect.frequency, output.frequency);
    length = freq_convert (convertb, &ptr, length, effect.frequency, output.frequency);
  }

  TRACE("length = %d\n", length);
  /* loop until we have written all the data out to the jack device */
  /* this is due to xmms' audio driver api */
  char *buf = (char*)ptr;
  while(length > 0)
  {
    TRACE("writing %d bytes\n", length);
    written = JACK_Write(driver, (unsigned char*)buf, length);
    length-=written;
    buf+=written;
  }
  TRACE("finished\n");
}


/* Flush any output currently buffered */
/* and set the number of bytes written based on ms_offset_time, */
/* the number of milliseconds of offset passed in */
/* This is done so the driver itself keeps track of */
/* current playing position of the mp3 */
void jack_flush(gint ms_offset_time)
{
  TRACE("setting values for ms_offset_time of %d\n", ms_offset_time);

  JACK_Reset(driver); /* flush buffers and set state to STOPPED */

  /* update the internal driver values to correspond to the input time given */
  JACK_SetPosition(driver, MILLISECONDS, ms_offset_time);

  JACK_SetState(driver, PLAYING);
}


/* Pause the jack device */
void jack_pause(short p)
{
  TRACE("p == %d\n", p);

  /* pause the device if p is non-zero, unpause the device if p is zero and */
  /* we are currently paused */
  if(p)
    JACK_SetState(driver, PAUSED);
  else if(JACK_GetState(driver) == PAUSED)
    JACK_SetState(driver, PLAYING);
}


/* Set the volume */
void jack_set_volume(int l, int r)
{
  if(output.channels == 1)
  {
    TRACE("l(%d)\n", l);
  } else if(output.channels > 1)
  {
    TRACE("l(%d), r(%d)\n", l, r);
  }

  if(output.channels > 0)
      JACK_SetVolumeForChannel(driver, 0, l);
  if(output.channels > 1)
      JACK_SetVolumeForChannel(driver, 1, r);
}


/* Get the current volume setting */
void jack_get_volume(int *l, int *r)
{
  unsigned int _l, _r;

  if(output.channels > 0)
  {
      JACK_GetVolumeForChannel(driver, 0, &_l);
      (*l) = _l;
  }
  if(output.channels > 1)
  {
      JACK_GetVolumeForChannel(driver, 1, &_r);
      (*r) = _r;
  }

#if VERBOSE_OUTPUT
  if(output.channels == 1)
      TRACE("l(%d)\n", *l);
  else if(output.channels > 1)
      TRACE("l(%d), r(%d)\n", *l, *r);
#endif
}


void jack_about(void)
{
	dialog = gtk_dialog_new();
	gtk_window_set_title(GTK_WINDOW(dialog), ("About jack Driver 0.16"));
	gtk_container_border_width(GTK_CONTAINER(dialog), 5);
	label = gtk_label_new((
		"XMMS jack Driver 0.16\n\n "
		"xmms-jack.sf.net\n"
		"Chris Morgan<cmorgan@alum.wpi.edu>\n"));

	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
	gtk_widget_show(label);

	button = gtk_button_new_with_label((" Close "));
	gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(dialog));
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
	gtk_widget_show(button);

	gtk_widget_show(dialog);
	gtk_widget_grab_focus(button);
}


OutputPlugin jack_op =
{
	NULL,
	NULL,
	"jack Driver " VERSION,
	jack_init,
	jack_about,
	jack_configure,
	jack_get_volume,
	jack_set_volume,
	jack_open,
	jack_write,
	jack_close,
	jack_flush,
	jack_pause,
	jack_free,
	jack_playing,
	jack_get_output_time,
	jack_get_written_time,
};


OutputPlugin *get_oplugin_info(void)
{
	return &jack_op;
}