fix compose mess, and a number of 64 bit printf specs
[ardour.git] / libs / ardour / mix.cc
1 /*
2     Copyright (C) 2000-2005 Paul Davis,
3         Copyright (C) 2005 Sampo Savolainen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20 */
21
22 #include <cmath>
23 #include <ardour/types.h>
24 #include <ardour/utils.h>
25 #include <ardour/mix.h>
26
27 #if defined (ARCH_X86) && defined (BUILD_SSE_OPTIMIZATIONS)
28
29 // Debug wrappers
30
31 float
32 debug_compute_peak (ARDOUR::Sample *buf, jack_nframes_t nsamples, float current) 
33 {
34         if ( ((int)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, jack_nframes_t nframes, float gain)
43 {
44         if ( ((int)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, jack_nframes_t nframes, float gain)
53 {
54         if ( ((int)dst & 15) != 0) {
55                 cerr << "mix_buffers_with_gain(): dst unaligned!" << endl;
56         }
57
58         if ( ((int)dst & 15) != ((int)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, jack_nframes_t nframes)
68 {
69         if ( ((int)dst & 15) != 0) {
70                 cerr << "mix_buffers_no_gain(): dst unaligned!" << endl;
71         }
72
73         if ( ((int)dst & 15) != ((int)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 compute_peak (ARDOUR::Sample *buf, jack_nframes_t nsamples, float current) 
86 {
87         for (jack_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 apply_gain_to_buffer (ARDOUR::Sample *buf, jack_nframes_t nframes, float gain)
96 {               
97         for (jack_nframes_t i=0; i<nframes; i++)
98                 buf[i] *= gain;
99 }
100
101 void
102 mix_buffers_with_gain (ARDOUR::Sample *dst, ARDOUR::Sample *src, jack_nframes_t nframes, float gain)
103 {
104         for (jack_nframes_t i = 0; i < nframes; i++) {
105                 dst[i] += src[i] * gain;
106         }
107 }
108
109 void
110 mix_buffers_no_gain (ARDOUR::Sample *dst, ARDOUR::Sample *src, jack_nframes_t nframes)
111 {
112         for (jack_nframes_t i=0; i < nframes; i++) {
113                 dst[i] += src[i];
114         }
115 }
116
117 #if defined (__APPLE__)
118 #include <Accelerate/Accelerate.h>
119
120 float
121 veclib_compute_peak (ARDOUR::Sample *buf, jack_nframes_t nsamples, float current)
122 {
123         vDSP_maxv(buf, 1, &current, nsamples);
124         return current;
125 }
126
127 void
128 veclib_apply_gain_to_buffer (ARDOUR::Sample *buf, jack_nframes_t nframes, float gain)
129 {
130         vDSP_vsmul(buf, 1, &gain, buf, 1, nframes);
131 }
132
133 void
134 veclib_mix_buffers_with_gain (ARDOUR::Sample *dst, ARDOUR::Sample *src, jack_nframes_t nframes, float gain)
135 {
136         vDSP_vsma(src, 1, &gain, dst, 1, dst, 1, nframes);
137 }
138
139 void
140 veclib_mix_buffers_no_gain (ARDOUR::Sample *dst, ARDOUR::Sample *src, jack_nframes_t nframes)
141 {
142         // It seems that a vector mult only operation does not exist...
143         float gain = 1.0f;
144         vDSP_vsma(src, 1, &gain, dst, 1, dst, 1, nframes);
145 }
146
147 #endif
148                 
149