Don't over-allocate the active effect slot array
[openal-soft.git] / examples / altonegen.c
blob52a57875ae4a8c06a9c6549fe360128ece847e23
1 /*
2 * OpenAL Tone Generator Test
4 * Copyright (c) 2015 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 a test for generating waveforms and plays them for a
26 * given length of time. Intended to inspect the behavior of the mixer by
27 * checking the output with a spectrum analyzer and oscilloscope.
29 * TODO: This would actually be nicer as a GUI app with buttons to start and
30 * stop individual waveforms, include additional whitenoise and pinknoise
31 * generators, and have the ability to hook up EFX filters and effects.
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <assert.h>
38 #include <limits.h>
39 #include <math.h>
41 #include "AL/al.h"
42 #include "AL/alc.h"
43 #include "AL/alext.h"
45 #include "common/alhelpers.h"
47 #include "win_main_utf8.h"
49 #ifndef M_PI
50 #define M_PI (3.14159265358979323846)
51 #endif
53 enum WaveType {
54 WT_Sine,
55 WT_Square,
56 WT_Sawtooth,
57 WT_Triangle,
58 WT_Impulse,
59 WT_WhiteNoise,
62 static const char *GetWaveTypeName(enum WaveType type)
64 switch(type)
66 case WT_Sine: return "sine";
67 case WT_Square: return "square";
68 case WT_Sawtooth: return "sawtooth";
69 case WT_Triangle: return "triangle";
70 case WT_Impulse: return "impulse";
71 case WT_WhiteNoise: return "noise";
73 return "(unknown)";
76 static inline ALuint dither_rng(ALuint *seed)
78 *seed = (*seed * 96314165) + 907633515;
79 return *seed;
82 static void ApplySin(ALfloat *data, ALuint length, ALdouble g, ALuint srate, ALuint freq)
84 ALdouble cycles_per_sample = (ALdouble)freq / srate;
85 ALuint i;
86 for(i = 0;i < length;i++)
88 ALdouble ival;
89 data[i] += (ALfloat)(sin(modf(i*cycles_per_sample, &ival) * 2.0*M_PI) * g);
93 /* Generates waveforms using additive synthesis. Each waveform is constructed
94 * by summing one or more sine waves, up to (and excluding) nyquist.
96 static ALuint CreateWave(enum WaveType type, ALuint seconds, ALuint freq, ALuint srate,
97 ALfloat gain)
99 ALuint seed = 22222;
100 ALuint num_samples;
101 ALuint data_size;
102 ALfloat *data;
103 ALuint buffer;
104 ALenum err;
105 ALuint i;
107 if(seconds > INT_MAX / srate / sizeof(ALfloat))
109 fprintf(stderr, "Too many seconds: %u * %u * %zu > %d\n", seconds, srate, sizeof(ALfloat),
110 INT_MAX);
111 return 0;
114 num_samples = seconds * srate;
116 data_size = (ALuint)(num_samples * sizeof(ALfloat));
117 data = calloc(1, data_size);
118 switch(type)
120 case WT_Sine:
121 ApplySin(data, num_samples, 1.0, srate, freq);
122 break;
123 case WT_Square:
124 for(i = 1;freq*i < srate/2;i+=2)
125 ApplySin(data, num_samples, 4.0/M_PI * 1.0/i, srate, freq*i);
126 break;
127 case WT_Sawtooth:
128 for(i = 1;freq*i < srate/2;i++)
129 ApplySin(data, num_samples, 2.0/M_PI * ((i&1)*2 - 1.0) / i, srate, freq*i);
130 break;
131 case WT_Triangle:
132 for(i = 1;freq*i < srate/2;i+=2)
133 ApplySin(data, num_samples, 8.0/(M_PI*M_PI) * (1.0 - (i&2)) / (i*i), srate, freq*i);
134 break;
135 case WT_Impulse:
136 /* NOTE: Impulse isn't handled using additive synthesis, and is
137 * instead just a non-0 sample. This can be useful to test (other
138 * than resampling, the ALSOFT_DEFAULT_REVERB environment variable
139 * can test the reverb response).
141 data[0] = 1.0f;
142 break;
143 case WT_WhiteNoise:
144 /* NOTE: WhiteNoise is just uniform set of uncorrelated values, and
145 * is not influenced by the waveform frequency.
147 for(i = 0;i < num_samples;i++)
149 ALuint rng0 = dither_rng(&seed);
150 ALuint rng1 = dither_rng(&seed);
151 data[i] = (ALfloat)(rng0*(1.0/UINT_MAX) - rng1*(1.0/UINT_MAX));
153 break;
156 if(gain != 1.0f)
158 for(i = 0;i < num_samples;i++)
159 data[i] *= gain;
162 /* Buffer the audio data into a new buffer object. */
163 buffer = 0;
164 alGenBuffers(1, &buffer);
165 alBufferData(buffer, AL_FORMAT_MONO_FLOAT32, data, (ALsizei)data_size, (ALsizei)srate);
166 free(data);
168 /* Check if an error occurred, and clean up if so. */
169 err = alGetError();
170 if(err != AL_NO_ERROR)
172 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
173 if(alIsBuffer(buffer))
174 alDeleteBuffers(1, &buffer);
175 return 0;
178 return buffer;
182 int main(int argc, char *argv[])
184 enum WaveType wavetype = WT_Sine;
185 const char *appname = argv[0];
186 ALuint source, buffer;
187 ALint last_pos;
188 ALint seconds = 4;
189 ALint srate = -1;
190 ALint tone_freq = 1000;
191 ALCint dev_rate;
192 ALenum state;
193 ALfloat gain = 1.0f;
194 int i;
196 argv++; argc--;
197 if(InitAL(&argv, &argc) != 0)
198 return 1;
200 if(!alIsExtensionPresent("AL_EXT_FLOAT32"))
202 fprintf(stderr, "Required AL_EXT_FLOAT32 extension not supported on this device!\n");
203 CloseAL();
204 return 1;
207 for(i = 0;i < argc;i++)
209 if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0
210 || strcmp(argv[i], "--help") == 0)
212 fprintf(stderr, "OpenAL Tone Generator\n"
213 "\n"
214 "Usage: %s [-device <name>] <options>\n"
215 "\n"
216 "Available options:\n"
217 " --help/-h This help text\n"
218 " -t <seconds> Time to play a tone (default 5 seconds)\n"
219 " --waveform/-w <type> Waveform type: sine (default), square, sawtooth,\n"
220 " triangle, impulse, noise\n"
221 " --freq/-f <hz> Tone frequency (default 1000 hz)\n"
222 " --gain/-g <gain> gain 0.0 to 1 (default 1)\n"
223 " --srate/-s <sample rate> Sampling rate (default output rate)\n",
224 appname
226 CloseAL();
227 return 1;
230 if(i+1 < argc && strcmp(argv[i], "-t") == 0)
232 i++;
233 seconds = atoi(argv[i]);
234 if(seconds <= 0)
235 seconds = 4;
237 else if(i+1 < argc && (strcmp(argv[i], "--waveform") == 0 || strcmp(argv[i], "-w") == 0))
239 i++;
240 if(strcmp(argv[i], "sine") == 0)
241 wavetype = WT_Sine;
242 else if(strcmp(argv[i], "square") == 0)
243 wavetype = WT_Square;
244 else if(strcmp(argv[i], "sawtooth") == 0)
245 wavetype = WT_Sawtooth;
246 else if(strcmp(argv[i], "triangle") == 0)
247 wavetype = WT_Triangle;
248 else if(strcmp(argv[i], "impulse") == 0)
249 wavetype = WT_Impulse;
250 else if(strcmp(argv[i], "noise") == 0)
251 wavetype = WT_WhiteNoise;
252 else
253 fprintf(stderr, "Unhandled waveform: %s\n", argv[i]);
255 else if(i+1 < argc && (strcmp(argv[i], "--freq") == 0 || strcmp(argv[i], "-f") == 0))
257 i++;
258 tone_freq = atoi(argv[i]);
259 if(tone_freq < 1)
261 fprintf(stderr, "Invalid tone frequency: %s (min: 1hz)\n", argv[i]);
262 tone_freq = 1;
265 else if(i+1 < argc && (strcmp(argv[i], "--gain") == 0 || strcmp(argv[i], "-g") == 0))
267 i++;
268 gain = (ALfloat)atof(argv[i]);
269 if(gain < 0.0f || gain > 1.0f)
271 fprintf(stderr, "Invalid gain: %s (min: 0.0, max 1.0)\n", argv[i]);
272 gain = 1.0f;
275 else if(i+1 < argc && (strcmp(argv[i], "--srate") == 0 || strcmp(argv[i], "-s") == 0))
277 i++;
278 srate = atoi(argv[i]);
279 if(srate < 40)
281 fprintf(stderr, "Invalid sample rate: %s (min: 40hz)\n", argv[i]);
282 srate = 40;
288 ALCdevice *device = alcGetContextsDevice(alcGetCurrentContext());
289 alcGetIntegerv(device, ALC_FREQUENCY, 1, &dev_rate);
290 assert(alcGetError(device)==ALC_NO_ERROR && "Failed to get device sample rate");
292 if(srate < 0)
293 srate = dev_rate;
295 /* Load the sound into a buffer. */
296 buffer = CreateWave(wavetype, (ALuint)seconds, (ALuint)tone_freq, (ALuint)srate, gain);
297 if(!buffer)
299 CloseAL();
300 return 1;
303 printf("Playing %dhz %s-wave tone with %dhz sample rate and %dhz output, for %d second%s...\n",
304 tone_freq, GetWaveTypeName(wavetype), srate, dev_rate, seconds, (seconds!=1)?"s":"");
305 fflush(stdout);
307 /* Create the source to play the sound with. */
308 source = 0;
309 alGenSources(1, &source);
310 alSourcei(source, AL_BUFFER, (ALint)buffer);
311 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
313 /* Play the sound for a while. */
314 last_pos = -1;
315 alSourcePlay(source);
316 do {
317 ALint pos;
318 al_nssleep(10000000);
319 alGetSourcei(source, AL_SOURCE_STATE, &state);
320 alGetSourcei(source, AL_SAMPLE_OFFSET, &pos);
321 pos /= srate;
323 if(pos > last_pos)
325 printf("%d...\n", seconds - pos);
326 fflush(stdout);
328 last_pos = pos;
329 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
331 /* All done. Delete resources, and close OpenAL. */
332 alDeleteSources(1, &source);
333 alDeleteBuffers(1, &buffer);
335 /* Close up OpenAL. */
336 CloseAL();
338 return 0;