Jack2: JackAudioPort.cpp Source File

Jack2 1.9.12

JackAudioPort.cpp
1 /*
2 Copyright (C) 2001-2003 Paul Davis
3 Copyright (C) 2004-2008 Grame
4
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.
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 Lesser General Public License for more details.
14
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.
18
19 */
20
21 #include "JackGlobals.h"
22 #include "JackEngineControl.h"
23 #include "JackPortType.h"
24
25 #include <string.h>
26
27 #if defined (__APPLE__)
28 #include <Accelerate/Accelerate.h>
29 #elif defined (__SSE__) && !defined (__sun__)
30 #include <xmmintrin.h>
31 #elif defined (__ARM_NEON__)
32 #include <arm_neon.h>
33 #endif
34
35 namespace Jack
36 {
37
38 static void AudioBufferInit(void* buffer, size_t buffer_size, jack_nframes_t)
39 {
40 memset(buffer, 0, buffer_size);
41 }
42
43 static inline void MixAudioBuffer(jack_default_audio_sample_t* mixbuffer, jack_default_audio_sample_t* buffer, jack_nframes_t frames)
44 {
45 #ifdef __APPLE__
46 vDSP_vadd(buffer, 1, mixbuffer, 1, mixbuffer, 1, frames);
47 #else
48 jack_nframes_t frames_group = frames / 4;
49 frames = frames % 4;
50
51 while (frames_group > 0) {
52 #if defined (__SSE__) && !defined (__sun__)
53 __m128 vec = _mm_add_ps(_mm_load_ps(mixbuffer), _mm_load_ps(buffer));
54 _mm_store_ps(mixbuffer, vec);
55
56 mixbuffer += 4;
57 buffer += 4;
58 frames_group--;
59 #elif defined (__ARM_NEON__)
60 float32x4_t vec = vaddq_f32(vld1q_f32(mixbuffer), vld1q_f32(buffer));
61 vst1q_f32(mixbuffer, vec);
62
63 mixbuffer += 4;
64 buffer += 4;
65 frames_group--;
66 #else
67 register jack_default_audio_sample_t mixFloat1 = *mixbuffer;
68 register jack_default_audio_sample_t sourceFloat1 = *buffer;
69 register jack_default_audio_sample_t mixFloat2 = *(mixbuffer + 1);
70 register jack_default_audio_sample_t sourceFloat2 = *(buffer + 1);
71 register jack_default_audio_sample_t mixFloat3 = *(mixbuffer + 2);
72 register jack_default_audio_sample_t sourceFloat3 = *(buffer + 2);
73 register jack_default_audio_sample_t mixFloat4 = *(mixbuffer + 3);
74 register jack_default_audio_sample_t sourceFloat4 = *(buffer + 3);
75
76 buffer += 4;
77 frames_group--;
78
79 mixFloat1 += sourceFloat1;
80 mixFloat2 += sourceFloat2;
81 mixFloat3 += sourceFloat3;
82 mixFloat4 += sourceFloat4;
83
84 *mixbuffer = mixFloat1;
85 *(mixbuffer + 1) = mixFloat2;
86 *(mixbuffer + 2) = mixFloat3;
87 *(mixbuffer + 3) = mixFloat4;
88
89 mixbuffer += 4;
90 #endif
91 }
92
93 while (frames > 0) {
94 register jack_default_audio_sample_t mixFloat1 = *mixbuffer;
95 register jack_default_audio_sample_t sourceFloat1 = *buffer;
96 buffer++;
97 frames--;
98 mixFloat1 += sourceFloat1;
99 *mixbuffer = mixFloat1;
100 mixbuffer++;
101 }
102 #endif
103 }
104
105 static void AudioBufferMixdown(void* mixbuffer, void** src_buffers, int src_count, jack_nframes_t nframes)
106 {
107 void* buffer;
108
109 // Copy first buffer
110 #if defined (__SSE__) && !defined (__sun__)
111 jack_nframes_t frames_group = nframes / 4;
112 jack_nframes_t remaining_frames = nframes % 4;
113
114 jack_default_audio_sample_t* source = static_cast<jack_default_audio_sample_t*>(src_buffers[0]);
115 jack_default_audio_sample_t* target = static_cast<jack_default_audio_sample_t*>(mixbuffer);
116
117 while (frames_group > 0) {
118 __m128 vec = _mm_load_ps(source);
119 _mm_store_ps(target, vec);
120 source += 4;
121 target += 4;
122 --frames_group;
123 }
124
125 for (jack_nframes_t i = 0; i != remaining_frames; ++i) {
126 target[i] = source[i];
127 }
128 #elif defined (__ARM_NEON__)
129 jack_nframes_t frames_group = nframes / 4;
130 jack_nframes_t remaining_frames = nframes % 4;
131
132 jack_default_audio_sample_t* source = static_cast<jack_default_audio_sample_t*>(src_buffers[0]);
133 jack_default_audio_sample_t* target = static_cast<jack_default_audio_sample_t*>(mixbuffer);
134
135 while (frames_group > 0) {
136 float32x4_t vec = vld1q_f32(source);
137 vst1q_f32(target, vec);
138 source += 4;
139 target += 4;
140 --frames_group;
141 }
142
143 for (jack_nframes_t i = 0; i != remaining_frames; ++i) {
144 target[i] = source[i];
145 }
146 #else
147 memcpy(mixbuffer, src_buffers[0], nframes * sizeof(jack_default_audio_sample_t));
148 #endif
149
150 // Mix remaining buffers
151 for (int i = 1; i < src_count; ++i) {
152 buffer = src_buffers[i];
153 MixAudioBuffer(static_cast<jack_default_audio_sample_t*>(mixbuffer), static_cast<jack_default_audio_sample_t*>(buffer), nframes);
154 }
155 }
156
157 static size_t AudioBufferSize()
158 {
159 return GetEngineControl()->fBufferSize * sizeof(jack_default_audio_sample_t);
160 }
161
162 const JackPortType gAudioPortType =
163 {
164 JACK_DEFAULT_AUDIO_TYPE,
165 AudioBufferSize,
166 AudioBufferInit,
167 AudioBufferMixdown
168 };
169
170 } // namespace Jack
171