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

353 lines
10 KiB

  1. /*
  2. * OpenAL Audio Stream Example
  3. *
  4. * Copyright (c) 2011 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 relatively simple streaming audio player. */
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <signal.h>
  29. #include <assert.h>
  30. #include <SDL_sound.h>
  31. #include "AL/al.h"
  32. #include "AL/alc.h"
  33. #include "AL/alext.h"
  34. #include "common/alhelpers.h"
  35. #ifndef SDL_AUDIO_MASK_BITSIZE
  36. #define SDL_AUDIO_MASK_BITSIZE (0xFF)
  37. #endif
  38. #ifndef SDL_AUDIO_BITSIZE
  39. #define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
  40. #endif
  41. /* Define the number of buffers and buffer size (in milliseconds) to use. 4
  42. * buffers with 200ms each gives a nice per-chunk size, and lets the queue last
  43. * for almost one second. */
  44. #define NUM_BUFFERS 4
  45. #define BUFFER_TIME_MS 200
  46. typedef struct StreamPlayer {
  47. /* These are the buffers and source to play out through OpenAL with */
  48. ALuint buffers[NUM_BUFFERS];
  49. ALuint source;
  50. /* Handle for the audio file */
  51. Sound_Sample *sample;
  52. /* The format of the output stream */
  53. ALenum format;
  54. ALsizei srate;
  55. } StreamPlayer;
  56. static StreamPlayer *NewPlayer(void);
  57. static void DeletePlayer(StreamPlayer *player);
  58. static int OpenPlayerFile(StreamPlayer *player, const char *filename);
  59. static void ClosePlayerFile(StreamPlayer *player);
  60. static int StartPlayer(StreamPlayer *player);
  61. static int UpdatePlayer(StreamPlayer *player);
  62. /* Creates a new player object, and allocates the needed OpenAL source and
  63. * buffer objects. Error checking is simplified for the purposes of this
  64. * example, and will cause an abort if needed. */
  65. static StreamPlayer *NewPlayer(void)
  66. {
  67. StreamPlayer *player;
  68. player = calloc(1, sizeof(*player));
  69. assert(player != NULL);
  70. /* Generate the buffers and source */
  71. alGenBuffers(NUM_BUFFERS, player->buffers);
  72. assert(alGetError() == AL_NO_ERROR && "Could not create buffers");
  73. alGenSources(1, &player->source);
  74. assert(alGetError() == AL_NO_ERROR && "Could not create source");
  75. /* Set parameters so mono sources play out the front-center speaker and
  76. * won't distance attenuate. */
  77. alSource3i(player->source, AL_POSITION, 0, 0, -1);
  78. alSourcei(player->source, AL_SOURCE_RELATIVE, AL_TRUE);
  79. alSourcei(player->source, AL_ROLLOFF_FACTOR, 0);
  80. assert(alGetError() == AL_NO_ERROR && "Could not set source parameters");
  81. return player;
  82. }
  83. /* Destroys a player object, deleting the source and buffers. No error handling
  84. * since these calls shouldn't fail with a properly-made player object. */
  85. static void DeletePlayer(StreamPlayer *player)
  86. {
  87. ClosePlayerFile(player);
  88. alDeleteSources(1, &player->source);
  89. alDeleteBuffers(NUM_BUFFERS, player->buffers);
  90. if(alGetError() != AL_NO_ERROR)
  91. fprintf(stderr, "Failed to delete object IDs\n");
  92. memset(player, 0, sizeof(*player));
  93. free(player);
  94. }
  95. /* Opens the first audio stream of the named file. If a file is already open,
  96. * it will be closed first. */
  97. static int OpenPlayerFile(StreamPlayer *player, const char *filename)
  98. {
  99. Uint32 frame_size;
  100. ClosePlayerFile(player);
  101. /* Open the file and get the first stream from it */
  102. player->sample = Sound_NewSampleFromFile(filename, NULL, 0);
  103. if(!player->sample)
  104. {
  105. fprintf(stderr, "Could not open audio in %s\n", filename);
  106. goto error;
  107. }
  108. /* Get the stream format, and figure out the OpenAL format */
  109. if(player->sample->actual.channels == 1)
  110. {
  111. if(player->sample->actual.format == AUDIO_U8)
  112. player->format = AL_FORMAT_MONO8;
  113. else if(player->sample->actual.format == AUDIO_S16SYS)
  114. player->format = AL_FORMAT_MONO16;
  115. else
  116. {
  117. fprintf(stderr, "Unsupported sample format: 0x%04x\n", player->sample->actual.format);
  118. goto error;
  119. }
  120. }
  121. else if(player->sample->actual.channels == 2)
  122. {
  123. if(player->sample->actual.format == AUDIO_U8)
  124. player->format = AL_FORMAT_STEREO8;
  125. else if(player->sample->actual.format == AUDIO_S16SYS)
  126. player->format = AL_FORMAT_STEREO16;
  127. else
  128. {
  129. fprintf(stderr, "Unsupported sample format: 0x%04x\n", player->sample->actual.format);
  130. goto error;
  131. }
  132. }
  133. else
  134. {
  135. fprintf(stderr, "Unsupported channel count: %d\n", player->sample->actual.channels);
  136. goto error;
  137. }
  138. player->srate = player->sample->actual.rate;
  139. frame_size = player->sample->actual.channels *
  140. SDL_AUDIO_BITSIZE(player->sample->actual.format) / 8;
  141. /* Set the buffer size, given the desired millisecond length. */
  142. Sound_SetBufferSize(player->sample, (Uint32)((Uint64)player->srate*BUFFER_TIME_MS/1000) *
  143. frame_size);
  144. return 1;
  145. error:
  146. if(player->sample)
  147. Sound_FreeSample(player->sample);
  148. player->sample = NULL;
  149. return 0;
  150. }
  151. /* Closes the audio file stream */
  152. static void ClosePlayerFile(StreamPlayer *player)
  153. {
  154. if(player->sample)
  155. Sound_FreeSample(player->sample);
  156. player->sample = NULL;
  157. }
  158. /* Prebuffers some audio from the file, and starts playing the source */
  159. static int StartPlayer(StreamPlayer *player)
  160. {
  161. size_t i;
  162. /* Rewind the source position and clear the buffer queue */
  163. alSourceRewind(player->source);
  164. alSourcei(player->source, AL_BUFFER, 0);
  165. /* Fill the buffer queue */
  166. for(i = 0;i < NUM_BUFFERS;i++)
  167. {
  168. /* Get some data to give it to the buffer */
  169. Uint32 slen = Sound_Decode(player->sample);
  170. if(slen == 0) break;
  171. alBufferData(player->buffers[i], player->format,
  172. player->sample->buffer, slen, player->srate);
  173. }
  174. if(alGetError() != AL_NO_ERROR)
  175. {
  176. fprintf(stderr, "Error buffering for playback\n");
  177. return 0;
  178. }
  179. /* Now queue and start playback! */
  180. alSourceQueueBuffers(player->source, i, player->buffers);
  181. alSourcePlay(player->source);
  182. if(alGetError() != AL_NO_ERROR)
  183. {
  184. fprintf(stderr, "Error starting playback\n");
  185. return 0;
  186. }
  187. return 1;
  188. }
  189. static int UpdatePlayer(StreamPlayer *player)
  190. {
  191. ALint processed, state;
  192. /* Get relevant source info */
  193. alGetSourcei(player->source, AL_SOURCE_STATE, &state);
  194. alGetSourcei(player->source, AL_BUFFERS_PROCESSED, &processed);
  195. if(alGetError() != AL_NO_ERROR)
  196. {
  197. fprintf(stderr, "Error checking source state\n");
  198. return 0;
  199. }
  200. /* Unqueue and handle each processed buffer */
  201. while(processed > 0)
  202. {
  203. ALuint bufid;
  204. Uint32 slen;
  205. alSourceUnqueueBuffers(player->source, 1, &bufid);
  206. processed--;
  207. if((player->sample->flags&(SOUND_SAMPLEFLAG_EOF|SOUND_SAMPLEFLAG_ERROR)))
  208. continue;
  209. /* Read the next chunk of data, refill the buffer, and queue it
  210. * back on the source */
  211. slen = Sound_Decode(player->sample);
  212. if(slen > 0)
  213. {
  214. alBufferData(bufid, player->format, player->sample->buffer, slen,
  215. player->srate);
  216. alSourceQueueBuffers(player->source, 1, &bufid);
  217. }
  218. if(alGetError() != AL_NO_ERROR)
  219. {
  220. fprintf(stderr, "Error buffering data\n");
  221. return 0;
  222. }
  223. }
  224. /* Make sure the source hasn't underrun */
  225. if(state != AL_PLAYING && state != AL_PAUSED)
  226. {
  227. ALint queued;
  228. /* If no buffers are queued, playback is finished */
  229. alGetSourcei(player->source, AL_BUFFERS_QUEUED, &queued);
  230. if(queued == 0)
  231. return 0;
  232. alSourcePlay(player->source);
  233. if(alGetError() != AL_NO_ERROR)
  234. {
  235. fprintf(stderr, "Error restarting playback\n");
  236. return 0;
  237. }
  238. }
  239. return 1;
  240. }
  241. int main(int argc, char **argv)
  242. {
  243. StreamPlayer *player;
  244. int i;
  245. /* Print out usage if no arguments were specified */
  246. if(argc < 2)
  247. {
  248. fprintf(stderr, "Usage: %s [-device <name>] <filenames...>\n", argv[0]);
  249. return 1;
  250. }
  251. argv++; argc--;
  252. if(InitAL(&argv, &argc) != 0)
  253. return 1;
  254. Sound_Init();
  255. player = NewPlayer();
  256. /* Play each file listed on the command line */
  257. for(i = 0;i < argc;i++)
  258. {
  259. const char *namepart;
  260. if(!OpenPlayerFile(player, argv[i]))
  261. continue;
  262. /* Get the name portion, without the path, for display. */
  263. namepart = strrchr(argv[i], '/');
  264. if(namepart || (namepart=strrchr(argv[i], '\\')))
  265. namepart++;
  266. else
  267. namepart = argv[i];
  268. printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->format),
  269. player->srate);
  270. fflush(stdout);
  271. if(!StartPlayer(player))
  272. {
  273. ClosePlayerFile(player);
  274. continue;
  275. }
  276. while(UpdatePlayer(player))
  277. al_nssleep(10000000);
  278. /* All done with this file. Close it and go to the next */
  279. ClosePlayerFile(player);
  280. }
  281. printf("Done.\n");
  282. /* All files done. Delete the player, and close down SDL_sound and OpenAL */
  283. DeletePlayer(player);
  284. player = NULL;
  285. Sound_Quit();
  286. CloseAL();
  287. return 0;
  288. }