aboutsummaryrefslogtreecommitdiff
path: root/cdplayer.c
blob: 4622abfe0c06c2896be752ef3e31c5ae949d15ba (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
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
#ifdef 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/cdrom.h>
#include <errno.h>
#include <string.h>



/* -------------------------- cdplayer ------------------------------ */
static t_class *cdplayer_class;

typedef struct _cdplayer
{
    t_object x_obj;
    t_symbol* device;
} t_cdplayer;

static void *cdplayer_new()
{
    char* devicename;
    int fd;
    t_cdplayer *x = (t_cdplayer *)pd_new(cdplayer_class);

    outlet_new(&x->x_obj, &s_bang);
    sprintf(devicename,"/dev/cdrom");
    x->device = gensym(devicename);
/*
    fd = open(x->device->s_name, O_RDONLY);
    if (fd < 0){
       post ("cdplayer: could not open %s",x->device->s_name);
    } else close(fd);
*/
    return (x);
}

static void cdplayer_play(t_cdplayer *x, t_floatarg f)
{
    struct cdrom_tochdr header;
    struct cdrom_ti index;
    int cdrom;
    int childpid;
    int maxTrack;
    int t = (int)f;

    childpid = fork();
    if (childpid < 0) {
        error ("cdplayer: could not fork!");
    }
    if (!childpid){
       cdrom = open(x->device->s_name,O_RDONLY);    // Open device
       if (cdrom > -1){
          ioctl(cdrom,CDROMREADTOCHDR,(void *) &header); // Get start and end tracks
          maxTrack = header.cdth_trk1;
          if (t < 1){
            post ("track number must be 0 or higher");
          }
          if (t > maxTrack){
            post ("track number too high");
          }
           
          index.cdti_trk0=t;   // Set first track
          index.cdti_ind0=0;                  // Start of track
    
          index.cdti_trk1=t;   // Set final track
          index.cdti_ind1=99;                // End of track
      
          ioctl(cdrom,CDROMPLAYTRKIND,(void *) &index); // Play the tracks
          exit (0);
       }else{
         error ("cdplayer: could not open %s",x->device->s_name);
       }
       close (cdrom);
    }
}

static void cdplayer_pause(t_cdplayer *x)
{
    int cdrom;
    int childpid;
    childpid = fork();
    if (childpid < 0) {
       error ("cdplayer: could not fork!");
    }
    if (!childpid){
       cdrom = open(x->device->s_name,O_RDONLY);
       if (cdrom > -1){
         ioctl(cdrom,CDROMPAUSE,0);
         close (cdrom);
         exit (0);
       }else{
         error ("cdplayer: could not open %s",x->device->s_name);
       }
    }
}

static void cdplayer_resume(t_cdplayer *x)
{                         
    int cdrom;
    int childpid;
    childpid = fork();
    if (childpid < 0) {
       error ("cdplayer: could not fork!");
    }
    if (!childpid){
       cdrom = open(x->device->s_name,O_RDONLY);
       if (cdrom > -1){
         ioctl(cdrom,CDROMRESUME,0);
         close (cdrom);
         exit (0);
       }else{
         error ("cdplayer: could not open %s",x->device->s_name);
       }
    }
}

static void cdplayer_stop(t_cdplayer *x)
{
    int cdrom;
    int childpid;
    childpid = fork();
    if (childpid < 0) {
       error ("cdplayer: could not fork!");
    }
    if (!childpid){
       cdrom = open(x->device->s_name,O_RDONLY);
       if (cdrom > -1){
         ioctl(cdrom,CDROMSTOP,0);
         close (cdrom);
         exit (0);
       }else{
         error ("cdplayer: could not open %s",x->device->s_name);
       }
    }
}

static void cdplayer_eject(t_cdplayer *x)
{
    int cdrom;
    int childpid;
    childpid = fork();
    if (childpid < 0) {
       error ("cdplayer: could not fork!");
    }
    if (!childpid){
       cdrom = open(x->device->s_name,O_RDONLY);
       if (cdrom > -1){
         ioctl(cdrom,CDROMEJECT,0);
         close (cdrom);
         exit (0);
       }else{
         error ("cdplayer: could not open %s",x->device->s_name);
       }
    }
}

static void cdplayer_float(t_cdplayer *x, t_float f)
{
    cdplayer_play(x,f);
}


void cdplayer_setup(void)
{
    cdplayer_class = class_new(gensym("cdplayer"), (t_newmethod)cdplayer_new, 0, sizeof(t_cdplayer), 0, 0);
    class_addfloat(cdplayer_class, cdplayer_float);
    class_addmethod(cdplayer_class, (t_method) cdplayer_play, gensym("play"), A_DEFFLOAT,0);
    class_addmethod(cdplayer_class, (t_method) cdplayer_pause, gensym("pause"), 0);
    class_addmethod(cdplayer_class, (t_method) cdplayer_resume, gensym("resume"), 0);
    class_addmethod(cdplayer_class, (t_method) cdplayer_stop, gensym("stop"), 0);
    class_addmethod(cdplayer_class, (t_method) cdplayer_eject, gensym("eject"), 0);
}

#endif