summaryrefslogtreecommitdiffstats
path: root/flow/audioioesd.cc
blob: 30f18c511206030d98831e1632c26c1f889803f4 (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
    /*

    Copyright (C) 2001 Jochen Hoenicke
                       jochen@gnu.org

    Copyright (C) 2003 Ian Chiew
                       ian@snork.net

    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 esound AudioIO class if libesd was detected during
 * configure
 */
#ifdef HAVE_LIBESD

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

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

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

#include <esd.h>

#ifdef __BIG_ENDIAN__
#define _format_bits 17
#else
#define _format_bits 16
#endif

namespace Arts {

class AudioIOESD : public AudioIO, public IONotify {
protected:
	int server_fd, read_fd, write_fd;

public:
	AudioIOESD();

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

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

REGISTER_AUDIO_IO(AudioIOESD,"esd","Enlightened Sound Daemon");
}

using namespace std;
using namespace Arts;

AudioIOESD::AudioIOESD()
{
	/*
	 * default parameters
	 */
	param(samplingRate) = 44100;
	paramStr(deviceName) = "null";
	param(fragmentSize) = 1024;
	param(fragmentCount) = 7;
	param(format) = _format_bits;
	param(channels) = 2;
	param(direction) = 2;

	server_fd = -1;
	read_fd = -1;
	write_fd = -1;
}

bool AudioIOESD::open()
{
	int& _channels = param(channels);
	int& _direction = param(direction);
	int& _samplingRate = param(samplingRate);
	int& _format = param(format);
	string& _error = paramStr(lastError);

	if ((server_fd = esd_open_sound(NULL)) == -1)
	{
		_error = "Couldn't connect to server";
		return false;
	}

	esd_server_info_t *server_info = esd_get_server_info(server_fd);

	if (server_info == NULL)
	{
		_error = "Unable to query EsounD server";
		return false;
	}
	
	esd_format_t server_format = server_info->format;

	_samplingRate = server_info->rate;
	_channels     = (server_format & ESD_STEREO) ? 2            : 1;
	_format       = (server_format & ESD_BITS16) ? _format_bits : 8;

	esd_free_server_info(server_info);
	
	if (_direction & directionRead)
	{
		read_fd = esd_record_stream(server_format, _samplingRate,
					    NULL, "aRts");
		if (read_fd == -1)
		{
			_error = "Couldn't create read uflow";
			return false;
		}
	}

	if (_direction & directionWrite)
	{
		write_fd = esd_play_stream(server_format, _samplingRate,
					   NULL, "aRts");
		if (write_fd == -1)
		{
			_error = "Couldn't create write flow";
			return false;
		}
	}

	return true;
}

void AudioIOESD::notifyIO(int, int)
{
}

void AudioIOESD::close()
{
	if (write_fd != -1) 
		esd_close(write_fd);

	if (read_fd != -1)
		esd_close(read_fd);

	if (server_fd != -1)
		esd_close(server_fd);
}

void AudioIOESD::setParam(AudioParam p, int& value)
{
	switch(p)
	{
	case channels:
	case format:
	case direction:
	case samplingRate:
		param(p) = value;
		close();
		open();
		return;

	default:
		param(p) = value;
		return;
	}
}

int AudioIOESD::getParam(AudioParam p)
{
	switch(p)
	{
	case selectReadFD:
		return read_fd;

	case selectWriteFD:
		return write_fd;

	case canRead:
		return ESD_BUF_SIZE;

	case canWrite:
		return ESD_BUF_SIZE;

		// ESD handles are actually socket descriptors, and I know not
		// of any portable way to peek at the socket's send or receive
		// buffers.

	default:
		return param(p);
	}
}

int AudioIOESD::read(void *buffer, int size)
{
	return ::read(read_fd, buffer, size);
}

int AudioIOESD::write(void *buffer, int size)
{
	return ::write(write_fd, buffer, size);
}

#endif