1 /*
2  * libInstPatch
3  * Copyright (C) 1999-2014 Element Green <element@elementsofsound.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; version 2.1
8  * of the License only.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA or on the web at http://www.gnu.org.
19  */
20 #ifndef __SAMPLE_H__
21 #define __SAMPLE_H__
22 
23 #include <glib.h>
24 
25 /**
26  * IPATCH_SAMPLE_MAX_TRANSFORM_FUNCS: (skip)
27  *
28  * Maximum number of transform functions returned by
29  * ipatch_sample_get_transform_funcs().  Is larger than current actual maximum
30  * to allow for future backwards compatible expansion (8 is the real current
31  * maximum).
32  */
33 #define IPATCH_SAMPLE_MAX_TRANSFORM_FUNCS	16
34 
35 #include <libinstpatch/IpatchSampleTransform.h>
36 
37 /**
38  * IPATCH_SAMPLE_FORMAT_MASK:
39  *
40  * Mask for all fields of sample format integers (width, sign, endian, channel).
41  */
42 #define IPATCH_SAMPLE_FORMAT_MASK   0x1FF
43 
44 /**
45  * IPATCH_SAMPLE_FORMAT_BITCOUNT:
46  *
47  * Number of bits used for sample format integers.
48  */
49 #define IPATCH_SAMPLE_FORMAT_BITCOUNT  9
50 
51 
52 #define IPATCH_SAMPLE_WIDTH_MASK    0x00F  /* total of 16 formats (8 reserved) */
53 #define IPATCH_SAMPLE_CHANNEL_MASK  0x070  /* channel count (8 channels max) */
54 #define IPATCH_SAMPLE_SIGN_MASK     0x080  /* sign or unsigned (for PCM formats) */
55 #define IPATCH_SAMPLE_ENDIAN_MASK   0x100  /* endian byte order */
56 
57 #define IPATCH_SAMPLE_WIDTH_SHIFT   0
58 #define IPATCH_SAMPLE_CHANNEL_SHIFT 4
59 #define IPATCH_SAMPLE_SIGN_SHIFT    7
60 #define IPATCH_SAMPLE_ENDIAN_SHIFT  8
61 
62 /**
63  * IpatchSampleWidth:
64  * @IPATCH_SAMPLE_INVALID: Invalid format (so 0 can be used to indicate a NULL state)
65  * @IPATCH_SAMPLE_8BIT: 8 bit integer PCM
66  * @IPATCH_SAMPLE_16BIT: 16 bit integer PCM
67  * @IPATCH_SAMPLE_24BIT: 24 bit integer PCM (32 bit ints)
68  * @IPATCH_SAMPLE_32BIT: 32 bit integer PCM
69  * @IPATCH_SAMPLE_FLOAT: 32 bit IEEE float (-1.0 - 1.0)
70  * @IPATCH_SAMPLE_DOUBLE: 64 bit IEEE double (-1.0 - 1.0)
71  * @IPATCH_SAMPLE_REAL24BIT: Real 3 byte 24 bit data (not padded to 32 bits)
72  *
73  * Sample data widths/formats.
74  */
75 typedef enum
76 {
77     IPATCH_SAMPLE_INVALID = 0,
78     IPATCH_SAMPLE_BIT8    = 1,
79     IPATCH_SAMPLE_BIT16   = 2,
80     IPATCH_SAMPLE_BIT24   = 3,
81     IPATCH_SAMPLE_BIT32   = 4,
82     IPATCH_SAMPLE_FLOAT   = 5,
83     IPATCH_SAMPLE_DOUBLE  = 6,
84     IPATCH_SAMPLE_REAL24BIT = 7
85 } IpatchSampleWidth;
86 
87 /* Renamed to be GObject Introspection friendly (symbols can't start with numbers).
88  * Backwards compatible defines below. */
89 
90 /**
91  * IPATCH_SAMPLE_8BIT: (skip)
92  */
93 #define IPATCH_SAMPLE_8BIT      IPATCH_SAMPLE_BIT8
94 
95 /**
96  * IPATCH_SAMPLE_16BIT: (skip)
97  */
98 #define IPATCH_SAMPLE_16BIT     IPATCH_SAMPLE_BIT16
99 
100 /**
101  * IPATCH_SAMPLE_24BIT: (skip)
102  */
103 #define IPATCH_SAMPLE_24BIT     IPATCH_SAMPLE_BIT24
104 
105 /**
106  * IPATCH_SAMPLE_32BIT: (skip)
107  */
108 #define IPATCH_SAMPLE_32BIT     IPATCH_SAMPLE_BIT32
109 
110 /**
111  * IpatchSampleChannel:
112  * @IPATCH_SAMPLE_MONO: Mono audio
113  * @IPATCH_SAMPLE_STEREO: Stereo audio
114  *
115  * Descriptive enums for common audio channel configurations.  These values
116  * are actually channel count - 1 (0 = mono, 1 = stereo, etc) and can be compared
117  * with the macro IPATCH_SAMPLE_FORMAT_GET_CHANNELS().
118  */
119 typedef enum
120 {
121     IPATCH_SAMPLE_MONO    = 0 << IPATCH_SAMPLE_CHANNEL_SHIFT,
122     IPATCH_SAMPLE_STEREO  = 1 << IPATCH_SAMPLE_CHANNEL_SHIFT
123 } IpatchSampleChannel;
124 
125 /**
126  * IpatchSampleChannelType:
127  * @IPATCH_SAMPLE_LEFT: Left channel comes first
128  * @IPATCH_SAMPLE_RIGHT: Right channel comes second
129  *
130  * Channel designation.  Currently there are only 2 designated channels,
131  * though the remaining 6 supported channels may be defined to be surround
132  * sound channels in the future.
133  */
134 typedef enum
135 {
136     IPATCH_SAMPLE_LEFT =  0,
137     IPATCH_SAMPLE_RIGHT = 1
138 } IpatchSampleChannelType;
139 
140 /**
141  * IPATCH_SAMPLE_MAX_CHANNELS:
142  *
143  * Maximum number of audio channels handled by libInstPatch.
144  */
145 #define IPATCH_SAMPLE_MAX_CHANNELS      8
146 
147 /**
148  * IpatchSampleSign:
149  * @IPATCH_SAMPLE_SIGNED: Signed PCM audio data.
150  * @IPATCH_SAMPLE_UNSIGNED: Unsigned PCM audio data.
151  *
152  * Defines the sign of PCM integer audio data.
153  */
154 typedef enum
155 {
156     IPATCH_SAMPLE_SIGNED   = 0 << IPATCH_SAMPLE_SIGN_SHIFT,
157     IPATCH_SAMPLE_UNSIGNED = 1 << IPATCH_SAMPLE_SIGN_SHIFT
158 } IpatchSampleSign;
159 
160 /**
161  * IpatchSampleEndian:
162  * @IPATCH_SAMPLE_LENDIAN: Little endian byte order
163  * @IPATCH_SAMPLE_BENDIAN: Big endian byte order
164  *
165  * Defines the byte order of multi-byte audio data.
166  */
167 typedef enum
168 {
169     IPATCH_SAMPLE_LENDIAN = 0 << IPATCH_SAMPLE_ENDIAN_SHIFT,
170     IPATCH_SAMPLE_BENDIAN = 1 << IPATCH_SAMPLE_ENDIAN_SHIFT
171 } IpatchSampleEndian;
172 
173 /**
174  * IPATCH_SAMPLE_ENDIAN_HOST:
175  *
176  * Host byte order value (#IPATCH_SAMPLE_LENDIAN or #IPATCH_SAMPLE_BENDIAN).
177  */
178 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
179 #define IPATCH_SAMPLE_ENDIAN_HOST IPATCH_SAMPLE_LENDIAN
180 #else
181 #define IPATCH_SAMPLE_ENDIAN_HOST IPATCH_SAMPLE_BENDIAN
182 #endif
183 
184 /**
185  * IPATCH_SAMPLE_FORMAT_GET_WIDTH:
186  * @format: Sample format integer
187  *
188  * Get #IpatchSampleWidth enum from a sample format integer.
189  *
190  * Returns: Format field of sample format integer.
191  */
192 #define IPATCH_SAMPLE_FORMAT_GET_WIDTH(format) \
193   ((format) & IPATCH_SAMPLE_WIDTH_MASK)
194 
195 /**
196  * IPATCH_SAMPLE_FORMAT_IS_FLOATING:
197  * @format: Sample format integer
198  *
199  * Check if a sample format integer defines floating point audio.
200  *
201  * Returns: %TRUE if sample format is #IPATCH_SAMPLE_FLOAT or #IPATCH_SAMPLE_DOUBLE.
202  */
203 #define IPATCH_SAMPLE_FORMAT_IS_FLOATING(format) \
204   (((format) & IPATCH_SAMPLE_WIDTH_MASK) == IPATCH_SAMPLE_FLOAT \
205    || ((format) & IPATCH_SAMPLE_WIDTH_MASK) == IPATCH_SAMPLE_DOUBLE)
206 
207 /**
208  * IPATCH_SAMPLE_FORMAT_GET_CHANNELS:
209  * @format: Sample format integer
210  *
211  * Get the channel field from a sample format integer.
212  *
213  * Returns: Channel field value (see #IpatchSampleChannel)
214  */
215 #define IPATCH_SAMPLE_FORMAT_GET_CHANNELS(format) \
216   ((format) & IPATCH_SAMPLE_CHANNEL_MASK)
217 
218 /**
219  * IPATCH_SAMPLE_FORMAT_GET_CHANNEL_COUNT:
220  * @format: Sample format integer
221  *
222  * Get the channel count from a sample format integer.
223  *
224  * Returns: Channel count (starting at 1 for mono).
225  */
226 #define IPATCH_SAMPLE_FORMAT_GET_CHANNEL_COUNT(format) \
227   ((((format) & IPATCH_SAMPLE_CHANNEL_MASK) >> IPATCH_SAMPLE_CHANNEL_SHIFT) + 1)
228 
229 /**
230  * IPATCH_SAMPLE_FORMAT_IS_SIGNED:
231  * @format: Sample format integer
232  *
233  * Check if a sample format integer defines signed audio.
234  *
235  * Returns: %TRUE if sample format is signed.
236  */
237 #define IPATCH_SAMPLE_FORMAT_IS_SIGNED(format) \
238   (((format) & IPATCH_SAMPLE_UNSIGNED) == 0)
239 
240 /**
241  * IPATCH_SAMPLE_FORMAT_IS_UNSIGNED:
242  * @format: Sample format integer
243  *
244  * Check if a sample format integer defines unsigned audio.
245  *
246  * Returns: %TRUE if sample format is unsigned.
247  */
248 #define IPATCH_SAMPLE_FORMAT_IS_UNSIGNED(format) \
249   (((format) & IPATCH_SAMPLE_UNSIGNED) != 0)
250 
251 /**
252  * IPATCH_SAMPLE_FORMAT_IS_LENDIAN:
253  * @format: Sample format integer
254  *
255  * Check if a sample format integer defines little endian audio.
256  *
257  * Returns: %TRUE if sample format is little endian.
258  */
259 #define IPATCH_SAMPLE_FORMAT_IS_LENDIAN(format) \
260   (((format) & IPATCH_SAMPLE_BENDIAN) == 0)
261 
262 /**
263  * IPATCH_SAMPLE_FORMAT_IS_BENDIAN:
264  * @format: Sample format integer
265  *
266  * Check if a sample format integer defines big endian audio.
267  *
268  * Returns: %TRUE if sample format is big endian.
269  */
270 #define IPATCH_SAMPLE_FORMAT_IS_BENDIAN(format) \
271   (((format) & IPATCH_SAMPLE_BENDIAN) != 0)
272 
273 /**
274  * ipatch_sample_format_size:
275  * @format: Sample format integer
276  *
277  * Get frame byte size for a given sample format (sample byte size * channels).
278  *
279  * Returns: Size in bytes of a single sample frame.
280  */
281 #define ipatch_sample_format_size(format) \
282   (ipatch_sample_width_sizes[(format) & IPATCH_SAMPLE_WIDTH_MASK] \
283    * IPATCH_SAMPLE_FORMAT_GET_CHANNEL_COUNT (format))
284 
285 /**
286  * ipatch_sample_format_width:
287  * @format: Sample format
288  *
289  * Gets the number of bytes used for storing a single sample for @format.
290  * Doesn't take into account channels.  This is the number of bytes used
291  * to store the samples, not the effective bit width.  For example:
292  * #IPATCH_SAMPLE_24BIT uses 4 bytes for each sample.
293  *
294  * Returns: Byte width of a single sample (not including channels).
295  */
296 #define ipatch_sample_format_width(format) \
297   (ipatch_sample_width_sizes[(format) & IPATCH_SAMPLE_WIDTH_MASK])
298 
299 
300 /**
301  * IPATCH_SAMPLE_MAP_CHANNEL:
302  * @dest: Destination channel in mapping bit field (0-7)
303  * @src: Source channel (0-7)
304  *
305  * Macro to calculate a channel mapping value for a given destination and
306  * source.  A channel mapping is composed of up to 24 bits
307  * (3 bits * 8 channels = 24).  Channel mappings are used for sample
308  * conversions to route channels from a source format to a destination format.
309  * Multiple channel map values should be OR'd together.
310  */
311 #define IPATCH_SAMPLE_MAP_CHANNEL(dest, src)   ((src) << (3 * (dest)))
312 
313 /**
314  * IPATCH_SAMPLE_MAP_GET_CHANNEL:
315  * @map: Channel map value (#guint32 - only 24 bits are used)
316  * @dest: Destination channel in @map bit field (0-7)
317  *
318  * Macro to get a source channel value given a destination channel.
319  *
320  * Returns: Source channel for @dest (0-7)
321  */
322 #define IPATCH_SAMPLE_MAP_GET_CHANNEL(map, dest)   (((map) >> ((dest) * 3)) & 0x07)
323 
324 /**
325  * IPATCH_SAMPLE_UNITY_CHANNEL_MAP:
326  *
327  * Unity channel mapping which routes each input channel to the same output
328  * channel.
329  */
330 #define IPATCH_SAMPLE_UNITY_CHANNEL_MAP   0xFAC688
331 
332 
333 extern guint ipatch_sample_width_sizes[16];
334 
335 int ipatch_sample_format_bit_width(int format);
336 gboolean ipatch_sample_format_verify(int format);
337 gboolean ipatch_sample_format_transform_verify(int src_format, int dest_format,
338         guint32 channel_map);
339 guint ipatch_sample_get_transform_funcs(int src_format, int dest_format,
340                                         guint32 channel_map,
341                                         guint *buf1_max_frame,
342                                         guint *buf2_max_frame,
343                                         IpatchSampleTransformFunc *funcs);
344 #endif
345