wwviaudio.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef _WWVIAUDIO_H_
  2. #define _WWVIAUDIO_H_
  3. /*
  4. (C) Copyright 2007,2008, Stephen M. Cameron.
  5. This file is part of wordwarvi.
  6. wordwarvi is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. wordwarvi is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with wordwarvi; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifdef WWVIAUDIO_DEFINE_GLOBALS
  19. #define GLOBAL
  20. #else
  21. #define GLOBAL extern
  22. #endif
  23. #define WWVIAUDIO_MUSIC_SLOT (0)
  24. #define WWVIAUDIO_SAMPLE_RATE (44100)
  25. #define WWVIAUDIO_ANY_SLOT (-1)
  26. /*
  27. * Configuration functions.
  28. */
  29. /* Disables the music channel. Meant to be called prior to
  30. * wwviaudio_initialize_portaudio
  31. */
  32. GLOBAL void wwviaudio_set_nomusic(void);
  33. /* Set the audio device number to the given value
  34. * This is meant to be called prior to calling
  35. * wwviaudio_initialize_portaudio. If you don't use
  36. * this function, portaudio's default device will be
  37. * used. This function provides a way to use a
  38. * device other than the default
  39. */
  40. GLOBAL int wwviaudio_set_sound_device(int device);
  41. /* Initialize portaudio and start the audio engine.
  42. * Space will be allocated to allow for the specified
  43. * number of concurrently playing sounds. The 2nd parameter
  44. * indicates how many different sound clips are to be held
  45. * in memory at once. e.g. if your app has five different
  46. * sounds that it plays, then this would be 5.
  47. * 0 is returned on success, -1 otherwise.
  48. */
  49. GLOBAL int wwviaudio_initialize_portaudio(int maximum_concurrent_sounds,
  50. int maximum_sound_clips);
  51. /* Stop portaudio and the audio engine. Space allocated
  52. * during initialization is freed.
  53. */
  54. GLOBAL void wwviaudio_stop_portaudio(void);
  55. /*
  56. * Audio data functions
  57. */
  58. /* Read and decode an ogg vorbis audio file into a numbered buffer
  59. * The sound_number parameter is used later with wwviaudio_play_music and
  60. * wwviaudio_add_sound. 0 is returned on success, -1 otherwise.
  61. * Audio files should be 44100Hz, MONO. The sound number is one you
  62. * provide which will then be associated with that sound.
  63. */
  64. GLOBAL int wwviaudio_read_ogg_clip(int sound_number, char *filename);
  65. /*
  66. * Global sound control functions.
  67. */
  68. /* Suspend all audio playback. Silence is output. */
  69. GLOBAL void wwviaudio_pause_audio(void);
  70. /* Resume all previously playing audio from whence it was previusly paused. */
  71. GLOBAL void wwviaudio_resume_audio(void);
  72. /*
  73. * Music channel related functions
  74. */
  75. /* Begin playing a numbered buffer into the mix on the music channel
  76. * The channel number of the music channel is returned.
  77. */
  78. GLOBAL int wwviaudio_play_music(int sound_number);
  79. /* Output silence on the music channel (pointer still advances though.) */
  80. GLOBAL void wwviaudio_silence_music(void);
  81. /* Unsilence the music channel */
  82. GLOBAL void wwviaudio_resume_music(void);
  83. /* Silence or unsilence the music channe. */
  84. GLOBAL void wwviaudio_toggle_music(void);
  85. /* Stop playing the playing buffer from the music channel */
  86. GLOBAL void wwviaudio_cancel_music(void);
  87. /*
  88. * Sound effect (not music) related functions
  89. */
  90. /* Begin playing a sound on a non-music channel. The channel is returned.
  91. * sound_number refers to a sound previously associated with the number by
  92. * wwviaudio_read_ogg_clip()
  93. */
  94. GLOBAL /* channel */ int wwviaudio_add_sound(int sound_number);
  95. /* Begin playing a sound on a non-music channel. The channel is returned.
  96. * If fewer than five channels are open, the sound is not played, and -1
  97. * is returned.
  98. */
  99. GLOBAL void wwviaudio_add_sound_low_priority(int sound_number);
  100. /* Silence all channels but the music channel (pointers still advance though) */
  101. GLOBAL void wwviaudio_silence_sound_effects(void);
  102. /* Unsilence all channels but the music channel */
  103. GLOBAL void wwviaudio_resume_sound_effects(void);
  104. /* Either silence or unsilence all but the music channel */
  105. GLOBAL void wwviaudio_toggle_sound_effects(void);
  106. /* Stop playing the playing buffer from the given channel */
  107. GLOBAL void wwviaudio_cancel_sound(int channel);
  108. /* Stop playing the playing buffer from all channels */
  109. GLOBAL void wwviaudio_cancel_all_sounds(void);
  110. /*
  111. Example usage, something along these lines:
  112. if (wwviaudio_initialize_portaudio() != 0)
  113. bail_out_and_die();
  114. You would probably use #defines or enums rather than bare ints...
  115. wwviaudio_read_ogg_clip(1, "mysound1.ogg");
  116. wwviaudio_read_ogg_clip(2, "mysound2.ogg");
  117. wwviaudio_read_ogg_clip(3, "mysound3.ogg");
  118. wwviaudio_read_ogg_clip(4, "mymusic.ogg");
  119. ...
  120. wwviaudio_play_music(4); <-- begins playing music in background, returns immediately
  121. while (program isn't done) {
  122. do_stuff();
  123. if (something happened)
  124. wwviaudio_add_sound(1);
  125. if (something else happened)
  126. wwviaudio_add_sound(2);
  127. time_passes();
  128. }
  129. wwviaudio_cancel_all_sounds();
  130. wwviaduio_stop_portaudio();
  131. */
  132. #undef GLOBAL
  133. #endif