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