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. */
38 #include "alhelpers.h"
41 /* InitAL opens a device and sets up a context using default attributes, making
42 * the program ready to call OpenAL functions. */
43 int InitAL(char ***argv
, int *argc
)
49 /* Open and initialize a device */
51 if(argc
&& argv
&& *argc
> 1 && strcmp((*argv
)[0], "-device") == 0)
53 device
= alcOpenDevice((*argv
)[1]);
55 fprintf(stderr
, "Failed to open \"%s\", trying default\n", (*argv
)[1]);
60 device
= alcOpenDevice(NULL
);
63 fprintf(stderr
, "Could not open a device!\n");
67 ctx
= alcCreateContext(device
, NULL
);
68 if(ctx
== NULL
|| alcMakeContextCurrent(ctx
) == ALC_FALSE
)
71 alcDestroyContext(ctx
);
72 alcCloseDevice(device
);
73 fprintf(stderr
, "Could not set a context!\n");
78 if(alcIsExtensionPresent(device
, "ALC_ENUMERATE_ALL_EXT"))
79 name
= alcGetString(device
, ALC_ALL_DEVICES_SPECIFIER
);
80 if(!name
|| alcGetError(device
) != AL_NO_ERROR
)
81 name
= alcGetString(device
, ALC_DEVICE_SPECIFIER
);
82 printf("Opened \"%s\"\n", name
);
87 /* CloseAL closes the device belonging to the current context, and destroys the
94 ctx
= alcGetCurrentContext();
98 device
= alcGetContextsDevice(ctx
);
100 alcMakeContextCurrent(NULL
);
101 alcDestroyContext(ctx
);
102 alcCloseDevice(device
);
106 const char *FormatName(ALenum format
)
110 case AL_FORMAT_MONO8
: return "Mono, U8";
111 case AL_FORMAT_MONO16
: return "Mono, S16";
112 case AL_FORMAT_STEREO8
: return "Stereo, U8";
113 case AL_FORMAT_STEREO16
: return "Stereo, S16";
115 return "Unknown Format";