altonegen.c 10 KB

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