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