1/*
2 * Wuff - A very basic WAVE reader
3 */
4
5#ifndef WUFF_H
6#define WUFF_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12
13#define WUFF_VERSION_MAJOR 0
14#define WUFF_VERSION_MINOR 0
15#define WUFF_VERSION_BUILD 0
16#define WUFF_VERSION_REVISION 2
17
18
19#ifndef WUFF_API_OVERRIDE
20 #if defined(_WIN32) && defined(WUFF_DYNAMIC_LIB)
21 #define WUFF_EXPORT __declspec(dllexport)
22 #define WUFF_IMPORT __declspec(dllimport)
23 #else
24 #define WUFF_EXPORT
25 #define WUFF_IMPORT
26 #endif
27#endif
28
29#ifdef WUFF_BUILDING_CORE
30 #define WUFF_API WUFF_EXPORT
31#else
32 #define WUFF_API WUFF_IMPORT
33#endif
34
35
36#ifdef WUFF_FORCE_STDINT_H
37 #include <stdint.h>
38typedef uint8_t wuff_uint8;
39typedef int8_t wuff_sint8;
40typedef uint16_t wuff_uint16;
41typedef int16_t wuff_sint16;
42typedef uint32_t wuff_uint32;
43typedef int32_t wuff_sint32;
44 #ifdef WUFF_NO_64BIT_TYPE
45typedef uint32_t wuff_uint64;
46typedef int32_t wuff_sint64;
47 #else
48typedef uint64_t wuff_uint64;
49typedef int64_t wuff_sint64;
50 #endif
51#elif _MSC_VER
52typedef unsigned __int8 wuff_uint8;
53typedef signed __int8 wuff_sint8;
54typedef unsigned __int16 wuff_uint16;
55typedef signed __int16 wuff_sint16;
56typedef unsigned __int32 wuff_uint32;
57typedef signed __int32 wuff_sint32;
58typedef unsigned __int64 wuff_uint64;
59typedef signed __int64 wuff_sint64;
60#else
61typedef unsigned char wuff_uint8;
62typedef signed char wuff_sint8;
63typedef unsigned short wuff_uint16;
64typedef signed short wuff_sint16;
65typedef unsigned int wuff_uint32;
66typedef signed int wuff_sint32;
67 #ifdef WUFF_NO_64BIT_TYPE
68typedef unsigned long wuff_uint64;
69typedef signed long wuff_sint64;
70 #else
71typedef unsigned long long wuff_uint64;
72typedef signed long long wuff_sint64;
73 #endif
74#endif
75
76/** @file */
77/**
78 * Opaque structure used to identify the open Wuff streams.
79 */
80struct wuff_handle;
81
82/**
83 * Callbacks that control the delivery of the data of the WAVE file.
84 *
85 * The return values of the functions indicate their status. A zero or positive
86 * value means success and a negative value failure. The macros WUFF_SUCCESS and
87 * WUFF_ERROR, or a value equal or below WUFF_USER_ERROR can be used. The error
88 * value will be returned by the function called by the application.
89 */
90struct wuff_callback
91{
92 /**
93 * The read callback requests the linking application to write at least
94 * 'size' bytes into the memory where 'buffer' is pointing to. The value
95 * pointed to by 'size' must be update to the actual number of bytes
96 * written. Zero will be interepreted as the end-of-file.
97 *
98 * @param userdata The userdata set with wuff_open.
99 * @param buffer A pointer to the memory where the data can be written to.
100 * @param size A pointer to the size of the buffer and the bytes written.
101 */
102 wuff_sint32 (* read)(void * userdata, wuff_uint8 * buffer, size_t * size);
103
104 /**
105 * The seek callback requests the linking application to seek to a new byte
106 * offset in the WAVE data. The next call to the read callback must then
107 * write data starting from this position. The offset is always relative
108 * to the beginning of the WAVE data.
109 *
110 * @param userdata The userdata set with wuff_open.
111 * @param offset The new offset.
112 */
113 wuff_sint32 (* seek)(void * userdata, wuff_uint64 offset);
114
115 /**
116 * The tell callback requests the linking application to write the current
117 * byte position to the integer pointed to by 'offset'.
118 *
119 * @param userdata The userdata set with wuff_open.
120 * @param offset A pointer to an integer where the current position can be written to.
121 */
122 wuff_sint32 (* tell)(void * userdata, wuff_uint64 * offset);
123};
124
125
126/**
127 * Stream information structure.
128 */
129struct wuff_info
130{
131 wuff_uint16 format; /**< The format of the stream.
132 * See "Wuff raw sample formats" below. */
133 wuff_uint16 channels; /**< The number of channels in the stream. */
134 wuff_uint32 sample_rate; /**< The sample rate in hertz. */
135 wuff_uint16 bits_per_sample; /**< The number of bits per sample. */
136 wuff_uint64 length; /**< The length of the stream in samples. */
137};
138
139
140/**
141 * Version information structure.
142 */
143struct wuff_version
144{
145 wuff_uint16 major;
146 wuff_uint16 minor;
147 wuff_uint16 build;
148 wuff_uint16 revision;
149};
150
151
152/**
153 * Opens a new Wuff stream. This will read from the callbacks immediately, make
154 * sure they're ready. It will check if the WAVE file is supported.
155 *
156 * @param handle A pointer to pointer of a wuff_handle that will be
157 * initialized if the function succeeds.
158 * @param callback The callbacks for the data of the WAVE file.
159 * @param userdata A void pointer that will be passed to the callbacks.
160 * @return Returns a negative value if an error occured.
161 */
162WUFF_API wuff_sint32 wuff_open(struct wuff_handle ** handle, struct wuff_callback * callback, void * userdata);
163
164/**
165 * Closes a Wuff stream.
166 *
167 * @param handle The Wuff stream handle.
168 * @return Returns a negative value if an error occured.
169 */
170WUFF_API wuff_sint32 wuff_close(struct wuff_handle * handle);
171
172/**
173 * Fills the wuff_info struct with information about the stream.
174 *
175 * @param handle The Wuff stream handle.
176 * @param info A pointer to a wuff_info struct.
177 * @return Returns a negative value if an error occured.
178 */
179WUFF_API wuff_sint32 wuff_stream_info(struct wuff_handle * handle, struct wuff_info * info);
180
181/**
182 * Sets the output format of the decoder. A new format resets the decoder output
183 * to the beginning of the current block (the sample of the first channel).
184 *
185 * @param handle The Wuff stream handle.
186 * @param format The new output format.
187 * @return Returns a negative value if an error occured.
188 */
189WUFF_API wuff_sint32 wuff_format(struct wuff_handle * handle, wuff_uint16 format);
190
191/**
192 * Decodes samples to the passed memory location. The size_t pointer points to
193 * the maximum number of bytes that can be written to the buffer. This count
194 * will be adjusted to the number of bytes written to the buffer.
195 *
196 * @param handle The Wuff stream handle.
197 * @param buffer The buffer to write to.
198 * @param size The maximum number of bytes to write to the buffer.
199 * @return Returns a negative value if an error occured.
200 */
201WUFF_API wuff_sint32 wuff_read(struct wuff_handle * handle, wuff_uint8 * buffer, size_t * size);
202
203/**
204 * Seeks to a sample location.
205 * The next call to wuff_read will return samples starting from this position.
206 *
207 * @param handle The Wuff stream handle.
208 * @param offset The sample offset to seek to.
209 * @return Returns a negative value if an error occured.
210 */
211WUFF_API wuff_sint32 wuff_seek(struct wuff_handle * handle, wuff_uint64 offset);
212
213/**
214 * Sets the current position.
215 *
216 * @param handle The Wuff stream handle.
217 * @param offset A pointer to a integer that will receive the sample offset.
218 * @return Returns a negative value if an error occured.
219 */
220WUFF_API wuff_sint32 wuff_tell(struct wuff_handle * handle, wuff_uint64 * offset);
221
222/**
223 * Copies the Wuff version of the binary into the struct.
224 * For compile-time version information use the WUFF_VERSION_MAJOR,
225 * WUFF_VERSION_MINOR, WUFF_VERSION_BUILD, and WUFF_VERSION_REVISION macros.
226 *
227 * @param version A pointer to a wuff_version struct that will receive the
228 * version information.
229 */
230WUFF_API void wuff_version(struct wuff_version * version);
231
232
233/* Wuff raw sample formats. */
234#define WUFF_FORMAT_PCM_U8 0
235#define WUFF_FORMAT_PCM_S16 1
236#define WUFF_FORMAT_PCM_S24 2
237#define WUFF_FORMAT_PCM_S32 3
238#define WUFF_FORMAT_IEEE_FLOAT_32 4
239#define WUFF_FORMAT_IEEE_FLOAT_64 5
240#define WUFF_FORMAT_MAX 6
241
242
243/* Success and error return values for all functions. */
244#define WUFF_STREAM_EOF 100
245
246#define WUFF_SUCCESS 0
247
248#define WUFF_ERROR -1
249#define WUFF_INVALID_PARAM -2
250#define WUFF_MEMALLOC_ERROR -3
251
252#define WUFF_STREAM_NOT_RIFF -100
253#define WUFF_STREAM_NOT_WAVE -101
254#define WUFF_STREAM_INVALID -102
255#define WUFF_STREAM_ZERO_CHANNELS -103
256#define WUFF_STREAM_ZERO_SAMPLE_RATE -104
257#define WUFF_STREAM_ZERO_BITS_PER_SAMPLE -105
258#define WUFF_STREAM_FORMAT_CHUNK_MISSING -106
259#define WUFF_STREAM_DATA_CHUNK_MISSING -107
260#define WUFF_STREAM_CHUNK_NOT_FOUND -108
261
262#define WUFF_FORMAT_UNSUPPORTED -200
263
264#define WUFF_BUFFER_INVALID_SIZE -300
265#define WUFF_BUFFER_INVALID_STREAM_POSITION -301
266
267#define WUFF_USER_ERROR -10000
268
269
270#ifdef __cplusplus
271}
272#endif
273
274#endif /* WUFF_H */
275