KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
sfxmgr.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/sound/sfxmgr.h
4 Copyright (C) 2002 Megan Potter
5 Copyright (C) 2023, 2024 Ruslan Rostovtsev
6 Copyright (C) 2023 Andy Barajas
7
8*/
9
10/** \file dc/sound/sfxmgr.h
11 \brief Basic sound effect support.
12 \ingroup audio_sfx
13
14 This file contains declarations for doing simple sound effects. This code is
15 only usable for simple WAV files containing either 8-bit or 16-bit samples (stereo
16 or mono) or Yamaha ADPCM (4-bits, stereo or mono). Also, all sounds played in
17 this manner must be at most 65534 samples in length, as this does not handle
18 buffer chaining or anything else complex. For more interesting stuff, you
19 should probably look at the sound stream stuff instead.
20
21 \author Megan Potter
22 \author Ruslan Rostovtsev
23 \author Andy Barajas
24*/
25
26#ifndef __DC_SOUND_SFXMGR_H
27#define __DC_SOUND_SFXMGR_H
28
29#include <sys/cdefs.h>
30__BEGIN_DECLS
31
32#include <kos/fs.h>
33#include <stdint.h>
34
35/** \defgroup audio_sfx Sound Effects
36 \brief Sound Effect Playback and Management
37 \ingroup audio
38
39 @{
40*/
41
42/** \brief Sound effect handle type.
43
44 Each loaded sound effect will be assigned one of these, which is to be used
45 for operations related to the effect, including playing it or unloading it.
46*/
47typedef uint32_t sfxhnd_t;
48
49/** \brief Invalid sound effect handle value.
50
51 If a sound effect cannot be loaded, this value will be returned as the error
52 condition.
53*/
54#define SFXHND_INVALID 0
55
56/** \brief Data structure for sound effect playback.
57
58 This structure is used to pass data to the extended version of sound effect
59 playback functions.
60*/
61typedef struct sfx_play_data {
62 int chn; /**< \brief The channel to play on. If chn == -1, the next
63 available channel will be used automatically. */
64 sfxhnd_t idx; /**< \brief The handle to the sound effect to play. */
65 int vol; /**< \brief The volume to play at (between 0 and 255). */
66 int pan; /**< \brief The panning value of the sound effect. 0 is all
67 the way to the left, 128 is center, 255 is all the way
68 to the right. */
69 int loop; /**< \brief Whether to loop the sound effect or not. */
70 int freq; /**< \brief Frequency */
71 unsigned int loopstart; /**< \brief Loop start index (in samples). */
72 unsigned int loopend; /**< \brief Loop end index (in samples). If loopend == 0,
73 the loop end will default to sfx size in samples. */
75
76/** \brief Load a sound effect.
77
78 This function loads a sound effect from a WAV file and returns a handle to
79 it. The sound effect can be either stereo or mono, and must either be 8-bit
80 or 16-bit uncompressed PCM samples, or 4-bit Yamaha ADPCM.
81
82 \warning The sound effect you are loading must be at most 65534 samples
83 in length.
84
85 \param fn The file to load.
86 \return A handle to the sound effect on success. On error,
87 SFXHND_INVALID is returned.
88*/
89sfxhnd_t snd_sfx_load(const char *fn);
90
91/** \brief Load a sound effect without wav header.
92
93 This function loads a sound effect from a RAW file and returns a handle to
94 it. The sound effect can be either stereo or mono, and must either be 8-bit
95 or 16-bit uncompressed PCM samples, or 4-bit Yamaha ADPCM.
96
97 \warning The sound effect you are loading must be at most 65534 samples
98 in length and multiple by 32 bytes for each channel.
99
100 \param fn The file to load.
101 \param rate The frequency of the sound.
102 \param bitsize The sample size (bits per sample).
103 \param channels Number of channels.
104 \return A handle to the sound effect on success. On error,
105 SFXHND_INVALID is returned.
106*/
107sfxhnd_t snd_sfx_load_ex(const char *fn, uint32_t rate, uint16_t bitsize, uint16_t channels);
108
109/** \brief Load a sound effect without wav header by file handler.
110
111 This function loads a sound effect from a RAW file and returns a handle to
112 it. The sound effect can be either stereo or mono, and must either be 8-bit
113 or 16-bit uncompressed PCM samples, or 4-bit Yamaha ADPCM.
114
115 \warning The sound effect you are loading must be at most 65534 samples
116 in length and multiple by 32 bytes for each channel.
117
118 \param fd The file handler.
119 \param len The file length.
120 \param rate The frequency of the sound.
121 \param bitsize The sample size (bits per sample).
122 \param channels Number of channels.
123 \return A handle to the sound effect on success. On error,
124 SFXHND_INVALID is returned.
125*/
126sfxhnd_t snd_sfx_load_fd(file_t fd, size_t len, uint32_t rate, uint16_t bitsize, uint16_t channels);
127
128/** \brief Load a sound effect.
129
130 This function loads a sound effect from a WAV file contained in memory and
131 returns a handle to it. The sound effect can be either stereo or mono, and
132 must either be 8-bit or 16-bit uncompressed PCM samples, or 4-bit Yamaha
133 ADPCM.
134
135 \warning The sound effect you are loading must be at most 65534 samples
136 in length.
137
138 \param buf The buffer to load.
139 \return A handle to the sound effect on success. On error,
140 SFXHND_INVALID is returned.
141*/
143
144/** \brief Load a sound effect without wav header from buffer.
145
146 This function loads a sound effect from raw data contained in memory and
147 returns a handle to it. The sound effect can be either stereo or mono, and
148 must either be 8-bit or 16-bit uncompressed PCM samples, or 4-bit Yamaha
149 ADPCM.
150
151 \warning The sound effect you are loading must be at most 65534 samples
152 in length and multiple by 32 bytes for each channel.
153
154 \param buf The buffer.
155 \param len The file length.
156 \param rate The frequency of the sound.
157 \param bitsize The sample size (bits per sample).
158 \param channels Number of channels.
159 \return A handle to the sound effect on success. On error,
160 SFXHND_INVALID is returned.
161*/
162sfxhnd_t snd_sfx_load_raw_buf(char *buf, size_t len, uint32_t rate, uint16_t bitsize, uint16_t channels);
163
164/** \brief Unload a sound effect.
165
166 This function unloads a previously loaded sound effect, and frees the memory
167 associated with it.
168
169 \param idx A handle to the sound effect to unload.
170*/
172
173/** \brief Unload all loaded sound effects.
174
175 This function unloads all previously loaded sound effect, and frees the
176 memory associated with them.
177*/
179
180/** \brief Play a sound effect.
181
182 This function plays a loaded sound effect with the specified volume (for
183 both stereo or mono) and panning values (for mono sounds only).
184
185 \param idx The handle to the sound effect to play.
186 \param vol The volume to play at (between 0 and 255).
187 \param pan The panning value of the sound effect. 0 is all the
188 way to the left, 128 is center, 255 is all the way
189 to the right.
190
191 \return The channel used to play the sound effect (or the
192 left channel in the case of a stereo sound, the
193 right channel will be the next one) on success, or
194 -1 on failure.
195*/
196int snd_sfx_play(sfxhnd_t idx, int vol, int pan);
197
198/** \brief Play a sound effect on a specific channel.
199
200 This function works similar to snd_sfx_play(), but allows you to specify the
201 channel to play on. No error checking is done with regard to the channel, so
202 be sure its safe to play on that channel before trying.
203
204 \param chn The channel to play on (or in the case of stereo,
205 the left channel).
206 \param idx The handle to the sound effect to play.
207 \param vol The volume to play at (between 0 and 255).
208 \param pan The panning value of the sound effect. 0 is all the
209 way to the left, 128 is center, 255 is all the way
210 to the right.
211
212 \return chn
213*/
214int snd_sfx_play_chn(int chn, sfxhnd_t idx, int vol, int pan);
215
216/** \brief Extended sound effect playback function.
217
218 This function plays a sound effect with the specified parameters. This is
219 the extended version of the sound effect playback functions, and is used to
220 pass a structure containing the parameters to the function. With this
221 function, you can additionally specify extra parameters such as frequency
222 and looping (see sfx_play_data_t structure).
223
224 \param data The data structure containing the information needed
225 to play the sound effect.
226
227 \return chn
228*/
230
231/** \brief Stop a single channel of sound.
232
233 This function stops the specified channel of sound from playing. It does no
234 checking to make sure that a sound effect is playing on the channel
235 specified, and thus can be used even if you're using the channel for some
236 other purpose than sound effects.
237
238 \param chn The channel to stop.
239*/
240void snd_sfx_stop(int chn);
241
242/** \brief Stop all channels playing sound effects.
243
244 This function stops all channels currently allocated to sound effects from
245 playing. It does not affect channels allocated for use by something other
246 than sound effects..
247*/
249
250/** \brief Allocate a sound channel for use outside the sound effect system.
251
252 This function finds and allocates a channel for use for things other than
253 sound effects. This is useful for, for instance, the streaming code.
254
255 \returns The allocated channel on success, -1 on failure.
256*/
258
259/** \brief Free a previously allocated channel.
260
261 This function frees a channel that was allocated with snd_sfx_chn_alloc(),
262 returning it to the pool of available channels for sound effect use.
263
264 \param chn The channel to free.
265*/
266void snd_sfx_chn_free(int chn);
267
268/** @} */
269
270__END_DECLS
271
272#endif /* __DC_SOUND_SFXMGR_H */
273
Virtual filesystem support.
int snd_sfx_play_ex(sfx_play_data_t *data)
Extended sound effect playback function.
void snd_sfx_chn_free(int chn)
Free a previously allocated channel.
sfxhnd_t snd_sfx_load_fd(file_t fd, size_t len, uint32_t rate, uint16_t bitsize, uint16_t channels)
Load a sound effect without wav header by file handler.
void snd_sfx_stop(int chn)
Stop a single channel of sound.
int snd_sfx_play_chn(int chn, sfxhnd_t idx, int vol, int pan)
Play a sound effect on a specific channel.
int snd_sfx_play(sfxhnd_t idx, int vol, int pan)
Play a sound effect.
uint32_t sfxhnd_t
Sound effect handle type.
Definition sfxmgr.h:47
int snd_sfx_chn_alloc(void)
Allocate a sound channel for use outside the sound effect system.
void snd_sfx_stop_all(void)
Stop all channels playing sound effects.
sfxhnd_t snd_sfx_load_buf(char *buf)
Load a sound effect.
void snd_sfx_unload_all(void)
Unload all loaded sound effects.
sfxhnd_t snd_sfx_load_ex(const char *fn, uint32_t rate, uint16_t bitsize, uint16_t channels)
Load a sound effect without wav header.
sfxhnd_t snd_sfx_load_raw_buf(char *buf, size_t len, uint32_t rate, uint16_t bitsize, uint16_t channels)
Load a sound effect without wav header from buffer.
sfxhnd_t snd_sfx_load(const char *fn)
Load a sound effect.
void snd_sfx_unload(sfxhnd_t idx)
Unload a sound effect.
int file_t
File descriptor type.
Definition fs.h:94
Data structure for sound effect playback.
Definition sfxmgr.h:61
int loop
Whether to loop the sound effect or not.
Definition sfxmgr.h:69
unsigned int loopstart
Loop start index (in samples).
Definition sfxmgr.h:71
int pan
The panning value of the sound effect.
Definition sfxmgr.h:66
unsigned int loopend
Loop end index (in samples).
Definition sfxmgr.h:72
int freq
Frequency.
Definition sfxmgr.h:70
int chn
The channel to play on.
Definition sfxmgr.h:62
sfxhnd_t idx
The handle to the sound effect to play.
Definition sfxmgr.h:64
int vol
The volume to play at (between 0 and 255).
Definition sfxmgr.h:65