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 a stream. */
43#define SND_STREAM_BUFFER_MAX 0x10000
44
45/** \brief Stream handle type.
46
47 Each stream will be assigned a handle, which will be of this type. Further
48 operations on the stream will use the handle to identify which stream is
49 being referred to.
50*/
51typedef int snd_stream_hnd_t;
52
53/** \brief Invalid stream handle.
54
55 If a stream cannot be allocated, this will be returned.
56*/
57#define SND_STREAM_INVALID -1
58
59/** \brief Stream get data callback type.
60
61 Functions for providing stream data will be of this type, and can be
62 registered with snd_stream_set_callback().
63
64 \param hnd The stream handle being referred to.
65 \param smp_req The number of samples requested.
66 \param smp_recv Used to return the number of samples available.
67 \return A pointer to the buffer of samples. If stereo, the
68 samples should be interleaved. For best performance
69 use 32-byte aligned pointer.
70*/
71typedef void *(*snd_stream_callback_t)(snd_stream_hnd_t hnd, int smp_req,
72 int *smp_recv);
73
74/** \brief Direct stream data transfer callback type.
75
76 Functions for providing stream data will be of this type, and can be
77 registered with snd_stream_set_callback_direct().
78
79 \param hnd The stream handle being referred to.
80 \param left Left channel buffer address on AICA side.
81 \param right Right channel buffer address on AICA side.
82 \param size_req Requested size for each channel.
83 \retval -1 On failure.
84 \retval size_recv On success, received size.
85*/
87 uintptr_t left, uintptr_t right, size_t size_req);
88
89/** \brief Set the callback for a given stream.
90
91 This function sets the get data callback function for a given stream,
92 overwriting any old callback that may have been in place.
93
94 \param hnd The stream handle for the callback.
95 \param cb A pointer to the callback function.
96*/
98
99/** \brief Set the callback for a given stream with direct transfer.
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 user data for a given stream.
110
111 This function sets the user data pointer for the given stream, overwriting
112 any existing one that may have been in place. This is designed to allow the
113 user the ability to associate a piece of data with the stream for instance
114 to assist in identifying what sound is playing on a stream. The driver does
115 not attempt to use this data in any way.
116
117 \param hnd The stream handle to look up.
118 \param d A pointer to the user data.
119*/
121
122/** \brief Get the user data for a given stream.
123
124 This function retrieves the set user data pointer for a given stream.
125
126 \param hnd The stream handle to look up.
127 \return The user data pointer set for this stream or NULL
128 if no data pointer has been set.
129*/
131
132/** \brief Stream filter callback type.
133
134 Functions providing filters over the stream data will be of this type, and
135 can be set with snd_stream_filter_add().
136
137 \param hnd The stream being referred to.
138 \param obj Filter user data.
139 \param hz The frequency of the sound data.
140 \param channels The number of channels in the sound data.
141 \param buffer A pointer to the buffer to process. This is before
142 any stereo separation is done. Can be changed by the
143 filter, if appropriate.
144 \param samplecnt A pointer to the number of samples. This can be
145 modified by the filter, if appropriate.
146*/
147typedef void (*snd_stream_filter_t)(snd_stream_hnd_t hnd, void *obj, int hz,
148 int channels, void **buffer,
149 int *samplecnt);
150
151/** \brief Add a filter to the specified stream.
152
153 This function adds a filter to the specified stream. The filter will be
154 called on each block of data input to the stream from then forward.
155
156 When the stream buffer filler needs more data, it starts out by calling
157 the initial callback (set above). It then calls each function in the
158 effect filter chain, which can modify the buffer and the amount of data
159 available as well. Filters persist across multiple calls to _init()
160 but will be emptied by _shutdown().
161
162 \param hnd The stream to add the filter to.
163 \param filtfunc A pointer to the filter function.
164 \param obj Filter function user data.
165*/
167 void *obj);
168
169/** \brief Remove a filter from the specified stream.
170
171 This function removes a filter that was previously added to the specified
172 stream.
173
174 \param hnd The stream to remove the filter from.
175 \param filtfunc A pointer to the filter function to remove.
176 \param obj The filter function's user data. Must be the same as
177 what was passed as obj to snd_stream_filter_add().
178*/
180 snd_stream_filter_t filtfunc, void *obj);
181
182/** \brief Prefill the stream buffers.
183
184 This function prefills the stream buffers before starting it. This is
185 implicitly called by snd_stream_start(), so there's probably no good reason
186 to call this yourself.
187
188 \param hnd The stream to prefill buffers on.
189*/
191
192/** \brief Initialize the stream system.
193
194 This function initializes the sound stream system and allocates memory for
195 it as needed. Note, this is not done by the default init, so if you're using
196 the streaming support and not using something like the kos-ports Ogg Vorbis
197 library, you'll need to call this yourself. This will implicitly call
198 snd_init(), so it will potentially overwrite anything going on the AICA.
199
200 \retval -1 On failure.
201 \retval 0 On success.
202*/
204
205/** \brief Initialize the stream system with limits.
206
207 The same as \ref snd_stream_init but it can either reduce or not allocate
208 the buffer for splitting the stereo stream at all.
209
210 \param channels Max channels for any streams.
211 \param buffer_size Max channel buffer size for any streams.
212
213 \retval -1 On failure.
214 \retval 0 On success.
215*/
216int snd_stream_init_ex(int channels, size_t buffer_size);
217
218/** \brief Shut down the stream system.
219
220 This function shuts down the stream system and frees the memory associated
221 with it. This does not call snd_shutdown().
222*/
224
225/** \brief Allocate a stream.
226
227 This function allocates a stream and sets its parameters.
228
229 \param cb The get data callback for the stream.
230 \param bufsize The size of the buffer for each channel of the stream.
231 \return A handle to the new stream on success,
232 SND_STREAM_INVALID on failure.
233*/
235
236/** \brief Reinitialize a stream.
237
238 This function reinitializes a stream, resetting its callback function.
239
240 \param hnd The stream handle to reinit.
241 \param cb The new get data callback for the stream.
242 \return hnd
243*/
245
246/** \brief Destroy a stream.
247
248 This function destroys a previously created stream, freeing all memory
249 associated with it.
250
251 \param hnd The stream to clean up.
252*/
254
255/** \brief Enable queueing on a stream.
256
257 This function enables queueing on the specified stream. This will make it so
258 that you must call snd_stream_queue_go() to actually start the stream, after
259 scheduling the start. This is useful for getting something ready but not
260 firing it right away.
261
262 \param hnd The stream to enable queueing on.
263*/
265
266/** \brief Disable queueing on a stream.
267
268 This function disables queueing on the specified stream. This does not imply
269 that a previously queued start on the stream will be fired if queueing was
270 enabled before.
271
272 \param hnd The stream to disable queueing on.
273*/
275
276/** \brief Start a stream after queueing the request.
277
278 This function makes the stream start once a start request has been queued,
279 if queueing mode is enabled on the stream.
280
281 \param hnd The stream to start the queue on.
282*/
284
285/** \brief Start a 16-bit PCM stream.
286
287 This function starts processing the given stream, prefilling the buffers as
288 necessary. In queueing mode, this will not start playback.
289
290 \param hnd The stream to start.
291 \param freq The frequency of the sound.
292 \param st 1 if the sound is stereo, 0 if mono.
293*/
295
296/** \brief Start a 8-bit PCM stream.
297
298 This function starts processing the given stream, prefilling the buffers as
299 necessary. In queueing mode, this will not start playback.
300
301 \param hnd The stream to start.
302 \param freq The frequency of the sound.
303 \param st 1 if the sound is stereo, 0 if mono.
304*/
306
307/** \brief Start a 4-bit ADPCM stream.
308
309 This function starts processing the given stream, prefilling the buffers as
310 necessary. In queueing mode, this will not start playback.
311
312 \param hnd The stream to start.
313 \param freq The frequency of the sound.
314 \param st 1 if the sound is stereo, 0 if mono.
315*/
317
318/** \brief Stop a stream.
319
320 This function stops a stream, stopping any sound playing from it. This will
321 happen immediately, regardless of whether queueing is enabled or not.
322
323 \param hnd The stream to stop.
324*/
326
327/** \brief Poll a stream.
328
329 This function polls the specified stream to load more data if necessary. If
330 using the streaming support, you must call this function periodically (most
331 likely in a thread), or you won't get any sound output.
332
333 \param hnd The stream to poll.
334 \retval -3 If NULL was returned from the callback.
335 \retval -1 If no callback is set, or if the state has been
336 corrupted.
337 \retval 0 On success.
338*/
340
341/** \brief Set the volume on the stream.
342
343 This function sets the volume of the specified stream.
344
345 \param hnd The stream to set volume on.
346 \param vol The volume to set. Valid values are 0-255.
347*/
349
350/** \brief Set the panning on the stream.
351
352 This function sets the panning of the specified stream.
353
354 \param hnd The stream to set volume on.
355 \param left_pan The left panning to set. Valid values are 0-255.
356 \param right_pan The right panning to set. Valid values are 0-255.
357*/
358void snd_stream_pan(snd_stream_hnd_t hnd, int left_pan, int right_pan);
359
360/** @} */
361
362__END_DECLS
363
364#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:86
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:51
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:71
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:147
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.