1 /*	$Id: libaiff.h,v 1.28 2007/09/19 11:57:58 toad32767 Exp $ */
2 
3 /*-
4  * Copyright (c) 2005, 2006, 2007 Marco Trillo
5  *
6  * Permission is hereby granted, free of charge, to any
7  * person obtaining a copy of this software and associated
8  * documentation files (the "Software"), to deal in the
9  * Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the
12  * Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice
16  * shall be included in all copies or substantial portions of
17  * the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
20  * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
21  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
23  * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 #ifndef LIBAIFF_H_INCL
30 #define LIBAIFF_H_INCL 1
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <libaiff/config.h>
35 #include <libaiff/endian.h>
36 
37 #ifdef HAVE_SYS_TYPES_H
38 #include <sys/types.h>
39 #endif
40 
41 #ifdef HAVE_STDINT_H
42 #include <stdint.h>
43 #endif
44 
45 #ifdef HAVE_INTTYPES_H
46 #include <inttypes.h>
47 #endif
48 
49 #define LIBAIFF_API_VERSION	499
50 
51 
52 /* == Typedefs == */
53 typedef uint32_t IFFType ;
54 typedef uint8_t iext ;
55 typedef uint16_t MarkerId ;
56 
57 /* Struct for AIFF I/O functions */
58 struct s_AIFF_Ref {
59 	FILE* fd;
60 	int flags;
61 	int stat; /* status */
62 	int segmentSize;
63 	int bitsPerSample;
64 	int nMarkers;
65 	int nChannels;
66 	double samplingRate;
67 	uint64_t nSamples;
68 	int markerPos;
69 	uint64_t len;
70 	uint64_t soundLen;
71 	uint64_t pos;
72 	uint64_t sampleBytes;
73 	uint64_t commonOffSet;
74 	uint64_t soundOffSet;
75 	uint64_t markerOffSet;
76 	IFFType format;
77 	IFFType audioFormat;
78 	void* decoder;
79 	void* pdata;
80 	void* buffer;
81 	size_t buflen;
82 	void* buffer2;
83 	size_t buflen2;
84 	int tics;
85 } ;
86 
87 typedef struct s_AIFF_Ref* AIFF_Ref;
88 #ifdef LIBAIFF
89 static const size_t kAIFFRefSize = sizeof(struct s_AIFF_Ref);
90 #endif /* LIBAIFF */
91 
92 
93 /*
94  * == Interchange File Format (IFF) attributes ==
95  */
96 #define AIFF_NAME		0x4e414d45
97 #define AIFF_AUTH		0x41555448
98 #define AIFF_COPY		0x28632920
99 #define AIFF_ANNO		0x414e4e4f
100 
101 #if !defined(LIBAIFF) && !defined(LIBAIFF_NOCOMPAT)
102 /*
103  * Backwards compatibility with LibAiff 1 and 2
104  */
105 #ifdef WORDS_BIGENDIAN
106 static char NameID[4] = {'N', 'A', 'M', 'E'};
107 static char AuthID[4] = {'A', 'U', 'T', 'H'};
108 static char CopyID[4] = {'(', 'c', ')', ' '};
109 static char AnnoID[4] = {'A', 'N', 'N', 'O'};
110 #else
111 static char NameID[4] = {'E', 'M', 'A', 'N'};
112 static char AuthID[4] = {'H', 'T', 'U', 'A'};
113 static char CopyID[4] = {' ', ')', 'c', '('};
114 static char AnnoID[4] = {'O', 'N', 'N', 'A'};
115 #endif /* WORDS_BIGENDIAN */
116 #endif /* !LIBAIFF && !LIBAIFF_NOCOMPAT */
117 
118 /*
119  * Flags.
120  * Flags which start with the F_ prefix are
121  * user-specified and are documented.
122  * Flags from (1<<20) to (1<<29) are reserved
123  * to module private flags.
124  */
125 #define F_RDONLY	(1<<0)
126 #define F_WRONLY	(1<<1)
127 #define LPCM_BIG_ENDIAN	(1<<2)
128 #define LPCM_LTE_ENDIAN	(1<<3)
129 #ifdef WORDS_BIGENDIAN
130 #define LPCM_SYS_ENDIAN	LPCM_BIG_ENDIAN
131 #define LPCM_NEED_SWAP	LPCM_LTE_ENDIAN
132 #else
133 #define LPCM_SYS_ENDIAN	LPCM_LTE_ENDIAN
134 #define LPCM_NEED_SWAP	LPCM_BIG_ENDIAN
135 #endif /* WORDS_BIGENDIAN */
136 #define F_AIFC		(1<<4)
137 #define F_OPTIMIZE	(F_AIFC | LPCM_SYS_ENDIAN)
138 #define F_NOTSEEKABLE	(1<<5)
139 
140 /* Play modes */
141 #define kModeNoLooping			0
142 #define kModeForwardLooping		1
143 #define kModeForwardBackwardLooping	2
144 
145 struct s_Loop
146 {
147 	int16_t playMode ;
148 	uint64_t beginLoop ;
149 	uint64_t endLoop ;
150 } ;
151 
152 struct s_Instrument
153 {
154 	int8_t baseNote ;
155 	int8_t detune ;
156 	int8_t lowNote ;
157 	int8_t highNote ;
158 	int8_t lowVelocity ;
159 	int8_t highVelocity ;
160 	int16_t gain ;
161 	struct s_Loop sustainLoop ;
162 	struct s_Loop releaseLoop ;
163 } ;
164 typedef struct s_Instrument Instrument ;
165 
166 /* == Function prototypes == */
167 AIFF_Ref AIFF_OpenFile(const char *, int) ;
168 int AIFF_CloseFile(AIFF_Ref) ;
169 char* AIFF_GetAttribute(AIFF_Ref,IFFType) ;
170 int AIFF_GetInstrumentData(AIFF_Ref,Instrument*) ;
171 size_t AIFF_ReadSamples(AIFF_Ref,void*,size_t) ;
172 int AIFF_ReadSamplesFloat(AIFF_Ref r, float *buffer, int n) ;
173 int AIFF_Seek(AIFF_Ref,uint64_t) ;
174 int AIFF_ReadSamples32Bit(AIFF_Ref,int32_t*,int) ;
175 int AIFF_ReadMarker(AIFF_Ref,int*,uint64_t*,char**) ;
176 int AIFF_GetAudioFormat(AIFF_Ref,uint64_t*,int*,double*,int*,int*) ;
177 int AIFF_SetAttribute(AIFF_Ref,IFFType,char*) ;
178 int AIFF_CloneAttributes(AIFF_Ref w, AIFF_Ref r, int cloneMarkers) ;
179 int AIFF_SetAudioFormat(AIFF_Ref,int,double,int ) ;
180 int AIFF_StartWritingSamples(AIFF_Ref) ;
181 int AIFF_WriteSamples(AIFF_Ref,void*,size_t) ;
182 int AIFF_WriteSamples32Bit(AIFF_Ref,int32_t*,int) ;
183 int AIFF_EndWritingSamples(AIFF_Ref) ;
184 int AIFF_StartWritingMarkers(AIFF_Ref) ;
185 int AIFF_WriteMarker(AIFF_Ref,uint64_t,char*) ;
186 int AIFF_EndWritingMarkers(AIFF_Ref) ;
187 
188 #if !defined(LIBAIFF) && !defined(LIBAIFF_NOCOMPAT)
189 #define AIFF_ReadRef		AIFF_Ref
190 #define AIFF_WriteRef		AIFF_Ref
191 #define AIFF_Open(f)		AIFF_OpenFile((f), F_RDONLY)
192 #define AIFF_WriteOpen(f)	AIFF_OpenFile((f), F_WRONLY)
193 #define AIFF_Close(f)		(void) AIFF_CloseFile(f)
194 #define AIFF_WriteClose(f)	AIFF_CloseFile(f)
195 #define AIFF_SetSoundFormat(r,a,s,b) AIFF_SetAudioFormat((r),(a),(double)(s),(b))
196 #endif /* !LIBAIFF && !LIBAIFF_NOCOMPAT */
197 
198 #ifndef LIBAIFF
199 
200 #ifdef HAVE_BZERO
201 #undef HAVE_BZERO
202 #endif
203 #ifdef HAVE_ABORT
204 #undef HAVE_ABORT
205 #endif
206 #ifdef HAVE_INTTYPES_H
207 #undef HAVE_INTTYPES_H
208 #endif
209 #ifdef HAVE_MEMORY_H
210 #undef HAVE_MEMORY_H
211 #endif
212 #ifdef HAVE_MEMSET
213 #undef HAVE_MEMSET
214 #endif
215 #ifdef HAVE_STDINT_H
216 #undef HAVE_STDINT_H
217 #endif
218 #ifdef HAVE_STDLIB_H
219 #undef HAVE_STDLIB_H
220 #endif
221 #ifdef HAVE_STRINGS_H
222 #undef HAVE_STRINGS_H
223 #endif
224 #ifdef HAVE_STRING_H
225 #undef HAVE_STRING_H
226 #endif
227 #ifdef HAVE_SYS_STAT_H
228 #undef HAVE_SYS_STAT_H
229 #endif
230 #ifdef HAVE_SYS_TYPES_H
231 #undef HAVE_SYS_TYPES_H
232 #endif
233 #ifdef HAVE_UNISTD_H
234 #undef HAVE_UNISTD_H
235 #endif
236 
237 #undef PACKAGE_BUGREPORT
238 #undef PACKAGE_NAME
239 #undef PACKAGE_STRING
240 #undef PACKAGE_TARNAME
241 #undef PACKAGE_VERSION
242 
243 #ifdef STDC_HEADERS
244 #undef STDC_HEADERS
245 #endif
246 #ifdef WORDS_BIGENDIAN
247 #undef WORDS_BIGENDIAN
248 #endif
249 
250 #endif /* !LIBAIFF */
251 
252 
253 #endif /* LIBAIFF_H_INCL */
254 
255 
256