-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoss.c
249 lines (186 loc) · 5.33 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (C) 2010 Mark Hills <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* This program 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 version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#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 "device.h"
#include "timecoder.h"
#include "player.h"
#define FRAME 32 /* maximum read size */
struct oss_t {
int fd;
struct pollfd *pe;
unsigned int rate;
};
static void clear(struct device_t *dv)
{
int r;
struct oss_t *oss = (struct oss_t*)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_t *dv)
{
signed short pcm[FRAME * DEVICE_CHANNELS];
int samples;
struct oss_t *oss = (struct oss_t*)dv->local;
/* Check input buffer for recording */
if (oss->pe->revents & POLLIN) {
samples = pull(oss->fd, pcm, FRAME);
if (samples == -1)
return -1;
if (dv->timecoder)
timecoder_submit(dv->timecoder, pcm, samples);
}
/* Check the output buffer for playback */
if (oss->pe->revents & POLLOUT) {
/* Always push some audio to the soundcard, even if it means
* silence. This has shown itself to be much more reliable
* than starting and stopping -- which can affect other
* devices in the system. */
if (dv->player)
player_collect(dv->player, pcm, FRAME, oss->rate);
else
memset(pcm, 0, FRAME * DEVICE_CHANNELS * sizeof(short));
samples = push(oss->fd, pcm, FRAME);
if (samples == -1)
return -1;
}
return 0;
}
static ssize_t pollfds(struct device_t *dv, struct pollfd *pe, size_t z)
{
struct oss_t *oss = (struct oss_t*)dv->local;
if (z < 1)
return -1;
pe->fd = oss->fd;
pe->events = POLLIN | POLLOUT | POLLHUP;
oss->pe = pe;
return 1;
}
static unsigned int sample_rate(struct device_t *dv)
{
struct oss_t *oss = (struct oss_t*)dv->local;
return oss->rate;
}
static struct device_type_t oss_type = {
.pollfds = pollfds,
.handle = handle,
.sample_rate = sample_rate,
.start = NULL,
.stop = NULL,
.clear = clear
};
int oss_init(struct device_t *dv, const char *filename, unsigned int rate,
unsigned short buffers, unsigned short fragment)
{
int p, fd;
struct oss_t *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;
}
dv->local = malloc(sizeof(struct oss_t));
if (!dv->local) {
perror("malloc");
goto fail;
}
oss = (struct oss_t*)dv->local;
oss->fd = fd;
oss->pe = NULL;
oss->rate = rate;
dv->type = &oss_type;
return 0;
fail:
close(fd);
return -1;
}