1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/*
* wavlib.h - simple WAV I/O library interface
* Copyright (C) 2006-2010 Francesco Romani <fromani at gmail dot com>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef _WAVLIB_H_
#define _WAVLIB_H_
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <stdint.h>
/* transcode build system integration */
#ifdef WORDS_BIGENDIAN
#define WAV_BIG_ENDIAN 1
#else
#define WAV_LITTLE_ENDIAN 1
#endif
#define WAVLIB_VERSION "0.2.0"
#define WAVLIB_MAJOR 0
#define WAVLIB_MINOR 2
#define WAVLIB_PATCH 0
typedef enum {
WAV_READ = 1, /* open WAV file in read-only mode */
WAV_WRITE = 2, /* open WAV file in write-only mode */
WAV_PIPE = 4, /* cut SEEEKs */
} WAVMode;
typedef enum {
WAV_SUCCESS, /* no error so far */
WAV_NO_MEM, /* can't acquire requested amount of memory */
WAV_IO_ERROR, /* unable to read/write (often write) data */
WAV_BAD_FORMAT, /* acquired data doesn't seem wav-compliant */
WAV_BAD_PARAM, /* bad parameter for requested operation */
WAV_UNSUPPORTED, /* feature not yet supported by wavlib */
} WAVError;
typedef struct wav_ *WAV;
/*
* wav_open:
* open a WAVE file in given mode, either read-only or write-only,
* and associate a WAV descriptor to it.
*
* Parameters:
* filename: path of WAVE file to open.
* mode: mode to open WAVE file.
* Can be either WAV_READ (open read-only)
* or WAV_WRITE (open write only).
* WAV_PIPE can be OR'd with this parameter if given
* filename is a named pipe. This flag is honoured only
* if mode is WAV_READ.
* err: if not NULL, reason for error, if any, will be stored
* on WAVError pointed by this parameter.
*
* Return Value:
* a valid WAV descriptor if successfull, or NULL otherwise.
* Side effects:
* N/A
* Preconditions:
* N/A
* Postconditions:
* N/A
*/
WAV wav_open(const char *filename, WAVMode mode, WAVError *err);
/*
* wav_fdopen:
* attach a WAV descriptor to already open POSIX file descriptor.
*
* Parameters:
* fd: existing file descriptor; WAV descriptor will be attached here.
* mode: mode to open WAVE file.
* Can be either WAV_READ (open read-only)
* or WAV_WRITE (open write only).
* WAV_PIPE can be OR'd with this parameter if given
* filename is a pipe.
* value of 'mode' MUST be congruent with mode of given
* file descriptor.
* err: if not NULL, reason for error, if any, will be stored
* on WAVError pointed by this parameter.
*
* Return Value:
* a valid WAV descriptor if successfull, or NULL otherwise.
* Side effects:
* N/A
* Preconditions:
* N/A
* Postconditions:
* N/A
*/
WAV wav_fdopen(int fd, WAVMode mode, WAVError *err);
/*
* wav_close:
* close a WAV descriptor, freeing acquired resources,
*
* Parameters:
* handle: WAV descriptor to close
* Return Value:
* 0 if successfull, -1 otherwise
* Side effects:
* if error, error code is stored in WAV descritptor, and
* can be fetched using wav_last_error.
* If WAV descriptor was open using WAV_WRITE mode, WAVE
* header will be updated using wav_write_header.
* Preconditions:
* given wav handle is a valid one obtained as return value of
* wav_open/wav_fdopen.
* Postconditions:
* N/A
*/
int wav_close(WAV handle);
/*
* wav_read_data:
* read a buffer of pcm data from given wav file. Delivers data
* in wav-native-byte-order (little endian).
* This function doesn't mess with given data, it just reads
* data verbatim from wav file. so caller must take care to
* split/join channel data or do any needed operation.
*
* Parameters:
* handle: wav handle to write data in
* buffer: pointer to data to store the data readed
* bufsize: size of given buffer.
* Return Value:
* return of bytes effectively readed from wav file.
* -1 means an error.
* Side effects:
* N/A
* Preconditions:
* given wav handle is a valid one obtained as return value of
* wav_open/wav_fdopen; wav handle was opened in WAV_READ mode.
* bufsize is a multiple of 2.
* Postconditions:
* N/A
*/
ssize_t wav_read_data(WAV handle, uint8_t *buffer, size_t bufsize);
/*
* wav_write_data:
* write a buffer of pcm data in given wav file. Expect data
* in wav-native-byte-order (little endian).
* This function doesn't mess with given data, it just writes
* data verbatim on wav file. so caller must take care to
* split/join channel data or do any needed operation.
*
* Parameters:
* handle: wav handle to write data in
* buffer: pointer to data to be written
* bufsize: number of bytes of data to write
* Return Value:
* return of bytes effectively written on wav file.
* -1 means an error.
* Side effects:
* N/A
* Preconditions:
* given wav handle is a valid one obtained as return value of
* wav_open/wav_fdopen; wav handle was opened in WAV_WRITE mode.
* buffer contains data in wav-native-byte-order (little endian)
* bufsize is a multiple of 2.
* Postconditions:
* N/A
*/
ssize_t wav_write_data(WAV handle, const uint8_t *buffer, size_t bufsize);
/*
* wav_chunk_size:
* guess^Wcompute the appropriate buffer size for reading/writing data
* with given wav descriptor.
*
* Parameters:
* handle: wav descriptor to work on
* Return Value:
* suggested size of buffer for R/W operations
* Side effects:
* N/A
* Preconditions:
* Of course given wav handle is must be a valid one obtained as return
* value of wav_open/wav_fdopen; additionally, wav header for given
* descriptor must be fully avalaible.
* This is always true if wav file was opened in read mode. If wav file
* was opened in write mode, caller must ensure to issue all stream
* parameters using wav_set_<someting> (and possibly to use
* wav_write_header) BEFORE to use this function.
* Otherwise, caller will get an unreliable result (aka: garbage).
* Postconditions:
* N/A
*/
uint32_t wav_chunk_size(WAV handle, double fps);
/*
* wav_last_error:
* get descriptor of last error related to given wav descriptor.
*
* Parameters:
* handle: a wav descriptor.
* Return Value:
* code of last error occurred.
* Side effects:
* N/A
* Preconditions:
* given wav descriptor was obtained as valid return value of
* wav_open/wav_fdopen.
* Postconditions:
* N/A
*/
WAVError wav_last_error(WAV handle);
/*
* wav_strerror:
* get a human-readable short description of an error code
*
* Parameters:
* err: error code to describe
* Return Value:
* a pointer to a C-string describing the given error code,
* or NULL if given error code isn't known.
* Side effects:
* N/A
* Preconditions:
* N/A
* Postconditions:
* N/A
*/
const char *wav_strerror(WAVError err);
/*
* wav_write_header:
* update header for WAVE file using information stored
* in attached WAV descriptor.
* This function MAKE NO SENSE if applied to a WAV
* descriptor open in WAV_READ mode.
*
* IMPORTANT NOTICE:
* If wav_write_header is applied to a WAV descriptor
* using WAV_WRITE|WAV_PIPE mode, *it doesn't seek at all*,
* so it will simply write the full WAVE header into
* stream at current position.
* This actually is a *design decision*, the intended usage
* of this function when dealing with WAV_WRITE|WAV_PIPE
* descriptor is something like this:
*
* WAV wav = wav_fdopen(my_fd, WAV_WRITE|WAV_PIPE, NULL);
*
* <set WAVE parameters using multiple wav_set_*>
*
* wav_write_header(wav, 1);
*
* while (1) {
* bytes = get_data(buffer, bufsize);
* if(no_more_data()) {
* break;
* }
* if (wav_write_data(wav, buffer, bytes) != 0) {
* break;
* }
* }
*
* Parameters:
* handle: update header for WAVE file attached to this
* descriptor
* force: if !0, update header even if isn't needed
* Return Value:
* 0 successfull, -1 otherwise
* Side effects:
* WAVE header is (re)written on file. This usually
* requires some seeks on file.
* See above if WAV_PIPE mode is used
* Preconditions:
* given wav descriptor is a valid one obtained as return value of
* wav_open or wav_fdopen.
* Postconditions:
* N/A
*/
int wav_write_header(WAV handle, int force);
/*
* wav_{get,set}_*:
* set or get interesting WAV parameters.
* wav_get_* functions can always be used if WAV descriptor is both
* in read or write mode, but wav_set_* functions hare honoured
* only if wav descriptor is in write mode.
* wav_set_* functions applied to a read-mode wav are silently
* discarderd.
*
* avalaible parameters:
* rate (Average Samples Per Second): quantization rate of stream, Hz.
* channels: number of channels in stream.
* bits: size in bits for every sample.
* bitrate (derived from Average Bytes per Second):
* bytes needed to store a second of data.
* Expressed in *KILOBIT/second*.
*
* Parameters:
* handle: handle to a WAV descriptor returned by wav_open/wav_fdopen.
* <parameter>: (only wav_set_*) value of parameter to set in descriptor.
* Return Value:
* wav_get_*: value of requested parameter.
* wav_set_*: None.
* Side effects:
* N/A
* Preconditions:
* given wav descriptor is a valid one obtained as return value of
* wav_open or wav_fdopen.
* Postconditions:
* N/A
*/
uint16_t wav_get_rate(WAV handle);
void wav_set_rate(WAV handle, uint16_t rate);
uint8_t wav_get_channels(WAV handle);
void wav_set_channels(WAV handle, uint8_t channels);
uint8_t wav_get_bits(WAV handle);
void wav_set_bits(WAV handle, uint8_t bits);
uint32_t wav_get_bitrate(WAV handle);
void wav_set_bitrate(WAV handle, uint32_t bitrate);
#endif /* _WAVLIB_H_ */
|