Reorganize some convolution fields
[openal-soft.git] / examples / common / alhelpers.c
blobc432ea46bcf90e5178b184bd5f5da415b27435af
1 /*
2 * OpenAL Helpers
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
22 * THE SOFTWARE.
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"
33 #include <stdio.h>
34 #include <errno.h>
35 #include <string.h>
37 #include "AL/al.h"
38 #include "AL/alc.h"
39 #include "AL/alext.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)
46 const ALCchar *name;
47 ALCdevice *device;
48 ALCcontext *ctx;
50 /* Open and initialize a device */
51 device = NULL;
52 if(argc && argv && *argc > 1 && strcmp((*argv)[0], "-device") == 0)
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;
60 if(!device)
61 device = alcOpenDevice(NULL);
62 if(!device)
64 fprintf(stderr, "Could not open a device!\n");
65 return 1;
68 ctx = alcCreateContext(device, NULL);
69 if(ctx == NULL || alcMakeContextCurrent(ctx) == ALC_FALSE)
71 if(ctx != NULL)
72 alcDestroyContext(ctx);
73 alcCloseDevice(device);
74 fprintf(stderr, "Could not set a context!\n");
75 return 1;
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);
85 return 0;
88 /* CloseAL closes the device belonging to the current context, and destroys the
89 * context. */
90 void CloseAL(void)
92 ALCdevice *device;
93 ALCcontext *ctx;
95 ctx = alcGetCurrentContext();
96 if(ctx == NULL)
97 return;
99 device = alcGetContextsDevice(ctx);
101 alcMakeContextCurrent(NULL);
102 alcDestroyContext(ctx);
103 alcCloseDevice(device);
107 const char *FormatName(ALenum format)
109 switch(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";
118 return "Unknown Format";
122 #ifdef _WIN32
124 #define WIN32_LEAN_AND_MEAN
125 #include <windows.h>
126 #include <mmsystem.h>
128 int altime_get(void)
130 static int start_time = 0;
131 int cur_time;
132 union {
133 FILETIME ftime;
134 ULARGE_INTEGER ulint;
135 } systime;
136 GetSystemTimeAsFileTime(&systime.ftime);
137 /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */
138 cur_time = (int)(systime.ulint.QuadPart/10000);
140 if(!start_time)
141 start_time = cur_time;
142 return cur_time - start_time;
145 void al_nssleep(unsigned long nsec)
147 Sleep(nsec / 1000000);
150 #else
152 #include <sys/time.h>
153 #include <unistd.h>
154 #include <time.h>
156 int altime_get(void)
158 static int start_time = 0u;
159 int cur_time;
161 #if _POSIX_TIMERS > 0
162 struct timespec ts;
163 int ret = clock_gettime(CLOCK_REALTIME, &ts);
164 if(ret != 0) return 0;
165 cur_time = (int)(ts.tv_sec*1000 + ts.tv_nsec/1000000);
166 #else /* _POSIX_TIMERS > 0 */
167 struct timeval tv;
168 int ret = gettimeofday(&tv, NULL);
169 if(ret != 0) return 0;
170 cur_time = (int)(tv.tv_sec*1000 + tv.tv_usec/1000);
171 #endif
173 if(!start_time)
174 start_time = cur_time;
175 return cur_time - start_time;
178 void al_nssleep(unsigned long nsec)
180 struct timespec ts, rem;
181 ts.tv_sec = (time_t)(nsec / 1000000000ul);
182 ts.tv_nsec = (long)(nsec % 1000000000ul);
183 while(nanosleep(&ts, &rem) == -1 && errno == EINTR)
184 ts = rem;
187 #endif