summaryrefslogtreecommitdiffstats
path: root/flow/audioiosun.cpp
blob: 16dee36d0023a2e1fa6e6ccf5b529df87fca9d5e (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
    /*

    Copyright (C) 2001 Aaron Williams
                       [email protected]
	          (C) 2001 Stefan Westerfeld
                       [email protected]
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library 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
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

/*
 * Audio support for Sun Solaris, written by Aaron Williams
 *
 *
 * Please send comments to [email protected]
 *
 * This code has been tested with Solaris 7 running on a Sun Ultra 5
 *
 * Note that in Solaris the select support appears to be broken.
 * Because of this, we use a timer and a dispatcher instead so we
 * arn't polling (and sucking up most of the CPU).
 *
 * Currently read support has not been tested and will likely break
 * other code
 *
 * 8-bit audio support also does not work (which I don't consider a 
 * big deal).
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*
 * Only compile this AudioIO class if we're on Solaris or NetBSD
 */
#if defined(__sun) || defined(__NetBSD__)

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/stat.h>

#include <sys/audioio.h>
#ifdef __sun
#include <stropts.h>
#include <sys/conf.h>
#endif

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <algorithm>

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

#ifdef __NetBSD__
typedef u_int uint_t;
#endif

// This looks like the maximum buffer size according to the sys/audio*.h 
// files on Solaris7
#define SUN_MAX_BUFFER_SIZE		(65536)

namespace Arts {

    class AudioIOSun : public AudioIO, public TimeNotify {
    protected:
		uint_t bytesRead, bytesWritten, bytesPerSec;
		uint_t bytesPerSample;
		timeval start;
		int audio_fd;
		int requestedFragmentSize;
		int requestedFragmentCount;
		audio_info_t auinfo;
		
#ifdef WORDS_BIGENDIAN
		static const int defaultFormat = 17;
#else
		static const int defaultFormat = 16;
#endif

    public:
		AudioIOSun();

		// Timer callback
		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(AudioIOSun,"sun","Sun Audio Input/Output");

};

using namespace std;
using namespace Arts;

AudioIOSun::AudioIOSun()
{
    /*
     * default parameters
     */
    param(samplingRate) = 44100;
	// solaris convention to support SunRays run out-of-the-box
	const char *audioDev = getenv("AUDIODEV");
    paramStr(deviceName) = (audioDev != 0)?audioDev:"/dev/audio";
    param(fragmentSize) = 1024;
    param(fragmentCount) = 7;
    param(channels) = 2;
    param(direction) = 2;
    param(format) = defaultFormat;
}

// Opens the audio device
bool AudioIOSun::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);

    int mode;

    if (param(direction) == 3)
		mode = O_RDWR;
    else if (param(direction) == 2)
		mode = O_WRONLY;
    else
    {
		_error = "invalid direction";
		return false;
    }

    audio_fd = ::open(_deviceName.c_str(), mode, 0);

    if (audio_fd < 0)
    {
		_error = "device ";
		_error += _deviceName.c_str();
		_error += " can't be opened (";
		_error += strerror(errno);
		_error += ")";
		return false;
    }

    fcntl(audio_fd, F_SETFL, O_NDELAY);

    AUDIO_INITINFO(&auinfo);

    if (ioctl(audio_fd, AUDIO_GETINFO, &auinfo) < 0)
    {
		_error = "device ";
		_error += _deviceName.c_str();
		_error += " AUDIO_GETINFO failed (";
		_error += strerror(errno);
		_error += ")";
		return false;
    }

    if(_format != 8) _format = defaultFormat;
#if 0
    printf("param(direction)=%d\n", param(direction));
    printf("format: %d\n", _format);
    printf("channels: %d\n", _channels);		
    printf("sampling rate: %d\n", _samplingRate);
#endif

    bytesPerSample = ((_format == 8) ? 8 : 16)/8 * _channels;

    auinfo.play.precision = (_format == 8) ? 8 : 16;
    if (param(direction) == 3)
		auinfo.record.precision = (_format == 8) ? 8 : 16;

    auinfo.play.encoding = AUDIO_ENCODING_LINEAR;
    if (param(direction) == 3)
		auinfo.record.encoding = AUDIO_ENCODING_LINEAR;

    auinfo.play.channels = _channels;
    if (param(direction) == 3)
		auinfo.record.channels = _channels;

    auinfo.play.sample_rate = _samplingRate;
    if (param(direction) == 3)
		auinfo.record.sample_rate = _samplingRate;

    if (ioctl(audio_fd, AUDIO_SETINFO, &auinfo) < 0)
    {
		_error = "AUDIO_SETINFO failed - ";
		_error += strerror(errno);

		close();
		return false;
    }

    
    if (ioctl(audio_fd, AUDIO_GETINFO, &auinfo) < 0)
    {
		_error = "device ";
		_error += _deviceName.c_str();
		_error += " AUDIO_GETINFO failed (";
		_error += strerror(errno);
		_error += ")";
		return false;
    }

    if (auinfo.play.precision != (uint_t)((_format == 8) ? 8 : 16) ||
		(param(direction) == 3 && 
		 auinfo.record.precision != (uint_t)((_format == 8) ? 8 : 16)))
    {
		char play_details[80];
		char record_details[80];
		sprintf(play_details, 
				" (_format = %d, asked driver to give %d, got %d)",
				_format, _format, auinfo.play.precision);
		if (param(direction) == 3)
		    sprintf(record_details, 
				    " (_format = %d, asked driver to give %d, got %d)",
				    _format, _format, auinfo.record.precision);

		_error = "Can't set playback and/or record format  ";
		_error += "Play format: ";
		_error += play_details;
		if (param(direction) == 3) {
		    _error += "  Record format: ";
		    _error += record_details;
		}
		close();
		return false;
    }
    if ((auinfo.play.encoding != (uint_t)AUDIO_ENCODING_LINEAR) ||
		(param(direction) == 3 &&
		 auinfo.record.encoding != (uint_t)AUDIO_ENCODING_LINEAR))
    {
		char play_encoding[80], record_encoding[80];
		sprintf(play_encoding, "(%d bits, %d encoding)",
				auinfo.play.precision, auinfo.play.encoding);
		sprintf(record_encoding, "(%d bits, %d encoding)",
				auinfo.record.precision, auinfo.record.encoding);

		_error = "Can't set playback and/or record format";
		_error += "requested format was ";
		_error += (_format == 8) ? "8-bit AUDIO_ENCODING_LINEAR" :
		    "16-bit AUDIO_ENCODING_LINEAR";
		_error += ", got playback format ";
		_error += play_encoding;
		if (param(direction) == 3) {
		    _error += ", record format ";
		    _error += record_encoding;
		}

		close();
		return false;
    }

    if (auinfo.play.channels != (uint_t)_channels) {
		_error = "Audio device doesn't support number of ";
		_error += "requested playback channels";
		close();
		return false;
    }
    if (param(direction) == 3 && auinfo.record.channels != (uint_t)_channels) {
		_error = "Audio device doesn't support number of ";
		_error += "requested record channels";
		close();
		return false;
    }

    int tolerance = _samplingRate/10+1000;

    if (abs(int(auinfo.play.sample_rate - _samplingRate)) > tolerance)
    {
		_error = "can't set requested playback sampling rate";

		char details[80];
		sprintf(details," (requested rate %d, got rate %d)",
				_samplingRate, auinfo.play.sample_rate);
		_error += details;

		close();
		return false;
    }

    if (param(direction) == 3 &&
		abs(int(auinfo.record.sample_rate - _samplingRate)) > tolerance)
    {
		_error = "can't set requested record sampling rate";

		char details[80];
		sprintf(details," (requested rate %d, got rate %d)",
				_samplingRate, auinfo.play.sample_rate);
		_error += details;

		close();
		return false;
    }


    /*
     * don't allow unreasonable large fragmentSize/Count combinations,
     * because "real" hardware also doesn't
     */

    if(_fragmentSize > SUN_MAX_BUFFER_SIZE) 
		_fragmentSize = SUN_MAX_BUFFER_SIZE;

    while(_fragmentSize * _fragmentCount > SUN_MAX_BUFFER_SIZE)
		_fragmentCount--;

    bytesRead = bytesWritten = 0;
    bytesPerSec = _channels * 2 * _samplingRate;

    // Install the timer
    Dispatcher::the()->ioManager()->addTimer(10, this);

    gettimeofday(&start,0);

    return true;
}

