File: FloatBuffer.h

package info (click to toggle)
lsp-plugins 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 91,856 kB
  • sloc: cpp: 427,831; xml: 57,779; makefile: 9,961; php: 1,005; sh: 18
file content (147 lines) | stat: -rw-r--r-- 5,863 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
/*
 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
 *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
 *
 * This file is part of lsp-test-fw
 * Created on: 21 авг. 2018 г.
 *
 * lsp-test-fw is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * lsp-test-fw 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
 * along with lsp-test-fw. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef LSP_PLUG_IN_TEST_FW_FLOATBUFFER_H_
#define LSP_PLUG_IN_TEST_FW_FLOATBUFFER_H_

#include <lsp-plug.in/test-fw/version.h>
#include <lsp-plug.in/test-fw/types.h>
#include <lsp-plug.in/test-fw/helpers.h>

#include <stddef.h>
#include <stdio.h>
#include <stdint.h>

namespace lsp
{
    namespace test
    {
        /**
         * Represents a float buffer with specific memory alignment
         * and constant boundary markers that helps to detect buffer
         * overruns/underruns
         */
        class LSP_TEST_FW_PUBLIC FloatBuffer
        {
            private:
                float      *pBuffer;
                uint8_t    *pData;
                size_t      nLength;
                size_t      nAlign;
                bool        bAligned;
                mutable ssize_t  nLastDiff;

            private:
                void allocate(size_t samples, size_t align, bool aligned);

            public:
                explicit FloatBuffer(size_t samples, size_t align = LSP_TEST_FW_DEFAULT_ALIGN, bool aligned = true);
                explicit FloatBuffer(const FloatBuffer &src);
                ~FloatBuffer();

            public:
                inline void randomize_positive()            { ::lsp::test::randomize_positive(pBuffer, nLength); };
                inline void randomize_negative()            { ::lsp::test::randomize_negative(pBuffer, nLength); };
                inline void randomize_sign()                { ::lsp::test::randomize_sign(pBuffer, nLength); };
                inline void randomize_0to1()                { ::lsp::test::randomize_0to1(pBuffer, nLength); };
                inline void randomize(float min, float max) { ::lsp::test::randomize(pBuffer, nLength, min, max); }
                void randomize()                            { ::lsp::test::randomize_positive(pBuffer, nLength); };
                void fill_zero();
                void fill(float v);
                void copy(const FloatBuffer &buf);
                void copy(const float *buf, size_t count);
                void replace(const float *buf, size_t count);
                void vfill(size_t off, size_t n, ...);

                bool validate() const;
                inline bool valid() const { return validate(); };
                inline bool corrupted() const { return !validate(); };
                bool equals_relative(const FloatBuffer &src, float tolerance = 1e-5f) const;
                bool equals_absolute(const FloatBuffer &src, float tolerance = 1e-5f) const;
                bool equals_adaptive(const FloatBuffer &src, float tolerance = 1e-5f) const;

                void dump(FILE *out, const char *text) const;
                void dump(FILE *out, const char *text, size_t from, size_t count) const;
                inline void dump(const char *text) const { dump(stdout, text); }
                inline void dump(const char *text, size_t from, size_t count) const { dump(stdout, text, from, count); }

                void dump_hex(FILE *out, const char *text) const;
                void dump_hex(FILE *out, const char *text, size_t from, size_t count) const;
                void dump_hex(const char *text) const { dump(stdout, text); }
                void dump_hex(const char *text, size_t from, size_t count) const { dump(stdout, text, from, count); }

                inline bool aligned() const         { return bAligned; }
                inline size_t size() const          { return nLength; }
                inline ssize_t last_diff() const    { return nLastDiff; }
                inline float get_diff() const       { return (nLastDiff >= 0) ? pBuffer[nLastDiff] : 0.0f; }

            public:
                inline float &operator [] (int index) {
                    return pBuffer[index];
                }

                inline float operator [] (int index) const {
                    return pBuffer[index];
                }

                inline float &operator [] (size_t index) {
                    return pBuffer[index];
                }

                inline float operator [] (size_t index) const {
                    return pBuffer[index];
                }

                inline float get(size_t index) const {
                    return pBuffer[index];
                }

                template <typename T>
                    inline T *data()
                    {
                        return reinterpret_cast<T *>(pBuffer);
                    }

                inline float *data()
                {
                    return pBuffer;
                }

                inline float *data(size_t offset)
                {
                    return &pBuffer[offset];
                }

                inline operator float *()
                {
                    return pBuffer;
                }

                inline operator const float *() const
                {
                    return pBuffer;
                }

        };
    }
}

#endif /* LSP_PLUG_IN_TEST_FW_FLOATBUFFER_H_ */