2 Copyright (C) 2006 Paul Davis , portions Erik de Castro Lopo
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <ardour/pcm_utils.h>
27 // TODO: check CPU_CLIPS_POSITIVE and CPU_CLIPS_NEGATIVE with scons
28 #define CPU_CLIPS_NEGATIVE 0
29 #define CPU_CLIPS_POSITIVE 0
31 /* these routines deal with 24 bit int handling (tribytes)
32 * originally from libsndfile, but modified. XXX - Copyright Erik de Castro Lopo
36 pcm_let2f_array (tribyte *src, int count, float *dest)
38 /* Special normfactor because tribyte value is read into an int. */
39 static const float normfact = 1.0 / ((float) 0x80000000);
41 unsigned char *ucptr ;
44 ucptr = ((unsigned char*) src) + 3 * count ;
47 value = LET2H_INT_PTR (ucptr) ;
48 dest [count] = ((float) value) * normfact ;
53 pcm_bet2f_array (tribyte *src, int count, float *dest)
55 /* Special normfactor because tribyte value is read into an int. */
56 static const float normfact = 1.0 / ((float) 0x80000000);
58 unsigned char *ucptr ;
62 ucptr = ((unsigned char*) src) + 3 * count ;
65 value = BET2H_INT_PTR (ucptr) ;
66 dest [count] = ((float) value) * normfact ;
71 pcm_f2let_array (float *src, tribyte *dest, int count)
73 static const float normfact = (1.0 * 0x7FFFFF);
75 unsigned char *ucptr ;
78 ucptr = ((unsigned char*) dest) + 3 * count ;
83 value = lrintf (src [count] * normfact) ;
85 ucptr [1] = value >> 8 ;
86 ucptr [2] = value >> 16 ;
91 pcm_f2let_clip_array (float *src, tribyte *dest, int count)
93 static const float normfact = (8.0 * 0x10000000);
95 unsigned char *ucptr ;
99 ucptr = ((unsigned char*) dest) + 3 * count ;
104 scaled_value = src [count] * normfact ;
105 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
111 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
118 value = lrintf (scaled_value) ;
119 ucptr [0] = value >> 8 ;
120 ucptr [1] = value >> 16 ;
121 ucptr [2] = value >> 24 ;
123 } /* f2let_clip_array */
126 pcm_f2bet_array (const float *src, tribyte *dest, int count)
128 static const float normfact = (1.0 * 0x7FFFFF);
130 unsigned char *ucptr ;
133 ucptr = ((unsigned char*) dest) + 3 * count ;
137 value = lrintf (src [count] * normfact) ;
138 ucptr [0] = value >> 16 ;
139 ucptr [1] = value >> 8 ;
145 pcm_f2bet_clip_array (const float *src, tribyte *dest, int count)
147 static const float normfact = (8.0 * 0x10000000);
149 unsigned char *ucptr ;
153 ucptr = ((unsigned char*) dest) + 3 * count ;
157 scaled_value = src [count] * normfact ;
158 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
164 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
171 value = lrint (scaled_value) ;
172 ucptr [0] = value >> 24 ;
173 ucptr [1] = value >> 16 ;
174 ucptr [2] = value >> 8 ;
176 } /* f2bet_clip_array */