aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/source/samplebuffer.hpp
blob: 83287701e0c448ac3365b85fb45768c3983dd33d (plain)
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
/**
 * @file samplebuffer.hpp
 * @brief Manages ADC/DAC buffer data.
 *
 * Copyright (C) 2023 Clyne Sullivan
 *
 * Distributed under the GNU GPL v3 or later. You should have received a copy of
 * the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef SAMPLEBUFFER_HPP_
#define SAMPLEBUFFER_HPP_

#include <array>
#include <cstdint>

using Sample = uint16_t;

constexpr unsigned int MAX_SAMPLE_BUFFER_BYTESIZE = sizeof(Sample) * 8192;
constexpr unsigned int MAX_SAMPLE_BUFFER_SIZE = MAX_SAMPLE_BUFFER_BYTESIZE / sizeof(Sample);

/**
 * Manages a buffer of sample data from the ADC or DAC with facilities to
 * work with separate halves of the total buffer (which is necessary when
 * streaming data to or from the buffer).
 */
class SampleBuffer
{
public:
    // Manage the sample data memory at 'buffer'.
    SampleBuffer(Sample *buffer);

    /**
     * Fill the current buffer with midpoint (2048/0V) values.
     */
    void clear();

    /**
     * Copy 'srcsize' samples from 'data' into the first half of the current
     * buffer. Also do equivalent of setModified().
     */
    void modify(Sample *data, unsigned int srcsize);

    /**
     * Copy 'srcsize' samples from 'data' into the second half of the current
     * buffer. Also do equivalent of setMidmodified().
     */
    void midmodify(Sample *data, unsigned int srcsize);

    /**
     * Set modified buffer pointer to first half of the current buffer.
     */
    void setModified();

    /**
     * Set modified buffer pointer to second half of the current buffer.
     */
    void setMidmodified();

    /**
     * Return pointer to most recently modified buffer portion.
     * Clears this internal pointer when called.
     */
    Sample *modified();

    /**
     * Returns pointer to first half of current buffer.
     */
    Sample *data();

    /**
     * Returns pointer to second half of current buffer.
     */
    Sample *middata();

    /**
     * Returns uint8_t-casted pointer to the current buffer.
     */
    uint8_t *bytedata();

    /**
     * Sets the total working size of the current buffer. Current buffer must
     * be able to accommodate.
     */
    void setSize(unsigned int size);

    /**
     * Returns the current total working size (number of samples).
     */
    unsigned int size() const;

    /**
     * Returns the current total working size (number of bytes).
     */
    unsigned int bytesize() const;

private:
    Sample *m_buffer = nullptr;
    unsigned int m_size = MAX_SAMPLE_BUFFER_SIZE;
    Sample *m_modified = nullptr;
};

#endif // SAMPLEBUFFER_HPP_