summaryrefslogtreecommitdiffstats
path: root/artsc/artsc.c
blob: 61a02b69782c48f98a17d20ec6b8f054f29c2825 (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
    /*

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

    */

#include "artsc.h"
#include "ltdl.h"
#include <assert.h>

typedef int (*backend_init_ptr)();
typedef int (*backend_suspend_ptr)();
typedef int (*backend_suspended_ptr)();
typedef void (*backend_free_ptr)();
typedef arts_stream_t (*backend_play_stream_ptr)(int,int,int,const char*);
typedef arts_stream_t (*backend_record_stream_ptr)(int,int,int,const char*);
typedef void (*backend_close_stream_ptr)(arts_stream_t);
typedef int (*backend_read_ptr)(arts_stream_t,void*,int);
typedef int (*backend_write_ptr)(arts_stream_t,const void*,int);
typedef int (*backend_stream_set_ptr)(arts_stream_t, arts_parameter_t, int);
typedef int (*backend_stream_get_ptr)(arts_stream_t, arts_parameter_t);

static struct arts_backend {
	int available;
	int refcnt;
	lt_dlhandle handle;

	backend_init_ptr init;
	backend_suspend_ptr suspend;
	backend_suspended_ptr suspended;
	backend_free_ptr free;
	backend_play_stream_ptr play_stream;
	backend_record_stream_ptr record_stream;
	backend_close_stream_ptr close_stream;
	backend_read_ptr read;
	backend_write_ptr write;
	backend_stream_set_ptr stream_set;
	backend_stream_get_ptr stream_get;
} backend = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 };

static void arts_backend_ref()
{
	if(backend.refcnt == 0)
	{
    	lt_dlinit();
		backend.handle = lt_dlopen(ARTSC_BACKEND);

		if(backend.handle)
		{
			backend.init = (backend_init_ptr)
				lt_dlsym(backend.handle, "arts_backend_init");
			backend.suspend = (backend_suspend_ptr)
				lt_dlsym(backend.handle, "arts_backend_suspend");
			backend.suspended = (backend_suspended_ptr)
				lt_dlsym(backend.handle, "arts_backend_suspended");
			backend.free = (backend_free_ptr)
				lt_dlsym(backend.handle, "arts_backend_free");
			backend.play_stream = (backend_play_stream_ptr)
				lt_dlsym(backend.handle, "arts_backend_play_stream");
			backend.record_stream = (backend_record_stream_ptr)
				lt_dlsym(backend.handle, "arts_backend_record_stream");
			backend.close_stream = (backend_close_stream_ptr)
				lt_dlsym(backend.handle, "arts_backend_close_stream");
			backend.write = (backend_write_ptr)
				lt_dlsym(backend.handle, "arts_backend_write");
			backend.read = (backend_read_ptr)
				lt_dlsym(backend.handle, "arts_backend_read");
			backend.stream_set = (backend_stream_set_ptr)
				lt_dlsym(backend.handle, "arts_backend_stream_set");
			backend.stream_get = (backend_stream_get_ptr)
				lt_dlsym(backend.handle, "arts_backend_stream_get");
		}

		if(backend.handle && backend.init && backend.free && backend.play_stream
			&& backend.record_stream && backend.close_stream && backend.write
			&& backend.read && backend.stream_set && backend.stream_get
			&& backend.suspend)
			backend.available = 1;
		else
			backend.available = 0;
	}
	backend.refcnt++;
}

static void arts_backend_release()
{
	assert(backend.refcnt > 0);
	backend.refcnt--;
	if(backend.refcnt == 0)
	{
		if(backend.available)
		{
			backend.available = 0;

			if(backend.handle) lt_dlclose(backend.handle);
		}
		lt_dlexit();
	}
}

int arts_init()
{
	int rc = ARTS_E_NOBACKEND;

	arts_backend_ref();
	if(backend.available) rc = backend.init();

	/* init failed: the user may not call other arts_xxx functions now */
	if(rc < 0) arts_backend_release();

	return rc;
}

int arts_suspend()
{
	int rc = ARTS_E_NOBACKEND;

	if(backend.available) rc = backend.suspend();
	return rc;
}

int arts_suspended()
{
	int rc = ARTS_E_NOBACKEND;

	if(backend.available && backend.suspended) rc = backend.suspended();
	return rc;
}

void arts_free()
{
	if(backend.available)
	{
		backend.free();
		arts_backend_release();
	}
}

arts_stream_t arts_play_stream(int rate, int bits, int channels, const char *name)
{
	arts_stream_t rc = 0;

	if(backend.available) rc = backend.play_stream(rate,bits,channels,name);
	return rc;
}

arts_stream_t arts_record_stream(int rate, int bits, int channels, const char *name)
{
	arts_stream_t rc = 0;

	if(backend.available) rc = backend.record_stream(rate,bits,channels,name);
	return rc;
}

void arts_close_stream(arts_stream_t stream)
{
	if(backend.available) backend.close_stream(stream);
}

int arts_read(arts_stream_t stream, void *buffer, int count)
{
	int rc = ARTS_E_NOBACKEND;

	if(backend.available) rc = backend.read(stream,buffer,count);
	return rc;
}

int arts_write(arts_stream_t stream, const void *buffer, int count)
{
	int rc = ARTS_E_NOBACKEND;

	if(backend.available) rc = backend.write(stream,buffer,count);
	return rc;
}

int arts_stream_set(arts_stream_t stream, arts_parameter_t param, int value)
{
	int rc = ARTS_E_NOBACKEND;

	if(backend.available) rc = backend.stream_set(stream,param,value);
	return rc;
}

int arts_stream_get(arts_stream_t stream, arts_parameter_t param)
{
	int rc = ARTS_E_NOBACKEND;

	if(backend.available) rc = backend.stream_get(stream,param);
	return rc;
}

const char *arts_error_text(int errorcode)
{
	switch(errorcode) {
		case 0:
			return "success";
		case ARTS_E_NOSERVER:
			return "can't connect to aRts soundserver";
		case ARTS_E_NOBACKEND:
			return "loading the aRts backend \""
						ARTSC_BACKEND "\" failed";
		case ARTS_E_NOIMPL:
			return "this aRts function is not yet implemented";
		case ARTS_E_NOINIT:
			return "need to use arts_init() before using other functions";
		case ARTS_E_NOSTREAM:
			return "you passed no valid aRts stream to a function";
	}
	return "unknown arts error happened";
}