1 | /* -*- mode: C; tab-width:8; c-basic-offset:8 -*- |
---|
2 | * vi:set ts=8: |
---|
3 | * |
---|
4 | * arts.c |
---|
5 | * |
---|
6 | * arts backend. |
---|
7 | */ |
---|
8 | #include "al_siteconfig.h" |
---|
9 | |
---|
10 | #include <AL/al.h> |
---|
11 | #include <fcntl.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <sys/ioctl.h> |
---|
15 | #include <sys/mman.h> |
---|
16 | #include <sys/stat.h> |
---|
17 | #include <sys/time.h> |
---|
18 | #include <sys/types.h> |
---|
19 | #include <unistd.h> |
---|
20 | #include <artsc.h> |
---|
21 | |
---|
22 | #ifdef OPENAL_DLOPEN_ARTS |
---|
23 | #include <dlfcn.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #include "al_main.h" |
---|
27 | #include "al_debug.h" |
---|
28 | |
---|
29 | #include "backends/alc_backend.h" |
---|
30 | |
---|
31 | #define DEF_SPEED _ALC_CANON_SPEED |
---|
32 | #define DEF_SIZE _AL_DEF_BUFSIZ |
---|
33 | #define DEF_SAMPLES (DEF_SIZE / 2) |
---|
34 | #define DEF_BITS 16 |
---|
35 | #define DEF_CHANNELS 2 |
---|
36 | |
---|
37 | static int openal_arts_ref_count = 0; |
---|
38 | |
---|
39 | typedef struct { |
---|
40 | arts_stream_t stream; |
---|
41 | } t_arts_handle; |
---|
42 | |
---|
43 | static const char *genartskey(void); |
---|
44 | static int openal_load_arts_library(void); |
---|
45 | |
---|
46 | /* |
---|
47 | * aRts library functions. |
---|
48 | */ |
---|
49 | static int (*parts_init)(void); |
---|
50 | static void (*parts_free)(void); |
---|
51 | static int (*parts_suspend)(void); |
---|
52 | static int (*parts_suspended)(void); |
---|
53 | static const char *(*parts_error_text)(int errorcode); |
---|
54 | static arts_stream_t (*parts_play_stream)(int rate, int bits, int channels, const char *name); |
---|
55 | static arts_stream_t (*parts_record_stream)(int rate, int bits, int channels, const char *name); |
---|
56 | static void (*parts_close_stream)(arts_stream_t stream); |
---|
57 | static int (*parts_read)(arts_stream_t stream, void *buffer, int count); |
---|
58 | static int (*parts_write)(arts_stream_t stream, const void *buffer, int count); |
---|
59 | static int (*parts_stream_set)(arts_stream_t stream, arts_parameter_t param, int value); |
---|
60 | static int (*parts_stream_get)(arts_stream_t stream, arts_parameter_t param); |
---|
61 | |
---|
62 | /* |
---|
63 | * aRts library handle. |
---|
64 | */ |
---|
65 | static void * arts_lib_handle = NULL; |
---|
66 | |
---|
67 | static int openal_load_arts_library(void) |
---|
68 | { |
---|
69 | #ifdef OPENAL_DLOPEN_ARTS |
---|
70 | char * error = NULL; |
---|
71 | #endif |
---|
72 | |
---|
73 | if (arts_lib_handle != NULL) |
---|
74 | return 1; /* already loaded. */ |
---|
75 | |
---|
76 | #ifdef OPENAL_DLOPEN_ARTS |
---|
77 | #define OPENAL_LOAD_ARTS_SYMBOL(x) p##x = dlsym(arts_lib_handle, #x); \ |
---|
78 | error = dlerror(); \ |
---|
79 | if (p##x == NULL) { \ |
---|
80 | fprintf(stderr,"Could not resolve aRts symbol %s: %s\n", #x, ((error!=NULL)?(error):("(null)"))); \ |
---|
81 | dlclose(arts_lib_handle); arts_lib_handle = NULL; \ |
---|
82 | return 0; } |
---|
83 | dlerror(); /* clear error state */ |
---|
84 | arts_lib_handle = dlopen("libartsc.so", RTLD_LAZY | RTLD_GLOBAL); |
---|
85 | error = dlerror(); |
---|
86 | if (arts_lib_handle == NULL) { |
---|
87 | fprintf(stderr,"Could not open aRts library: %s\n",((error!=NULL)?(error):("(null)"))); |
---|
88 | return 0; |
---|
89 | } |
---|
90 | #else |
---|
91 | #define OPENAL_LOAD_ARTS_SYMBOL(x) p##x = x; |
---|
92 | arts_lib_handle = (void *) 0xF00DF00D; |
---|
93 | #endif |
---|
94 | |
---|
95 | OPENAL_LOAD_ARTS_SYMBOL(arts_init); |
---|
96 | OPENAL_LOAD_ARTS_SYMBOL(arts_free); |
---|
97 | OPENAL_LOAD_ARTS_SYMBOL(arts_suspend); |
---|
98 | OPENAL_LOAD_ARTS_SYMBOL(arts_suspended); |
---|
99 | OPENAL_LOAD_ARTS_SYMBOL(arts_error_text); |
---|
100 | OPENAL_LOAD_ARTS_SYMBOL(arts_play_stream); |
---|
101 | OPENAL_LOAD_ARTS_SYMBOL(arts_record_stream); |
---|
102 | OPENAL_LOAD_ARTS_SYMBOL(arts_close_stream); |
---|
103 | OPENAL_LOAD_ARTS_SYMBOL(arts_read); |
---|
104 | OPENAL_LOAD_ARTS_SYMBOL(arts_write); |
---|
105 | OPENAL_LOAD_ARTS_SYMBOL(arts_stream_set); |
---|
106 | OPENAL_LOAD_ARTS_SYMBOL(arts_stream_get); |
---|
107 | |
---|
108 | return 1; |
---|
109 | } |
---|
110 | |
---|
111 | static void *grab_read_arts(void) { |
---|
112 | return NULL; |
---|
113 | } |
---|
114 | |
---|
115 | static void *grab_write_arts(void) { |
---|
116 | int err; |
---|
117 | t_arts_handle * ahandle; |
---|
118 | |
---|
119 | if (!openal_load_arts_library()) { |
---|
120 | return NULL; |
---|
121 | } |
---|
122 | |
---|
123 | if (!(ahandle=malloc(sizeof(t_arts_handle)))) |
---|
124 | return NULL; |
---|
125 | |
---|
126 | if (openal_arts_ref_count==0) { |
---|
127 | err = parts_init(); |
---|
128 | |
---|
129 | if(err < 0) { |
---|
130 | fprintf(stderr, "aRTs init failed: %s\n", |
---|
131 | parts_error_text(err)); |
---|
132 | free(ahandle); |
---|
133 | return NULL; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | openal_arts_ref_count++; |
---|
138 | |
---|
139 | #if 1 |
---|
140 | ahandle->stream = NULL; |
---|
141 | #else |
---|
142 | ahandle->stream = parts_play_stream(DEF_SPEED, |
---|
143 | DEF_BITS, |
---|
144 | DEF_CHANNELS, |
---|
145 | genartskey()); |
---|
146 | #endif |
---|
147 | |
---|
148 | fprintf(stderr, "arts grab audio ok\n"); |
---|
149 | |
---|
150 | _alDebug(ALD_CONTEXT, __FILE__, __LINE__, |
---|
151 | "arts grab audio ok"); |
---|
152 | |
---|
153 | return ahandle; |
---|
154 | } |
---|
155 | |
---|
156 | void * |
---|
157 | alcBackendOpenARts_( ALC_OpenMode mode ) |
---|
158 | { |
---|
159 | return mode == ALC_OPEN_INPUT_ ? grab_read_arts() : grab_write_arts(); |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | void arts_blitbuffer(void *handle, void *data, int bytes) { |
---|
165 | t_arts_handle * ahandle = (t_arts_handle *) handle; |
---|
166 | |
---|
167 | if ((ahandle == NULL)||(ahandle->stream == NULL)) { |
---|
168 | return; |
---|
169 | } |
---|
170 | |
---|
171 | parts_write(ahandle->stream, data, bytes); |
---|
172 | |
---|
173 | return; |
---|
174 | } |
---|
175 | |
---|
176 | void release_arts(void *handle) { |
---|
177 | t_arts_handle * ahandle = (t_arts_handle *) handle; |
---|
178 | |
---|
179 | if ((ahandle == NULL)||(ahandle->stream == NULL)) { |
---|
180 | return; |
---|
181 | } |
---|
182 | |
---|
183 | openal_arts_ref_count--; |
---|
184 | |
---|
185 | parts_close_stream(ahandle->stream); |
---|
186 | free(ahandle); |
---|
187 | |
---|
188 | if (openal_arts_ref_count==0) |
---|
189 | parts_free(); |
---|
190 | |
---|
191 | return; |
---|
192 | } |
---|
193 | |
---|
194 | static const char *genartskey(void) { |
---|
195 | static char retval[1024]; |
---|
196 | |
---|
197 | snprintf(retval, sizeof(retval), "openal%d", getpid()); |
---|
198 | |
---|
199 | return retval; |
---|
200 | } |
---|
201 | |
---|
202 | static ALboolean set_write_arts(void *handle, |
---|
203 | ALuint *bufsiz, |
---|
204 | ALenum *fmt, |
---|
205 | ALuint *speed) { |
---|
206 | t_arts_handle * ahandle = (t_arts_handle *) handle; |
---|
207 | |
---|
208 | if (ahandle == NULL) { |
---|
209 | return AL_FALSE; |
---|
210 | } |
---|
211 | |
---|
212 | #if 0 |
---|
213 | fprintf(stderr, "set_arts forcing speed from %d to 44100\n", *speed); |
---|
214 | |
---|
215 | /* @@@ FIXME: What was this good for?! */ |
---|
216 | *speed = 44100; |
---|
217 | #endif |
---|
218 | |
---|
219 | if (ahandle->stream != NULL) |
---|
220 | parts_close_stream(ahandle->stream); |
---|
221 | |
---|
222 | /* According to the aRtsC library source, this function |
---|
223 | * returns 0 if it fails. |
---|
224 | */ |
---|
225 | ahandle->stream = parts_play_stream(*speed, |
---|
226 | _alGetBitsFromFormat(*fmt), |
---|
227 | _alGetChannelsFromFormat(*fmt), |
---|
228 | genartskey()); |
---|
229 | |
---|
230 | if (ahandle->stream == 0) { |
---|
231 | ahandle->stream = NULL; |
---|
232 | return AL_FALSE; |
---|
233 | } |
---|
234 | |
---|
235 | parts_stream_set(ahandle->stream, ARTS_P_BUFFER_SIZE, *bufsiz); |
---|
236 | parts_stream_set(ahandle->stream, ARTS_P_BLOCKING, 1); |
---|
237 | |
---|
238 | *bufsiz = parts_stream_get(ahandle->stream, ARTS_P_BUFFER_SIZE); |
---|
239 | |
---|
240 | return AL_TRUE; |
---|
241 | } |
---|
242 | |
---|
243 | static ALboolean set_read_arts(UNUSED(void *handle), |
---|
244 | UNUSED(ALuint *bufsiz), |
---|
245 | UNUSED(ALenum *fmt), |
---|
246 | UNUSED(ALuint *speed)) { |
---|
247 | return AL_FALSE; |
---|
248 | } |
---|
249 | |
---|
250 | ALboolean |
---|
251 | alcBackendSetAttributesARts_(ALC_OpenMode mode, void *handle, ALuint *bufsiz, ALenum *fmt, ALuint *speed) |
---|
252 | { |
---|
253 | return mode == ALC_OPEN_INPUT_ ? |
---|
254 | set_read_arts(handle, bufsiz, fmt, speed) : |
---|
255 | set_write_arts(handle, bufsiz, fmt, speed); |
---|
256 | } |
---|
257 | |
---|
258 | void |
---|
259 | pause_arts( UNUSED(void *handle) ) |
---|
260 | { |
---|
261 | } |
---|
262 | |
---|
263 | void |
---|
264 | resume_arts( UNUSED(void *handle) ) |
---|
265 | { |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | ALsizei |
---|
270 | capture_arts( UNUSED(void *handle), UNUSED(void *capture_buffer), UNUSED(int bufsiz) ) |
---|
271 | { |
---|
272 | return 0; |
---|
273 | } |
---|
274 | |
---|
275 | ALfloat |
---|
276 | get_artschannel( UNUSED(void *handle), UNUSED(ALuint channel) ) |
---|
277 | { |
---|
278 | return 0.0; |
---|
279 | } |
---|
280 | |
---|
281 | int |
---|
282 | set_artschannel( UNUSED(void *handle), UNUSED(ALuint channel), UNUSED(ALfloat volume) ) |
---|
283 | { |
---|
284 | return 0; |
---|
285 | } |
---|