File: ossmixer.c

package info (click to toggle)
pd-ext13 0.17.1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 392 kB
  • ctags: 638
  • sloc: ansic: 3,618; makefile: 251
file content (333 lines) | stat: -rw-r--r-- 10,765 bytes parent folder | download | duplicates (3)
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

#ifdef __gnu_linux__

#include "ext13.h"
#include "m_pd.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>
#include <errno.h>
#include <string.h>

#ifndef SOUND_MIXER_READ
# define SOUND_MIXER_READ(x) MIXER_READ(x)
#endif
#ifndef SOUND_MIXER_WRITE
# define SOUND_MIXER_WRITE(x) MIXER_WRITE(x)
#endif


/* -------------------------- ossmixer ------------------------------ */
static t_class *ossmixer_class;

typedef struct _ossmixer
{
    t_object x_obj;
    int channel;
    t_symbol* channel_name;
    t_symbol* device;
    t_outlet *x_out1;
} t_ossmixer;

static void *ossmixer_new(t_floatarg f)
{
    char devicename[FILENAME_MAX];
    int fd = -1;
    t_ossmixer *x = (t_ossmixer *)pd_new(ossmixer_class);
    outlet_new(&x->x_obj, &s_bang);
    x->x_out1 = outlet_new(&x->x_obj, &s_symbol);
    sprintf(devicename,"/dev/mixer%d",(int)f);
//    x->device = gensym(devicename);
    x->device = gensym ("/dev/mixer");
    fd = open(x->device->s_name, O_WRONLY);
    if (fd < 0){
       post ("ossmixer: could not open %s",x->device->s_name);
       x->device = gensym("/dev/mixer");
       close (fd);
    }else{
       post ("ossmixer: device set to %s",x->device->s_name);
    }
    close(fd);
    return (x);
}

static void ossmixer_set_device(t_ossmixer *x, t_floatarg f)
{
   char devicename[FILENAME_MAX];
   int fd = -1;
   sprintf(devicename,"/dev/mixer%d",(int)f);
   x->device = gensym(devicename);
   fd = open(x->device->s_name, O_WRONLY);
   if (fd < 0){
     post ("ossmixer: could not open %s",x->device->s_name);
     x->device = gensym("/dev/mixer");
     close (fd);
   }else{
     post ("ossmixer: device set to %s",devicename);
   }
}

static void ossmixer_bang(t_ossmixer *x)
{
  post ("ossmixer: what should a mixer do with a bang?");

}

static void ossmixer_get(t_ossmixer *x, t_symbol* s)
{
  int vol = -1;
  x->channel = -1;
  if (!strncmp(s->s_name,"main",4))
        x->channel = (int) SOUND_MIXER_VOLUME;
  if (!strncmp(s->s_name,"treble",6))
        x->channel = (int) SOUND_MIXER_TREBLE;
  if (!strncmp(s->s_name,"bass",4))
        x->channel = (int) SOUND_MIXER_BASS;
  if (!strncmp(s->s_name,"synth",5))
        x->channel = (int) SOUND_MIXER_SYNTH;
  if (!strncmp(s->s_name,"pcm",3))
        x->channel = (int) SOUND_MIXER_PCM;
  if (!strncmp(s->s_name,"speaker",7))
        x->channel = (int) SOUND_MIXER_SPEAKER;
  if (!strncmp(s->s_name,"line",4))
        x->channel = (int) SOUND_MIXER_LINE;
  if (!strncmp(s->s_name,"line1",5))
        x->channel = (int) SOUND_MIXER_LINE1;
  if (!strncmp(s->s_name,"line2",5))
        x->channel = (int) SOUND_MIXER_LINE2;
  if (!strncmp(s->s_name,"line3",5))
        x->channel = (int) SOUND_MIXER_LINE3;
  if (!strncmp(s->s_name,"mic",3))
        x->channel = (int) SOUND_MIXER_MIC;
  if (!strncmp(s->s_name,"cd",2))
        x->channel = (int) SOUND_MIXER_CD;
  if (!strncmp(s->s_name,"imix",4))
        x->channel = (int) SOUND_MIXER_IMIX;
  if (!strncmp(s->s_name,"altpcm",6))
         x->channel = (int) SOUND_MIXER_ALTPCM;
  if (!strncmp(s->s_name,"reclev",6))
         x->channel = (int) SOUND_MIXER_RECLEV;
  if (!strncmp(s->s_name,"reclevel",8))
         x->channel = (int) SOUND_MIXER_RECLEV;
  if (!strncmp(s->s_name,"igain",5))
         x->channel = (int) SOUND_MIXER_IGAIN;
  if (!strncmp(s->s_name,"ogain",5))
         x->channel = (int) SOUND_MIXER_OGAIN;

/*
braucht das wer?
         #define SOUND_MIXER_DIGITAL1    17  
         #define SOUND_MIXER_DIGITAL2    18 
         #define SOUND_MIXER_DIGITAL3    19  
         #define SOUND_MIXER_PHONEIN     20  
         #define SOUND_MIXER_PHONEOUT    21    
         #define SOUND_MIXER_VIDEO       22  
         #define SOUND_MIXER_RADIO       23 
         #define SOUND_MIXER_MONITOR     24 
*/         


  if (x->channel > -1){
    int fd = -1;
/*    fd = open("/dev/mixer0", O_RDONLY);*/
    fd = open(x->device->s_name, O_RDONLY);
    if (fd > 0){
      int vol = 50;
      if (ioctl(fd, SOUND_MIXER_READ( x->channel ), &vol)==-1){
        post("ossmixer: undefined mixer channel");
      }else{
        x->channel_name = s;
        vol = vol & 255;
        outlet_symbol (x->x_out1,s);
        outlet_float(x->x_obj.ob_outlet,(t_float)vol);
      }
      close(fd);
    }else{
      post ("ossmixer: could not open device %s",x->device->s_name);
    }
  }
}

