aboutsummaryrefslogtreecommitdiff
path: root/xmms.c
blob: ef17659d9ea55f3c86951a3e382138551d81f7eb (plain)
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
/**
 * [xmms] is a Pure Data object to control the X Multimedia System, an audio player.
 *
 * @url http://alexandre.quessy.net/
 * @author Alexandre Quessy <alexandre@quessy.net>
 * @license GNU General Public License 2006
 * thanks to Andy Gimblett for code examples
 */
/*
To know which flags to use for compilation: 
`xmms-config --cflags --libs`

On Debian, you need libxmms and xmms-festalon if you want to read nsf files.
*/

//#define _GNU_SOURCE

/* PD includes */
#include "m_pd.h"
#include <math.h>
// #include <string.h>
#include <stdlib.h>
// #include <stdio.h>

/* XMMS includes */
#include <xmms/xmmsctrl.h>
// #include <glib.h>
/* local constants */
#define PDXMMS_DEFAULTSKIP 5000


//need following declarations so internal XMMS functions can be called
//(ripped from XMMS source)
//from xmms.c
//gboolean playlist_load(gchar * inpipefile);


/** variables of the pd object */
typedef struct xmms {
  t_object x_ob; /* The instance. Contains inlets and outlets */
  /* xmms session. 0 is the first one when we start it */
  int session;
  //t_outlet *x_outlet;
} t_xmms;



void xmms_prev(t_xmms *x, t_symbol *s, int argc, t_atom *argv) {
  xmms_remote_playlist_prev(x->session);
}

void xmms_next(t_xmms *x, t_symbol *s, int argc, t_atom *argv) {
  xmms_remote_playlist_next(x->session);
}

void xmms_play(t_xmms *x, t_symbol *s, int argc, t_atom *argv) {
  xmms_remote_play(x->session);
}

void xmms_pause(t_xmms *x, t_symbol *s, int argc, t_atom *argv) {
  xmms_remote_pause(x->session);
}

void xmms_stop(t_xmms *x, t_symbol *s, int argc, t_atom *argv) {
  xmms_remote_stop(x->session);
}

void xmms_volume(t_xmms *x, t_symbol *s, int argc, t_atom *argv) {
  float f;
  int l, r;
  if (argc >= 1) {
    if (argv[0].a_type == A_FLOAT) { 
      f = (float) atom_getfloatarg(0, argc, argv);
      l = (int) f;
      //if (mute_flag) mute_flag = 0;
      if (l > 100) l = 100;
      else if (l < 0) l = 0;
      //if (r > 100) r = 100;
      //else if (r < 0) r = 0;
      r = f;
      xmms_remote_set_volume(x->session, l, r);
    }
  }
}


/* skipf and skipb */
void xmms_forward(t_xmms *x, t_symbol *s, int argc, t_atom *argv) {
  int time = xmms_remote_get_output_time(x->session);
  double skip = PDXMMS_DEFAULTSKIP;
  
  if (argc >= 1) {
    if (argv[0].a_type == A_FLOAT) { 
      skip = (double) atom_getfloatarg(0, argc, argv);
    }
  }
  xmms_remote_jump_to_time(x->session, (int) rint(time + skip));
}

void xmms_backward(t_xmms *x, t_symbol *s, int argc, t_atom *argv) {
  int time = xmms_remote_get_output_time(x->session);
  double skip = PDXMMS_DEFAULTSKIP;
  
  if (argc >= 1) {
    if (argv[0].a_type == A_FLOAT) { 
      skip = (double) atom_getfloatarg(0, argc, argv);
    }
  }
  xmms_remote_jump_to_time(x->session, (int) rint(time - skip));
}

/*
void xmms_load0(t_xmms *x, t_symbol *s, int argc, t_atom *argv) {
  if (argc >= 1) { 
    if (argv[0].a_type == A_SYMBOL) {
      t_symbol *tmp = atom_getsymbol(&argv[0]);
      playlist_load(tmp->s_name);
    }
  }
}
  */
/** The class */
t_class *xmms_class;

/** constructor */
void *xmms_new(t_symbol *selector, int argc, t_atom *argv) {
  t_xmms *x = (t_xmms *) pd_new(xmms_class);
  //x->x_outlet = outlet_new(&x->x_ob, gensym("symbol"));
  x->session = 0;
  // if (!xmms_remote_is_running(x->session)) return 0;
  return (void *)x;
}

/** setup */
void xmms_setup(void) {
  xmms_class = class_new(gensym("xmms"), (t_newmethod) xmms_new, 0, sizeof(t_xmms), 0, A_GIMME, 0);
  
  class_addmethod(xmms_class, (t_method)xmms_stop, gensym("stop"), A_GIMME, 0);
  class_addmethod(xmms_class, (t_method)xmms_play, gensym("play"), A_GIMME, 0);
  class_addmethod(xmms_class, (t_method)xmms_pause, gensym("pause"), A_GIMME, 0);
  class_addmethod(xmms_class, (t_method)xmms_next, gensym("next"), A_GIMME, 0);
  class_addmethod(xmms_class, (t_method)xmms_prev, gensym("prev"), A_GIMME, 0);
  class_addmethod(xmms_class, (t_method)xmms_forward, gensym("forward"), A_GIMME, 0);
  class_addmethod(xmms_class, (t_method)xmms_backward, gensym("backward"), A_GIMME, 0);
  class_addmethod(xmms_class, (t_method)xmms_volume, gensym("volume"), A_GIMME, 0);
  //class_addmethod(xmms_class, (t_method)xmms_load0, gensym("load"), A_GIMME, 0);
}