File: Queue.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 (74 lines) | stat: -rw-r--r-- 2,655 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
/*
 ==============================================================================
 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 <http://www.gnu.org/licenses/>.
 ==============================================================================
 */

#pragma once

// A simple queue of arbitrary sample type (SampleType) with fixed numbers of samples (BufferSize).
// A good thing to transfer data between processor and editoras it should be lock-free.
// IMPORTANT INFORMATION: If this queue is full, new data WON'T be inserted!
// The two methods return the number of actually written or read samples.

template <typename SampleType, int BufferSize>
class Queue
{
public:
    Queue() : abstractFifo (BufferSize) {}

    int addToQueue (const SampleType* samples, size_t numSamples)
    {
        jassert (numSamples <= BufferSize); // don't push more samples than the buffer holds

        int start1, size1, start2, size2;
        abstractFifo.prepareToWrite (numSamples, start1, size1, start2, size2);

        if (size1 > 0)
            for (int i = 0; i < size1; ++i)
                buffer[start1 + i] = samples[i];
        if (size2 > 0)
            for (int i = 0; i < size2; ++i)
                buffer[i] = samples[size1 + i];
        abstractFifo.finishedWrite (size1 + size2);

        return size1 + size2;
    }

    int readFromQueue (SampleType* outputBuffer, int numItems)
    {
        int start1, size1, start2, size2;
        abstractFifo.prepareToRead (numItems, start1, size1, start2, size2);

        if (size1 > 0)
            for (int i = 0; i < size1; ++i)
                outputBuffer[i] = buffer[start1 + i];
        if (size2 > 0)
            for (int i = 0; i < size2; ++i)
                outputBuffer[start1 + i] = buffer[i];

        abstractFifo.finishedRead (size1 + size2);

        return size1 + size2;
    }

private:
    AbstractFifo abstractFifo;
    std::array<SampleType, BufferSize> buffer;
};