🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
9.6 KiB

  1. /*
  2. * OpenAL Tone Generator Test
  3. *
  4. * Copyright (c) 2015 by Chris Robinson <chris.kcat@gmail.com>
  5. *
  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:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  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.
  23. */
  24. /* This file contains a test for generating waveforms and plays them for a
  25. * given length of time. Intended to inspect the behavior of the mixer by
  26. * checking the output with a spectrum analyzer and oscilloscope.
  27. *
  28. * TODO: This would actually be nicer as a GUI app with buttons to start and
  29. * stop individual waveforms, include additional whitenoise and pinknoise
  30. * generators, and have the ability to hook up EFX filters and effects.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <assert.h>
  36. #include <limits.h>
  37. #include <math.h>
  38. #include "AL/al.h"
  39. #include "AL/alc.h"
  40. #include "AL/alext.h"
  41. #include "common/alhelpers.h"
  42. #ifndef M_PI
  43. #define M_PI (3.14159265358979323846)
  44. #endif
  45. enum WaveType {
  46. WT_Sine,
  47. WT_Square,
  48. WT_Sawtooth,
  49. WT_Triangle,
  50. WT_Impulse,
  51. WT_WhiteNoise,
  52. };
  53. static const char *GetWaveTypeName(enum WaveType type)
  54. {
  55. switch(type)
  56. {
  57. case WT_Sine: return "sine";
  58. case WT_Square: return "square";
  59. case WT_Sawtooth: return "sawtooth";
  60. case WT_Triangle: return "triangle";
  61. case WT_Impulse: return "impulse";
  62. case WT_WhiteNoise: return "noise";
  63. }
  64. return "(unknown)";
  65. }
  66. static inline ALuint dither_rng(ALuint *seed)
  67. {
  68. *seed = (*seed * 96314165) + 907633515;
  69. return *seed;
  70. }
  71. static void ApplySin(ALfloat *data, ALdouble g, ALuint srate, ALuint freq)
  72. {
  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);
  77. }
  78. /* Generates waveforms using additive synthesis. Each waveform is constructed
  79. * by summing one or more sine waves, up to (and excluding) nyquist.
  80. */
  81. static ALuint CreateWave(enum WaveType type, ALuint freq, ALuint srate)
  82. {
  83. ALuint seed = 22222;
  84. ALint data_size;
  85. ALfloat *data;
  86. ALuint buffer;
  87. ALenum err;
  88. ALuint i;
  89. data_size = srate * sizeof(ALfloat);
  90. data = calloc(1, data_size);
  91. switch(type)
  92. {
  93. case WT_Sine:
  94. ApplySin(data, 1.0, srate, freq);
  95. break;
  96. case WT_Square:
  97. for(i = 1;freq*i < srate/2;i+=2)
  98. ApplySin(data, 4.0/M_PI * 1.0/i, srate, freq*i);
  99. break;
  100. case WT_Sawtooth:
  101. for(i = 1;freq*i < srate/2;i++)
  102. ApplySin(data, 2.0/M_PI * ((i&1)*2 - 1.0) / i, srate, freq*i);
  103. break;
  104. case WT_Triangle:
  105. for(i = 1;freq*i < srate/2;i+=2)
  106. ApplySin(data, 8.0/(M_PI*M_PI) * (1.0 - (i&2)) / (i*i), srate, freq*i);
  107. break;
  108. case WT_Impulse:
  109. /* NOTE: Impulse isn't handled using additive synthesis, and is
  110. * instead just a non-0 sample at a given rate. This can still be
  111. * useful to test (other than resampling, the ALSOFT_DEFAULT_REVERB
  112. * environment variable can prove useful here to test the reverb
  113. * response).
  114. */
  115. for(i = 0;i < srate;i++)
  116. data[i] = (i%(srate/freq)) ? 0.0f : 1.0f;
  117. break;
  118. case WT_WhiteNoise:
  119. /* NOTE: WhiteNoise is just uniform set of uncorrelated values, and
  120. * is not influenced by the waveform frequency.
  121. */
  122. for(i = 0;i < srate;i++)
  123. {
  124. ALuint rng0 = dither_rng(&seed);
  125. ALuint rng1 = dither_rng(&seed);
  126. data[i] = (ALfloat)(rng0*(1.0/UINT_MAX) - rng1*(1.0/UINT_MAX));
  127. }
  128. break;
  129. }
  130. /* Buffer the audio data into a new buffer object. */
  131. buffer = 0;
  132. alGenBuffers(1, &buffer);
  133. alBufferData(buffer, AL_FORMAT_MONO_FLOAT32, data, data_size, srate);
  134. free(data);
  135. /* Check if an error occured, and clean up if so. */
  136. err = alGetError();
  137. if(err != AL_NO_ERROR)
  138. {
  139. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  140. if(alIsBuffer(buffer))
  141. alDeleteBuffers(1, &buffer);
  142. return 0;
  143. }
  144. return buffer;
  145. }
  146. int main(int argc, char *argv[])
  147. {
  148. enum WaveType wavetype = WT_Sine;
  149. const char *appname = argv[0];
  150. ALuint source, buffer;
  151. ALint last_pos, num_loops;
  152. ALint max_loops = 4;
  153. ALint srate = -1;
  154. ALint tone_freq = 1000;
  155. ALCint dev_rate;
  156. ALenum state;
  157. int i;
  158. argv++; argc--;
  159. if(InitAL(&argv, &argc) != 0)
  160. return 1;
  161. if(!alIsExtensionPresent("AL_EXT_FLOAT32"))
  162. {
  163. fprintf(stderr, "Required AL_EXT_FLOAT32 extension not supported on this device!\n");
  164. CloseAL();
  165. return 1;
  166. }
  167. for(i = 0;i < argc;i++)
  168. {
  169. if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
  170. {
  171. fprintf(stderr, "OpenAL Tone Generator\n"
  172. "\n"
  173. "Usage: %s [-device <name>] <options>\n"
  174. "\n"
  175. "Available options:\n"
  176. " --help/-h This help text\n"
  177. " -t <seconds> Time to play a tone (default 5 seconds)\n"
  178. " --waveform/-w <type> Waveform type: sine (default), square, sawtooth,\n"
  179. " triangle, impulse, noise\n"
  180. " --freq/-f <hz> Tone frequency (default 1000 hz)\n"
  181. " --srate/-s <sample rate> Sampling rate (default output rate)\n",
  182. appname
  183. );
  184. CloseAL();
  185. return 1;
  186. }
  187. else if(i+1 < argc && strcmp(argv[i], "-t") == 0)
  188. {
  189. i++;
  190. max_loops = atoi(argv[i]) - 1;
  191. }
  192. else if(i+1 < argc && (strcmp(argv[i], "--waveform") == 0 || strcmp(argv[i], "-w") == 0))
  193. {
  194. i++;
  195. if(strcmp(argv[i], "sine") == 0)
  196. wavetype = WT_Sine;
  197. else if(strcmp(argv[i], "square") == 0)
  198. wavetype = WT_Square;
  199. else if(strcmp(argv[i], "sawtooth") == 0)
  200. wavetype = WT_Sawtooth;
  201. else if(strcmp(argv[i], "triangle") == 0)
  202. wavetype = WT_Triangle;
  203. else if(strcmp(argv[i], "impulse") == 0)
  204. wavetype = WT_Impulse;
  205. else if(strcmp(argv[i], "noise") == 0)
  206. wavetype = WT_WhiteNoise;
  207. else
  208. fprintf(stderr, "Unhandled waveform: %s\n", argv[i]);
  209. }
  210. else if(i+1 < argc && (strcmp(argv[i], "--freq") == 0 || strcmp(argv[i], "-f") == 0))
  211. {
  212. i++;
  213. tone_freq = atoi(argv[i]);
  214. if(tone_freq < 1)
  215. {
  216. fprintf(stderr, "Invalid tone frequency: %s (min: 1hz)\n", argv[i]);
  217. tone_freq = 1;
  218. }
  219. }
  220. else if(i+1 < argc && (strcmp(argv[i], "--srate") == 0 || strcmp(argv[i], "-s") == 0))
  221. {
  222. i++;
  223. srate = atoi(argv[i]);
  224. if(srate < 40)
  225. {
  226. fprintf(stderr, "Invalid sample rate: %s (min: 40hz)\n", argv[i]);
  227. srate = 40;
  228. }
  229. }
  230. }
  231. {
  232. ALCdevice *device = alcGetContextsDevice(alcGetCurrentContext());
  233. alcGetIntegerv(device, ALC_FREQUENCY, 1, &dev_rate);
  234. assert(alcGetError(device)==ALC_NO_ERROR && "Failed to get device sample rate");
  235. }
  236. if(srate < 0)
  237. srate = dev_rate;
  238. /* Load the sound into a buffer. */
  239. buffer = CreateWave(wavetype, tone_freq, srate);
  240. if(!buffer)
  241. {
  242. CloseAL();
  243. return 1;
  244. }
  245. printf("Playing %dhz %s-wave tone with %dhz sample rate and %dhz output, for %d second%s...\n",
  246. tone_freq, GetWaveTypeName(wavetype), srate, dev_rate, max_loops+1, max_loops?"s":"");
  247. fflush(stdout);
  248. /* Create the source to play the sound with. */
  249. source = 0;
  250. alGenSources(1, &source);
  251. alSourcei(source, AL_BUFFER, buffer);
  252. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  253. /* Play the sound for a while. */
  254. num_loops = 0;
  255. last_pos = 0;
  256. alSourcei(source, AL_LOOPING, (max_loops > 0) ? AL_TRUE : AL_FALSE);
  257. alSourcePlay(source);
  258. do {
  259. ALint pos;
  260. al_nssleep(10000000);
  261. alGetSourcei(source, AL_SAMPLE_OFFSET, &pos);
  262. alGetSourcei(source, AL_SOURCE_STATE, &state);
  263. if(pos < last_pos && state == AL_PLAYING)
  264. {
  265. ++num_loops;
  266. if(num_loops >= max_loops)
  267. alSourcei(source, AL_LOOPING, AL_FALSE);
  268. printf("%d...\n", max_loops - num_loops + 1);
  269. fflush(stdout);
  270. }
  271. last_pos = pos;
  272. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  273. /* All done. Delete resources, and close OpenAL. */
  274. alDeleteSources(1, &source);
  275. alDeleteBuffers(1, &buffer);
  276. /* Close up OpenAL. */
  277. CloseAL();
  278. return 0;
  279. }