1 #ifndef PA_CONVERTERS_H
2 #define PA_CONVERTERS_H
3 /*
4  * $Id$
5  * Portable Audio I/O Library sample conversion mechanism
6  *
7  * Based on the Open Source API proposed by Ross Bencina
8  * Copyright (c) 1999-2002 Phil Burk, Ross Bencina
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files
12  * (the "Software"), to deal in the Software without restriction,
13  * including without limitation the rights to use, copy, modify, merge,
14  * publish, distribute, sublicense, and/or sell copies of the Software,
15  * and to permit persons to whom the Software is furnished to do so,
16  * subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
25  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 /*
31  * The text above constitutes the entire PortAudio license; however,
32  * the PortAudio community also makes the following non-binding requests:
33  *
34  * Any person wishing to distribute modifications to the Software is
35  * requested to send the modifications to the original developer so that
36  * they can be incorporated into the canonical version. It is also
37  * requested that these non-binding requests be included along with the
38  * license above.
39  */
40 
41 /** @file
42  @ingroup common_src
43 
44  @brief Conversion functions used to convert buffers of samples from one
45  format to another.
46 */
47 
48 
49 #include "portaudio.h"  /* for PaSampleFormat */
50 
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #endif /* __cplusplus */
55 
56 
57 struct PaUtilTriangularDitherGenerator;
58 
59 
60 /** Choose an available sample format which is most appropriate for
61  representing the requested format. If the requested format is not available
62  higher quality formats are considered before lower quality formates.
63  @param availableFormats A variable containing the logical OR of all available
64  formats.
65  @param format The desired format.
66  @return The most appropriate available format for representing the requested
67  format.
68 */
69 PaSampleFormat PaUtil_SelectClosestAvailableFormat(
70         PaSampleFormat availableFormats, PaSampleFormat format );
71 
72 
73 /* high level conversions functions for use by implementations */
74 
75 
76 /** The generic sample converter prototype. Sample converters convert count
77     samples from sourceBuffer to destinationBuffer. The actual type of the data
78     pointed to by these parameters varys for different converter functions.
79     @param destinationBuffer A pointer to the first sample of the destination.
80     @param destinationStride An offset between successive destination samples
81     expressed in samples (not bytes.) It may be negative.
82     @param sourceBuffer A pointer to the first sample of the source.
83     @param sourceStride An offset between successive source samples
84     expressed in samples (not bytes.) It may be negative.
85     @param count The number of samples to convert.
86     @param ditherState State information used to calculate dither. Converters
87     that do not perform dithering will ignore this parameter, in which case
88     NULL or invalid dither state may be passed.
89 */
90 typedef void PaUtilConverter(
91     void *destinationBuffer, signed int destinationStride,
92     void *sourceBuffer, signed int sourceStride,
93     unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator );
94 
95 
96 /** Find a sample converter function for the given source and destinations
97     formats and flags (clip and dither.)
98     @return
99     A pointer to a PaUtilConverter which will perform the requested
100     conversion, or NULL if the given format conversion is not supported.
101     For conversions where clipping or dithering is not necessary, the
102     clip and dither flags are ignored and a non-clipping or dithering
103     version is returned.
104     If the source and destination formats are the same, a function which
105     copies data of the appropriate size will be returned.
106 */
107 PaUtilConverter* PaUtil_SelectConverter( PaSampleFormat sourceFormat,
108         PaSampleFormat destinationFormat, PaStreamFlags flags );
109 
110 
111 /** The generic buffer zeroer prototype. Buffer zeroers copy count zeros to
112     destinationBuffer. The actual type of the data pointed to varys for
113     different zeroer functions.
114     @param destinationBuffer A pointer to the first sample of the destination.
115     @param destinationStride An offset between successive destination samples
116     expressed in samples (not bytes.) It may be negative.
117     @param count The number of samples to zero.
118 */
119 typedef void PaUtilZeroer(
120     void *destinationBuffer, signed int destinationStride, unsigned int count );
121 
122 
123 /** Find a buffer zeroer function for the given destination format.
124     @return
125     A pointer to a PaUtilZeroer which will perform the requested
126     zeroing.
127 */
128 PaUtilZeroer* PaUtil_SelectZeroer( PaSampleFormat destinationFormat );
129 
130 /*----------------------------------------------------------------------------*/
131 /* low level functions and data structures which may be used for
132     substituting conversion functions */
133 
134 
135 /** The type used to store all sample conversion functions.
136     @see paConverters;
137 */
138 typedef struct{
139     PaUtilConverter *Float32_To_Int32;
140     PaUtilConverter *Float32_To_Int32_Dither;
141     PaUtilConverter *Float32_To_Int32_Clip;
142     PaUtilConverter *Float32_To_Int32_DitherClip;
143 
144     PaUtilConverter *Float32_To_Int24;
145     PaUtilConverter *Float32_To_Int24_Dither;
146     PaUtilConverter *Float32_To_Int24_Clip;
147     PaUtilConverter *Float32_To_Int24_DitherClip;
148 
149     PaUtilConverter *Float32_To_Int16;
150     PaUtilConverter *Float32_To_Int16_Dither;
151     PaUtilConverter *Float32_To_Int16_Clip;
152     PaUtilConverter *Float32_To_Int16_DitherClip;
153 
154     PaUtilConverter *Float32_To_Int8;
155     PaUtilConverter *Float32_To_Int8_Dither;
156     PaUtilConverter *Float32_To_Int8_Clip;
157     PaUtilConverter *Float32_To_Int8_DitherClip;
158 
159     PaUtilConverter *Float32_To_UInt8;
160     PaUtilConverter *Float32_To_UInt8_Dither;
161     PaUtilConverter *Float32_To_UInt8_Clip;
162     PaUtilConverter *Float32_To_UInt8_DitherClip;
163 
164     PaUtilConverter *Int32_To_Float32;
165     PaUtilConverter *Int32_To_Int24;
166     PaUtilConverter *Int32_To_Int24_Dither;
167     PaUtilConverter *Int32_To_Int16;
168     PaUtilConverter *Int32_To_Int16_Dither;
169     PaUtilConverter *Int32_To_Int8;
170     PaUtilConverter *Int32_To_Int8_Dither;
171     PaUtilConverter *Int32_To_UInt8;
172     PaUtilConverter *Int32_To_UInt8_Dither;
173 
174     PaUtilConverter *Int24_To_Float32;
175     PaUtilConverter *Int24_To_Int32;
176     PaUtilConverter *Int24_To_Int16;
177     PaUtilConverter *Int24_To_Int16_Dither;
178     PaUtilConverter *Int24_To_Int8;
179     PaUtilConverter *Int24_To_Int8_Dither;
180     PaUtilConverter *Int24_To_UInt8;
181     PaUtilConverter *Int24_To_UInt8_Dither;
182 
183     PaUtilConverter *Int16_To_Float32;
184     PaUtilConverter *Int16_To_Int32;
185     PaUtilConverter *Int16_To_Int24;
186     PaUtilConverter *Int16_To_Int8;
187     PaUtilConverter *Int16_To_Int8_Dither;
188     PaUtilConverter *Int16_To_UInt8;
189     PaUtilConverter *Int16_To_UInt8_Dither;
190 
191     PaUtilConverter *Int8_To_Float32;
192     PaUtilConverter *Int8_To_Int32;
193     PaUtilConverter *Int8_To_Int24;
194     PaUtilConverter *Int8_To_Int16;
195     PaUtilConverter *Int8_To_UInt8;
196 
197     PaUtilConverter *UInt8_To_Float32;
198     PaUtilConverter *UInt8_To_Int32;
199     PaUtilConverter *UInt8_To_Int24;
200     PaUtilConverter *UInt8_To_Int16;
201     PaUtilConverter *UInt8_To_Int8;
202 
203     PaUtilConverter *Copy_8_To_8;       /* copy without any conversion */
204     PaUtilConverter *Copy_16_To_16;     /* copy without any conversion */
205     PaUtilConverter *Copy_24_To_24;     /* copy without any conversion */
206     PaUtilConverter *Copy_32_To_32;     /* copy without any conversion */
207 } PaUtilConverterTable;
208 
209 
210 /** A table of pointers to all required converter functions.
211     PaUtil_SelectConverter() uses this table to lookup the appropriate
212     conversion functions. The fields of this structure are initialized
213     with default conversion functions. Fields may be NULL, indicating that
214     no conversion function is available. User code may substitue optimised
215     conversion functions by assigning different function pointers to
216     these fields.
217 
218     @note
219     If the PA_NO_STANDARD_CONVERTERS preprocessor variable is defined,
220     PortAudio's standard converters will not be compiled, and all fields
221     of this structure will be initialized to NULL. In such cases, users
222     should supply their own conversion functions if the require PortAudio
223     to open a stream that requires sample conversion.
224 
225     @see PaUtilConverterTable, PaUtilConverter, PaUtil_SelectConverter
226 */
227 extern PaUtilConverterTable paConverters;
228 
229 
230 /** The type used to store all buffer zeroing functions.
231     @see paZeroers;
232 */
233 typedef struct{
234     PaUtilZeroer *ZeroU8; /* unsigned 8 bit, zero == 128 */
235     PaUtilZeroer *Zero8;
236     PaUtilZeroer *Zero16;
237     PaUtilZeroer *Zero24;
238     PaUtilZeroer *Zero32;
239 } PaUtilZeroerTable;
240 
241 
242 /** A table of pointers to all required zeroer functions.
243     PaUtil_SelectZeroer() uses this table to lookup the appropriate
244     conversion functions. The fields of this structure are initialized
245     with default conversion functions. User code may substitue optimised
246     conversion functions by assigning different function pointers to
247     these fields.
248 
249     @note
250     If the PA_NO_STANDARD_ZEROERS preprocessor variable is defined,
251     PortAudio's standard zeroers will not be compiled, and all fields
252     of this structure will be initialized to NULL. In such cases, users
253     should supply their own zeroing functions for the sample sizes which
254     they intend to use.
255 
256     @see PaUtilZeroerTable, PaUtilZeroer, PaUtil_SelectZeroer
257 */
258 extern PaUtilZeroerTable paZeroers;
259 
260 #ifdef __cplusplus
261 }
262 #endif /* __cplusplus */
263 #endif /* PA_CONVERTERS_H */
264