1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/*
* OpenAL Helpers
*
* Copyright (c) 2011 by Chris Robinson <chris.kcat@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* This file contains routines to help with some menial OpenAL-related tasks,
* such as opening a device and setting up a context, closing the device and
* destroying its context, converting between frame counts and byte lengths,
* finding an appropriate buffer format, and getting readable strings for
* channel configs and sample types. */
#include "alhelpers.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "AL/al.h"
#include "AL/alc.h"
/* InitAL opens a device and sets up a context using default attributes, making
* the program ready to call OpenAL functions. */
int InitAL(char ***argv, int *argc)
{
const ALCchar *name;
ALCdevice *device;
ALCcontext *ctx;
/* Open and initialize a device */
device = NULL;
if(argc && argv && *argc > 1 && strcmp((*argv)[0], "-device") == 0)
{
device = alcOpenDevice((*argv)[1]);
if(!device)
fprintf(stderr, "Failed to open \"%s\", trying default\n", (*argv)[1]);
(*argv) += 2;
(*argc) -= 2;
}
if(!device)
device = alcOpenDevice(NULL);
if(!device)
{
fprintf(stderr, "Could not open a device!\n");
return 1;
}
ctx = alcCreateContext(device, NULL);
if(ctx == NULL || alcMakeContextCurrent(ctx) == ALC_FALSE)
{
if(ctx != NULL)
alcDestroyContext(ctx);
alcCloseDevice(device);
fprintf(stderr, "Could not set a context!\n");
return 1;
}
name = NULL;
if(alcIsExtensionPresent(device, "ALC_ENUMERATE_ALL_EXT"))
name = alcGetString(device, ALC_ALL_DEVICES_SPECIFIER);
if(!name || alcGetError(device) != AL_NO_ERROR)
name = alcGetString(device, ALC_DEVICE_SPECIFIER);
printf("Opened \"%s\"\n", name);
return 0;
}
/* CloseAL closes the device belonging to the current context, and destroys the
* context. */
void CloseAL(void)
{
ALCdevice *device;
ALCcontext *ctx;
ctx = alcGetCurrentContext();
if(ctx == NULL)
return;
device = alcGetContextsDevice(ctx);
alcMakeContextCurrent(NULL);
alcDestroyContext(ctx);
alcCloseDevice(device);
}
const char *FormatName(ALenum format)
{
switch(format)
{
case AL_FORMAT_MONO8: return "Mono, U8";
case AL_FORMAT_MONO16: return "Mono, S16";
case AL_FORMAT_STEREO8: return "Stereo, U8";
case AL_FORMAT_STEREO16: return "Stereo, S16";
}
return "Unknown Format";
}
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
int altime_get(void)
{
static int start_time = 0;
int cur_time;
union {
FILETIME ftime;
ULARGE_INTEGER ulint;
} systime;
GetSystemTimeAsFileTime(&systime.ftime);
/* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */
cur_time = (int)(systime.ulint.QuadPart/10000);
if(!start_time)
start_time = cur_time;
return cur_time - start_time;
}
void al_nssleep(unsigned long nsec)
{
Sleep(nsec / 1000000);
}
#else
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
int altime_get(void)
{
static int start_time = 0u;
int cur_time;
#if _POSIX_TIMERS > 0
struct timespec ts;
int ret = clock_gettime(CLOCK_REALTIME, &ts);
if(ret != 0) return 0;
cur_time = (int)(ts.tv_sec*1000 + ts.tv_nsec/1000000);
#else /* _POSIX_TIMERS > 0 */
struct timeval tv;
int ret = gettimeofday(&tv, NULL);
if(ret != 0) return 0;
cur_time = (int)(tv.tv_sec*1000 + tv.tv_usec/1000);
#endif
if(!start_time)
start_time = cur_time;
return cur_time - start_time;
}
void al_nssleep(unsigned long nsec)
{
struct timespec ts, rem;
ts.tv_sec = nsec / 1000000000ul;
ts.tv_nsec = nsec % 1000000000ul;
while(nanosleep(&ts, &rem) == -1 && errno == EINTR)
ts = rem;
}
#endif
|