1 | /* -*- mode: C; tab-width:8; c-basic-offset:8 -*- |
---|
2 | * vi:set ts=8: |
---|
3 | * |
---|
4 | * alc_device.c |
---|
5 | * |
---|
6 | */ |
---|
7 | #include "al_siteconfig.h" |
---|
8 | |
---|
9 | #include <AL/al.h> |
---|
10 | #include <AL/alc.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <string.h> |
---|
13 | |
---|
14 | #include "al_config.h" |
---|
15 | #include "al_main.h" |
---|
16 | #include "al_debug.h" |
---|
17 | |
---|
18 | #include "alc/alc_device.h" |
---|
19 | #include "alc/alc_error.h" |
---|
20 | |
---|
21 | #include "backends/alc_backend.h" |
---|
22 | |
---|
23 | static int num_devices = 0; |
---|
24 | |
---|
25 | /* |
---|
26 | * Opens a device, using the alrc expression deviceSpecifier to specify |
---|
27 | * attributes for the device. Returns the device if successful, NULL |
---|
28 | * otherwise. |
---|
29 | */ |
---|
30 | ALCdevice *alcOpenDevice( const ALchar *deviceSpecifier ) { |
---|
31 | ALCdevice *retval; |
---|
32 | char dirstr[65]; |
---|
33 | Rcvar foo = NULL; |
---|
34 | Rcvar direction = NULL; |
---|
35 | Rcvar freq_sym = NULL; |
---|
36 | Rcvar speakers = NULL; |
---|
37 | Rcvar devices = NULL; |
---|
38 | int openForInput; |
---|
39 | |
---|
40 | if( num_devices == 0 ) { |
---|
41 | /* first initialization */ |
---|
42 | if( _alParseConfig() == AL_FALSE ) { |
---|
43 | _alDebug(ALD_CONFIG, __FILE__, __LINE__, |
---|
44 | "Couldn't parse config file."); |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | /* see if the user defined devices, sampling-rate, or direction */ |
---|
49 | devices = rc_lookup( "devices" ); |
---|
50 | direction = rc_lookup( "direction" ); |
---|
51 | freq_sym = rc_lookup( "sampling-rate" ); |
---|
52 | speakers = rc_lookup( "speaker-num" ); |
---|
53 | |
---|
54 | /* get the attributes requested in the args */ |
---|
55 | if( deviceSpecifier ) { |
---|
56 | foo = rc_eval( (const char *) deviceSpecifier ); |
---|
57 | } |
---|
58 | |
---|
59 | /* define each attribute pair */ |
---|
60 | rc_foreach( foo, rc_define_list ); |
---|
61 | |
---|
62 | /* redefine the stuff we saved */ |
---|
63 | if( direction != NULL ) { |
---|
64 | rc_define( "direction", alrc_quote( direction )); |
---|
65 | } |
---|
66 | |
---|
67 | if( devices != NULL ) { |
---|
68 | rc_define( "devices", alrc_quote( devices ) ); |
---|
69 | } |
---|
70 | |
---|
71 | if( freq_sym != NULL ) { |
---|
72 | rc_define( "sampling-rate", alrc_quote( freq_sym )); |
---|
73 | } |
---|
74 | |
---|
75 | if( speakers != NULL ) { |
---|
76 | rc_define( "speaker-num", alrc_quote( speakers )); |
---|
77 | } |
---|
78 | |
---|
79 | direction = rc_lookup( "direction" ); |
---|
80 | devices = rc_lookup( "devices" ); |
---|
81 | freq_sym = rc_lookup( "sampling-rate" ); |
---|
82 | speakers = rc_lookup( "speaker-num" ); |
---|
83 | |
---|
84 | memset( dirstr, 0, sizeof(dirstr) ); |
---|
85 | |
---|
86 | if( direction != NULL ) { |
---|
87 | switch( rc_type( direction ) ) { |
---|
88 | case ALRC_STRING: |
---|
89 | rc_tostr0(direction, dirstr, 64); |
---|
90 | break; |
---|
91 | case ALRC_SYMBOL: |
---|
92 | rc_symtostr0(direction, dirstr, 64); |
---|
93 | break; |
---|
94 | default: |
---|
95 | break; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | retval = malloc( sizeof *retval ); |
---|
100 | if( retval == NULL) { |
---|
101 | /* FIXME: set AL_OUT_OF_MEMORY here? */ |
---|
102 | |
---|
103 | return NULL; |
---|
104 | } |
---|
105 | |
---|
106 | /* copy specifier */ |
---|
107 | if(deviceSpecifier) |
---|
108 | { |
---|
109 | size_t len; |
---|
110 | len = strlen((const char *) deviceSpecifier); |
---|
111 | retval->specifier = malloc(len+1); |
---|
112 | if(retval->specifier == NULL) |
---|
113 | { |
---|
114 | free(retval); |
---|
115 | return NULL; |
---|
116 | } |
---|
117 | |
---|
118 | memcpy(retval->specifier, deviceSpecifier, len); |
---|
119 | retval->specifier[len] = '\0'; |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | /* JIV FIXME: maybe set to default string? */ |
---|
124 | retval->specifier = malloc(1); |
---|
125 | retval->specifier[0] = '\0'; |
---|
126 | } |
---|
127 | |
---|
128 | /* defaults */ |
---|
129 | retval->format = _ALC_EXTERNAL_FMT; |
---|
130 | retval->speed = _ALC_EXTERNAL_SPEED; |
---|
131 | retval->bufsiz = _ALC_DEF_BUFSIZ; |
---|
132 | retval->flags = ALCD_NONE; |
---|
133 | |
---|
134 | if( freq_sym != NULL ) { |
---|
135 | switch(rc_type( freq_sym )) { |
---|
136 | case ALRC_INTEGER: |
---|
137 | case ALRC_FLOAT: |
---|
138 | retval->speed = rc_toint( freq_sym ); |
---|
139 | break; |
---|
140 | default: |
---|
141 | _alDebug(ALD_CONVERT, __FILE__, __LINE__, |
---|
142 | "invalid type %s for sampling-rate", |
---|
143 | rc_typestr( rc_type( freq_sym ) )); |
---|
144 | break; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | if( speakers != NULL ) { |
---|
149 | switch(rc_type( speakers )) { |
---|
150 | case ALRC_INTEGER: |
---|
151 | case ALRC_FLOAT: |
---|
152 | { |
---|
153 | ALint s = rc_toint( speakers ); |
---|
154 | if (s >= 0) { |
---|
155 | ALenum fmt = _al_formatscale( retval->format, (ALuint)s ); |
---|
156 | if ( fmt >= 0 ) { |
---|
157 | retval->format = fmt; |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | break; |
---|
162 | default: |
---|
163 | break; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | openForInput = (strncmp( dirstr, "read", 64 ) == 0); |
---|
168 | retval->handle = alcBackendOpen_( openForInput ? ALC_OPEN_INPUT_ : ALC_OPEN_OUTPUT_ ); |
---|
169 | if( retval->handle == NULL ) { |
---|
170 | free( retval ); |
---|
171 | _alcSetError(ALC_INVALID_DEVICE); |
---|
172 | return NULL; |
---|
173 | } |
---|
174 | retval->flags |= ( openForInput ? ALCD_READ : ALCD_WRITE ); |
---|
175 | |
---|
176 | num_devices++; |
---|
177 | |
---|
178 | return retval; |
---|
179 | } |
---|
180 | |
---|
181 | /* |
---|
182 | * Closes the device referred to by dev. |
---|
183 | */ |
---|
184 | ALCboolean |
---|
185 | alcCloseDevice( ALCdevice *dev ) |
---|
186 | { |
---|
187 | /* ToDo: Is this test really necessary? */ |
---|
188 | if ( dev->handle != NULL) { |
---|
189 | /* ToDo: Use return value */ |
---|
190 | alcBackendClose_( dev->handle ); |
---|
191 | } |
---|
192 | |
---|
193 | free( dev->specifier ); |
---|
194 | free( dev ); |
---|
195 | |
---|
196 | num_devices--; |
---|
197 | |
---|
198 | return ALC_TRUE; |
---|
199 | } |
---|
200 | |
---|
201 | /* |
---|
202 | * Sets the attributes for the device from the settings in the device. The |
---|
203 | * library is free to change the parameters associated with the device, but |
---|
204 | * until _alcDeviceSet is called, none of the changes are important. |
---|
205 | * |
---|
206 | * Sets ALC_INVALID_DEVICE if the setting operation failed. After a call to |
---|
207 | * this function, the caller should check the members in dev is see what the |
---|
208 | * actual values set where. |
---|
209 | */ |
---|
210 | void |
---|
211 | _alcDeviceSet( AL_device *dev ) |
---|
212 | { |
---|
213 | if( alcBackendSetAttributes_(dev->handle, &dev->bufsiz, &dev->format, &dev->speed) != AL_TRUE ) { |
---|
214 | _alDebug(ALD_CONTEXT, __FILE__, __LINE__, "_alcDeviceSet failed."); |
---|
215 | _alcSetError( ALC_INVALID_DEVICE ); |
---|
216 | } |
---|
217 | _alDebug( ALD_CONVERT, __FILE__, __LINE__, |
---|
218 | "after set_audiodevice, f|s|b 0x%x|%d|%d", |
---|
219 | dev->format, |
---|
220 | dev->speed, |
---|
221 | dev->bufsiz ); |
---|
222 | } |
---|
223 | |
---|
224 | /* |
---|
225 | * Pauses a device. |
---|
226 | */ |
---|
227 | void |
---|
228 | _alcDevicePause( AL_device *dev ) |
---|
229 | { |
---|
230 | alcBackendPause_( dev->handle ); |
---|
231 | } |
---|
232 | |
---|
233 | /* |
---|
234 | * Resumes a device. |
---|
235 | */ |
---|
236 | void |
---|
237 | _alcDeviceResume( AL_device *dev ) |
---|
238 | { |
---|
239 | alcBackendResume_( dev->handle ); |
---|
240 | } |
---|