Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / mix.cc
1 /*
2     Copyright (C) 2000-2005 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cmath>
21 #include <ardour/types.h>
22 #include <ardour/utils.h>
23 #include <ardour/mix.h>
24 #include <stdint.h>
25
26 using namespace ARDOUR;
27
28 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
29 // Debug wrappers
30
31 float
32 debug_compute_peak (ARDOUR::Sample *buf, nframes_t nsamples, float current) 
33 {
34         if ( ((intptr_t)buf % 16) != 0) {
35                 cerr << "compute_peak(): buffer unaligned!" << endl;
36         }
37
38         return x86_sse_compute_peak(buf, nsamples, current);
39 }
40
41 void
42 debug_apply_gain_to_buffer (ARDOUR::Sample *buf, nframes_t nframes, float gain)
43 {
44         if ( ((intptr_t)buf % 16) != 0) {
45                 cerr << "apply_gain_to_buffer(): buffer unaligned!" << endl;
46         }
47
48         x86_sse_apply_gain_to_buffer(buf, nframes, gain);
49 }
50
51 void
52 debug_mix_buffers_with_gain (ARDOUR::Sample *dst, ARDOUR::Sample *src, nframes_t nframes, float gain)
53 {
54         if ( ((intptr_t)dst & 15) != 0) {
55                 cerr << "mix_buffers_with_gain(): dst unaligned!" << endl;
56         }
57
58         if ( ((intptr_t)dst & 15) != ((intptr_t)src & 15) ) {
59                 cerr << "mix_buffers_with_gain(): dst & src don't have the same alignment!" << endl;
60                 mix_buffers_with_gain(dst, src, nframes, gain);
61         } else {
62                 x86_sse_mix_buffers_with_gain(dst, src, nframes, gain);
63         }
64 }
65
66 void
67 debug_mix_buffers_no_gain (ARDOUR::Sample *dst, ARDOUR::Sample *src, nframes_t nframes)
68 {
69         if ( ((intptr_t)dst & 15) != 0) {
70                 cerr << "mix_buffers_no_gain(): dst unaligned!" << endl;
71         }
72
73         if ( ((intptr_t)dst & 15) != ((intptr_t)src & 15) ) {
74                 cerr << "mix_buffers_no_gain(): dst & src don't have the same alignment!" << endl;
75                 mix_buffers_no_gain(dst, src, nframes);
76         } else {
77                 x86_sse_mix_buffers_no_gain(dst, src, nframes);
78         }
79 }
80
81 #endif
82
83
84 float
85 default_compute_peak (const ARDOUR::Sample * buf, nframes_t nsamples, float current) 
86 {
87         for (nframes_t i = 0; i < nsamples; ++i) {
88                 current = f_max (current, fabsf (buf[i]));
89         }
90
91         return current;
92 }       
93
94 void
95 default_find_peaks (const ARDOUR::Sample * buf, nframes_t nframes, float *min, float *max)
96 {
97         nframes_t i;
98         float a, b;
99
100         a = *max;
101         b = *min;
102
103         for (i = 0; i < nframes; i++) 
104         {
105                 a = fmax (buf[i], a);
106                 b = fmin (buf[i], b);
107         }
108
109         *max = a;
110         *min = b;
111 }
112
113 void
114 default_apply_gain_to_buffer (ARDOUR::Sample * buf, nframes_t nframes, float gain)
115 {               
116         for (nframes_t i=0; i<nframes; i++)
117                 buf[i] *= gain;
118 }
119
120 void
121 default_mix_buffers_with_gain (ARDOUR::Sample * dst, const ARDOUR::Sample * src, nframes_t nframes, float gain)
122 {
123         for (nframes_t i = 0; i < nframes; i++) {
124                 dst[i] += src[i] * gain;
125         }
126 }
127
128 void
129 default_mix_buffers_no_gain (ARDOUR::Sample * dst, const ARDOUR::Sample * src, nframes_t nframes)
130 {
131         for (nframes_t i=0; i < nframes; i++) {
132                 dst[i] += src[i];
133         }
134 }
135
136 #if defined (__APPLE__) && defined (BUILD_VECLIB_OPTIMIZATIONS)
137 #include <Accelerate/Accelerate.h>
138
139 float
140 veclib_compute_peak (const ARDOUR::Sample * buf, nframes_t nsamples, float current)
141 {
142         float tmpmax = 0.0f;
143         vDSP_maxmgv(buf, 1, &tmpmax, nsamples);
144         return f_max(current, tmpmax);
145 }
146
147 void
148 veclib_find_peaks (const ARDOUR::Sample * buf, nframes_t nframes, float *min, float *max)
149 {
150         vDSP_maxv (const_cast<ARDOUR::Sample*>(buf), 1, max, nframes);
151         vDSP_minv (const_cast<ARDOUR::Sample*>(buf), 1, min, nframes);
152 }
153
154 void
155 veclib_apply_gain_to_buffer (ARDOUR::Sample * buf, nframes_t nframes, float gain)
156 {
157         vDSP_vsmul(buf, 1, &gain, buf, 1, nframes);
158 }
159
160 void
161 veclib_mix_buffers_with_gain (ARDOUR::Sample * dst, const ARDOUR::Sample * src, nframes_t nframes, float gain)
162 {
163         vDSP_vsma(src, 1, &gain, dst, 1, dst, 1, nframes);
164 }
165
166 void
167 veclib_mix_buffers_no_gain (ARDOUR::Sample * dst, const ARDOUR::Sample * src, nframes_t nframes)
168 {
169         // It seems that a vector mult only operation does not exist...
170         float gain = 1.0f;
171         vDSP_vsma(src, 1, &gain, dst, 1, dst, 1, nframes);
172 }
173
174 #endif
175                 
176