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

180 lines
5.2 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 <assert.h>
  26. #include <inttypes.h>
  27. #include <limits.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "sndfile.h"
  31. #include "AL/al.h"
  32. #include "AL/alext.h"
  33. #include "common/alhelpers.h"
  34. /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
  35. * returns the new buffer ID.
  36. */
  37. static ALuint LoadSound(const char *filename)
  38. {
  39. ALenum err, format;
  40. ALuint buffer;
  41. SNDFILE *sndfile;
  42. SF_INFO sfinfo;
  43. short *membuf;
  44. sf_count_t num_frames;
  45. ALsizei num_bytes;
  46. /* Open the audio file and check that it's usable. */
  47. sndfile = sf_open(filename, SFM_READ, &sfinfo);
  48. if(!sndfile)
  49. {
  50. fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
  51. return 0;
  52. }
  53. if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
  54. {
  55. fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
  56. sf_close(sndfile);
  57. return 0;
  58. }
  59. /* Get the sound format, and figure out the OpenAL format */
  60. format = AL_NONE;
  61. if(sfinfo.channels == 1)
  62. format = AL_FORMAT_MONO16;
  63. else if(sfinfo.channels == 2)
  64. format = AL_FORMAT_STEREO16;
  65. else if(sfinfo.channels == 3)
  66. {
  67. if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
  68. format = AL_FORMAT_BFORMAT2D_16;
  69. }
  70. else if(sfinfo.channels == 4)
  71. {
  72. if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
  73. format = AL_FORMAT_BFORMAT3D_16;
  74. }
  75. if(!format)
  76. {
  77. fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
  78. sf_close(sndfile);
  79. return 0;
  80. }
  81. /* Decode the whole audio file to a buffer. */
  82. membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
  83. num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
  84. if(num_frames < 1)
  85. {
  86. free(membuf);
  87. sf_close(sndfile);
  88. fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
  89. return 0;
  90. }
  91. num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
  92. /* Buffer the audio data into a new buffer object, then free the data and
  93. * close the file.
  94. */
  95. buffer = 0;
  96. alGenBuffers(1, &buffer);
  97. alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
  98. free(membuf);
  99. sf_close(sndfile);
  100. /* Check if an error occured, and clean up if so. */
  101. err = alGetError();
  102. if(err != AL_NO_ERROR)
  103. {
  104. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  105. if(buffer && alIsBuffer(buffer))
  106. alDeleteBuffers(1, &buffer);
  107. return 0;
  108. }
  109. return buffer;
  110. }
  111. int main(int argc, char **argv)
  112. {
  113. ALuint source, buffer;
  114. ALfloat offset;
  115. ALenum state;
  116. /* Print out usage if no arguments were specified */
  117. if(argc < 2)
  118. {
  119. fprintf(stderr, "Usage: %s [-device <name>] <filename>\n", argv[0]);
  120. return 1;
  121. }
  122. /* Initialize OpenAL. */
  123. argv++; argc--;
  124. if(InitAL(&argv, &argc) != 0)
  125. return 1;
  126. /* Load the sound into a buffer. */
  127. buffer = LoadSound(argv[0]);
  128. if(!buffer)
  129. {
  130. CloseAL();
  131. return 1;
  132. }
  133. /* Create the source to play the sound with. */
  134. source = 0;
  135. alGenSources(1, &source);
  136. alSourcei(source, AL_BUFFER, (ALint)buffer);
  137. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  138. /* Play the sound until it finishes. */
  139. alSourcePlay(source);
  140. do {
  141. al_nssleep(10000000);
  142. alGetSourcei(source, AL_SOURCE_STATE, &state);
  143. /* Get the source offset. */
  144. alGetSourcef(source, AL_SEC_OFFSET, &offset);
  145. printf("\rOffset: %f ", offset);
  146. fflush(stdout);
  147. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  148. printf("\n");
  149. /* All done. Delete resources, and close down OpenAL. */
  150. alDeleteSources(1, &source);
  151. alDeleteBuffers(1, &buffer);
  152. CloseAL();
  153. return 0;
  154. }