sdl_sound.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * SDL_sound Decoder Helpers
  3. *
  4. * Copyright (c) 2013 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 for helping to decode audio using SDL_sound.
  25. * There's very little OpenAL-specific code here.
  26. */
  27. #include "sdl_sound.h"
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <signal.h>
  32. #include <assert.h>
  33. #include <SDL_sound.h>
  34. #include "AL/al.h"
  35. #include "AL/alc.h"
  36. #include "AL/alext.h"
  37. #include "alhelpers.h"
  38. static int done_init = 0;
  39. FilePtr openAudioFile(const char *fname, size_t buftime_ms)
  40. {
  41. FilePtr file;
  42. ALuint rate;
  43. Uint32 bufsize;
  44. ALenum chans, type;
  45. /* We need to make sure SDL_sound is initialized. */
  46. if(!done_init)
  47. {
  48. Sound_Init();
  49. done_init = 1;
  50. }
  51. file = Sound_NewSampleFromFile(fname, NULL, 0);
  52. if(!file)
  53. {
  54. fprintf(stderr, "Failed to open %s: %s\n", fname, Sound_GetError());
  55. return NULL;
  56. }
  57. if(getAudioInfo(file, &rate, &chans, &type) != 0)
  58. {
  59. Sound_FreeSample(file);
  60. return NULL;
  61. }
  62. bufsize = FramesToBytes((ALsizei)(buftime_ms/1000.0*rate), chans, type);
  63. if(Sound_SetBufferSize(file, bufsize) == 0)
  64. {
  65. fprintf(stderr, "Failed to set buffer size to %u bytes: %s\n", bufsize, Sound_GetError());
  66. Sound_FreeSample(file);
  67. return NULL;
  68. }
  69. return file;
  70. }
  71. void closeAudioFile(FilePtr file)
  72. {
  73. if(file)
  74. Sound_FreeSample(file);
  75. }
  76. int getAudioInfo(FilePtr file, ALuint *rate, ALenum *channels, ALenum *type)
  77. {
  78. if(file->actual.channels == 1)
  79. *channels = AL_MONO_SOFT;
  80. else if(file->actual.channels == 2)
  81. *channels = AL_STEREO_SOFT;
  82. else
  83. {
  84. fprintf(stderr, "Unsupported channel count: %d\n", file->actual.channels);
  85. return 1;
  86. }
  87. if(file->actual.format == AUDIO_U8)
  88. *type = AL_UNSIGNED_BYTE_SOFT;
  89. else if(file->actual.format == AUDIO_S8)
  90. *type = AL_BYTE_SOFT;
  91. else if(file->actual.format == AUDIO_U16LSB || file->actual.format == AUDIO_U16MSB)
  92. *type = AL_UNSIGNED_SHORT_SOFT;
  93. else if(file->actual.format == AUDIO_S16LSB || file->actual.format == AUDIO_S16MSB)
  94. *type = AL_SHORT_SOFT;
  95. else
  96. {
  97. fprintf(stderr, "Unsupported sample format: 0x%04x\n", file->actual.format);
  98. return 1;
  99. }
  100. *rate = file->actual.rate;
  101. return 0;
  102. }
  103. uint8_t *getAudioData(FilePtr file, size_t *length)
  104. {
  105. *length = Sound_Decode(file);
  106. if(*length == 0)
  107. return NULL;
  108. if((file->actual.format == AUDIO_U16LSB && AUDIO_U16LSB != AUDIO_U16SYS) ||
  109. (file->actual.format == AUDIO_U16MSB && AUDIO_U16MSB != AUDIO_U16SYS) ||
  110. (file->actual.format == AUDIO_S16LSB && AUDIO_S16LSB != AUDIO_S16SYS) ||
  111. (file->actual.format == AUDIO_S16MSB && AUDIO_S16MSB != AUDIO_S16SYS))
  112. {
  113. /* Swap bytes if the decoded endianness doesn't match the system. */
  114. char *buffer = file->buffer;
  115. size_t i;
  116. for(i = 0;i < *length;i+=2)
  117. {
  118. char b = buffer[i];
  119. buffer[i] = buffer[i+1];
  120. buffer[i+1] = b;
  121. }
  122. }
  123. return file->buffer;
  124. }
  125. void *decodeAudioStream(FilePtr file, size_t *length)
  126. {
  127. Uint32 got;
  128. char *mem;
  129. got = Sound_DecodeAll(file);
  130. if(got == 0)
  131. {
  132. *length = 0;
  133. return NULL;
  134. }
  135. mem = malloc(got);
  136. memcpy(mem, file->buffer, got);
  137. *length = got;
  138. return mem;
  139. }