-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
184 lines (169 loc) · 2.84 KB
/
cd.c
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
#include "quakedef.h"
#include <thread.h>
#include <regexp.h>
cvar_t bgmvolume = {"bgmvolume", "1", 1};
static char cdfile[13];
static int trk, chtrk, ntrk;
static int cdread, cdloop;
static void
cproc(void *)
{
int n, afd, fd, v;
char f[32];
s16int buf[4096];
short *p;
double vol;
if((afd = open("/dev/audio", OWRITE)) < 0){
Con_DPrintf("cd: open: %r\n");
return;
}
fd = -1;
for(;;){
if(chtrk > 0){
close(fd);
trk = chtrk;
snprint(f, sizeof f, "%s%03ud", cdfile, trk);
if((fd = open(f, OREAD)) < 0)
Con_DPrintf("cd: open: %r\n");
chtrk = 0;
}
if(!cdread || fd < 0){
sleep(1);
continue;
}
if((n = read(fd, buf, sizeof buf)) < 0)
break;
if(n == 0){
if(cdloop)
seek(fd, 0, 0);
else{
close(fd);
fd = -1;
}
continue;
}
vol = bgmvolume.value;
for(p=buf; p<buf+n/sizeof*buf; p++){
v = *p * vol;
if(v > 0x7fff)
v = 0x7fff;
else if(v < -0x8000)
v = -0x8000;
*p = v;
}
if(write(afd, buf, n) != n)
break;
}
}
void
stopcd(void)
{
cdread = 0;
cdloop = 0;
}
void
pausecd(void)
{
cdread = 0;
}
void
resumecd(void)
{
cdread = 1;
}
void
shutcd(void)
{
stopcd();
}
void
stepcd(void)
{
if(bgmvolume.value <= 0.0 || cdread == 0)
return;
cdread = bgmvolume.value > 0.0;
}
void
playcd(int nt, int loop)
{
if(ntrk < 1)
return;
nt -= 1; /* d001 assumed part of track list */
if(nt < 1 || nt > ntrk){
Con_Printf("cd: invalid track number %d\n", nt);
return;
}
chtrk = nt;
cdloop = loop;
cdread = bgmvolume.value > 0.0;
}
static void
cdcmd(void)
{
char *c;
if(Cmd_Argc() < 2){
usage:
Con_Printf("cd (play|loop|stop|pause|resume|info) [track]\n");
return;
}
c = Cmd_Argv(1);
if(cistrcmp(c, "play") == 0){
if(Cmd_Argc() < 3)
goto usage;
playcd(atoi(Cmd_Argv(2)), 0);
}else if(cistrcmp(c, "loop") == 0){
if(Cmd_Argc() < 3)
goto usage;
playcd(atoi(Cmd_Argv(2)), 1);
}else if(cistrcmp(c, "stop") == 0)
stopcd();
else if(cistrcmp(c, "pause") == 0)
pausecd();
else if(cistrcmp(c, "resume") == 0)
resumecd();
else if(cistrcmp(c, "info") == 0)
Con_Printf("track %d/%d; loop %d; vol %.1f\n", trk, ntrk, cdloop, bgmvolume.value);
else
goto usage;
}
static int
cdinfo(void)
{
int fd, i, n;
char type;
Reprog *pat;
Dir *d;
ntrk = 0;
if((fd = open("/mnt/cd", OREAD)) < 0)
return -1;
n = dirreadall(fd, &d);
close(fd);
if(n < 0)
return -1;
pat = regcomp("[au][0-9][0-9][0-9]");
for(type=0, i=0; i<n; i++)
if(regexec(pat, d[i].name, nil, 0)){
if(type == 0)
type = d[i].name[0];
ntrk++;
}
free(pat);
free(d);
if(ntrk < 1)
return -1;
snprint(cdfile, sizeof cdfile, "/mnt/cd/%c", type);
return 0;
}
int
initcd(void)
{
if(cdinfo() < 0){
Con_DPrintf("cdinfo: %r\n");
return -1;
}
if(proccreate(cproc, nil, 16384) < 0)
sysfatal("proccreate: %r");
Cvar_RegisterVariable(&bgmvolume);
Cmd_AddCommand("cd", cdcmd);
return 0;
}