-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaudio.c
More file actions
328 lines (253 loc) · 7.55 KB
/
Copy pathaudio.c
File metadata and controls
328 lines (253 loc) · 7.55 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2011, 2012, 2013, 2014, 2015 John Langner, WB2OSZ
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// 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 for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// I've extracted the OSS bits from Direwolf's audio.c for use in QtSoundModem
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#ifdef __OpenBSD__
#include <soundcard.h>
#else
#include <sys/soundcard.h>
#endif
void Debugprintf(const char * format, ...);
void Sleep(int mS);
extern int Closing;
int oss_fd = -1; /* Single device, both directions. */
int insize = 0;
short rxbuffer[2400]; // 1200 stereo samples
int num_channels = 2; /* Should be 1 for mono or 2 for stereo. */
int samples_per_sec = 12000; /* Audio sampling rate. Typically 11025, 22050, or 44100. */
int bits_per_sample = 16; /* 8 (unsigned char) or 16 (signed short). */
// Originally 40. Version 1.2, try 10 for lower latency.
#define ONE_BUF_TIME 10
static int set_oss_params(int fd);
#define roundup1k(n) (((n) + 0x3ff) & ~0x3ff)
static int calcbufsize(int rate, int chans, int bits)
{
int size1 = (rate * chans * bits / 8 * ONE_BUF_TIME) / 1000;
int size2 = roundup1k(size1);
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
printf("audio_open: calcbufsize (rate=%d, chans=%d, bits=%d) calc size=%d, round up to %d\n",
rate, chans, bits, size1, size2);
#endif
return (size2);
}
int oss_audio_open(char * adevice_in, char * adevice_out)
{
char audio_in_name[30];
char audio_out_name[30];
strcpy(audio_in_name, adevice_in);
strcpy(audio_out_name, adevice_out);
if (strcmp(audio_in_name, audio_out_name) == 0)
{
printf("Audio device for both receive and transmit: %s \n", audio_in_name);
}
else
{
printf("Audio input device for receive: %s\n", audio_in_name);
printf("Audio out device for transmit: %s\n", audio_out_name);
}
oss_fd = open(audio_in_name, O_RDWR);
if (oss_fd < 0)
{
printf("Could not open audio device %s\n", audio_in_name);
return 0;
}
else
printf("OSS fd = %d\n", oss_fd);
return set_oss_params(oss_fd);
}
static int set_oss_params(int fd)
{
int err;
int devcaps;
int asked_for;
int ossbuf_size_in_bytes;
int frag = (5 << 16) | (11);
err = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
if (err == -1)
{
perror("Not able to set fragment size");
// ossbuf_size_in_bytes = 2048; /* pick something reasonable */
}
err = ioctl(fd, SNDCTL_DSP_CHANNELS, &num_channels);
if (err == -1)
{
perror("Not able to set audio device number of channels");
return (0);
}
asked_for = samples_per_sec;
err = ioctl(fd, SNDCTL_DSP_SPEED, &samples_per_sec);
if (err == -1)
{
perror("Not able to set audio device sample rate");
return (0);
}
printf("Asked for %d samples/sec but actually using %d.\n", asked_for, samples_per_sec);
/* This is actually a bit mask but it happens that */
/* 0x8 is unsigned 8 bit samples and */
/* 0x10 is signed 16 bit little endian. */
err = ioctl(fd, SNDCTL_DSP_SETFMT, &bits_per_sample);
if (err == -1)
{
perror("Not able to set audio device sample size");
return (0);
}
/*
* Determine capabilities.
*/
err = ioctl(fd, SNDCTL_DSP_GETCAPS, &devcaps);
if (err == -1)
{
perror("Not able to get audio device capabilities");
// Is this fatal? // return (-1);
}
printf("audio_open(): devcaps = %08x\n", devcaps);
if (devcaps & DSP_CAP_DUPLEX) printf("Full duplex record/playback.\n");
if (devcaps & DSP_CAP_BATCH) printf("Device has some kind of internal buffers which may cause delays.\n");
if (devcaps & ~(DSP_CAP_DUPLEX | DSP_CAP_BATCH)) printf("Others...\n");
if (!(devcaps & DSP_CAP_DUPLEX))
{
printf("Audio device does not support full duplex\n");
// Do we care? // return (-1);
}
err = ioctl(fd, SNDCTL_DSP_SETDUPLEX, NULL);
if (err == -1)
{
perror("Not able to set audio full duplex mode");
// Unfortunate but not a disaster.
}
/*
* Get preferred block size.
* Presumably this will provide the most efficient transfer.
*
* In my particular situation, this turned out to be
* 2816 for 11025 Hz 16 bit mono
* 5568 for 11025 Hz 16 bit stereo
* 11072 for 44100 Hz 16 bit mono
*
* This was long ago under different conditions.
* Should study this again some day.
*
* Your milage may vary.
*/
err = ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &ossbuf_size_in_bytes);
if (err == -1)
{
perror("Not able to get audio block size");
ossbuf_size_in_bytes = 2048; /* pick something reasonable */
}
printf("audio_open(): suggestd block size is %d\n", ossbuf_size_in_bytes);
/*
* That's 1/8 of a second which seems rather long if we want to
* respond quickly.
*/
ossbuf_size_in_bytes = calcbufsize(samples_per_sec, num_channels, bits_per_sample);
printf("audio_open(): using block size of %d\n", ossbuf_size_in_bytes);
/* Version 1.3 - after a report of this situation for Mac OSX version. */
if (ossbuf_size_in_bytes < 256 || ossbuf_size_in_bytes > 32768)
{
printf("Audio buffer has unexpected extreme size of %d bytes.\n", ossbuf_size_in_bytes);
printf("Detected at %s, line %d.\n", __FILE__, __LINE__);
printf("This might be caused by unusual audio device configuration values.\n");
ossbuf_size_in_bytes = 2048;
printf("Using %d to attempt recovery.\n", ossbuf_size_in_bytes);
}
return (ossbuf_size_in_bytes);
}
int oss_read(short * samples, int nSamples)
{
int n;
int nBytes = nSamples * 4;
if (oss_fd < 0)
return 0;
// printf("audio_get(): read %d\n", nBytes - insize);
n = read(oss_fd, &rxbuffer[insize], nBytes - insize);
if (n < 0)
{
perror("Can't read from audio device");
insize = 0;
return (0);
}
insize += n;
if (n == nSamples * 4)
{
memcpy(samples, rxbuffer, insize);
insize = 0;
return nSamples;
}
return 0;
}
int oss_write(short * ptr, int len)
{
int k;
// int delay;
// ioctl(oss_fd, SNDCTL_DSP_GETODELAY, &delay);
// Debugprintf("Delay %d", delay);
k = write(oss_fd, ptr, len * 4);
//
if (k < 0)
{
perror("Can't write to audio device");
return (-1);
}
if (k < len * 4)
{
printf("oss_write(): write %d returns %d\n", len * 4, k);
/* presumably full but didn't block. */
usleep(10000);
}
ptr += k;
len -= k;
return 0;
}
void oss_flush()
{
int delay;
if (oss_fd < 0)
{
Debugprintf("OSS Flush Called when OSS closed");
return;
}
ioctl(oss_fd, SNDCTL_DSP_GETODELAY, &delay);
Debugprintf("OSS Flush Delay %d", delay);
while (delay)
{
Sleep(10);
ioctl(oss_fd, SNDCTL_DSP_GETODELAY, &delay);
// Debugprintf("Flush Delay %d", delay);
}
}
void oss_audio_close(void)
{
if (oss_fd > 0)
{
close(oss_fd);
oss_fd = -1;
}
return;
}