comparison libcore/core/sound.h @ 203:d3ef968745f5

core: rework audio API - sound: module to play sounds (several at once), - music: loopable music that can only be played one at once.
author David Demelier <markand@malikania.fr>
date Mon, 09 Nov 2020 21:18:41 +0100
parents eb0a7ab71023
children c9fbb822d269
comparison
equal deleted inserted replaced
202:baf7e6575181 203:d3ef968745f5
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #ifndef MOLKO_SOUND_H 19 #ifndef MOLKO_CORE_SOUND_H
20 #define MOLKO_SOUND_H 20 #define MOLKO_CORE_SOUND_H
21 21
22 /** 22 /**
23 * \file sound.h 23 * \file sound.h
24 * \brief Sound support. 24 * \brief Sound support.
25 */ 25 */
28 #include <stddef.h> 28 #include <stddef.h>
29 29
30 #include "plat.h" 30 #include "plat.h"
31 31
32 /** 32 /**
33 * \brief Sound flags. 33 * \brief Number of channels allocated.
34 */ 34 */
35 enum sound_flags { 35 #define SOUND_MAX_CHANNELS (256)
36 SOUND_NONE, /*!< No flags. */
37 SOUND_LOOP = (1 << 0) /*!< Loop the music. */
38 };
39 36
40 /** 37 /**
41 * \brief Sound chunk. 38 * \brief Sound chunk.
42 */ 39 */
43 struct sound { 40 struct sound {
44 enum sound_flags flags; /*!< (+) Flags. */
45 void *handle; /*!< (*) Native handle. */ 41 void *handle; /*!< (*) Native handle. */
42 int channel; /*!< (*) Current channel. */
46 }; 43 };
47 44
48 /** 45 /**
49 * Open a sound audio file. 46 * Open a sound audio file.
50 * 47 *
67 * \param buffersz the buffer size 64 * \param buffersz the buffer size
68 * \return False on errors. 65 * \return False on errors.
69 * \warning The buffer must exists until the sound object is closed. 66 * \warning The buffer must exists until the sound object is closed.
70 */ 67 */
71 bool 68 bool
72 sound_openmem(struct sound *snd, const void *buffer, size_t buffersz); 69 sound_openmem(struct sound *snd, const void *buffer, size_t buffersz) PLAT_NODISCARD;
70
71 /**
72 * Check if this sound handle is properly loaded.
73 *
74 * \param snd the sound to check (may be NULL)
75 */
76 bool
77 sound_ok(const struct sound *snd);
73 78
74 /** 79 /**
75 * Start playing the sound. 80 * Start playing the sound.
76 * 81 *
77 * This function will resume the playback since the beginning. 82 * This function will resume the playback since the beginning.
78 * 83 *
79 * \pre snd != NULL 84 * \pre sound_ok(snd)
80 * \param snd the sound object 85 * \param snd the sound object
86 * \param channel the channel to use (-1 for a default)
87 * \param fadein a fade in delay in milliseconds (0 to disable)
81 * \return False on errors. 88 * \return False on errors.
82 */ 89 */
83 bool 90 bool
84 sound_play(struct sound *snd); 91 sound_play(struct sound *snd, int channel, unsigned int fadein);
85 92
86 /** 93 /**
87 * Pause the sound music. 94 * Pause the given sound or all sounds currently playing.
88 * 95 *
89 * \pre snd != NULL 96 * \param snd the sound object (or NULL to pause all)
90 * \param snd the sound object
91 */ 97 */
92 void 98 void
93 sound_pause(struct sound *snd); 99 sound_pause(struct sound *snd);
94 100
95 /** 101 /**
96 * Resume the sound music. 102 * Resume the current sound or all sounds currently paused.
97 * 103 *
98 * \pre snd != NULL 104 * \param snd the sound object (or NULL to resume all)
99 * \param snd the sound object
100 */ 105 */
101 void 106 void
102 sound_resume(struct sound *snd); 107 sound_resume(struct sound *snd);
103 108
104 /** 109 /**
105 * Stop the sound music. 110 * Stop the sound music or all sounds currently playing.
106 * 111 *
107 * \pre snd != NULL 112 * \pre sound_ok(snd)
108 * \param snd the sound object 113 * \param snd the sound object
114 * \param fadeout a fade out delay in milliseconds (0 to disable)
109 */ 115 */
110 void 116 void
111 sound_stop(struct sound *snd); 117 sound_stop(struct sound *snd, unsigned int fadeout);
112 118
113 /** 119 /**
114 * Close the associated resources. 120 * Close the associated resources. This will also stop the playback of the
121 * given sound.
115 * 122 *
116 * \pre snd != NULL 123 * \pre snd != NULL
117 * \param snd the sound object 124 * \param snd the sound object
118 */ 125 */
119 void 126 void
120 sound_finish(struct sound *snd); 127 sound_finish(struct sound *snd);
121 128
122 #endif /* !MOLKO_SOUND_H */ 129 #endif /* !MOLKO_CORE_SOUND_H */