summaryrefslogtreecommitdiffstats
path: root/flow/audioiolibaudioio.cc
blob: c3a887b8ade402937dd21ec2a7550a78f84e9cd2 (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
    /*

    Copyright (C) 2001 Robert Lunnon
                       bob@yarrabee.net.au
    Copyright (C) 2001 Stefan Westerfeld
                       stefan@space.twc.de

    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.

    */

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

/**
 * only compile 'LibAudioIO' AudioIO class if libaudio was detected during
 * configure
 */
#ifdef HAVE_LIBAUDIOIO

#include <libaudioio.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 AudioIOLibAudioIO : public AudioIO {
protected:
	int audio_fd;
	int requestedFragmentSize;
	int requestedFragmentCount;
	SampleSpec_t spec;

public:
	AudioIOLibAudioIO();

	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(AudioIOLibAudioIO,"libaudioio"," Portable Audio Library");
};

using namespace std;
using namespace Arts;

AudioIOLibAudioIO::AudioIOLibAudioIO()
{
	/*
	 * default parameters
	 */
	param(samplingRate) = spec.rate=44100;
	paramStr(deviceName) = "/dev/Audio";
	requestedFragmentSize = param(fragmentSize) = 4096;
	requestedFragmentCount  =spec.max_blocks= param(fragmentCount) = 7;
	param(channels) = spec.channels=2;
	param(direction) = 2;
}

bool AudioIOLibAudioIO::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;
	spec.channels=_channels;
	spec.max_blocks= param(fragmentCount);
	spec.rate= _samplingRate;
	spec.encoding= ENCODE_PCM;
	spec.precision=16 ;
	spec.endian=ENDIAN_NATURAL;
	spec.disable_threads=1;

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

	audio_fd = ::AudioIOOpenX( mode, &spec,&spec );
	if(audio_fd == -1)
	{
		_error = "device ";
		_error += _deviceName.c_str();
		_error += " can't be opened (";
		_error += strerror(errno);
		_error += ")";
		return false;
	}


	/*
	 * since we use spec.endian=ENDIAN_NATURAL we'll have little endian audio
	 * on little endian machines and big endian audio on big endian machines:
	 */
#ifdef WORDS_BIGENDIAN
	_format = 17;
#else
	_format = 16;
#endif

	spec.channels=_channels;

	spec.rate = _samplingRate;


	_fragmentSize = requestedFragmentSize;
	spec.max_blocks=_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 AudioIOLibAudioIO::close()
{
	::AudioIOClose();
}

void AudioIOLibAudioIO::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 AudioIOLibAudioIO::getParam(AudioParam p)
{
	switch(p)
	{
		case canRead:
				return AudioIOCheckRead();
			break;

		case canWrite:
				return (AudioIOCheckWriteReady()) ? (16*1024) : 0;
				// Assume if writable can write 16K
				// Arts Really doesn't care
			break;

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

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

		case autoDetect:
				/*
				 * if there is a "native" aRts driver, we'll rather use this
				 * than the generic libaudioio one, because the native one
				 * is likely to be better optimized to the needs of aRts than
				 * a generic driver, so keep the value small
				 */
				return 3;
			break;

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

int AudioIOLibAudioIO::read(void *buffer, int size)
{
	arts_assert(audio_fd != 0);
	return ::AudioIORead(buffer,size);
}

int AudioIOLibAudioIO::write(void *buffer, int size)
{
	arts_assert(audio_fd != 0);
	return ::AudioIOWrite(buffer,size);
}

#endif