Recognize %-encoded characters for config section names
[openal-soft.git] / examples / alloopback.c
blob95ac433f7221b2083f63f9e7202a0a3bc34a1cbf
1 /*
2 * OpenAL Loopback Example
4 * Copyright (c) 2013 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 an example for using the loopback device for custom
26 * output handling.
29 #include <stdio.h>
30 #include <assert.h>
31 #include <math.h>
33 #include <SDL.h>
35 #include "AL/al.h"
36 #include "AL/alc.h"
37 #include "AL/alext.h"
39 #include "common/alhelpers.h"
41 #ifndef M_PI
42 #define M_PI (3.14159265358979323846)
43 #endif
45 typedef struct {
46 ALCdevice *Device;
47 ALCcontext *Context;
49 ALCsizei FrameSize;
50 } PlaybackInfo;
52 static LPALCLOOPBACKOPENDEVICESOFT alcLoopbackOpenDeviceSOFT;
53 static LPALCISRENDERFORMATSUPPORTEDSOFT alcIsRenderFormatSupportedSOFT;
54 static LPALCRENDERSAMPLESSOFT alcRenderSamplesSOFT;
57 void SDLCALL RenderSDLSamples(void *userdata, Uint8 *stream, int len)
59 PlaybackInfo *playback = (PlaybackInfo*)userdata;
60 alcRenderSamplesSOFT(playback->Device, stream, len/playback->FrameSize);
64 static const char *ChannelsName(ALCenum chans)
66 switch(chans)
68 case ALC_MONO_SOFT: return "Mono";
69 case ALC_STEREO_SOFT: return "Stereo";
70 case ALC_QUAD_SOFT: return "Quadraphonic";
71 case ALC_5POINT1_SOFT: return "5.1 Surround";
72 case ALC_6POINT1_SOFT: return "6.1 Surround";
73 case ALC_7POINT1_SOFT: return "7.1 Surround";
75 return "Unknown Channels";
78 static const char *TypeName(ALCenum type)
80 switch(type)
82 case ALC_BYTE_SOFT: return "S8";
83 case ALC_UNSIGNED_BYTE_SOFT: return "U8";
84 case ALC_SHORT_SOFT: return "S16";
85 case ALC_UNSIGNED_SHORT_SOFT: return "U16";
86 case ALC_INT_SOFT: return "S32";
87 case ALC_UNSIGNED_INT_SOFT: return "U32";
88 case ALC_FLOAT_SOFT: return "Float32";
90 return "Unknown Type";
93 /* Creates a one second buffer containing a sine wave, and returns the new
94 * buffer ID. */
95 static ALuint CreateSineWave(void)
97 ALshort data[44100*4];
98 ALuint buffer;
99 ALenum err;
100 ALuint i;
102 for(i = 0;i < 44100*4;i++)
103 data[i] = (ALshort)(sin(i/44100.0 * 1000.0 * 2.0*M_PI) * 32767.0);
105 /* Buffer the audio data into a new buffer object. */
106 buffer = 0;
107 alGenBuffers(1, &buffer);
108 alBufferData(buffer, AL_FORMAT_MONO16, data, sizeof(data), 44100);
110 /* Check if an error occured, and clean up if so. */
111 err = alGetError();
112 if(err != AL_NO_ERROR)
114 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
115 if(alIsBuffer(buffer))
116 alDeleteBuffers(1, &buffer);
117 return 0;
120 return buffer;
124 int main(int argc, char *argv[])
126 PlaybackInfo playback = { NULL, NULL, 0 };
127 SDL_AudioSpec desired, obtained;
128 ALuint source, buffer;
129 ALCint attrs[16];
130 ALenum state;
131 (void)argc;
132 (void)argv;
134 /* Print out error if extension is missing. */
135 if(!alcIsExtensionPresent(NULL, "ALC_SOFT_loopback"))
137 fprintf(stderr, "Error: ALC_SOFT_loopback not supported!\n");
138 return 1;
141 /* Define a macro to help load the function pointers. */
142 #define LOAD_PROC(x) ((x) = alcGetProcAddress(NULL, #x))
143 LOAD_PROC(alcLoopbackOpenDeviceSOFT);
144 LOAD_PROC(alcIsRenderFormatSupportedSOFT);
145 LOAD_PROC(alcRenderSamplesSOFT);
146 #undef LOAD_PROC
148 if(SDL_Init(SDL_INIT_AUDIO) == -1)
150 fprintf(stderr, "Failed to init SDL audio: %s\n", SDL_GetError());
151 return 1;
154 /* Set up SDL audio with our requested format and callback. */
155 desired.channels = 2;
156 desired.format = AUDIO_S16SYS;
157 desired.freq = 44100;
158 desired.padding = 0;
159 desired.samples = 4096;
160 desired.callback = RenderSDLSamples;
161 desired.userdata = &playback;
162 if(SDL_OpenAudio(&desired, &obtained) != 0)
164 SDL_Quit();
165 fprintf(stderr, "Failed to open SDL audio: %s\n", SDL_GetError());
166 return 1;
169 /* Set up our OpenAL attributes based on what we got from SDL. */
170 attrs[0] = ALC_FORMAT_CHANNELS_SOFT;
171 if(obtained.channels == 1)
172 attrs[1] = ALC_MONO_SOFT;
173 else if(obtained.channels == 2)
174 attrs[1] = ALC_STEREO_SOFT;
175 else
177 fprintf(stderr, "Unhandled SDL channel count: %d\n", obtained.channels);
178 goto error;
181 attrs[2] = ALC_FORMAT_TYPE_SOFT;
182 if(obtained.format == AUDIO_U8)
183 attrs[3] = ALC_UNSIGNED_BYTE_SOFT;
184 else if(obtained.format == AUDIO_S8)
185 attrs[3] = ALC_BYTE_SOFT;
186 else if(obtained.format == AUDIO_U16SYS)
187 attrs[3] = ALC_UNSIGNED_SHORT_SOFT;
188 else if(obtained.format == AUDIO_S16SYS)
189 attrs[3] = ALC_SHORT_SOFT;
190 else
192 fprintf(stderr, "Unhandled SDL format: 0x%04x\n", obtained.format);
193 goto error;
196 attrs[4] = ALC_FREQUENCY;
197 attrs[5] = obtained.freq;
199 attrs[6] = 0; /* end of list */
201 playback.FrameSize = obtained.channels * SDL_AUDIO_BITSIZE(obtained.format) / 8;
203 /* Initialize OpenAL loopback device, using our format attributes. */
204 playback.Device = alcLoopbackOpenDeviceSOFT(NULL);
205 if(!playback.Device)
207 fprintf(stderr, "Failed to open loopback device!\n");
208 goto error;
210 /* Make sure the format is supported before setting them on the device. */
211 if(alcIsRenderFormatSupportedSOFT(playback.Device, attrs[5], attrs[1], attrs[3]) == ALC_FALSE)
213 fprintf(stderr, "Render format not supported: %s, %s, %dhz\n",
214 ChannelsName(attrs[1]), TypeName(attrs[3]), attrs[5]);
215 goto error;
217 playback.Context = alcCreateContext(playback.Device, attrs);
218 if(!playback.Context || alcMakeContextCurrent(playback.Context) == ALC_FALSE)
220 fprintf(stderr, "Failed to set an OpenAL audio context\n");
221 goto error;
224 /* Start SDL playing. Our callback (thus alcRenderSamplesSOFT) will now
225 * start being called regularly to update the AL playback state. */
226 SDL_PauseAudio(0);
228 /* Load the sound into a buffer. */
229 buffer = CreateSineWave();
230 if(!buffer)
232 SDL_CloseAudio();
233 alcDestroyContext(playback.Context);
234 alcCloseDevice(playback.Device);
235 SDL_Quit();
236 return 1;
239 /* Create the source to play the sound with. */
240 source = 0;
241 alGenSources(1, &source);
242 alSourcei(source, AL_BUFFER, buffer);
243 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
245 /* Play the sound until it finishes. */
246 alSourcePlay(source);
247 do {
248 al_nssleep(10000000);
249 alGetSourcei(source, AL_SOURCE_STATE, &state);
250 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
252 /* All done. Delete resources, and close OpenAL. */
253 alDeleteSources(1, &source);
254 alDeleteBuffers(1, &buffer);
256 /* Stop SDL playing. */
257 SDL_PauseAudio(1);
259 /* Close up OpenAL and SDL. */
260 SDL_CloseAudio();
261 alcDestroyContext(playback.Context);
262 alcCloseDevice(playback.Device);
263 SDL_Quit();
265 return 0;
267 error:
268 SDL_CloseAudio();
269 if(playback.Context)
270 alcDestroyContext(playback.Context);
271 if(playback.Device)
272 alcCloseDevice(playback.Device);
273 SDL_Quit();
275 return 1;