forked from xwax/xwax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oss.c
231 lines (174 loc) · 4.67 KB
/
oss.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
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
/*
* Copyright (C) 2021 Mark Hills <mark@xwax.org>
*
* This file is part of "xwax".
*
* "xwax" is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 3 as
* published by the Free Software Foundation.
*
* "xwax" is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/soundcard.h>
#include "oss.h"
#define FRAME 32 /* maximum read size */
struct oss {
int fd;
struct pollfd *pe;
unsigned int rate;
};
static void clear(struct device *dv)
{
int r;
struct oss *oss = (struct oss*)dv->local;
r = close(oss->fd);
if (r == -1) {
perror("close");
abort();
}
free(dv->local);
}
/* Push audio into the device's buffer, for playback */
static int push(int fd, signed short *pcm, int samples)
{
int r, bytes;
bytes = samples * DEVICE_CHANNELS * sizeof(short);
r = write(fd, pcm, bytes);
if (r == -1) {
perror("write");
return -1;
}
if (r < bytes)
fprintf(stderr, "Device output overrun.\n");
return r / DEVICE_CHANNELS / sizeof(short);
}
/* Pull audio from the device, for recording */
static int pull(int fd, signed short *pcm, int samples)
{
int r, bytes;
bytes = samples * DEVICE_CHANNELS * sizeof(short);
r = read(fd, pcm, bytes);
if (r == -1) {
perror("read");
return -1;
}
if (r < bytes)
fprintf(stderr, "Device input underrun.\n");
return r / DEVICE_CHANNELS / sizeof(short);
}
static int handle(struct device *dv)
{
signed short pcm[FRAME * DEVICE_CHANNELS];
int samples;
struct oss *oss = (struct oss*)dv->local;
/* Check input buffer for recording */
if (oss->pe->revents & POLLIN) {
samples = pull(oss->fd, pcm, FRAME);
if (samples == -1)
return -1;
device_submit(dv, pcm, samples);
}
/* Check the output buffer for playback */
if (oss->pe->revents & POLLOUT) {
device_collect(dv, pcm, FRAME);
samples = push(oss->fd, pcm, FRAME);
if (samples == -1)
return -1;
}
return 0;
}
static ssize_t pollfds(struct device *dv, struct pollfd *pe, size_t z)
{
struct oss *oss = (struct oss*)dv->local;
if (z < 1)
return -1;
pe->fd = oss->fd;
pe->events = POLLIN | POLLOUT;
oss->pe = pe;
return 1;
}
static unsigned int sample_rate(struct device *dv)
{
struct oss *oss = (struct oss*)dv->local;
return oss->rate;
}
static struct device_ops oss_ops = {
.pollfds = pollfds,
.handle = handle,
.sample_rate = sample_rate,
.clear = clear
};
int oss_init(struct device *dv, const char *filename, unsigned int rate,
unsigned short buffers, unsigned short fragment)
{
int p, fd;
struct oss *oss;
fd = open(filename, O_RDWR, 0);
if (fd == -1) {
perror("open");
return -1;
}
/* Ideally would set independent settings for the record and
* playback buffers. Recording needs to buffer where necessary, as
* lost audio results in zero-crossings which corrupt the
* timecode. Playback buffer needs to be short to avoid high
* latency */
p = (buffers << 16) | fragment;
if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &p) == -1) {
perror("SNDCTL_DSP_SETFRAGMENT");
goto fail;
}
p = AFMT_S16_LE;
if (ioctl(fd, SNDCTL_DSP_SETFMT, &p) == -1) {
perror("SNDCTL_DSP_SETFMT");
goto fail;
}
p = DEVICE_CHANNELS;
if (ioctl(fd, SNDCTL_DSP_CHANNELS, &p) == -1) {
perror("SNDCTL_DSP_CHANNELS");
goto fail;
}
p = rate;
if (ioctl(fd, SNDCTL_DSP_SPEED, &p) == -1) {
perror("SNDCTL_DSP_SPEED");
goto fail;
}
p = fcntl(fd, F_GETFL);
if (p == -1) {
perror("F_GETFL");
goto fail;
}
p |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, p) == -1) {
perror("fcntl");
return -1;
}
oss = malloc(sizeof *oss);
if (oss == NULL) {
perror("malloc");
goto fail;
}
oss->fd = fd;
oss->pe = NULL;
oss->rate = rate;
device_init(dv, &oss_ops);
dv->local = oss;
return 0;
fail:
close(fd);
return -1;
}