File: MatrixMultiplication.h

package info (click to toggle)
iem-plugin-suite 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,080 kB
  • sloc: cpp: 58,973; python: 269; sh: 79; makefile: 41
file content (188 lines) | stat: -rw-r--r-- 7,338 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 ==============================================================================
 This file is part of the IEM plug-in suite.
 Author: Daniel Rudrich
 Copyright (c) 2017 - Institute of Electronic Music and Acoustics (IEM)
 https://iem.at

 The IEM plug-in suite is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 The IEM plug-in suite 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this software.  If not, see <https://www.gnu.org/licenses/>.
 ==============================================================================
 */

#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include "ReferenceCountedDecoder.h"
#include "ReferenceCountedMatrix.h"

class MatrixMultiplication
{
public:
    MatrixMultiplication() {}

    void prepare (const juce::dsp::ProcessSpec& newSpec, bool prepareInputBuffering = true)
    {
        spec = newSpec;

        if (prepareInputBuffering)
        {
            buffer.setSize (buffer.getNumChannels(), spec.maximumBlockSize);
            bufferPrepared = true;
        }
        else
        {
            buffer.setSize (0, 0);
            bufferPrepared = false;
        }

        checkIfNewMatrixAvailable();
    }

    void processReplacing (juce::dsp::AudioBlock<float> data)
    {
        checkIfNewMatrixAvailable();

        // you have to call the prepare method with prepareInputBuffering set to true in order tu use the processReplacing method
        jassert (bufferPrepared);

        ReferenceCountedMatrix::Ptr retainedCurrentMatrix (currentMatrix);
        if (retainedCurrentMatrix == nullptr || ! bufferPrepared)
        {
            data.clear();
            return;
        }

        auto& T = retainedCurrentMatrix->getMatrix();

        const int nInputChannels = juce::jmin (static_cast<int> (data.getNumChannels()),
                                               static_cast<int> (T.getNumColumns()));
        const int nSamples = static_cast<int> (data.getNumSamples());

        // copy input data to buffer
        for (int ch = 0; ch < nInputChannels; ++ch)
            buffer.copyFrom (ch, 0, data.getChannelPointer (ch), nSamples);

        juce::dsp::AudioBlock<float> ab (buffer.getArrayOfWritePointers(),
                                         nInputChannels,
                                         0,
                                         nSamples);
        processNonReplacing (ab, data, false);
    }

    void processNonReplacing (const juce::dsp::AudioBlock<float> inputBlock,
                              juce::dsp::AudioBlock<float> outputBlock,
                              const bool checkNewMatrix = true)
    {
        // you should call the processReplacing instead, it will buffer the input data
        // this is a weak check, as e.g. if number channels differ, it won't trigger
        jassert (inputBlock != outputBlock);

        juce::ScopedNoDenormals noDenormals;

        if (checkNewMatrix)
            checkIfNewMatrixAvailable();

        ReferenceCountedMatrix::Ptr retainedCurrentMatrix (currentMatrix);
        if (retainedCurrentMatrix == nullptr)
        {
            outputBlock.clear();
            return;
        }

        auto& T = retainedCurrentMatrix->getMatrix();

        const int nInputChannels = juce::jmin (static_cast<int> (inputBlock.getNumChannels()),
                                               static_cast<int> (T.getNumColumns()));
        const int nSamples = static_cast<int> (inputBlock.getNumSamples());

        for (int row = 0; row < T.getNumRows(); ++row)
        {
            const int destCh = retainedCurrentMatrix->getRoutingArrayReference().getUnchecked (row);
            if (destCh < outputBlock.getNumChannels())
            {
                float* dest = outputBlock.getChannelPointer (destCh);
                juce::FloatVectorOperations::multiply (dest,
                                                       inputBlock.getChannelPointer (0),
                                                       T (row, 0),
                                                       nSamples); // first channel
                for (int i = 1; i < nInputChannels; ++i) // remaining channels
                    juce::FloatVectorOperations::addWithMultiply (dest,
                                                                  inputBlock.getChannelPointer (i),
                                                                  T (row, i),
                                                                  nSamples);
            }
        }

        juce::Array<int> routingCopy (retainedCurrentMatrix->getRoutingArrayReference());
        routingCopy.sort();
        int lastDest = -1;
        const int nElements = routingCopy.size();
        for (int i = 0; i < nElements; ++i)
        {
            const int destCh = routingCopy[i];
            for (; ++lastDest < destCh;)
                if (lastDest < outputBlock.getNumChannels())
                    juce::FloatVectorOperations::clear (outputBlock.getChannelPointer (lastDest),
                                                        (int) outputBlock.getNumSamples());
            lastDest = destCh;
        }

        for (int ch = routingCopy.getLast() + 1; ch < outputBlock.getNumChannels(); ++ch)
            juce::FloatVectorOperations::clear (outputBlock.getChannelPointer (ch),
                                                (int) outputBlock.getNumSamples());
    }

    const bool checkIfNewMatrixAvailable()
    {
        if (newMatrixAvailable)
        {
            newMatrixAvailable = false;
            currentMatrix = newMatrix;
            newMatrix = nullptr;

            if (currentMatrix != nullptr)
            {
                DBG ("MatrixTransformer: New matrix with name '" << currentMatrix->getName()
                                                                 << "' set.");
                const int cols = (int) currentMatrix->getMatrix().getNumColumns();
                buffer.setSize (cols, buffer.getNumSamples());
                DBG ("MatrixTransformer: buffer resized to " << buffer.getNumChannels() << "x"
                                                             << buffer.getNumSamples());
            }

            return true;
        }
        return false;
    };

    void setMatrix (ReferenceCountedMatrix::Ptr newMatrixToUse, bool force = false)
    {
        newMatrix = newMatrixToUse;
        newMatrixAvailable = true;
        if (force)
            checkIfNewMatrixAvailable();
    }

    ReferenceCountedMatrix::Ptr getMatrix() { return currentMatrix; }

private:
    //==============================================================================
    juce::dsp::ProcessSpec spec = { -1, 0, 0 };
    ReferenceCountedMatrix::Ptr currentMatrix { nullptr };
    ReferenceCountedMatrix::Ptr newMatrix { nullptr };

    juce::AudioBuffer<float> buffer;
    bool bufferPrepared { false };

    bool newMatrixAvailable { false };
};