]> icculus.org git repositories - btb/d2x.git/blob - arch/sdl/rbaudio.c
remove rcs tags
[btb/d2x.git] / arch / sdl / rbaudio.c
1 /*
2  *
3  * SDL CD Audio functions
4  *
5  *
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <conf.h>
10 #endif
11
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include <SDL.h>
16
17 #ifdef __linux__
18 #include <sys/ioctl.h>
19 #include <linux/cdrom.h>
20 #endif
21
22 #include "pstypes.h"
23 #include "error.h"
24 #include "args.h"
25 #include "rbaudio.h"
26
27 static SDL_CD *s_cd = NULL;
28 static int initialised = 0;
29
30 void RBAExit()
31 {
32         if (initialised)
33         {
34                 SDL_CDStop(s_cd);
35                 SDL_CDClose(s_cd);
36         }
37 }
38
39 void RBAInit()
40 {
41         if (initialised) return;
42         if (FindArg("-nocdrom")) return; 
43
44         if (SDL_Init(SDL_INIT_CDROM) < 0)
45         {
46                 Warning("SDL library initialisation failed: %s.",SDL_GetError());
47                 return;
48         }
49
50         if (SDL_CDNumDrives() == 0)
51         {
52                 Warning("No cdrom drives found!\n");
53                 return;
54         }
55         s_cd = SDL_CDOpen(0);
56         if (s_cd == NULL) {
57                 Warning("Could not open cdrom for redbook audio!\n");
58                 return;
59         }
60
61         SDL_CDStatus(s_cd); /* update the drive status */
62
63         atexit(RBAExit);
64         initialised = 1;
65 }
66
67 int RBAEnabled()
68 {
69         return 1;
70 }
71
72 void RBARegisterCD()
73 {
74
75 }
76
77 int RBAPlayTrack(int a)
78 {
79         if (!initialised) return -1;
80
81         if (CD_INDRIVE(SDL_CDStatus(s_cd)) ) {
82                 SDL_CDPlayTracks(s_cd, a-1, 0, 0, 0);
83         }
84         return a;
85 }
86
87 void RBAStop()
88 {
89         if (!initialised) return;
90         SDL_CDStop(s_cd);
91 }
92
93 void RBASetVolume(int volume)
94 {
95 #ifdef __linux__
96         int cdfile, level;
97         struct cdrom_volctrl volctrl;
98
99         if (!initialised) return;
100
101         cdfile = s_cd->id;
102         level = volume * 3;
103
104         if ((level<0) || (level>255)) {
105                 fprintf(stderr, "illegal volume value (allowed values 0-255)\n");
106                 return;
107         }
108
109         volctrl.channel0
110                 = volctrl.channel1
111                 = volctrl.channel2
112                 = volctrl.channel3
113                 = level;
114         if ( ioctl(cdfile, CDROMVOLCTRL, &volctrl) == -1 ) {
115                 fprintf(stderr, "CDROMVOLCTRL ioctl failed\n");
116                 return;
117         }
118 #endif
119 }
120
121 void RBAPause()
122 {
123         if (!initialised) return;
124         SDL_CDPause(s_cd);
125 }
126
127 int RBAResume()
128 {
129         if (!initialised) return -1;
130         SDL_CDResume(s_cd);
131         return 1;
132 }
133
134 int RBAGetNumberOfTracks()
135 {
136         if (!initialised) return -1;
137         SDL_CDStatus(s_cd);
138         return s_cd->numtracks;
139 }
140
141 // plays tracks first through last, inclusive
142 int RBAPlayTracks(int first, int last)
143 {
144         if (!initialised)
145                 return 0;
146
147         if (CD_INDRIVE(SDL_CDStatus(s_cd)))
148         {
149                 SDL_CDPlayTracks(s_cd, first - 1, 0, last - first + 1, 0);
150         }
151         return 1;
152 }
153
154 // return the track number currently playing.  Useful if RBAPlayTracks()
155 // is called.  Returns 0 if no track playing, else track number
156 int RBAGetTrackNum()
157 {
158         if (!initialised)
159                 return 0;
160
161         if (SDL_CDStatus(s_cd) != CD_PLAYING)
162                 return 0;
163
164         return s_cd->cur_track + 1;
165 }
166
167 int RBAPeekPlayStatus()
168 {
169         return (SDL_CDStatus(s_cd) == CD_PLAYING);
170 }
171
172 int CD_blast_mixer()
173 {
174         return 0;
175 }
176
177
178 static int cddb_sum(int n)
179 {
180         int ret;
181
182         /* For backward compatibility this algorithm must not change */
183
184         ret = 0;
185
186         while (n > 0) {
187                 ret = ret + (n % 10);
188                 n = n / 10;
189         }
190
191         return (ret);
192 }
193
194
195 uint32_t RBAGetDiscID()
196 {
197         int i, t = 0, n = 0;
198
199         if (!initialised)
200                 return 0;
201
202         /* For backward compatibility this algorithm must not change */
203
204         i = 0;
205
206         while (i < s_cd->numtracks) {
207                 n += cddb_sum(s_cd->track[i].offset / CD_FPS);
208                 i++;
209         }
210
211         t = (s_cd->track[s_cd->numtracks].offset / CD_FPS) -
212             (s_cd->track[0].offset / CD_FPS);
213
214         return ((n % 0xff) << 24 | t << 8 | s_cd->numtracks);
215 }