Simplify bsinc filter storage in the filter state
[openal-soft.git] / examples / common / alhelpers.c
blobfab039e90224b694b81dc7506673abfead2a6f2c
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 <stdio.h>
32 #include <string.h>
34 #include "AL/al.h"
35 #include "AL/alc.h"
36 #include "AL/alext.h"
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)
45 const ALCchar *name;
46 ALCdevice *device;
47 ALCcontext *ctx;
49 /* Open and initialize a device */
50 device = NULL;
51 if(argc && argv && *argc > 1 && strcmp((*argv)[0], "-device") == 0)
53 device = alcOpenDevice((*argv)[1]);
54 if(!device)
55 fprintf(stderr, "Failed to open \"%s\", trying default\n", (*argv)[1]);
56 (*argv) += 2;
57 (*argc) -= 2;
59 if(!device)
60 device = alcOpenDevice(NULL);
61 if(!device)
63 fprintf(stderr, "Could not open a device!\n");
64 return 1;
67 ctx = alcCreateContext(device, NULL);
68 if(ctx == NULL || alcMakeContextCurrent(ctx) == ALC_FALSE)
70 if(ctx != NULL)
71 alcDestroyContext(ctx);
72 alcCloseDevice(device);
73 fprintf(stderr, "Could not set a context!\n");
74 return 1;
77 name = NULL;
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);
84 return 0;
87 /* CloseAL closes the device belonging to the current context, and destroys the
88 * context. */
89 void CloseAL(void)
91 ALCdevice *device;
92 ALCcontext *ctx;
94 ctx = alcGetCurrentContext();
95 if(ctx == NULL)
96 return;
98 device = alcGetContextsDevice(ctx);
100 alcMakeContextCurrent(NULL);
101 alcDestroyContext(ctx);
102 alcCloseDevice(device);
106 const char *FormatName(ALenum format)
108 switch(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";