4 * Copyright (c) 2011 by Chris Robinson <chris.kcat@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* This file contains routines to help with some menial OpenAL-related tasks,
26 * such as opening a device and setting up a context, closing the device and
27 * destroying its context, converting between frame counts and byte lengths,
28 * finding an appropriate buffer format, and getting readable strings for
29 * channel configs and sample types. */
31 #include "alhelpers.h"
42 /* InitAL opens a device and sets up a context using default attributes, making
43 * the program ready to call OpenAL functions. */
44 int InitAL(char ***argv
, int *argc
)
50 /* Open and initialize a device */
52 if(argc
&& argv
&& *argc
> 1 && strcmp((*argv
)[0], "-device") == 0)
54 device
= alcOpenDevice((*argv
)[1]);
56 fprintf(stderr
, "Failed to open \"%s\", trying default\n", (*argv
)[1]);
61 device
= alcOpenDevice(NULL
);
64 fprintf(stderr
, "Could not open a device!\n");
68 ctx
= alcCreateContext(device
, NULL
);
69 if(ctx
== NULL
|| alcMakeContextCurrent(ctx
) == ALC_FALSE
)
72 alcDestroyContext(ctx
);
73 alcCloseDevice(device
);
74 fprintf(stderr
, "Could not set a context!\n");
79 if(alcIsExtensionPresent(device
, "ALC_ENUMERATE_ALL_EXT"))
80 name
= alcGetString(device
, ALC_ALL_DEVICES_SPECIFIER
);
81 if(!name
|| alcGetError(device
) != AL_NO_ERROR
)
82 name
= alcGetString(device
, ALC_DEVICE_SPECIFIER
);
83 printf("Opened \"%s\"\n", name
);
88 /* CloseAL closes the device belonging to the current context, and destroys the
95 ctx
= alcGetCurrentContext();
99 device
= alcGetContextsDevice(ctx
);
101 alcMakeContextCurrent(NULL
);
102 alcDestroyContext(ctx
);
103 alcCloseDevice(device
);
107 const char *FormatName(ALenum format
)
111 case AL_FORMAT_MONO8
: return "Mono, U8";
112 case AL_FORMAT_MONO16
: return "Mono, S16";
113 case AL_FORMAT_MONO_FLOAT32
: return "Mono, Float32";
114 case AL_FORMAT_STEREO8
: return "Stereo, U8";
115 case AL_FORMAT_STEREO16
: return "Stereo, S16";
116 case AL_FORMAT_STEREO_FLOAT32
: return "Stereo, Float32";
117 case AL_FORMAT_BFORMAT2D_8
: return "B-Format 2D, U8";
118 case AL_FORMAT_BFORMAT2D_16
: return "B-Format 2D, S16";
119 case AL_FORMAT_BFORMAT2D_FLOAT32
: return "B-Format 2D, Float32";
120 case AL_FORMAT_BFORMAT3D_8
: return "B-Format 3D, U8";
121 case AL_FORMAT_BFORMAT3D_16
: return "B-Format 3D, S16";
122 case AL_FORMAT_BFORMAT3D_FLOAT32
: return "B-Format 3D, Float32";
124 return "Unknown Format";
130 #define WIN32_LEAN_AND_MEAN
132 #include <mmsystem.h>
136 static int start_time
= 0;
140 ULARGE_INTEGER ulint
;
142 GetSystemTimeAsFileTime(&systime
.ftime
);
143 /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */
144 cur_time
= (int)(systime
.ulint
.QuadPart
/10000);
147 start_time
= cur_time
;
148 return cur_time
- start_time
;
151 void al_nssleep(unsigned long nsec
)
153 Sleep(nsec
/ 1000000);
158 #include <sys/time.h>
164 static int start_time
= 0u;
167 #if _POSIX_TIMERS > 0
169 int ret
= clock_gettime(CLOCK_REALTIME
, &ts
);
170 if(ret
!= 0) return 0;
171 cur_time
= (int)(ts
.tv_sec
*1000 + ts
.tv_nsec
/1000000);
172 #else /* _POSIX_TIMERS > 0 */
174 int ret
= gettimeofday(&tv
, NULL
);
175 if(ret
!= 0) return 0;
176 cur_time
= (int)(tv
.tv_sec
*1000 + tv
.tv_usec
/1000);
180 start_time
= cur_time
;
181 return cur_time
- start_time
;
184 void al_nssleep(unsigned long nsec
)
186 struct timespec ts
, rem
;
187 ts
.tv_sec
= (time_t)(nsec
/ 1000000000ul);
188 ts
.tv_nsec
= (long)(nsec
% 1000000000ul);
189 while(nanosleep(&ts
, &rem
) == -1 && errno
== EINTR
)