Fix a couple hard-coded array sizes
[openal-soft.git] / examples / alloopback.c
blobc5dee36d97fe9f480df3be21f35ffa4153fee7e2
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 /* Creates a one second buffer containing a sine wave, and returns the new
65 * buffer ID. */
66 static ALuint CreateSineWave(void)
68 ALshort data[44100*4];
69 ALuint buffer;
70 ALenum err;
71 ALuint i;
73 for(i = 0;i < 44100*4;i++)
74 data[i] = (ALshort)(sin(i/44100.0 * 1000.0 * 2.0*M_PI) * 32767.0);
76 /* Buffer the audio data into a new buffer object. */
77 buffer = 0;
78 alGenBuffers(1, &buffer);
79 alBufferData(buffer, AL_FORMAT_MONO16, data, sizeof(data), 44100);
81 /* Check if an error occured, and clean up if so. */
82 err = alGetError();
83 if(err != AL_NO_ERROR)
85 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
86 if(alIsBuffer(buffer))
87 alDeleteBuffers(1, &buffer);
88 return 0;
91 return buffer;
95 int main(int argc, char *argv[])
97 PlaybackInfo playback = { NULL, NULL, 0 };
98 SDL_AudioSpec desired, obtained;
99 ALuint source, buffer;
100 ALCint attrs[16];
101 ALenum state;
102 (void)argc;
103 (void)argv;
105 /* Print out error if extension is missing. */
106 if(!alcIsExtensionPresent(NULL, "ALC_SOFT_loopback"))
108 fprintf(stderr, "Error: ALC_SOFT_loopback not supported!\n");
109 return 1;
112 /* Define a macro to help load the function pointers. */
113 #define LOAD_PROC(x) ((x) = alcGetProcAddress(NULL, #x))
114 LOAD_PROC(alcLoopbackOpenDeviceSOFT);
115 LOAD_PROC(alcIsRenderFormatSupportedSOFT);
116 LOAD_PROC(alcRenderSamplesSOFT);
117 #undef LOAD_PROC
119 if(SDL_Init(SDL_INIT_AUDIO) == -1)
121 fprintf(stderr, "Failed to init SDL audio: %s\n", SDL_GetError());
122 return 1;
125 /* Set up SDL audio with our requested format and callback. */
126 desired.channels = 2;
127 desired.format = AUDIO_S16SYS;
128 desired.freq = 44100;
129 desired.padding = 0;
130 desired.samples = 4096;
131 desired.callback = RenderSDLSamples;
132 desired.userdata = &playback;
133 if(SDL_OpenAudio(&desired, &obtained) != 0)
135 SDL_Quit();
136 fprintf(stderr, "Failed to open SDL audio: %s\n", SDL_GetError());
137 return 1;
140 /* Set up our OpenAL attributes based on what we got from SDL. */
141 attrs[0] = ALC_FORMAT_CHANNELS_SOFT;
142 if(obtained.channels == 1)
143 attrs[1] = ALC_MONO_SOFT;
144 else if(obtained.channels == 2)
145 attrs[1] = ALC_STEREO_SOFT;
146 else
148 fprintf(stderr, "Unhandled SDL channel count: %d\n", obtained.channels);
149 goto error;
152 attrs[2] = ALC_FORMAT_TYPE_SOFT;
153 if(obtained.format == AUDIO_U8)
154 attrs[3] = ALC_UNSIGNED_BYTE_SOFT;
155 else if(obtained.format == AUDIO_S8)
156 attrs[3] = ALC_BYTE_SOFT;
157 else if(obtained.format == AUDIO_U16SYS)
158 attrs[3] = ALC_UNSIGNED_SHORT_SOFT;
159 else if(obtained.format == AUDIO_S16SYS)
160 attrs[3] = ALC_SHORT_SOFT;
161 else
163 fprintf(stderr, "Unhandled SDL format: 0x%04x\n", obtained.format);
164 goto error;
167 attrs[4] = ALC_FREQUENCY;
168 attrs[5] = obtained.freq;
170 attrs[6] = 0; /* end of list */
172 playback.FrameSize = FramesToBytes(1, attrs[1], attrs[3]);
174 /* Initialize OpenAL loopback device, using our format attributes. */
175 playback.Device = alcLoopbackOpenDeviceSOFT(NULL);
176 if(!playback.Device)
178 fprintf(stderr, "Failed to open loopback device!\n");
179 goto error;
181 /* Make sure the format is supported before setting them on the device. */
182 if(alcIsRenderFormatSupportedSOFT(playback.Device, attrs[5], attrs[1], attrs[3]) == ALC_FALSE)
184 fprintf(stderr, "Render format not supported: %s, %s, %dhz\n",
185 ChannelsName(attrs[1]), TypeName(attrs[3]), attrs[5]);
186 goto error;
188 playback.Context = alcCreateContext(playback.Device, attrs);
189 if(!playback.Context || alcMakeContextCurrent(playback.Context) == ALC_FALSE)
191 fprintf(stderr, "Failed to set an OpenAL audio context\n");
192 goto error;
195 /* Start SDL playing. Our callback (thus alcRenderSamplesSOFT) will now
196 * start being called regularly to update the AL playback state. */
197 SDL_PauseAudio(0);
199 /* Load the sound into a buffer. */
200 buffer = CreateSineWave();
201 if(!buffer)
203 SDL_CloseAudio();
204 alcDestroyContext(playback.Context);
205 alcCloseDevice(playback.Device);
206 SDL_Quit();
207 return 1;
210 /* Create the source to play the sound with. */
211 source = 0;
212 alGenSources(1, &source);
213 alSourcei(source, AL_BUFFER, buffer);
214 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
216 /* Play the sound until it finishes. */
217 alSourcePlay(source);
218 do {
219 al_nssleep(10000000);
220 alGetSourcei(source, AL_SOURCE_STATE, &state);
221 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
223 /* All done. Delete resources, and close OpenAL. */
224 alDeleteSources(1, &source);
225 alDeleteBuffers(1, &buffer);
227 /* Stop SDL playing. */
228 SDL_PauseAudio(1);
230 /* Close up OpenAL and SDL. */
231 SDL_CloseAudio();
232 alcDestroyContext(playback.Context);
233 alcCloseDevice(playback.Device);
234 SDL_Quit();
236 return 0;
238 error:
239 SDL_CloseAudio();
240 if(playback.Context)
241 alcDestroyContext(playback.Context);
242 if(playback.Device)
243 alcCloseDevice(playback.Device);
244 SDL_Quit();
246 return 1;