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 <ardourext/float_cast.h>
23 #include "ardour/pcm_utils.h"
28 // TODO: check CPU_CLIPS_POSITIVE and CPU_CLIPS_NEGATIVE with scons
29 #define CPU_CLIPS_NEGATIVE 0
30 #define CPU_CLIPS_POSITIVE 0
32 /* these routines deal with 24 bit int handling (tribytes)
33 * originally from libsndfile, but modified. XXX - Copyright Erik de Castro Lopo
37 pcm_let2f_array (tribyte *src, int count, float *dest)
39 /* Special normfactor because tribyte value is read into an int. */
40 static const float normfact = 1.0 / ((float) 0x80000000);
42 unsigned char *ucptr ;
45 ucptr = ((unsigned char*) src) + 3 * count ;
48 value = LET2H_INT_PTR (ucptr) ;
49 dest [count] = ((float) value) * normfact ;
54 pcm_bet2f_array (tribyte *src, int count, float *dest)
56 /* Special normfactor because tribyte value is read into an int. */
57 static const float normfact = 1.0 / ((float) 0x80000000);
59 unsigned char *ucptr ;
63 ucptr = ((unsigned char*) src) + 3 * count ;
66 value = BET2H_INT_PTR (ucptr) ;
67 dest [count] = ((float) value) * normfact ;
72 pcm_f2let_array (float *src, tribyte *dest, int count)
74 static const float normfact = (1.0 * 0x7FFFFF);
76 unsigned char *ucptr ;
79 ucptr = ((unsigned char*) dest) + 3 * count ;
84 value = lrintf (src [count] * normfact) ;
86 ucptr [1] = value >> 8 ;
87 ucptr [2] = value >> 16 ;
92 pcm_f2let_clip_array (float *src, tribyte *dest, int count)
94 static const float normfact = (8.0 * 0x10000000);
96 unsigned char *ucptr ;
100 ucptr = ((unsigned char*) dest) + 3 * count ;
105 scaled_value = src [count] * normfact ;
106 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
112 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
119 value = lrintf (scaled_value) ;
120 ucptr [0] = value >> 8 ;
121 ucptr [1] = value >> 16 ;
122 ucptr [2] = value >> 24 ;
124 } /* f2let_clip_array */
127 pcm_f2bet_array (const float *src, tribyte *dest, int count)
129 static const float normfact = (1.0 * 0x7FFFFF);
131 unsigned char *ucptr ;
134 ucptr = ((unsigned char*) dest) + 3 * count ;
138 value = lrintf (src [count] * normfact) ;
139 ucptr [0] = value >> 16 ;
140 ucptr [1] = value >> 8 ;
146 pcm_f2bet_clip_array (const float *src, tribyte *dest, int count)
148 static const float normfact = (8.0 * 0x10000000);
150 unsigned char *ucptr ;
154 ucptr = ((unsigned char*) dest) + 3 * count ;
158 scaled_value = src [count] * normfact ;
159 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
165 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
172 value = lrint (scaled_value) ;
173 ucptr [0] = value >> 24 ;
174 ucptr [1] = value >> 16 ;
175 ucptr [2] = value >> 8 ;
177 } /* f2bet_clip_array */