Better isolation of server and clients system resources to allow starting the server...
[jack2.git] / common / JackAudioPort.cpp
blob765c3c4ec69324ba03ea6b1c11a977facbbd72bd
1 /*
2 Copyright (C) 2001-2003 Paul Davis
3 Copyright (C) 2004-2008 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "JackPortType.h"
22 #include <string.h>
24 #if defined (__APPLE__)
25 #include <Accelerate/Accelerate.h>
26 #elif defined (__SSE__)
27 #include <xmmintrin.h>
28 #endif
30 namespace Jack
33 static void AudioBufferInit(void* buffer, size_t buffer_size, jack_nframes_t)
35 memset(buffer, 0, buffer_size);
38 static inline void MixAudioBuffer(float* mixbuffer, float* buffer, jack_nframes_t frames)
40 #ifdef __APPLE__
41 // It seems that a vector mult only operation does not exist...
42 float gain = 1.0f;
43 vDSP_vsma(buffer, 1, &gain, mixbuffer, 1, mixbuffer, 1, frames);
44 #else
45 jack_nframes_t frames_group = frames / 4;
46 frames = frames % 4;
48 while (frames_group > 0) {
49 #ifdef __SSE__
50 __m128 vec = _mm_add_ps(_mm_load_ps(mixbuffer), _mm_load_ps(buffer));
51 _mm_store_ps(mixbuffer, vec);
53 mixbuffer += 4;
54 buffer += 4;
55 frames_group--;
56 #else
57 register float mixFloat1 = *mixbuffer;
58 register float sourceFloat1 = *buffer;
59 register float mixFloat2 = *(mixbuffer + 1);
60 register float sourceFloat2 = *(buffer + 1);
61 register float mixFloat3 = *(mixbuffer + 2);
62 register float sourceFloat3 = *(buffer + 2);
63 register float mixFloat4 = *(mixbuffer + 3);
64 register float sourceFloat4 = *(buffer + 3);
66 buffer += 4;
67 frames_group--;
69 mixFloat1 += sourceFloat1;
70 mixFloat2 += sourceFloat2;
71 mixFloat3 += sourceFloat3;
72 mixFloat4 += sourceFloat4;
74 *mixbuffer = mixFloat1;
75 *(mixbuffer + 1) = mixFloat2;
76 *(mixbuffer + 2) = mixFloat3;
77 *(mixbuffer + 3) = mixFloat4;
79 mixbuffer += 4;
80 #endif
83 while (frames > 0) {
84 register float mixFloat1 = *mixbuffer;
85 register float sourceFloat1 = *buffer;
86 buffer++;
87 frames--;
88 mixFloat1 += sourceFloat1;
89 *mixbuffer = mixFloat1;
90 mixbuffer++;
92 #endif
95 static void AudioBufferMixdown(void* mixbuffer, void** src_buffers, int src_count, jack_nframes_t nframes)
97 void* buffer;
99 // Copy first buffer
100 #ifdef __SSE__
101 jack_nframes_t frames_group = nframes / 4;
102 jack_nframes_t remaining_frames = nframes % 4;
104 float * source = static_cast<float*>(src_buffers[0]);
105 float * target = static_cast<float*>(mixbuffer);
107 while (frames_group > 0)
109 __m128 vec = _mm_load_ps(source);
110 _mm_store_ps(target, vec);
111 source += 4;
112 target += 4;
113 --frames_group;
116 for (jack_nframes_t i = 0; i != remaining_frames; ++i)
117 target[i] = source[i];
119 #else
120 memcpy(mixbuffer, src_buffers[0], nframes * sizeof(float));
121 #endif
123 // Mix remaining buffers
124 for (int i = 1; i < src_count; ++i) {
125 buffer = src_buffers[i];
126 MixAudioBuffer(static_cast<float*>(mixbuffer), static_cast<float*>(buffer), nframes);
130 const JackPortType gAudioPortType =
132 JACK_DEFAULT_AUDIO_TYPE,
133 AudioBufferInit,
134 AudioBufferMixdown
137 } // namespace Jack