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

177 lines
5.0 KiB

  1. /*
  2. * OpenAL Source Play Example
  3. *
  4. * Copyright (c) 2017 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 an example for playing a sound buffer. */
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include <SDL_sound.h>
  28. #include "AL/al.h"
  29. #include "AL/alc.h"
  30. #include "common/alhelpers.h"
  31. /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
  32. * returns the new buffer ID.
  33. */
  34. static ALuint LoadSound(const char *filename)
  35. {
  36. Sound_Sample *sample;
  37. ALenum err, format;
  38. ALuint buffer;
  39. Uint32 slen;
  40. /* Open the audio file */
  41. sample = Sound_NewSampleFromFile(filename, NULL, 65536);
  42. if(!sample)
  43. {
  44. fprintf(stderr, "Could not open audio in %s\n", filename);
  45. return 0;
  46. }
  47. /* Get the sound format, and figure out the OpenAL format */
  48. if(sample->actual.channels == 1)
  49. {
  50. if(sample->actual.format == AUDIO_U8)
  51. format = AL_FORMAT_MONO8;
  52. else if(sample->actual.format == AUDIO_S16SYS)
  53. format = AL_FORMAT_MONO16;
  54. else
  55. {
  56. fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
  57. Sound_FreeSample(sample);
  58. return 0;
  59. }
  60. }
  61. else if(sample->actual.channels == 2)
  62. {
  63. if(sample->actual.format == AUDIO_U8)
  64. format = AL_FORMAT_STEREO8;
  65. else if(sample->actual.format == AUDIO_S16SYS)
  66. format = AL_FORMAT_STEREO16;
  67. else
  68. {
  69. fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
  70. Sound_FreeSample(sample);
  71. return 0;
  72. }
  73. }
  74. else
  75. {
  76. fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
  77. Sound_FreeSample(sample);
  78. return 0;
  79. }
  80. /* Decode the whole audio stream to a buffer. */
  81. slen = Sound_DecodeAll(sample);
  82. if(!sample->buffer || slen == 0)
  83. {
  84. fprintf(stderr, "Failed to read audio from %s\n", filename);
  85. Sound_FreeSample(sample);
  86. return 0;
  87. }
  88. /* Buffer the audio data into a new buffer object, then free the data and
  89. * close the file. */
  90. buffer = 0;
  91. alGenBuffers(1, &buffer);
  92. alBufferData(buffer, format, sample->buffer, slen, sample->actual.rate);
  93. Sound_FreeSample(sample);
  94. /* Check if an error occured, and clean up if so. */
  95. err = alGetError();
  96. if(err != AL_NO_ERROR)
  97. {
  98. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  99. if(buffer && alIsBuffer(buffer))
  100. alDeleteBuffers(1, &buffer);
  101. return 0;
  102. }
  103. return buffer;
  104. }
  105. int main(int argc, char **argv)
  106. {
  107. ALuint source, buffer;
  108. ALfloat offset;
  109. ALenum state;
  110. /* Print out usage if no arguments were specified */
  111. if(argc < 2)
  112. {
  113. fprintf(stderr, "Usage: %s [-device <name>] <filename>\n", argv[0]);
  114. return 1;
  115. }
  116. /* Initialize OpenAL. */
  117. argv++; argc--;
  118. if(InitAL(&argv, &argc) != 0)
  119. return 1;
  120. /* Initialize SDL_sound. */
  121. Sound_Init();
  122. /* Load the sound into a buffer. */
  123. buffer = LoadSound(argv[0]);
  124. if(!buffer)
  125. {
  126. Sound_Quit();
  127. CloseAL();
  128. return 1;
  129. }
  130. /* Create the source to play the sound with. */
  131. source = 0;
  132. alGenSources(1, &source);
  133. alSourcei(source, AL_BUFFER, buffer);
  134. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  135. /* Play the sound until it finishes. */
  136. alSourcePlay(source);
  137. do {
  138. al_nssleep(10000000);
  139. alGetSourcei(source, AL_SOURCE_STATE, &state);
  140. /* Get the source offset. */
  141. alGetSourcef(source, AL_SEC_OFFSET, &offset);
  142. printf("\rOffset: %f ", offset);
  143. fflush(stdout);
  144. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  145. printf("\n");
  146. /* All done. Delete resources, and close down SDL_sound and OpenAL. */
  147. alDeleteSources(1, &source);
  148. alDeleteBuffers(1, &buffer);
  149. Sound_Quit();
  150. CloseAL();
  151. return 0;
  152. }