KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
stream.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/sound/stream.h
4 Copyright (C) 2002, 2004 Megan Potter
5 Copyright (C) 2020 Lawrence Sebald
6 Copyright (C) 2023, 2024 Ruslan Rostovtsev
7
8*/
9
10/** \file dc/sound/stream.h
11 \brief Sound streaming support.
12 \ingroup audio_streaming
13
14 This file contains declarations for doing streams of sound. This underlies
15 pretty much any decoded sounds you might use, including the Ogg Vorbis
16 libraries. Note that this does not actually handle decoding, so you'll have
17 to worry about that yourself (or use something in kos-ports).
18
19 \author Megan Potter
20 \author Florian Schulze
21 \author Lawrence Sebald
22 \author Ruslan Rostovtsev
23*/
24
25#ifndef __DC_SOUND_STREAM_H
26#define __DC_SOUND_STREAM_H
27
28#include <sys/cdefs.h>
29__BEGIN_DECLS
30
31#include <arch/types.h>
32
33/** \defgroup audio_streaming Streaming
34 \brief Streaming audio playback and management
35 \ingroup audio
36 @{
37*/
38
39/** \brief The maximum number of streams that can be allocated at once. */
40#define SND_STREAM_MAX 4
41
42/** \brief The maximum buffer size for each channel of PCM 16-bit stream. */
43#define SND_STREAM_BUFFER_MAX_PCM16 (128 << 10)
44
45/** \brief The maximum buffer size for each channel of PCM 8-bit stream. */
46#define SND_STREAM_BUFFER_MAX_PCM8 (64 << 10)
47
48/** \brief The maximum buffer size for each channel of ADPCM stream. */
49#define SND_STREAM_BUFFER_MAX_ADPCM (32 << 10)
50
51/** \brief The maximum buffer size for each channel of streams by default
52 and for backward compatibility. */
53#define SND_STREAM_BUFFER_MAX (64 << 10)
54
55/** \brief Stream handle type.
56
57 Each stream will be assigned a handle, which will be of this type. Further
58 operations on the stream will use the handle to identify which stream is
59 being referred to.
60*/
61typedef int snd_stream_hnd_t;
62
63/** \brief Invalid stream handle.
64
65 If a stream cannot be allocated, this will be returned.
66*/
67#define SND_STREAM_INVALID -1
68
69/** \brief Stream get data callback type.
70
71 Functions for providing stream data will be of this type, and can be
72 registered with snd_stream_set_callback().
73
74 \param hnd The stream handle being referred to.
75 \param smp_req The number of samples requested.
76 \param smp_recv Used to return the number of samples available.
77 \return A pointer to the buffer of samples. If stereo, the
78 samples should be interleaved. For best performance
79 use 32-byte aligned pointer.
80*/
81typedef void *(*snd_stream_callback_t)(snd_stream_hnd_t hnd, int smp_req,
82 int *smp_recv);
83
84/** \brief Direct stream data transfer callback type.
85
86 Functions for providing stream data will be of this type, and can be
87 registered with snd_stream_set_callback_direct().
88
89 \param hnd The stream handle being referred to.
90 \param left Left channel buffer address on AICA side.
91 \param right Right channel buffer address on AICA side.
92 \param size_req Requested size for each channel.
93 \retval -1 On failure.
94 \retval size_recv On success, received size.
95*/
97 uintptr_t left, uintptr_t right, size_t size_req);
98
99/** \brief Set the callback for a given stream.
100
101 This function sets the get data callback function for a given stream,
102 overwriting any old callback that may have been in place.
103
104 \param hnd The stream handle for the callback.
105 \param cb A pointer to the callback function.
106*/
108
109/** \brief Set the callback for a given stream with direct transfer.
110
111 This function sets the get data callback function for a given stream,
112 overwriting any old callback that may have been in place.
113
114 \param hnd The stream handle for the callback.
115 \param cb A pointer to the callback function.
116*/
118
119/** \brief Set the user data for a given stream.
120
121 This function sets the user data pointer for the given stream, overwriting
122 any existing one that may have been in place. This is designed to allow the
123 user the ability to associate a piece of data with the stream for instance
124 to assist in identifying what sound is playing on a stream. The driver does
125 not attempt to use this data in any way.
126
127 \param hnd The stream handle to look up.
128 \param d A pointer to the user data.
129*/
131
132/** \brief Get the user data for a given stream.
133
134 This function retrieves the set user data pointer for a given stream.
135
136 \param hnd The stream handle to look up.
137 \return The user data pointer set for this stream or NULL
138 if no data pointer has been set.
139*/
141
142/** \brief Stream filter callback type.
143
144 Functions providing filters over the stream data will be of this type, and
145 can be set with snd_stream_filter_add().
146
147 \param hnd The stream being referred to.
148 \param obj Filter user data.
149 \param hz The frequency of the sound data.
150 \param channels The number of channels in the sound data.
151 \param buffer A pointer to the buffer to process. This is before
152 any stereo separation is done. Can be changed by the
153 filter, if appropriate.
154 \param samplecnt A pointer to the number of samples. This can be
155 modified by the filter, if appropriate.
156*/
157typedef void (*snd_stream_filter_t)(snd_stream_hnd_t hnd, void *obj, int hz,
158 int channels, void **buffer,
159 int *samplecnt);
160
161/** \brief Add a filter to the specified stream.
162
163 This function adds a filter to the specified stream. The filter will be
164 called on each block of data input to the stream from then forward.
165
166 When the stream buffer filler needs more data, it starts out by calling
167 the initial callback (set above). It then calls each function in the
168 effect filter chain, which can modify the buffer and the amount of data
169 available as well. Filters persist across multiple calls to _init()
170 but will be emptied by _shutdown().
171
172 \param hnd The stream to add the filter to.
173 \param filtfunc A pointer to the filter function.
174 \param obj Filter function user data.
175*/
177 void *obj);
178
179/** \brief Remove a filter from the specified stream.
180
181 This function removes a filter that was previously added to the specified
182 stream.
183
184 \param hnd The stream to remove the filter from.
185 \param filtfunc A pointer to the filter function to remove.
186 \param obj The filter function's user data. Must be the same as
187 what was passed as obj to snd_stream_filter_add().
188*/
190 snd_stream_filter_t filtfunc, void *obj);
191
192/** \brief Prefill the stream buffers.
193
194 This function prefills the stream buffers before starting it. This is
195 implicitly called by snd_stream_start(), so there's probably no good reason
196 to call this yourself.
197
198 \param hnd The stream to prefill buffers on.
199*/
201
202/** \brief Initialize the stream system.
203
204 This function initializes the sound stream system and allocates memory for
205 it as needed. Note, this is not done by the default init, so if you're using
206 the streaming support and not using something like the kos-ports Ogg Vorbis
207 library, you'll need to call this yourself. This will implicitly call
208 snd_init(), so it will potentially overwrite anything going on the AICA.
209
210 \retval -1 On failure.
211 \retval 0 On success.
212*/
214
215/** \brief Initialize the stream system with limits.
216
217 The same as \ref snd_stream_init but it can either reduce or not allocate
218 the buffer for splitting the stereo stream at all.
219
220 \param channels Max channels for any streams.
221 \param buffer_size Max channel buffer size for any streams.
222
223 \retval -1 On failure.
224 \retval 0 On success.
225*/
226int snd_stream_init_ex(int channels, size_t buffer_size);
227
228/** \brief Shut down the stream system.
229
230 This function shuts down the stream system and frees the memory associated
231 with it. This does not call snd_shutdown().
232*/
234
235/** \brief Allocate a stream.
236
237 This function allocates a stream and sets its parameters.
238
239 \param cb The get data callback for the stream.
240 \param bufsize The size of the buffer for each channel of the stream.
241 \return A handle to the new stream on success,
242 SND_STREAM_INVALID on failure.
243*/
245
246/** \brief Reinitialize a stream.
247
248 This function reinitializes a stream, resetting its callback function.
249
250 \param hnd The stream handle to reinit.
251 \param cb The new get data callback for the stream.
252 \return hnd
253*/
255
256/** \brief Destroy a stream.
257
258 This function destroys a previously created stream, freeing all memory
259 associated with it.
260
261 \param hnd The stream to clean up.
262*/
264
265/** \brief Enable queueing on a stream.
266
267 This function enables queueing on the specified stream. This will make it so
268 that you must call snd_stream_queue_go() to actually start the stream, after
269 scheduling the start. This is useful for getting something ready but not
270 firing it right away.
271
272 \param hnd The stream to enable queueing on.
273*/
275
276/** \brief Disable queueing on a stream.
277
278 This function disables queueing on the specified stream. This does not imply
279 that a previously queued start on the stream will be fired if queueing was
280 enabled before.
281
282 \param hnd The stream to disable queueing on.
283*/
285
286/** \brief Start a stream after queueing the request.
287
288 This function makes the stream start once a start request has been queued,
289 if queueing mode is enabled on the stream.
290
291 \param hnd The stream to start the queue on.
292*/
294
295/** \brief Start a 16-bit PCM stream.
296
297 This function starts processing the given stream, prefilling the buffers as
298 necessary. In queueing mode, this will not start playback.
299
300 \param hnd The stream to start.
301 \param freq The frequency of the sound.
302 \param st 1 if the sound is stereo, 0 if mono.
303*/
305
306/** \brief Start a 8-bit PCM stream.
307
308 This function starts processing the given stream, prefilling the buffers as
309 necessary. In queueing mode, this will not start playback.
310
311 \param hnd The stream to start.
312 \param freq The frequency of the sound.
313 \param st 1 if the sound is stereo, 0 if mono.
314*/
316
317/** \brief Start a 4-bit ADPCM stream.
318
319 This function starts processing the given stream, prefilling the buffers as
320 necessary. In queueing mode, this will not start playback.
321
322 \param hnd The stream to start.
323 \param freq The frequency of the sound.
324 \param st 1 if the sound is stereo, 0 if mono.
325*/
327
328/** \brief Stop a stream.
329
330 This function stops a stream, stopping any sound playing from it. This will
331 happen immediately, regardless of whether queueing is enabled or not.
332
333 \param hnd The stream to stop.
334*/
336
337/** \brief Poll a stream.
338
339 This function polls the specified stream to load more data if necessary. If
340 using the streaming support, you must call this function periodically (most
341 likely in a thread), or you won't get any sound output.
342
343 \param hnd The stream to poll.
344 \retval -3 If NULL was returned from the callback.
345 \retval -1 If no callback is set, or if the state has been
346 corrupted.
347 \retval 0 On success.
348*/
350
351/** \brief Set the volume on the stream.
352
353 This function sets the volume of the specified stream.
354
355 \param hnd The stream to set volume on.
356 \param vol The volume to set. Valid values are 0-255.
357*/
359
360/** \brief Set the panning on the stream.
361
362 This function sets the panning of the specified stream.
363
364 \param hnd The stream to set volume on.
365 \param left_pan The left panning to set. Valid values are 0-255.
366 \param right_pan The right panning to set. Valid values are 0-255.
367*/
368void snd_stream_pan(snd_stream_hnd_t hnd, int left_pan, int right_pan);
369
370/** @} */
371
372__END_DECLS
373
374#endif /* __DC_SOUND_STREAM_H */
void snd_stream_start(snd_stream_hnd_t hnd, uint32 freq, int st)
Start a 16-bit PCM stream.
int snd_stream_poll(snd_stream_hnd_t hnd)
Poll a stream.
void snd_stream_set_callback_direct(snd_stream_hnd_t hnd, snd_stream_callback_direct_t cb)
Set the callback for a given stream with direct transfer.
void snd_stream_stop(snd_stream_hnd_t hnd)
Stop a stream.
void snd_stream_queue_disable(snd_stream_hnd_t hnd)
Disable queueing on a stream.
void snd_stream_start_pcm8(snd_stream_hnd_t hnd, uint32 freq, int st)
Start a 8-bit PCM stream.
int snd_stream_init(void)
Initialize the stream system.
size_t(* snd_stream_callback_direct_t)(snd_stream_hnd_t hnd, uintptr_t left, uintptr_t right, size_t size_req)
Direct stream data transfer callback type.
Definition stream.h:96
void snd_stream_volume(snd_stream_hnd_t hnd, int vol)
Set the volume on the stream.
void snd_stream_filter_remove(snd_stream_hnd_t hnd, snd_stream_filter_t filtfunc, void *obj)
Remove a filter from the specified stream.
int snd_stream_hnd_t
Stream handle type.
Definition stream.h:61
void snd_stream_set_callback(snd_stream_hnd_t hnd, snd_stream_callback_t cb)
Set the callback for a given stream.
void *(* snd_stream_callback_t)(snd_stream_hnd_t hnd, int smp_req, int *smp_recv)
Stream get data callback type.
Definition stream.h:81
snd_stream_hnd_t snd_stream_alloc(snd_stream_callback_t cb, int bufsize)
Allocate a stream.
void snd_stream_set_userdata(snd_stream_hnd_t hnd, void *d)
Set the user data for a given stream.
void snd_stream_destroy(snd_stream_hnd_t hnd)
Destroy a stream.
int snd_stream_reinit(snd_stream_hnd_t hnd, snd_stream_callback_t cb)
Reinitialize a stream.
void snd_stream_filter_add(snd_stream_hnd_t hnd, snd_stream_filter_t filtfunc, void *obj)
Add a filter to the specified stream.
void snd_stream_shutdown(void)
Shut down the stream system.
void snd_stream_pan(snd_stream_hnd_t hnd, int left_pan, int right_pan)
Set the panning on the stream.
void snd_stream_queue_go(snd_stream_hnd_t hnd)
Start a stream after queueing the request.
void snd_stream_prefill(snd_stream_hnd_t hnd)
Prefill the stream buffers.
void * snd_stream_get_userdata(snd_stream_hnd_t hnd)
Get the user data for a given stream.
void snd_stream_start_adpcm(snd_stream_hnd_t hnd, uint32 freq, int st)
Start a 4-bit ADPCM stream.
void(* snd_stream_filter_t)(snd_stream_hnd_t hnd, void *obj, int hz, int channels, void **buffer, int *samplecnt)
Stream filter callback type.
Definition stream.h:157
void snd_stream_queue_enable(snd_stream_hnd_t hnd)
Enable queueing on a stream.
int snd_stream_init_ex(int channels, size_t buffer_size)
Initialize the stream system with limits.
unsigned long uint32
32-bit unsigned integer
Definition types.h:33
Common integer types.