static void ossmixer_set(t_ossmixer *x, t_symbol* s, t_float f)
{
  int vol = (int) f;
  if (vol < 0 ){
    post ("ossmixer: minimum volume: 0");
    vol = 0;
  }
  if (vol > 100 ){
    post ("ossmixer: maximum volume: 100");
    vol = 100;
  }
  x->channel = -1;
  
  if (!strncmp(s->s_name,"main",4))
          x->channel = (int) SOUND_MIXER_VOLUME;
  if (!strncmp(s->s_name,"treble",6))
          x->channel = (int) SOUND_MIXER_TREBLE;
  if (!strncmp(s->s_name,"bass",4))
          x->channel = (int) SOUND_MIXER_BASS;
  if (!strncmp(s->s_name,"synth",5))
          x->channel = (int) SOUND_MIXER_SYNTH;
  if (!strncmp(s->s_name,"pcm",3))
          x->channel = (int) SOUND_MIXER_PCM;
  if (!strncmp(s->s_name,"speaker",7))
          x->channel = (int) SOUND_MIXER_SPEAKER;
  if (!strncmp(s->s_name,"line",4))
          x->channel = (int) SOUND_MIXER_LINE;
  if (!strncmp(s->s_name,"line1",5))
          x->channel = (int) SOUND_MIXER_LINE1;
  if (!strncmp(s->s_name,"line2",5))
          x->channel = (int) SOUND_MIXER_LINE2;
  if (!strncmp(s->s_name,"line3",5))
          x->channel = (int) SOUND_MIXER_LINE3;
  if (!strncmp(s->s_name,"mic",3))
          x->channel = (int) SOUND_MIXER_MIC;
  if (!strncmp(s->s_name,"cd",2))
          x->channel = (int) SOUND_MIXER_CD;
  if (!strncmp(s->s_name,"imix",4))
          x->channel = (int) SOUND_MIXER_IMIX;
  if (!strncmp(s->s_name,"altpcm",6))
           x->channel = (int) SOUND_MIXER_ALTPCM;
  if (!strncmp(s->s_name,"reclev",6))
           x->channel = (int) SOUND_MIXER_RECLEV;
  if (!strncmp(s->s_name,"reclevel",8))
           x->channel = (int) SOUND_MIXER_RECLEV;
  if (!strncmp(s->s_name,"igain",5))
           x->channel = (int) SOUND_MIXER_IGAIN;
  if (!strncmp(s->s_name,"ogain",5))
           x->channel = (int) SOUND_MIXER_OGAIN;



  if (x->channel > -1){
    int fd = -1;
    fd = open(x->device->s_name, O_WRONLY);
    if (fd > 0){
      vol =  vol | (vol << 8);
      if (ioctl(fd, SOUND_MIXER_WRITE( x->channel ), &vol)==-1){
        post("ossmixer: undefined mixer channel");
      }else{
         x->channel_name = s;
         vol &= 255;
         outlet_symbol (x->x_out1,s);
         outlet_float(x->x_obj.ob_outlet,(t_float)vol);
      }
      close (fd);
    }else{
      post ("ossmixer: could not open device %s",x->device->s_name);
    }
  }
}
static void ossmixer_get_source(t_ossmixer *x)
{
    int fd = -1;
    int channel = -1;
    fd = open(x->device->s_name, O_WRONLY);
    if (fd > 0){
      if ( ioctl(fd, SOUND_MIXER_READ_RECSRC, &channel) ){
         post ("ossmixer: could not get recording source");
      }else{
         t_symbol* s_ch = gensym("no_source_found");
         if (channel & SOUND_MASK_VOLUME) s_ch = gensym("main");
         if (channel & SOUND_MASK_PCM) s_ch = gensym("pcm");
         if (channel & SOUND_MASK_MIC) s_ch = gensym("mic");
         if (channel & SOUND_MASK_CD) s_ch = gensym("cd");
         if (channel & SOUND_MASK_SYNTH) s_ch = gensym("synth");
         if (channel & SOUND_MASK_LINE) s_ch = gensym("line");
         if (channel & SOUND_MASK_LINE1) s_ch = gensym("line1");
         if (channel & SOUND_MASK_LINE2) s_ch = gensym("line2");
         if (channel & SOUND_MASK_LINE3) s_ch = gensym("line3");
         if (channel & SOUND_MASK_ALTPCM) s_ch = gensym("altpcm");

         outlet_symbol (x->x_out1,s_ch);
         outlet_symbol (x->x_obj.ob_outlet,gensym("source"));
      }
      close (fd);
    }else{
       post ("ossmixer: could not open mixer device");
    }

}

