🛠️🐜 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.

330 lines
10 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. #include "win_main_utf8.h"
  43. #ifndef M_PI
  44. #define M_PI (3.14159265358979323846)
  45. #endif
  46. enum WaveType {
  47. WT_Sine,
  48. WT_Square,
  49. WT_Sawtooth,
  50. WT_Triangle,
  51. WT_Impulse,
  52. WT_WhiteNoise,
  53. };
  54. static const char *GetWaveTypeName(enum WaveType type)
  55. {
  56. switch(type)
  57. {
  58. case WT_Sine: return "sine";
  59. case WT_Square: return "square";
  60. case WT_Sawtooth: return "sawtooth";
  61. case WT_Triangle: return "triangle";
  62. case WT_Impulse: return "impulse";
  63. case WT_WhiteNoise: return "noise";
  64. }
  65. return "(unknown)";
  66. }
  67. static inline ALuint dither_rng(ALuint *seed)
  68. {
  69. *seed = (*seed * 96314165) + 907633515;
  70. return *seed;
  71. }
  72. static void ApplySin(ALfloat *data, ALdouble g, ALuint srate, ALuint freq)
  73. {
  74. ALdouble smps_per_cycle = (ALdouble)srate / freq;
  75. ALuint i;
  76. for(i = 0;i < srate;i++)
  77. {
  78. ALdouble ival;
  79. data[i] += (ALfloat)(sin(modf(i/smps_per_cycle, &ival) * 2.0*M_PI) * g);
  80. }
  81. }
  82. /* Generates waveforms using additive synthesis. Each waveform is constructed
  83. * by summing one or more sine waves, up to (and excluding) nyquist.
  84. */
  85. static ALuint CreateWave(enum WaveType type, ALuint freq, ALuint srate, ALfloat gain)
  86. {
  87. ALuint seed = 22222;
  88. ALuint data_size;
  89. ALfloat *data;
  90. ALuint buffer;
  91. ALenum err;
  92. ALuint i;
  93. data_size = (ALuint)(srate * sizeof(ALfloat));
  94. data = calloc(1, data_size);
  95. switch(type)
  96. {
  97. case WT_Sine:
  98. ApplySin(data, 1.0, srate, freq);
  99. break;
  100. case WT_Square:
  101. for(i = 1;freq*i < srate/2;i+=2)
  102. ApplySin(data, 4.0/M_PI * 1.0/i, srate, freq*i);
  103. break;
  104. case WT_Sawtooth:
  105. for(i = 1;freq*i < srate/2;i++)
  106. ApplySin(data, 2.0/M_PI * ((i&1)*2 - 1.0) / i, srate, freq*i);
  107. break;
  108. case WT_Triangle:
  109. for(i = 1;freq*i < srate/2;i+=2)
  110. ApplySin(data, 8.0/(M_PI*M_PI) * (1.0 - (i&2)) / (i*i), srate, freq*i);
  111. break;
  112. case WT_Impulse:
  113. /* NOTE: Impulse isn't handled using additive synthesis, and is
  114. * instead just a non-0 sample at a given rate. This can still be
  115. * useful to test (other than resampling, the ALSOFT_DEFAULT_REVERB
  116. * environment variable can prove useful here to test the reverb
  117. * response).
  118. */
  119. for(i = 0;i < srate;i++)
  120. data[i] = (i%(srate/freq)) ? 0.0f : 1.0f;
  121. break;
  122. case WT_WhiteNoise:
  123. /* NOTE: WhiteNoise is just uniform set of uncorrelated values, and
  124. * is not influenced by the waveform frequency.
  125. */
  126. for(i = 0;i < srate;i++)
  127. {
  128. ALuint rng0 = dither_rng(&seed);
  129. ALuint rng1 = dither_rng(&seed);
  130. data[i] = (ALfloat)(rng0*(1.0/UINT_MAX) - rng1*(1.0/UINT_MAX));
  131. }
  132. break;
  133. }
  134. if(gain != 1.0f)
  135. {
  136. for(i = 0;i < srate;i++)
  137. data[i] *= gain;
  138. }
  139. /* Buffer the audio data into a new buffer object. */
  140. buffer = 0;
  141. alGenBuffers(1, &buffer);
  142. alBufferData(buffer, AL_FORMAT_MONO_FLOAT32, data, (ALsizei)data_size, (ALsizei)srate);
  143. free(data);
  144. /* Check if an error occured, and clean up if so. */
  145. err = alGetError();
  146. if(err != AL_NO_ERROR)
  147. {
  148. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  149. if(alIsBuffer(buffer))
  150. alDeleteBuffers(1, &buffer);
  151. return 0;
  152. }
  153. return buffer;
  154. }
  155. int main(int argc, char *argv[])
  156. {
  157. enum WaveType wavetype = WT_Sine;
  158. const char *appname = argv[0];
  159. ALuint source, buffer;
  160. ALint last_pos, num_loops;
  161. ALint max_loops = 4;
  162. ALint srate = -1;
  163. ALint tone_freq = 1000;
  164. ALCint dev_rate;
  165. ALenum state;
  166. ALfloat gain = 1.0f;
  167. int i;
  168. argv++; argc--;
  169. if(InitAL(&argv, &argc) != 0)
  170. return 1;
  171. if(!alIsExtensionPresent("AL_EXT_FLOAT32"))
  172. {
  173. fprintf(stderr, "Required AL_EXT_FLOAT32 extension not supported on this device!\n");
  174. CloseAL();
  175. return 1;
  176. }
  177. for(i = 0;i < argc;i++)
  178. {
  179. if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0
  180. || strcmp(argv[i], "--help") == 0)
  181. {
  182. fprintf(stderr, "OpenAL Tone Generator\n"
  183. "\n"
  184. "Usage: %s [-device <name>] <options>\n"
  185. "\n"
  186. "Available options:\n"
  187. " --help/-h This help text\n"
  188. " -t <seconds> Time to play a tone (default 5 seconds)\n"
  189. " --waveform/-w <type> Waveform type: sine (default), square, sawtooth,\n"
  190. " triangle, impulse, noise\n"
  191. " --freq/-f <hz> Tone frequency (default 1000 hz)\n"
  192. " --gain/-g <gain> gain 0.0 to 1 (default 1)\n"
  193. " --srate/-s <sample rate> Sampling rate (default output rate)\n",
  194. appname
  195. );
  196. CloseAL();
  197. return 1;
  198. }
  199. else if(i+1 < argc && strcmp(argv[i], "-t") == 0)
  200. {
  201. i++;
  202. max_loops = atoi(argv[i]) - 1;
  203. }
  204. else if(i+1 < argc && (strcmp(argv[i], "--waveform") == 0 || strcmp(argv[i], "-w") == 0))
  205. {
  206. i++;
  207. if(strcmp(argv[i], "sine") == 0)
  208. wavetype = WT_Sine;
  209. else if(strcmp(argv[i], "square") == 0)
  210. wavetype = WT_Square;
  211. else if(strcmp(argv[i], "sawtooth") == 0)
  212. wavetype = WT_Sawtooth;
  213. else if(strcmp(argv[i], "triangle") == 0)
  214. wavetype = WT_Triangle;
  215. else if(strcmp(argv[i], "impulse") == 0)
  216. wavetype = WT_Impulse;
  217. else if(strcmp(argv[i], "noise") == 0)
  218. wavetype = WT_WhiteNoise;
  219. else
  220. fprintf(stderr, "Unhandled waveform: %s\n", argv[i]);
  221. }
  222. else if(i+1 < argc && (strcmp(argv[i], "--freq") == 0 || strcmp(argv[i], "-f") == 0))
  223. {
  224. i++;
  225. tone_freq = atoi(argv[i]);
  226. if(tone_freq < 1)
  227. {
  228. fprintf(stderr, "Invalid tone frequency: %s (min: 1hz)\n", argv[i]);
  229. tone_freq = 1;
  230. }
  231. }
  232. else if(i+1 < argc && (strcmp(argv[i], "--gain") == 0 || strcmp(argv[i], "-g") == 0))
  233. {
  234. i++;
  235. gain = (ALfloat)atof(argv[i]);
  236. if(gain < 0.0f || gain > 1.0f)
  237. {
  238. fprintf(stderr, "Invalid gain: %s (min: 0.0, max 1.0)\n", argv[i]);
  239. gain = 1.0f;
  240. }
  241. }
  242. else if(i+1 < argc && (strcmp(argv[i], "--srate") == 0 || strcmp(argv[i], "-s") == 0))
  243. {
  244. i++;
  245. srate = atoi(argv[i]);
  246. if(srate < 40)
  247. {
  248. fprintf(stderr, "Invalid sample rate: %s (min: 40hz)\n", argv[i]);
  249. srate = 40;
  250. }
  251. }
  252. }
  253. {
  254. ALCdevice *device = alcGetContextsDevice(alcGetCurrentContext());
  255. alcGetIntegerv(device, ALC_FREQUENCY, 1, &dev_rate);
  256. assert(alcGetError(device)==ALC_NO_ERROR && "Failed to get device sample rate");
  257. }
  258. if(srate < 0)
  259. srate = dev_rate;
  260. /* Load the sound into a buffer. */
  261. buffer = CreateWave(wavetype, (ALuint)tone_freq, (ALuint)srate, gain);
  262. if(!buffer)
  263. {
  264. CloseAL();
  265. return 1;
  266. }
  267. printf("Playing %dhz %s-wave tone with %dhz sample rate and %dhz output, for %d second%s...\n",
  268. tone_freq, GetWaveTypeName(wavetype), srate, dev_rate, max_loops+1, max_loops?"s":"");
  269. fflush(stdout);
  270. /* Create the source to play the sound with. */
  271. source = 0;
  272. alGenSources(1, &source);
  273. alSourcei(source, AL_BUFFER, (ALint)buffer);
  274. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  275. /* Play the sound for a while. */
  276. num_loops = 0;
  277. last_pos = 0;
  278. alSourcei(source, AL_LOOPING, (max_loops > 0) ? AL_TRUE : AL_FALSE);
  279. alSourcePlay(source);
  280. do {
  281. ALint pos;
  282. al_nssleep(10000000);
  283. alGetSourcei(source, AL_SAMPLE_OFFSET, &pos);
  284. alGetSourcei(source, AL_SOURCE_STATE, &state);
  285. if(pos < last_pos && state == AL_PLAYING)
  286. {
  287. ++num_loops;
  288. if(num_loops >= max_loops)
  289. alSourcei(source, AL_LOOPING, AL_FALSE);
  290. printf("%d...\n", max_loops - num_loops + 1);
  291. fflush(stdout);
  292. }
  293. last_pos = pos;
  294. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  295. /* All done. Delete resources, and close OpenAL. */
  296. alDeleteSources(1, &source);
  297. alDeleteBuffers(1, &buffer);
  298. /* Close up OpenAL. */
  299. CloseAL();
  300. return 0;
  301. }