File: audioiosgi.cc

package info (click to toggle)
arts 1.5.9-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 8,436 kB
  • ctags: 9,848
  • sloc: ansic: 44,670; cpp: 33,776; sh: 10,486; perl: 3,470; makefile: 372; yacc: 347; lex: 160
file content (274 lines) | stat: -rw-r--r-- 6,324 bytes parent folder | download | duplicates (5)
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
    /*

    Copyright (C) 2001 Carsten Kroll
                       ckroll@pinnaclesys.com

    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., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    */

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

/**
 * only compile 'sgi' AudioIO class if compiled under IRIX
 */
#ifdef HAVE_IRIX

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

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>		// Needed on some systems.
#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"

namespace Arts {

class AudioIOSGI : public AudioIO {
protected:
	int requestedFragmentSize;
	int requestedFragmentCount;
	ALport audio_port,audio_port1;
	ALconfig audioconfig;
	int framesz;

public:
	AudioIOSGI();

	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(AudioIOSGI,"sgi","SGI dmedia Audio I/O");
};

using namespace std;
using namespace Arts;

AudioIOSGI::AudioIOSGI()
{
	/*
	 * default parameters
	 */
	param(samplingRate) = 44100;
	paramStr(deviceName) = "audioio";
	requestedFragmentSize = param(fragmentSize) = 4096;
	requestedFragmentCount = param(fragmentCount) = 10;
	audio_port=0;
	audio_port1=0;
	param(format)=17;
	param(channels) = 2;
	param(direction) = 2;
}

bool AudioIOSGI::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& _direction = param(direction);
	int err;

	if(_direction != 3  && _direction != 2){
		_error = "invalid direction";
		return false;
	}
	framesz=((_format & ~1) >> 3) * _channels;

	audioconfig = alNewConfig();
	alSetSampFmt(audioconfig,AL_SAMPFMT_TWOSCOMP);

	alSetWidth(audioconfig, _format==8 ? AL_SAMPLE_8 : _format==16 || _format==17 ? AL_SAMPLE_16 : AL_SAMPLE_24);
	alSetQueueSize(audioconfig,(requestedFragmentSize * requestedFragmentCount) / framesz);
	alSetChannels(audioconfig,_channels);

	audio_port = alOpenPort("out","w",audioconfig);


	if (audio_port == (ALport) 0 ) {
		err = oserror();
		if (err == AL_BAD_NO_PORTS) {
			_error = "System is out of audio ports";
		} else if (err == AL_BAD_DEVICE_ACCESS) {
			_error = "Couldn't access audio device";
		} else if (err == AL_BAD_OUT_OF_MEM) {
			_error = "Out of memory";
		}
		close();
		return false;
	}
	if (_direction == 3){
		audio_port1  = alOpenPort("in","r",audioconfig);
		if (audio_port1 == (ALport) 0 ) {
			err = oserror();
			if (err == AL_BAD_NO_PORTS) {
				_error = "System is out of audio ports";
			} else if (err == AL_BAD_DEVICE_ACCESS) {
				_error = "Couldn't access audio device";
			} else if (err == AL_BAD_OUT_OF_MEM) {
				_error = "Out of memory";
			}
			close();
			return false;
		}
	}
	/*
	 * Attempt to set a crystal-based sample-rate on the
	 * given device.
	 */
	ALpv x[2];
	x[0].param = AL_MASTER_CLOCK;
	x[0].value.i = AL_CRYSTAL_MCLK_TYPE;
	x[1].param = AL_RATE;
	x[1].value.ll = alDoubleToFixed(double(_samplingRate));
	if (alSetParams(alGetResource(audio_port),x, 2)<0) {
		_error="setparams failed: ";
		_error+=alGetErrorString(oserror());
		close();
		return false;
	}
	if (_direction == 3)
		if (alSetParams(alGetResource(audio_port1),x, 2)<0) {
			_error="setparams failed: ";
			_error+=alGetErrorString(oserror());
			close();
			return false;
		}
	if (x[1].sizeOut < 0) {
		_error="rate was invalid";
		close();
		return false;
	}

	alSetFillPoint(audio_port,(alGetQueueSize(audioconfig)*5)/10) ;//50 %
	if (_direction == 3)
		alSetFillPoint(audio_port1,(alGetQueueSize(audioconfig)*4)/10) ;//40 %
	/*
	 * set the fragment settings to what the user requested
	 */

	_fragmentSize = requestedFragmentSize;
	_fragmentCount = requestedFragmentCount;


	artsdebug("buffering: %d fragments with %d bytes "
		"(audio latency is %1.1f ms)", _fragmentCount, _fragmentSize,
		(float)(_fragmentSize*_fragmentCount) /
		(float)(2.0 * _samplingRate * _channels)*1000.0);


	return true;
}

void AudioIOSGI::close()
{
	alFreeConfig(audioconfig);
	alClosePort(audio_port);
	audio_port=0;
	if (param(direction) == 3) {
		alClosePort(audio_port1);
		audio_port1=0;
	}
}

void AudioIOSGI::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 AudioIOSGI::getParam(AudioParam p)
{
	int frames;
	switch(p)
	{
		case canRead:
				frames=alGetFilled(audio_port);
				return frames*framesz;
			break;

		case canWrite:
				frames=alGetFillable(audio_port);
				return frames*framesz;
			break;

		case selectReadFD:
				return (param(direction) & directionRead)?
						alGetFD(audio_port1):-1;
			break;

		case selectWriteFD:
				return (param(direction) & directionWrite)?
						alGetFD(audio_port):-1;
			break;

		default:
				return param(p);
			break;
	}
}

int AudioIOSGI::read(void *buffer, int size)
{
	arts_assert(audio_port1 != 0);
	::alReadFrames(audio_port1,buffer,size/framesz);
	return size;
}

int AudioIOSGI::write(void *buffer, int size)
{
	arts_assert(audio_port != 0);
	::alWriteFrames(audio_port,buffer,size/framesz);
	return size;
}

#endif