JackAudioPort.cpp 4.34 KB
Newer Older
sletz's avatar
sletz committed
1
2
/*
Copyright (C) 2001-2003 Paul Davis
sletz's avatar
sletz committed
3
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
4
5

This program is free software; you can redistribute it and/or modify
sletz's avatar
sletz committed
6
7
8
9
10
11
12
13
14
15
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
sletz's avatar
sletz committed
16
along with this program; if not, write to the Free Software
sletz's avatar
sletz committed
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
sletz's avatar
sletz committed
18
19
20

*/

sletz's avatar
sletz committed
21
22
#include "JackGlobals.h"
#include "JackEngineControl.h"
sletz's avatar
sletz committed
23
#include "JackPortType.h"
sletz's avatar
sletz committed
24

sletz's avatar
sletz committed
25
26
#include <string.h>

27
#if defined (__APPLE__)
sletz's avatar
sletz committed
28
#include <Accelerate/Accelerate.h>
sletz's avatar
sletz committed
29
#elif defined (__SSE__) && !defined (__sun__)
sletz's avatar
sletz committed
30
#include <xmmintrin.h>
31
32
#endif

sletz's avatar
sletz committed
33
34
35
36
37
38
39
40
namespace Jack
{

static void AudioBufferInit(void* buffer, size_t buffer_size, jack_nframes_t)
{
    memset(buffer, 0, buffer_size);
}

sletz's avatar
sletz committed
41
static inline void MixAudioBuffer(jack_default_audio_sample_t* mixbuffer, jack_default_audio_sample_t* buffer, jack_nframes_t frames)
sletz's avatar
sletz committed
42
{
43
#ifdef __APPLE__
sletz's avatar
sletz committed
44
    // It seems that a vector mult only operation does not exist...
sletz's avatar
sletz committed
45
    jack_default_audio_sample_t gain = jack_default_audio_sample_t(1.0);
sletz's avatar
sletz committed
46
    vDSP_vsma(buffer, 1, &gain, mixbuffer, 1, mixbuffer, 1, frames);
47
#else
sletz's avatar
sletz committed
48
49
50
51
    jack_nframes_t frames_group = frames / 4;
    frames = frames % 4;

    while (frames_group > 0) {
sletz's avatar
sletz committed
52
#if defined (__SSE__) && !defined (__sun__)
53
54
55
56
57
58
59
        __m128 vec = _mm_add_ps(_mm_load_ps(mixbuffer), _mm_load_ps(buffer));
        _mm_store_ps(mixbuffer, vec);

        mixbuffer += 4;
        buffer += 4;
        frames_group--;
#else
sletz's avatar
sletz committed
60
61
62
63
64
65
66
67
    register jack_default_audio_sample_t mixFloat1 = *mixbuffer;
    register jack_default_audio_sample_t sourceFloat1 = *buffer;
    register jack_default_audio_sample_t mixFloat2 = *(mixbuffer + 1);
    register jack_default_audio_sample_t sourceFloat2 = *(buffer + 1);
    register jack_default_audio_sample_t mixFloat3 = *(mixbuffer + 2);
    register jack_default_audio_sample_t sourceFloat3 = *(buffer + 2);
    register jack_default_audio_sample_t mixFloat4 = *(mixbuffer + 3);
    register jack_default_audio_sample_t sourceFloat4 = *(buffer + 3);
sletz's avatar
sletz committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

    buffer += 4;
    frames_group--;

    mixFloat1 += sourceFloat1;
    mixFloat2 += sourceFloat2;
    mixFloat3 += sourceFloat3;
    mixFloat4 += sourceFloat4;

    *mixbuffer = mixFloat1;
    *(mixbuffer + 1) = mixFloat2;
    *(mixbuffer + 2) = mixFloat3;
    *(mixbuffer + 3) = mixFloat4;

    mixbuffer += 4;
83
#endif
sletz's avatar
sletz committed
84
85
86
    }

    while (frames > 0) {
sletz's avatar
sletz committed
87
88
        register jack_default_audio_sample_t mixFloat1 = *mixbuffer;
        register jack_default_audio_sample_t sourceFloat1 = *buffer;
sletz's avatar
sletz committed
89
90
91
92
93
94
        buffer++;
        frames--;
        mixFloat1 += sourceFloat1;
        *mixbuffer = mixFloat1;
        mixbuffer++;
    }
95
#endif
sletz's avatar
sletz committed
96
97
98
99
100
101
102
}

static void AudioBufferMixdown(void* mixbuffer, void** src_buffers, int src_count, jack_nframes_t nframes)
{
    void* buffer;

    // Copy first buffer
sletz's avatar
sletz committed
103
#if defined (__SSE__) && !defined (__sun__)
sletz's avatar
sletz committed
104
105
106
    jack_nframes_t frames_group = nframes / 4;
    jack_nframes_t remaining_frames = nframes % 4;

sletz's avatar
sletz committed
107
108
    jack_default_audio_sample_t * source = static_cast<jack_default_audio_sample_t*>(src_buffers[0]);
    jack_default_audio_sample_t * target = static_cast<jack_default_audio_sample_t*>(mixbuffer);
sletz's avatar
sletz committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122

    while (frames_group > 0)
    {
        __m128 vec = _mm_load_ps(source);
        _mm_store_ps(target, vec);
        source += 4;
        target += 4;
        --frames_group;
    }

    for (jack_nframes_t i = 0; i != remaining_frames; ++i)
        target[i] = source[i];

#else
sletz's avatar
sletz committed
123
    memcpy(mixbuffer, src_buffers[0], nframes * sizeof(jack_default_audio_sample_t));
sletz's avatar
sletz committed
124
#endif
sletz's avatar
sletz committed
125

sletz's avatar
sletz committed
126
    // Mix remaining buffers
sletz's avatar
sletz committed
127
128
    for (int i = 1; i < src_count; ++i) {
        buffer = src_buffers[i];
sletz's avatar
sletz committed
129
        MixAudioBuffer(static_cast<jack_default_audio_sample_t*>(mixbuffer), static_cast<jack_default_audio_sample_t*>(buffer), nframes);
sletz's avatar
sletz committed
130
131
132
    }
}

sletz's avatar
sletz committed
133
134
static size_t AudioBufferSize()
{
sletz's avatar
sletz committed
135
    return GetEngineControl()->fBufferSize * sizeof(jack_default_audio_sample_t);
sletz's avatar
sletz committed
136
137
}

sletz's avatar
sletz committed
138
const JackPortType gAudioPortType =
sletz's avatar
sletz committed
139
140
141
142
143
144
{
    JACK_DEFAULT_AUDIO_TYPE,
    AudioBufferSize,
    AudioBufferInit,
    AudioBufferMixdown
};
sletz's avatar
sletz committed
145
146
147

} // namespace Jack