void AudioIOSun::close()
{
    ::close(audio_fd);
    Dispatcher::the()->ioManager()->removeTimer(this);
}

// This is called on each timer tick
void AudioIOSun::notifyTime()
{
    int& _direction = param(direction);
    int& _fragmentSize = param(fragmentSize);

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

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

		if (!todo)
		    return;

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

void AudioIOSun::setParam(AudioParam p, int& value)
{
    switch(p) {
    case fragmentSize:
		param(p) = requestedFragmentSize = value;
		break;
    case fragmentCount:
		param(p) = requestedFragmentCount = value;
		break;
    default:
		param(p) = value;
		break;
    }
}

int AudioIOSun::getParam(AudioParam p)
{
    int bytes;
    int count;

    switch(p)
    {
    case canRead:
		if (ioctl(audio_fd, AUDIO_GETINFO, &auinfo) < 0)
		    return (0);
		bytes = (auinfo.record.samples * bytesPerSample) - bytesRead;
		if (bytes < 0) {
		    printf("Error: bytes %d < 0, samples=%u, bytesRead=%u\n",
				   bytes, auinfo.record.samples, bytesRead);
		    bytes = 0;
		}
		return bytes;

    case canWrite:
		if (ioctl(audio_fd, AUDIO_GETINFO, &auinfo) < 0) 
		    return (0);
		count = SUN_MAX_BUFFER_SIZE - 
		    (bytesWritten - (auinfo.play.samples * bytesPerSample));
		return count;

	case autoDetect:
		/*
		 * If we're on Solaris, this driver is the one that will work,
		 * and if we're not on Solaris, it won't be compiled anyway.
		 */
		return 12;

    default:
		return param(p);
    }
}

int AudioIOSun::read(void *buffer, int size)
{
    size = ::read(audio_fd, buffer, size);
    if (size < 0)
		return 0;

    bytesRead += size;
    return size;
}

int AudioIOSun::write(void *buffer, int size)
{
    size = ::write(audio_fd, buffer, size);
    bytesWritten += size;
    return size;
}

#endif /* defined(__sun) || defined(__NetBSD__) */