]> git.tdb.fi Git - ext/openal.git/blob - examples/common/alhelpers.c
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / examples / common / alhelpers.c
1 /*
2  * OpenAL Helpers
3  *
4  * Copyright (c) 2011 by Chris Robinson <chris.kcat@gmail.com>
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  */
24
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. */
30
31 #include "alhelpers.h"
32
33 #include <stdio.h>
34 #include <errno.h>
35 #include <string.h>
36
37 #include "AL/al.h"
38 #include "AL/alc.h"
39 #include "AL/alext.h"
40
41
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)
45 {
46     const ALCchar *name;
47     ALCdevice *device;
48     ALCcontext *ctx;
49
50     /* Open and initialize a device */
51     device = NULL;
52     if(argc && argv && *argc > 1 && strcmp((*argv)[0], "-device") == 0)
53     {
54         device = alcOpenDevice((*argv)[1]);
55         if(!device)
56             fprintf(stderr, "Failed to open \"%s\", trying default\n", (*argv)[1]);
57         (*argv) += 2;
58         (*argc) -= 2;
59     }
60     if(!device)
61         device = alcOpenDevice(NULL);
62     if(!device)
63     {
64         fprintf(stderr, "Could not open a device!\n");
65         return 1;
66     }
67
68     ctx = alcCreateContext(device, NULL);
69     if(ctx == NULL || alcMakeContextCurrent(ctx) == ALC_FALSE)
70     {
71         if(ctx != NULL)
72             alcDestroyContext(ctx);
73         alcCloseDevice(device);
74         fprintf(stderr, "Could not set a context!\n");
75         return 1;
76     }
77
78     name = NULL;
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);
84
85     return 0;
86 }
87
88 /* CloseAL closes the device belonging to the current context, and destroys the
89  * context. */
90 void CloseAL(void)
91 {
92     ALCdevice *device;
93     ALCcontext *ctx;
94
95     ctx = alcGetCurrentContext();
96     if(ctx == NULL)
97         return;
98
99     device = alcGetContextsDevice(ctx);
100
101     alcMakeContextCurrent(NULL);
102     alcDestroyContext(ctx);
103     alcCloseDevice(device);
104 }
105
106
107 const char *FormatName(ALenum format)
108 {
109     switch(format)
110     {
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_MONO_MULAW: return "Mono, muLaw";
115     case AL_FORMAT_MONO_ALAW_EXT: return "Mono, aLaw";
116     case AL_FORMAT_MONO_IMA4: return "Mono, IMA4 ADPCM";
117     case AL_FORMAT_MONO_MSADPCM_SOFT: return "Mono, MS ADPCM";
118     case AL_FORMAT_STEREO8: return "Stereo, U8";
119     case AL_FORMAT_STEREO16: return "Stereo, S16";
120     case AL_FORMAT_STEREO_FLOAT32: return "Stereo, Float32";
121     case AL_FORMAT_STEREO_MULAW: return "Stereo, muLaw";
122     case AL_FORMAT_STEREO_ALAW_EXT: return "Stereo, aLaw";
123     case AL_FORMAT_STEREO_IMA4: return "Stereo, IMA4 ADPCM";
124     case AL_FORMAT_STEREO_MSADPCM_SOFT: return "Stereo, MS ADPCM";
125     case AL_FORMAT_QUAD8: return "Quadraphonic, U8";
126     case AL_FORMAT_QUAD16: return "Quadraphonic, S16";
127     case AL_FORMAT_QUAD32: return "Quadraphonic, Float32";
128     case AL_FORMAT_QUAD_MULAW: return "Quadraphonic, muLaw";
129     case AL_FORMAT_51CHN8: return "5.1 Surround, U8";
130     case AL_FORMAT_51CHN16: return "5.1 Surround, S16";
131     case AL_FORMAT_51CHN32: return "5.1 Surround, Float32";
132     case AL_FORMAT_51CHN_MULAW: return "5.1 Surround, muLaw";
133     case AL_FORMAT_61CHN8: return "6.1 Surround, U8";
134     case AL_FORMAT_61CHN16: return "6.1 Surround, S16";
135     case AL_FORMAT_61CHN32: return "6.1 Surround, Float32";
136     case AL_FORMAT_61CHN_MULAW: return "6.1 Surround, muLaw";
137     case AL_FORMAT_71CHN8: return "7.1 Surround, U8";
138     case AL_FORMAT_71CHN16: return "7.1 Surround, S16";
139     case AL_FORMAT_71CHN32: return "7.1 Surround, Float32";
140     case AL_FORMAT_71CHN_MULAW: return "7.1 Surround, muLaw";
141     case AL_FORMAT_BFORMAT2D_8: return "B-Format 2D, U8";
142     case AL_FORMAT_BFORMAT2D_16: return "B-Format 2D, S16";
143     case AL_FORMAT_BFORMAT2D_FLOAT32: return "B-Format 2D, Float32";
144     case AL_FORMAT_BFORMAT2D_MULAW: return "B-Format 2D, muLaw";
145     case AL_FORMAT_BFORMAT3D_8: return "B-Format 3D, U8";
146     case AL_FORMAT_BFORMAT3D_16: return "B-Format 3D, S16";
147     case AL_FORMAT_BFORMAT3D_FLOAT32: return "B-Format 3D, Float32";
148     case AL_FORMAT_BFORMAT3D_MULAW: return "B-Format 3D, muLaw";
149     case AL_FORMAT_UHJ2CHN8_SOFT: return "UHJ 2-channel, U8";
150     case AL_FORMAT_UHJ2CHN16_SOFT: return "UHJ 2-channel, S16";
151     case AL_FORMAT_UHJ2CHN_FLOAT32_SOFT: return "UHJ 2-channel, Float32";
152     case AL_FORMAT_UHJ3CHN8_SOFT: return "UHJ 3-channel, U8";
153     case AL_FORMAT_UHJ3CHN16_SOFT: return "UHJ 3-channel, S16";
154     case AL_FORMAT_UHJ3CHN_FLOAT32_SOFT: return "UHJ 3-channel, Float32";
155     case AL_FORMAT_UHJ4CHN8_SOFT: return "UHJ 4-channel, U8";
156     case AL_FORMAT_UHJ4CHN16_SOFT: return "UHJ 4-channel, S16";
157     case AL_FORMAT_UHJ4CHN_FLOAT32_SOFT: return "UHJ 4-channel, Float32";
158     }
159     return "Unknown Format";
160 }
161
162
163 #ifdef _WIN32
164
165 #define WIN32_LEAN_AND_MEAN
166 #include <windows.h>
167 #include <mmsystem.h>
168
169 int altime_get(void)
170 {
171     static int start_time = 0;
172     int cur_time;
173     union {
174         FILETIME ftime;
175         ULARGE_INTEGER ulint;
176     } systime;
177     GetSystemTimeAsFileTime(&systime.ftime);
178     /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */
179     cur_time = (int)(systime.ulint.QuadPart/10000);
180
181     if(!start_time)
182         start_time = cur_time;
183     return cur_time - start_time;
184 }
185
186 void al_nssleep(unsigned long nsec)
187 {
188     Sleep(nsec / 1000000);
189 }
190
191 #else
192
193 #include <sys/time.h>
194 #include <unistd.h>
195 #include <time.h>
196
197 int altime_get(void)
198 {
199     static int start_time = 0u;
200     int cur_time;
201
202 #if _POSIX_TIMERS > 0
203     struct timespec ts;
204     int ret = clock_gettime(CLOCK_REALTIME, &ts);
205     if(ret != 0) return 0;
206     cur_time = (int)(ts.tv_sec*1000 + ts.tv_nsec/1000000);
207 #else /* _POSIX_TIMERS > 0 */
208     struct timeval tv;
209     int ret = gettimeofday(&tv, NULL);
210     if(ret != 0) return 0;
211     cur_time = (int)(tv.tv_sec*1000 + tv.tv_usec/1000);
212 #endif
213
214     if(!start_time)
215         start_time = cur_time;
216     return cur_time - start_time;
217 }
218
219 void al_nssleep(unsigned long nsec)
220 {
221     struct timespec ts, rem;
222     ts.tv_sec = (time_t)(nsec / 1000000000ul);
223     ts.tv_nsec = (long)(nsec % 1000000000ul);
224     while(nanosleep(&ts, &rem) == -1 && errno == EINTR)
225         ts = rem;
226 }
227
228 #endif