1 ! Copyright (C) 2007 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel arrays alien system combinators alien.syntax namespaces
4 alien.c-types sequences vocabs.loader shuffle combinators.lib
5 openal.backend specialized-arrays.uint ;
9 { [ os windows? ] [ "alut.dll" ] }
11 "/System/Library/Frameworks/OpenAL.framework/OpenAL"
13 { [ os unix? ] [ "libalut.so" ] }
14 } cond "cdecl" add-library >>
17 { [ os windows? ] [ "OpenAL32.dll" ] }
19 "/System/Library/Frameworks/OpenAL.framework/OpenAL"
21 { [ os unix? ] [ "libopenal.so" ] }
22 } cond "cdecl" add-library >>
26 TYPEDEF: char ALboolean
29 TYPEDEF: uchar ALubyte
30 TYPEDEF: short ALshort
31 TYPEDEF: ushort ALushort
36 TYPEDEF: float ALfloat
37 TYPEDEF: double ALdouble
39 : AL_INVALID ( -- number ) -1 ; inline
40 : AL_NONE ( -- number ) 0 ; inline
41 : AL_FALSE ( -- number ) 0 ; inline
42 : AL_TRUE ( -- number ) 1 ; inline
43 : AL_SOURCE_RELATIVE ( -- number ) HEX: 202 ; inline
44 : AL_CONE_INNER_ANGLE ( -- nmber ) HEX: 1001 ; inline
45 : AL_CONE_OUTER_ANGLE ( -- number ) HEX: 1002 ; inline
46 : AL_PITCH ( -- number ) HEX: 1003 ; inline
47 : AL_POSITION ( -- number ) HEX: 1004 ; inline
48 : AL_DIRECTION ( -- number ) HEX: 1005 ; inline
49 : AL_VELOCITY ( -- number ) HEX: 1006 ; inline
50 : AL_LOOPING ( -- number ) HEX: 1007 ; inline
51 : AL_BUFFER ( -- number ) HEX: 1009 ; inline
52 : AL_GAIN ( -- number ) HEX: 100A ; inline
53 : AL_MIN_GAIN ( -- number ) HEX: 100D ; inline
54 : AL_MAX_GAIN ( -- number ) HEX: 100E ; inline
55 : AL_ORIENTATION ( -- number ) HEX: 100F ; inline
56 : AL_CHANNEL_MASK ( -- number ) HEX: 3000 ; inline
57 : AL_SOURCE_STATE ( -- number ) HEX: 1010 ; inline
58 : AL_INITIAL ( -- number ) HEX: 1011 ; inline
59 : AL_PLAYING ( -- number ) HEX: 1012 ; inline
60 : AL_PAUSED ( -- number ) HEX: 1013 ; inline
61 : AL_STOPPED ( -- number ) HEX: 1014 ; inline
62 : AL_BUFFERS_QUEUED ( -- number ) HEX: 1015 ; inline
63 : AL_BUFFERS_PROCESSED ( -- number ) HEX: 1016 ; inline
64 : AL_SEC_OFFSET ( -- number ) HEX: 1024 ; inline
65 : AL_SAMPLE_OFFSET ( -- number ) HEX: 1025 ; inline
66 : AL_BYTE_OFFSET ( -- number ) HEX: 1026 ; inline
67 : AL_SOURCE_TYPE ( -- number ) HEX: 1027 ; inline
68 : AL_STATIC ( -- number ) HEX: 1028 ; inline
69 : AL_STREAMING ( -- number ) HEX: 1029 ; inline
70 : AL_UNDETERMINED ( -- number ) HEX: 1030 ; inline
71 : AL_FORMAT_MONO8 ( -- number ) HEX: 1100 ; inline
72 : AL_FORMAT_MONO16 ( -- number ) HEX: 1101 ; inline
73 : AL_FORMAT_STEREO8 ( -- number ) HEX: 1102 ; inline
74 : AL_FORMAT_STEREO16 ( -- number ) HEX: 1103 ; inline
75 : AL_REFERENCE_DISTANCE ( -- number ) HEX: 1020 ; inline
76 : AL_ROLLOFF_FACTOR ( -- number ) HEX: 1021 ; inline
77 : AL_CONE_OUTER_GAIN ( -- number ) HEX: 1022 ; inline
78 : AL_MAX_DISTANCE ( -- number ) HEX: 1023 ; inline
79 : AL_FREQUENCY ( -- number ) HEX: 2001 ; inline
80 : AL_BITS ( -- number ) HEX: 2002 ; inline
81 : AL_CHANNELS ( -- number ) HEX: 2003 ; inline
82 : AL_SIZE ( -- number ) HEX: 2004 ; inline
83 : AL_UNUSED ( -- number ) HEX: 2010 ; inline
84 : AL_PENDING ( -- number ) HEX: 2011 ; inline
85 : AL_PROCESSED ( -- number ) HEX: 2012 ; inline
86 : AL_NO_ERROR ( -- number ) AL_FALSE ; inline
87 : AL_INVALID_NAME ( -- number ) HEX: A001 ; inline
88 : AL_ILLEGAL_ENUM ( -- number ) HEX: A002 ; inline
89 : AL_INVALID_ENUM ( -- number ) HEX: A002 ; inline
90 : AL_INVALID_VALUE ( -- number ) HEX: A003 ; inline
91 : AL_ILLEGAL_COMMAND ( -- number ) HEX: A004 ; inline
92 : AL_INVALID_OPERATION ( -- number ) HEX: A004 ; inline
93 : AL_OUT_OF_MEMORY ( -- number ) HEX: A005 ; inline
94 : AL_VENDOR ( -- number ) HEX: B001 ; inline
95 : AL_VERSION ( -- number ) HEX: B002 ; inline
96 : AL_RENDERER ( -- number ) HEX: B003 ; inline
97 : AL_EXTENSIONS ( -- number ) HEX: B004 ; inline
98 : AL_DOPPLER_FACTOR ( -- number ) HEX: C000 ; inline
99 : AL_DOPPLER_VELOCITY ( -- number ) HEX: C001 ; inline
100 : AL_SPEED_OF_SOUND ( -- number ) HEX: C003 ; inline
101 : AL_DISTANCE_MODEL ( -- number ) HEX: D000 ; inline
102 : AL_INVERSE_DISTANCE ( -- number ) HEX: D001 ; inline
103 : AL_INVERSE_DISTANCE_CLAMPED ( -- number ) HEX: D002 ; inline
104 : AL_LINEAR_DISTANCE ( -- number ) HEX: D003 ; inline
105 : AL_LINEAR_DISTANCE_CLAMPED ( -- number ) HEX: D004 ; inline
106 : AL_EXPONENT_DISTANCE ( -- number ) HEX: D005 ; inline
107 : AL_EXPONENT_DISTANCE_CLAMPED ( -- number ) HEX: D006 ; inline
109 FUNCTION: void alEnable ( ALenum capability ) ;
110 FUNCTION: void alDisable ( ALenum capability ) ;
111 FUNCTION: ALboolean alIsEnabled ( ALenum capability ) ;
112 FUNCTION: ALchar* alGetString ( ALenum param ) ;
113 FUNCTION: void alGetBooleanv ( ALenum param, ALboolean* data ) ;
114 FUNCTION: void alGetIntegerv ( ALenum param, ALint* data ) ;
115 FUNCTION: void alGetFloatv ( ALenum param, ALfloat* data ) ;
116 FUNCTION: void alGetDoublev ( ALenum param, ALdouble* data ) ;
117 FUNCTION: ALboolean alGetBoolean ( ALenum param ) ;
118 FUNCTION: ALint alGetInteger ( ALenum param ) ;
119 FUNCTION: ALfloat alGetFloat ( ALenum param ) ;
120 FUNCTION: ALdouble alGetDouble ( ALenum param ) ;
121 FUNCTION: ALenum alGetError ( ) ;
122 FUNCTION: ALboolean alIsExtensionPresent ( ALchar* extname ) ;
123 FUNCTION: void* alGetProcAddress ( ALchar* fname ) ;
124 FUNCTION: ALenum alGetEnumValue ( ALchar* ename ) ;
125 FUNCTION: void alListenerf ( ALenum param, ALfloat value ) ;
126 FUNCTION: void alListener3f ( ALenum param, ALfloat value1, ALfloat value2, ALfloat value3 ) ;
127 FUNCTION: void alListenerfv ( ALenum param, ALfloat* values ) ;
128 FUNCTION: void alListeneri ( ALenum param, ALint value ) ;
129 FUNCTION: void alListener3i ( ALenum param, ALint value1, ALint value2, ALint value3 ) ;
130 FUNCTION: void alListeneriv ( ALenum param, ALint* values ) ;
131 FUNCTION: void alGetListenerf ( ALenum param, ALfloat* value ) ;
132 FUNCTION: void alGetListener3f ( ALenum param, ALfloat* value1, ALfloat* value2, ALfloat* value3 ) ;
133 FUNCTION: void alGetListenerfv ( ALenum param, ALfloat* values ) ;
134 FUNCTION: void alGetListeneri ( ALenum param, ALint* value ) ;
135 FUNCTION: void alGetListener3i ( ALenum param, ALint* value1, ALint* value2, ALint* value3 ) ;
136 FUNCTION: void alGetListeneriv ( ALenum param, ALint* values ) ;
137 FUNCTION: void alGenSources ( ALsizei n, ALuint* sources ) ;
138 FUNCTION: void alDeleteSources ( ALsizei n, ALuint* sources ) ;
139 FUNCTION: ALboolean alIsSource ( ALuint sid ) ;
140 FUNCTION: void alSourcef ( ALuint sid, ALenum param, ALfloat value ) ;
141 FUNCTION: void alSource3f ( ALuint sid, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3 ) ;
142 FUNCTION: void alSourcefv ( ALuint sid, ALenum param, ALfloat* values ) ;
143 FUNCTION: void alSourcei ( ALuint sid, ALenum param, ALint value ) ;
144 FUNCTION: void alSource3i ( ALuint sid, ALenum param, ALint value1, ALint value2, ALint value3 ) ;
145 FUNCTION: void alSourceiv ( ALuint sid, ALenum param, ALint* values ) ;
146 FUNCTION: void alGetSourcef ( ALuint sid, ALenum param, ALfloat* value ) ;
147 FUNCTION: void alGetSource3f ( ALuint sid, ALenum param, ALfloat* value1, ALfloat* value2, ALfloat* value3) ;
148 FUNCTION: void alGetSourcefv ( ALuint sid, ALenum param, ALfloat* values ) ;
149 FUNCTION: void alGetSourcei ( ALuint sid, ALenum param, ALint* value ) ;
150 FUNCTION: void alGetSource3i ( ALuint sid, ALenum param, ALint* value1, ALint* value2, ALint* value3) ;
151 FUNCTION: void alGetSourceiv ( ALuint sid, ALenum param, ALint* values ) ;
152 FUNCTION: void alSourcePlayv ( ALsizei ns, ALuint* sids ) ;
153 FUNCTION: void alSourceStopv ( ALsizei ns, ALuint* sids ) ;
154 FUNCTION: void alSourceRewindv ( ALsizei ns, ALuint* sids ) ;
155 FUNCTION: void alSourcePausev ( ALsizei ns, ALuint* sids ) ;
156 FUNCTION: void alSourcePlay ( ALuint sid ) ;
157 FUNCTION: void alSourceStop ( ALuint sid ) ;
158 FUNCTION: void alSourceRewind ( ALuint sid ) ;
159 FUNCTION: void alSourcePause ( ALuint sid ) ;
160 FUNCTION: void alSourceQueueBuffers ( ALuint sid, ALsizei numEntries, ALuint* bids ) ;
161 FUNCTION: void alSourceUnqueueBuffers ( ALuint sid, ALsizei numEntries, ALuint* bids ) ;
162 FUNCTION: void alGenBuffers ( ALsizei n, ALuint* buffers ) ;
163 FUNCTION: void alDeleteBuffers ( ALsizei n, ALuint* buffers ) ;
164 FUNCTION: ALboolean alIsBuffer ( ALuint bid ) ;
165 FUNCTION: void alBufferData ( ALuint bid, ALenum format, void* data, ALsizei size, ALsizei freq ) ;
166 FUNCTION: void alBufferf ( ALuint bid, ALenum param, ALfloat value ) ;
167 FUNCTION: void alBuffer3f ( ALuint bid, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3 ) ;
168 FUNCTION: void alBufferfv ( ALuint bid, ALenum param, ALfloat* values ) ;
169 FUNCTION: void alBufferi ( ALuint bid, ALenum param, ALint value ) ;
170 FUNCTION: void alBuffer3i ( ALuint bid, ALenum param, ALint value1, ALint value2, ALint value3 ) ;
171 FUNCTION: void alBufferiv ( ALuint bid, ALenum param, ALint* values ) ;
172 FUNCTION: void alGetBufferf ( ALuint bid, ALenum param, ALfloat* value ) ;
173 FUNCTION: void alGetBuffer3f ( ALuint bid, ALenum param, ALfloat* value1, ALfloat* value2, ALfloat* value3) ;
174 FUNCTION: void alGetBufferfv ( ALuint bid, ALenum param, ALfloat* values ) ;
175 FUNCTION: void alGetBufferi ( ALuint bid, ALenum param, ALint* value ) ;
176 FUNCTION: void alGetBuffer3i ( ALuint bid, ALenum param, ALint* value1, ALint* value2, ALint* value3) ;
177 FUNCTION: void alGetBufferiv ( ALuint bid, ALenum param, ALint* values ) ;
178 FUNCTION: void alDopplerFactor ( ALfloat value ) ;
179 FUNCTION: void alDopplerVelocity ( ALfloat value ) ;
180 FUNCTION: void alSpeedOfSound ( ALfloat value ) ;
181 FUNCTION: void alDistanceModel ( ALenum distanceModel ) ;
185 : ALUT_API_MAJOR_VERSION ( -- number ) 1 ; inline
186 : ALUT_API_MINOR_VERSION ( -- number ) 1 ; inline
187 : ALUT_ERROR_NO_ERROR ( -- number ) 0 ; inline
188 : ALUT_ERROR_OUT_OF_MEMORY ( -- number ) HEX: 200 ; inline
189 : ALUT_ERROR_INVALID_ENUM ( -- number ) HEX: 201 ; inline
190 : ALUT_ERROR_INVALID_VALUE ( -- number ) HEX: 202 ; inline
191 : ALUT_ERROR_INVALID_OPERATION ( -- number ) HEX: 203 ; inline
192 : ALUT_ERROR_NO_CURRENT_CONTEXT ( -- number ) HEX: 204 ; inline
193 : ALUT_ERROR_AL_ERROR_ON_ENTRY ( -- number ) HEX: 205 ; inline
194 : ALUT_ERROR_ALC_ERROR_ON_ENTRY ( -- number ) HEX: 206 ; inline
195 : ALUT_ERROR_OPEN_DEVICE ( -- number ) HEX: 207 ; inline
196 : ALUT_ERROR_CLOSE_DEVICE ( -- number ) HEX: 208 ; inline
197 : ALUT_ERROR_CREATE_CONTEXT ( -- number ) HEX: 209 ; inline
198 : ALUT_ERROR_MAKE_CONTEXT_CURRENT ( -- number ) HEX: 20A ; inline
199 : ALUT_ERROR_DESTRY_CONTEXT ( -- number ) HEX: 20B ; inline
200 : ALUT_ERROR_GEN_BUFFERS ( -- number ) HEX: 20C ; inline
201 : ALUT_ERROR_BUFFER_DATA ( -- number ) HEX: 20D ; inline
202 : ALUT_ERROR_IO_ERROR ( -- number ) HEX: 20E ; inline
203 : ALUT_ERROR_UNSUPPORTED_FILE_TYPE ( -- number ) HEX: 20F ; inline
204 : ALUT_ERROR_UNSUPPORTED_FILE_SUBTYPE ( -- number ) HEX: 210 ; inline
205 : ALUT_ERROR_CORRUPT_OR_TRUNCATED_DATA ( -- number ) HEX: 211 ; inline
206 : ALUT_WAVEFORM_SINE ( -- number ) HEX: 100 ; inline
207 : ALUT_WAVEFORM_SQUARE ( -- number ) HEX: 101 ; inline
208 : ALUT_WAVEFORM_SAWTOOTH ( -- number ) HEX: 102 ; inline
209 : ALUT_WAVEFORM_WHITENOISE ( -- number ) HEX: 103 ; inline
210 : ALUT_WAVEFORM_IMPULSE ( -- number ) HEX: 104 ; inline
211 : ALUT_LOADER_BUFFER ( -- number ) HEX: 300 ; inline
212 : ALUT_LOADER_MEMORY ( -- number ) HEX: 301 ; inline
214 FUNCTION: ALboolean alutInit ( int* argcp, char** argv ) ;
215 FUNCTION: ALboolean alutInitWithoutContext ( int* argcp, char** argv ) ;
216 FUNCTION: ALboolean alutExit ( ) ;
217 FUNCTION: ALenum alutGetError ( ) ;
218 FUNCTION: char* alutGetErrorString ( ALenum error ) ;
219 FUNCTION: ALuint alutCreateBufferFromFile ( char* fileName ) ;
220 FUNCTION: ALuint alutCreateBufferFromFileImage ( void* data, ALsizei length ) ;
221 FUNCTION: ALuint alutCreateBufferHelloWorld ( ) ;
222 FUNCTION: ALuint alutCreateBufferWaveform ( ALenum waveshape, ALfloat frequency, ALfloat phase, ALfloat duration ) ;
223 FUNCTION: void* alutLoadMemoryFromFile ( char* fileName, ALenum* format, ALsizei* size, ALfloat* frequency ) ;
224 FUNCTION: void* alutLoadMemoryFromFileImage ( void* data, ALsizei length, ALenum* format, ALsizei* size, ALfloat* frequency ) ;
225 FUNCTION: void* alutLoadMemoryHelloWorld ( ALenum* format, ALsizei* size, ALfloat* frequency ) ;
226 FUNCTION: void* alutLoadMemoryWaveform ( ALenum waveshape, ALfloat frequency, ALfloat phase, ALfloat duration, ALenum* format, ALsizei* size, ALfloat* freq ) ;
227 FUNCTION: char* alutGetMIMETypes ( ALenum loader ) ;
228 FUNCTION: ALint alutGetMajorVersion ( ) ;
229 FUNCTION: ALint alutGetMinorVersion ( ) ;
230 FUNCTION: ALboolean alutSleep ( ALfloat duration ) ;
232 FUNCTION: void alutUnloadWAV ( ALenum format, void* data, ALsizei size, ALsizei frequency ) ;
237 init get-global expired? [
238 f f alutInit 0 = [ "Could not initialize OpenAL" throw ] when
239 1337 <alien> init set-global
243 init get-global expired? [
244 alutExit 0 = [ "Could not close OpenAL" throw ] when
248 : <uint-array> ( n -- byte-array ) "ALuint" <c-array> ;
250 : gen-sources ( size -- seq )
251 dup <uint-array> 2dup underlying>> alGenSources swap ;
253 : gen-buffers ( size -- seq )
254 dup <uint-array> 2dup underlying>> alGenBuffers swap ;
256 : gen-buffer ( -- buffer ) 1 gen-buffers first ;
258 : create-buffer-from-file ( filename -- buffer )
259 alutCreateBufferFromFile dup AL_NONE = [
260 "create-buffer-from-file failed" throw
263 os macosx? "openal.macosx" "openal.other" ? require
265 : create-buffer-from-wav ( filename -- buffer )
266 gen-buffer dup rot load-wav-file
267 [ alBufferData ] 4keep alutUnloadWAV ;
269 : queue-buffers ( source buffers -- )
270 [ length ] [ >uint-array underlying>> ] bi alSourceQueueBuffers ;
272 : queue-buffer ( source buffer -- )
273 1array queue-buffers ;
275 : set-source-param ( source param value -- )
278 : get-source-param ( source param -- value )
279 0 <uint> dup >r alGetSourcei r> *uint ;
281 : set-buffer-param ( source param value -- )
284 : get-buffer-param ( source param -- value )
285 0 <uint> dup >r alGetBufferi r> *uint ;
287 : source-play ( source -- )
290 : source-stop ( source -- )
294 alGetError dup ALUT_ERROR_NO_ERROR = [
300 : source-playing? ( source -- bool )
301 AL_SOURCE_STATE get-source-param AL_PLAYING = ;