summaryrefslogtreecommitdiffstats
path: root/flow/audioiosndio.cpp
blob: 03efd1b82360a6fa2657767d810f1602af76414b (plain)
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
/*
 * Copyright (c) 2008 Jacob Meuser <jakemsr@sdf.lonestar.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
	#include <config.h>
#endif

#ifdef HAVE_LIBSNDIO

#include <sys/types.h>
#include <errno.h>
#include <math.h>

#include <sndio.h>
#include <poll.h>

#include "debug.h"
#include "audioio.h"
#include "audiosubsys.h"
#include "iomanager.h"
#include "dispatcher.h"

#include <cstdlib>
#include <cstring>

int bps, chans;
long long realpos, playpos, recpos;

void movecb(void *addr, int delta)
{
	realpos += delta * (int)(bps * chans);
}

namespace Arts {

class AudioIOSNDIO : public AudioIO, public TimeNotify {

protected:
	struct sio_hdl *hdl;
	struct sio_par par;
	int bufsz;
	int bufused;
	int duplex;

public:
	AudioIOSNDIO();

	void notifyTime();

	void setParam(AudioParam param, int& value);
	int getParam(AudioParam param);

	bool open();
	void close();
	int read(void *buffer, int size);
	int write(void *buffer, int size);
};

REGISTER_AUDIO_IO(AudioIOSNDIO,"sndio","libsndio");
}

using namespace std;
using namespace Arts;

AudioIOSNDIO::AudioIOSNDIO()
{
	/* default parameters */
	param(samplingRate) = 48000;
	paramStr(deviceName) = "";
	param(fragmentSize) = 4096;
	param(fragmentCount) = 4;
	param(format) = 16;
	param(channels) = 2;
	param(direction) = directionWrite;
}

bool AudioIOSNDIO::open()
{
	string& _error = paramStr(lastError);
	string& _deviceName = paramStr(deviceName);
	int& _channels = param(channels);
	int& _fragmentSize = param(fragmentSize);
	int& _fragmentCount = param(fragmentCount);
	int& _samplingRate = param(samplingRate);
	int& _format = param(format);

	struct sio_par testpar;
	char dev[PATH_MAX];
	int mode, bpf;

	duplex = 0;
	if (param(direction) == (directionRead | directionWrite)) {
		mode = SIO_PLAY | SIO_REC;
		duplex = 1;
	} else if (param(direction) == directionWrite) {
		mode = SIO_PLAY;
	} else {
		_error = "invalid direction";
		return false;
	}

	strlcpy(dev, _deviceName.c_str(), PATH_MAX);

	if (strcmp(dev, "") == 0)
		hdl = sio_open(NULL, mode, 0);
	else
		hdl = sio_open(dev, mode, 0);

	if (hdl == NULL) {
		_error = "device ";
		if (strcmp(_deviceName.c_str(), "") == 0)
			_error += "(default sndio device)";
		else
			_error += _deviceName.c_str();
		_error += " can't be opened";
		return false;
	}

	sio_initpar(&par);

	if (_format == 8) {
		par.bits = 8;
		par.sig = 0;
	} else {
		par.bits = 16;
		par.sig = 1;
		par.le = 1;
	}

	if (duplex)
		par.pchan = par.rchan = _channels;
	else
		par.pchan = _channels;

	par.rate = _samplingRate;

	/* limit the buffer size for hardware constraints */

	if (_fragmentSize > 1024*16)
		_fragmentSize = 1024*16;

	while (_fragmentSize * _fragmentCount > 1024*32)
		_fragmentCount--;

	bpf = ((par.bits / NBBY) * par.pchan);
	par.round = _fragmentSize / bpf;
	par.appbufsz = _fragmentSize * _fragmentCount / bpf;

	testpar = par;

	char details[128];

	snprintf(details, 128, " rate=%d pchan=%d bits=%d le=%d sig=%d sz=%d",
	    par.rate, par.pchan, par.bits, par.le, par.sig, par.appbufsz);

	if (!sio_setpar(hdl, &par)) {
		_error = "sio_setpar failed:";
		_error += details;

		close();
		return false;
	}

	if (!sio_getpar(hdl, &par)) {
		_error = "sio_getpar failed";

		close();
		return false;
	}

	if (testpar.bits != par.bits ||
	    testpar.sig != par.sig ||
	    testpar.le != par.le ||
	    testpar.pchan != par.pchan ||
	    fabs((testpar.rate - par.rate)/testpar.rate) > 0.05) {
		_error = "returned params do not match requested params";

		close();
		return false;
	}

	bpf = par.pchan * par.bps;
	bufsz = par.bufsz * bpf;

	::bps = par.bps;
	::chans = par.pchan;
	::realpos = ::playpos = ::recpos = 0;

	sio_onmove(hdl, ::movecb, NULL);

	if (!sio_start(hdl)) {
		_error = "sio_start failed";

		close();
		return false;
	}

	/* use a timer instead of select() */
	Dispatcher::the()->ioManager()->addTimer(10, this);

	return true;
}

void AudioIOSNDIO::close()
{
	if (hdl != NULL) {
		sio_close(hdl);
		hdl = NULL;
	}
}

void AudioIOSNDIO::setParam(AudioParam p, int& value)
{
	param(p) = value;
}

int AudioIOSNDIO::getParam(AudioParam p)
{
	struct pollfd gpfd;
	int n, events;

	/* update ::realpos */
	if ((p == canRead || p == canWrite) && hdl != NULL) {
		events = POLLOUT;
		if (duplex)
			events |= POLLIN;
		n = sio_pollfd(hdl, &gpfd, events);
		while (poll(&gpfd, n, 0) < 0 && errno == EINTR)
			;
		sio_revents(hdl, &gpfd);
	}

	switch(p) {
	case canRead:
		bufused = (::realpos - ::recpos < 0) ? 0 : ::realpos - ::recpos;
		return bufused;
		break;
	case canWrite:
		bufused = (::realpos < 0) ? ::playpos : ::playpos - ::realpos;
		return bufsz - bufused;
		break;
	case autoDetect:
		return 15;
		break;
	default:
		return param(p);
		break;
	}
}

int AudioIOSNDIO::read(void *buffer, int size)
{
	arts_assert(hdl != NULL);

	size_t result;

	result = sio_read(hdl, buffer, size);

	::recpos += result;

	return (int)result;
}

int AudioIOSNDIO::write(void *buffer, int size)
{
	arts_assert(hdl != NULL);

	size_t result;

	result = sio_write(hdl, buffer, size);

	::playpos += result;

	return (int)result;
}

void AudioIOSNDIO::notifyTime()
{
	int& _direction = param(direction);

	for (;;) {
		int todo = 0;

		if ((_direction & directionRead) && (getParam(canRead) > 0))
			todo |= AudioSubSystem::ioRead;

		if ((_direction & directionWrite) && (getParam(canWrite) > 0))
			todo |= AudioSubSystem::ioWrite;

		if (todo == 0)
			return;

		AudioSubSystem::the()->handleIO(todo);
	}
}

#endif