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.
20 #include "ardour/pcm_utils.h"
26 // TODO: check CPU_CLIPS_POSITIVE and CPU_CLIPS_NEGATIVE with scons
27 #define CPU_CLIPS_NEGATIVE 0
28 #define CPU_CLIPS_POSITIVE 0
30 /* these routines deal with 24 bit int handling (tribytes)
31 * originally from libsndfile, but modified. XXX - Copyright Erik de Castro Lopo
35 pcm_let2f_array (tribyte *src, int count, float *dest)
37 /* Special normfactor because tribyte value is read into an int. */
38 static const float normfact = 1.0 / ((float) 0x80000000);
40 unsigned char *ucptr ;
43 ucptr = ((unsigned char*) src) + 3 * count ;
46 value = LET2H_INT_PTR (ucptr) ;
47 dest [count] = ((float) value) * normfact ;
52 pcm_bet2f_array (tribyte *src, int count, float *dest)
54 /* Special normfactor because tribyte value is read into an int. */
55 static const float normfact = 1.0 / ((float) 0x80000000);
57 unsigned char *ucptr ;
61 ucptr = ((unsigned char*) src) + 3 * count ;
64 value = BET2H_INT_PTR (ucptr) ;
65 dest [count] = ((float) value) * normfact ;
70 pcm_f2let_array (float *src, tribyte *dest, int count)
72 static const float normfact = (1.0 * 0x7FFFFF);
74 unsigned char *ucptr ;
77 ucptr = ((unsigned char*) dest) + 3 * count ;
82 value = lrintf (src [count] * normfact) ;
84 ucptr [1] = value >> 8 ;
85 ucptr [2] = value >> 16 ;
90 pcm_f2let_clip_array (float *src, tribyte *dest, int count)
92 static const float normfact = (8.0 * 0x10000000);
94 unsigned char *ucptr ;
98 ucptr = ((unsigned char*) dest) + 3 * count ;
103 scaled_value = src [count] * normfact ;
104 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
110 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
117 value = lrintf (scaled_value) ;
118 ucptr [0] = value >> 8 ;
119 ucptr [1] = value >> 16 ;
120 ucptr [2] = value >> 24 ;
122 } /* f2let_clip_array */
125 pcm_f2bet_array (const float *src, tribyte *dest, int count)
127 static const float normfact = (1.0 * 0x7FFFFF);
129 unsigned char *ucptr ;
132 ucptr = ((unsigned char*) dest) + 3 * count ;
136 value = lrintf (src [count] * normfact) ;
137 ucptr [0] = value >> 16 ;
138 ucptr [1] = value >> 8 ;
144 pcm_f2bet_clip_array (const float *src, tribyte *dest, int count)
146 static const float normfact = (8.0 * 0x10000000);
148 unsigned char *ucptr ;
152 ucptr = ((unsigned char*) dest) + 3 * count ;
156 scaled_value = src [count] * normfact ;
157 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
163 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
170 value = lrint (scaled_value) ;
171 ucptr [0] = value >> 24 ;
172 ucptr [1] = value >> 16 ;
173 ucptr [2] = value >> 8 ;
175 } /* f2bet_clip_array */