Mark a global variable declaration as extern
[openal-soft.git] / examples / altonegen.c
blob422e6d66bd970ddea6cfeebc76ed2701bbe02881
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 <math.h>
40 #include "AL/al.h"
41 #include "AL/alc.h"
42 #include "AL/alext.h"
44 #include "common/alhelpers.h"
46 #ifndef M_PI
47 #define M_PI (3.14159265358979323846)
48 #endif
50 enum WaveType {
51 WT_Sine,
52 WT_Square,
53 WT_Sawtooth,
54 WT_Triangle,
55 WT_Impulse,
58 static const char *GetWaveTypeName(enum WaveType type)
60 switch(type)
62 case WT_Sine: return "sine";
63 case WT_Square: return "square";
64 case WT_Sawtooth: return "sawtooth";
65 case WT_Triangle: return "triangle";
66 case WT_Impulse: return "impulse";
68 return "(unknown)";
71 static void ApplySin(ALfloat *data, ALdouble g, ALuint srate, ALuint freq)
73 ALdouble smps_per_cycle = (ALdouble)srate / freq;
74 ALuint i;
75 for(i = 0;i < srate;i++)
76 data[i] += (ALfloat)(sin(i/smps_per_cycle * 2.0*M_PI) * g);
79 /* Generates waveforms using additive synthesis. Each waveform is constructed
80 * by summing one or more sine waves, up to (and excluding) nyquist.
82 static ALuint CreateWave(enum WaveType type, ALuint freq, ALuint srate)
84 ALint data_size;
85 ALfloat *data;
86 ALuint buffer;
87 ALenum err;
88 ALuint i;
90 data_size = srate * sizeof(ALfloat);
91 data = calloc(1, data_size);
92 if(type == WT_Sine)
93 ApplySin(data, 1.0, srate, freq);
94 else if(type == WT_Square)
95 for(i = 1;freq*i < srate/2;i+=2)
96 ApplySin(data, 4.0/M_PI * 1.0/i, srate, freq*i);
97 else if(type == WT_Sawtooth)
98 for(i = 1;freq*i < srate/2;i++)
99 ApplySin(data, 2.0/M_PI * ((i&1)*2 - 1.0) / i, srate, freq*i);
100 else if(type == WT_Triangle)
101 for(i = 1;freq*i < srate/2;i+=2)
102 ApplySin(data, 8.0/(M_PI*M_PI) * (1.0 - (i&2)) / (i*i), srate, freq*i);
103 else if(type == WT_Impulse)
105 /* NOTE: Impulse isn't really a waveform, but it can still be useful to
106 * test (other than resampling, the ALSOFT_DEFAULT_REVERB environment
107 * variable can prove useful here to test the reverb response).
109 for(i = 0;i < srate;i++)
110 data[i] = (i%(srate/freq)) ? 0.0f : 1.0f;
113 /* Buffer the audio data into a new buffer object. */
114 buffer = 0;
115 alGenBuffers(1, &buffer);
116 alBufferData(buffer, AL_FORMAT_MONO_FLOAT32, data, data_size, srate);
117 free(data);
119 /* Check if an error occured, and clean up if so. */
120 err = alGetError();
121 if(err != AL_NO_ERROR)
123 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
124 if(alIsBuffer(buffer))
125 alDeleteBuffers(1, &buffer);
126 return 0;
129 return buffer;
133 int main(int argc, char *argv[])
135 enum WaveType wavetype = WT_Sine;
136 const char *appname = argv[0];
137 ALuint source, buffer;
138 ALint last_pos, num_loops;
139 ALint max_loops = 4;
140 ALint srate = -1;
141 ALint tone_freq = 1000;
142 ALCint dev_rate;
143 ALenum state;
144 int i;
146 argv++; argc--;
147 if(InitAL(&argv, &argc) != 0)
148 return 1;
150 if(!alIsExtensionPresent("AL_EXT_FLOAT32"))
152 fprintf(stderr, "Required AL_EXT_FLOAT32 extension not supported on this device!\n");
153 CloseAL();
154 return 1;
157 for(i = 0;i < argc;i++)
159 if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
161 fprintf(stderr, "OpenAL Tone Generator\n"
162 "\n"
163 "Usage: %s [-device <name>] <options>\n"
164 "\n"
165 "Available options:\n"
166 " --help/-h This help text\n"
167 " -t <seconds> Time to play a tone (default 5 seconds)\n"
168 " --waveform/-w <type> Waveform type: sine (default), square, sawtooth,\n"
169 " triangle, impulse\n"
170 " --freq/-f <hz> Tone frequency (default 1000 hz)\n"
171 " --srate/-s <sample rate> Sampling rate (default output rate)\n",
172 appname
174 CloseAL();
175 return 1;
177 else if(i+1 < argc && strcmp(argv[i], "-t") == 0)
179 i++;
180 max_loops = atoi(argv[i]) - 1;
182 else if(i+1 < argc && (strcmp(argv[i], "--waveform") == 0 || strcmp(argv[i], "-w") == 0))
184 i++;
185 if(strcmp(argv[i], "sine") == 0)
186 wavetype = WT_Sine;
187 else if(strcmp(argv[i], "square") == 0)
188 wavetype = WT_Square;
189 else if(strcmp(argv[i], "sawtooth") == 0)
190 wavetype = WT_Sawtooth;
191 else if(strcmp(argv[i], "triangle") == 0)
192 wavetype = WT_Triangle;
193 else if(strcmp(argv[i], "impulse") == 0)
194 wavetype = WT_Impulse;
195 else
196 fprintf(stderr, "Unhandled waveform: %s\n", argv[i]);
198 else if(i+1 < argc && (strcmp(argv[i], "--freq") == 0 || strcmp(argv[i], "-f") == 0))
200 i++;
201 tone_freq = atoi(argv[i]);
202 if(tone_freq < 1)
204 fprintf(stderr, "Invalid tone frequency: %s (min: 1hz)\n", argv[i]);
205 tone_freq = 1;
208 else if(i+1 < argc && (strcmp(argv[i], "--srate") == 0 || strcmp(argv[i], "-s") == 0))
210 i++;
211 srate = atoi(argv[i]);
212 if(srate < 40)
214 fprintf(stderr, "Invalid sample rate: %s (min: 40hz)\n", argv[i]);
215 srate = 40;
221 ALCdevice *device = alcGetContextsDevice(alcGetCurrentContext());
222 alcGetIntegerv(device, ALC_FREQUENCY, 1, &dev_rate);
223 assert(alcGetError(device)==ALC_NO_ERROR && "Failed to get device sample rate");
225 if(srate < 0)
226 srate = dev_rate;
228 /* Load the sound into a buffer. */
229 buffer = CreateWave(wavetype, tone_freq, srate);
230 if(!buffer)
232 CloseAL();
233 return 1;
236 printf("Playing %dhz %s-wave tone with %dhz sample rate and %dhz output, for %d second%s...\n",
237 tone_freq, GetWaveTypeName(wavetype), srate, dev_rate, max_loops+1, max_loops?"s":"");
238 fflush(stdout);
240 /* Create the source to play the sound with. */
241 source = 0;
242 alGenSources(1, &source);
243 alSourcei(source, AL_BUFFER, buffer);
244 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
246 /* Play the sound for a while. */
247 num_loops = 0;
248 last_pos = 0;
249 alSourcei(source, AL_LOOPING, (max_loops > 0) ? AL_TRUE : AL_FALSE);
250 alSourcePlay(source);
251 do {
252 ALint pos;
253 al_nssleep(10000000);
254 alGetSourcei(source, AL_SAMPLE_OFFSET, &pos);
255 alGetSourcei(source, AL_SOURCE_STATE, &state);
256 if(pos < last_pos && state == AL_PLAYING)
258 ++num_loops;
259 if(num_loops >= max_loops)
260 alSourcei(source, AL_LOOPING, AL_FALSE);
261 printf("%d...\n", max_loops - num_loops + 1);
262 fflush(stdout);
264 last_pos = pos;
265 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
267 /* All done. Delete resources, and close OpenAL. */
268 alDeleteSources(1, &source);
269 alDeleteBuffers(1, &buffer);
271 /* Close up OpenAL. */
272 CloseAL();
274 return 0;