Use ALsizei for some count/length variables
[openal-soft.git] / examples / alloopback.c
blob16553f9b619ef9693ab558b6afe5d17f6311b7c4
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 SDL_AUDIO_MASK_BITSIZE
42 #define SDL_AUDIO_MASK_BITSIZE (0xFF)
43 #endif
44 #ifndef SDL_AUDIO_BITSIZE
45 #define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
46 #endif
48 #ifndef M_PI
49 #define M_PI (3.14159265358979323846)
50 #endif
52 typedef struct {
53 ALCdevice *Device;
54 ALCcontext *Context;
56 ALCsizei FrameSize;
57 } PlaybackInfo;
59 static LPALCLOOPBACKOPENDEVICESOFT alcLoopbackOpenDeviceSOFT;
60 static LPALCISRENDERFORMATSUPPORTEDSOFT alcIsRenderFormatSupportedSOFT;
61 static LPALCRENDERSAMPLESSOFT alcRenderSamplesSOFT;
64 void SDLCALL RenderSDLSamples(void *userdata, Uint8 *stream, int len)
66 PlaybackInfo *playback = (PlaybackInfo*)userdata;
67 alcRenderSamplesSOFT(playback->Device, stream, len/playback->FrameSize);
71 static const char *ChannelsName(ALCenum chans)
73 switch(chans)
75 case ALC_MONO_SOFT: return "Mono";
76 case ALC_STEREO_SOFT: return "Stereo";
77 case ALC_QUAD_SOFT: return "Quadraphonic";
78 case ALC_5POINT1_SOFT: return "5.1 Surround";
79 case ALC_6POINT1_SOFT: return "6.1 Surround";
80 case ALC_7POINT1_SOFT: return "7.1 Surround";
82 return "Unknown Channels";
85 static const char *TypeName(ALCenum type)
87 switch(type)
89 case ALC_BYTE_SOFT: return "S8";
90 case ALC_UNSIGNED_BYTE_SOFT: return "U8";
91 case ALC_SHORT_SOFT: return "S16";
92 case ALC_UNSIGNED_SHORT_SOFT: return "U16";
93 case ALC_INT_SOFT: return "S32";
94 case ALC_UNSIGNED_INT_SOFT: return "U32";
95 case ALC_FLOAT_SOFT: return "Float32";
97 return "Unknown Type";
100 /* Creates a one second buffer containing a sine wave, and returns the new
101 * buffer ID. */
102 static ALuint CreateSineWave(void)
104 ALshort data[44100*4];
105 ALuint buffer;
106 ALenum err;
107 ALuint i;
109 for(i = 0;i < 44100*4;i++)
110 data[i] = (ALshort)(sin(i/44100.0 * 1000.0 * 2.0*M_PI) * 32767.0);
112 /* Buffer the audio data into a new buffer object. */
113 buffer = 0;
114 alGenBuffers(1, &buffer);
115 alBufferData(buffer, AL_FORMAT_MONO16, data, sizeof(data), 44100);
117 /* Check if an error occured, and clean up if so. */
118 err = alGetError();
119 if(err != AL_NO_ERROR)
121 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
122 if(alIsBuffer(buffer))
123 alDeleteBuffers(1, &buffer);
124 return 0;
127 return buffer;
131 int main(int argc, char *argv[])
133 PlaybackInfo playback = { NULL, NULL, 0 };
134 SDL_AudioSpec desired, obtained;
135 ALuint source, buffer;
136 ALCint attrs[16];
137 ALenum state;
138 (void)argc;
139 (void)argv;
141 /* Print out error if extension is missing. */
142 if(!alcIsExtensionPresent(NULL, "ALC_SOFT_loopback"))
144 fprintf(stderr, "Error: ALC_SOFT_loopback not supported!\n");
145 return 1;
148 /* Define a macro to help load the function pointers. */
149 #define LOAD_PROC(x) ((x) = alcGetProcAddress(NULL, #x))
150 LOAD_PROC(alcLoopbackOpenDeviceSOFT);
151 LOAD_PROC(alcIsRenderFormatSupportedSOFT);
152 LOAD_PROC(alcRenderSamplesSOFT);
153 #undef LOAD_PROC
155 if(SDL_Init(SDL_INIT_AUDIO) == -1)
157 fprintf(stderr, "Failed to init SDL audio: %s\n", SDL_GetError());
158 return 1;
161 /* Set up SDL audio with our requested format and callback. */
162 desired.channels = 2;
163 desired.format = AUDIO_S16SYS;
164 desired.freq = 44100;
165 desired.padding = 0;
166 desired.samples = 4096;
167 desired.callback = RenderSDLSamples;
168 desired.userdata = &playback;
169 if(SDL_OpenAudio(&desired, &obtained) != 0)
171 SDL_Quit();
172 fprintf(stderr, "Failed to open SDL audio: %s\n", SDL_GetError());
173 return 1;
176 /* Set up our OpenAL attributes based on what we got from SDL. */
177 attrs[0] = ALC_FORMAT_CHANNELS_SOFT;
178 if(obtained.channels == 1)
179 attrs[1] = ALC_MONO_SOFT;
180 else if(obtained.channels == 2)
181 attrs[1] = ALC_STEREO_SOFT;
182 else
184 fprintf(stderr, "Unhandled SDL channel count: %d\n", obtained.channels);
185 goto error;
188 attrs[2] = ALC_FORMAT_TYPE_SOFT;
189 if(obtained.format == AUDIO_U8)
190 attrs[3] = ALC_UNSIGNED_BYTE_SOFT;
191 else if(obtained.format == AUDIO_S8)
192 attrs[3] = ALC_BYTE_SOFT;
193 else if(obtained.format == AUDIO_U16SYS)
194 attrs[3] = ALC_UNSIGNED_SHORT_SOFT;
195 else if(obtained.format == AUDIO_S16SYS)
196 attrs[3] = ALC_SHORT_SOFT;
197 else
199 fprintf(stderr, "Unhandled SDL format: 0x%04x\n", obtained.format);
200 goto error;
203 attrs[4] = ALC_FREQUENCY;
204 attrs[5] = obtained.freq;
206 attrs[6] = 0; /* end of list */
208 playback.FrameSize = obtained.channels * SDL_AUDIO_BITSIZE(obtained.format) / 8;
210 /* Initialize OpenAL loopback device, using our format attributes. */
211 playback.Device = alcLoopbackOpenDeviceSOFT(NULL);
212 if(!playback.Device)
214 fprintf(stderr, "Failed to open loopback device!\n");
215 goto error;
217 /* Make sure the format is supported before setting them on the device. */
218 if(alcIsRenderFormatSupportedSOFT(playback.Device, attrs[5], attrs[1], attrs[3]) == ALC_FALSE)
220 fprintf(stderr, "Render format not supported: %s, %s, %dhz\n",
221 ChannelsName(attrs[1]), TypeName(attrs[3]), attrs[5]);
222 goto error;
224 playback.Context = alcCreateContext(playback.Device, attrs);
225 if(!playback.Context || alcMakeContextCurrent(playback.Context) == ALC_FALSE)
227 fprintf(stderr, "Failed to set an OpenAL audio context\n");
228 goto error;
231 /* Start SDL playing. Our callback (thus alcRenderSamplesSOFT) will now
232 * start being called regularly to update the AL playback state. */
233 SDL_PauseAudio(0);
235 /* Load the sound into a buffer. */
236 buffer = CreateSineWave();
237 if(!buffer)
239 SDL_CloseAudio();
240 alcDestroyContext(playback.Context);
241 alcCloseDevice(playback.Device);
242 SDL_Quit();
243 return 1;
246 /* Create the source to play the sound with. */
247 source = 0;
248 alGenSources(1, &source);
249 alSourcei(source, AL_BUFFER, buffer);
250 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
252 /* Play the sound until it finishes. */
253 alSourcePlay(source);
254 do {
255 al_nssleep(10000000);
256 alGetSourcei(source, AL_SOURCE_STATE, &state);
257 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
259 /* All done. Delete resources, and close OpenAL. */
260 alDeleteSources(1, &source);
261 alDeleteBuffers(1, &buffer);
263 /* Stop SDL playing. */
264 SDL_PauseAudio(1);
266 /* Close up OpenAL and SDL. */
267 SDL_CloseAudio();
268 alcDestroyContext(playback.Context);
269 alcCloseDevice(playback.Device);
270 SDL_Quit();
272 return 0;
274 error:
275 SDL_CloseAudio();
276 if(playback.Context)
277 alcDestroyContext(playback.Context);
278 if(playback.Device)
279 alcCloseDevice(playback.Device);
280 SDL_Quit();
282 return 1;