altonegen.c 8.5 KB

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