alhelpers.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * OpenAL Helpers
  3. *
  4. * Copyright (c) 2011 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 routines to help with some menial OpenAL-related tasks,
  25. * such as opening a device and setting up a context, closing the device and
  26. * destroying its context, converting between frame counts and byte lengths,
  27. * finding an appropriate buffer format, and getting readable strings for
  28. * channel configs and sample types. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include "AL/al.h"
  32. #include "AL/alc.h"
  33. #include "AL/alext.h"
  34. #include "alhelpers.h"
  35. /* InitAL opens a device and sets up a context using default attributes, making
  36. * the program ready to call OpenAL functions. */
  37. int InitAL(char ***argv, int *argc)
  38. {
  39. const ALCchar *name;
  40. ALCdevice *device;
  41. ALCcontext *ctx;
  42. /* Open and initialize a device */
  43. device = NULL;
  44. if(argc && argv && *argc > 1 && strcmp((*argv)[0], "-device") == 0)
  45. {
  46. device = alcOpenDevice((*argv)[1]);
  47. if(!device)
  48. fprintf(stderr, "Failed to open \"%s\", trying default\n", (*argv)[1]);
  49. (*argv) += 2;
  50. (*argc) -= 2;
  51. }
  52. if(!device)
  53. device = alcOpenDevice(NULL);
  54. if(!device)
  55. {
  56. fprintf(stderr, "Could not open a device!\n");
  57. return 1;
  58. }
  59. ctx = alcCreateContext(device, NULL);
  60. if(ctx == NULL || alcMakeContextCurrent(ctx) == ALC_FALSE)
  61. {
  62. if(ctx != NULL)
  63. alcDestroyContext(ctx);
  64. alcCloseDevice(device);
  65. fprintf(stderr, "Could not set a context!\n");
  66. return 1;
  67. }
  68. name = NULL;
  69. if(alcIsExtensionPresent(device, "ALC_ENUMERATE_ALL_EXT"))
  70. name = alcGetString(device, ALC_ALL_DEVICES_SPECIFIER);
  71. if(!name || alcGetError(device) != AL_NO_ERROR)
  72. name = alcGetString(device, ALC_DEVICE_SPECIFIER);
  73. printf("Opened \"%s\"\n", name);
  74. return 0;
  75. }
  76. /* CloseAL closes the device belonging to the current context, and destroys the
  77. * context. */
  78. void CloseAL(void)
  79. {
  80. ALCdevice *device;
  81. ALCcontext *ctx;
  82. ctx = alcGetCurrentContext();
  83. if(ctx == NULL)
  84. return;
  85. device = alcGetContextsDevice(ctx);
  86. alcMakeContextCurrent(NULL);
  87. alcDestroyContext(ctx);
  88. alcCloseDevice(device);
  89. }
  90. const char *FormatName(ALenum format)
  91. {
  92. switch(format)
  93. {
  94. case AL_FORMAT_MONO8: return "Mono, U8";
  95. case AL_FORMAT_MONO16: return "Mono, S16";
  96. case AL_FORMAT_STEREO8: return "Stereo, U8";
  97. case AL_FORMAT_STEREO16: return "Stereo, S16";
  98. }
  99. return "Unknown Format";
  100. }