static void ossmixer_set_source(t_ossmixer *x, t_symbol* s)
{
  int channel = -1;
  if (!strncmp(s->s_name,"main",4))
            channel = (int) SOUND_MASK_VOLUME;
  if (!strncmp(s->s_name,"treble",6))
            channel = (int) SOUND_MASK_TREBLE;
  if (!strncmp(s->s_name,"bass",4))
            channel = (int) SOUND_MASK_BASS;
  if (!strncmp(s->s_name,"synth",5))
            channel = (int) SOUND_MASK_SYNTH;
  if (!strncmp(s->s_name,"pcm",3))
            channel = (int) SOUND_MASK_PCM;
  if (!strncmp(s->s_name,"speaker",7))
            channel = (int) SOUND_MASK_SPEAKER;
  if (!strncmp(s->s_name,"line",4))
            channel = (int) SOUND_MASK_LINE;
  if (!strncmp(s->s_name,"line1",5))
            channel = (int) SOUND_MASK_LINE1;
  if (!strncmp(s->s_name,"line2",5))
            channel = (int) SOUND_MASK_LINE2;
  if (!strncmp(s->s_name,"line3",5))
            channel = (int) SOUND_MASK_LINE3;
  if (!strncmp(s->s_name,"mic",3))
            channel = (int) SOUND_MASK_MIC;
  if (!strncmp(s->s_name,"cd",2))
            channel = (int) SOUND_MASK_CD;
  if (!strncmp(s->s_name,"imix",4))
            channel = (int) SOUND_MASK_IMIX;
  if (!strncmp(s->s_name,"altpcm",6))
            channel = (int) SOUND_MASK_ALTPCM;
  if (!strncmp(s->s_name,"reclev",6))
            channel = (int) SOUND_MASK_RECLEV;
  if (!strncmp(s->s_name,"reclevel",8))
            channel = (int) SOUND_MASK_RECLEV;
  if (!strncmp(s->s_name,"igain",5))
            channel = (int) SOUND_MASK_IGAIN;
  if (!strncmp(s->s_name,"ogain",5))
            channel = (int) SOUND_MASK_OGAIN;

  if(channel > -1){
    int fd = -1;
    fd = open(x->device->s_name, O_WRONLY);
    if (fd > 0){
      if ( ioctl(fd, SOUND_MIXER_WRITE_RECSRC, &channel) ){
         post ("ossmixer: could not set recordiing source");
      }else{
        ossmixer_get_source(x);
      }
      close (fd);
    }else{
      post ("ossmixer: could not open mixer device %s",x->device->s_name);
    }
  }else{
    post ("ossmixer: channel unknown");
  }
}

static void ossmixer_float(t_ossmixer *x, t_float f)
{
  ossmixer_set (x, x->channel_name, f);
}


void ossmixer_setup(void)
{
    ossmixer_class = class_new(gensym("ossmixer"), (t_newmethod)ossmixer_new, 0, sizeof(t_ossmixer), 0, A_DEFFLOAT, 0);
    class_addbang(ossmixer_class, ossmixer_bang);
    class_addfloat(ossmixer_class, ossmixer_float);
    class_addmethod(ossmixer_class, (t_method) ossmixer_get, gensym("get"), A_DEFSYM, 0);
    class_addmethod(ossmixer_class, (t_method) ossmixer_set, gensym("set"), A_DEFSYM, A_DEFFLOAT, 0);
    class_addmethod(ossmixer_class, (t_method) ossmixer_get_source, gensym("get_source"), A_DEFSYM, 0);
    class_addmethod(ossmixer_class, (t_method) ossmixer_set_source, gensym("set_source"), A_DEFSYM, 0);
    class_addmethod(ossmixer_class, (t_method) ossmixer_set_device, gensym("set_device"), A_DEFFLOAT, 0);
}


#endif