|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 | + * (C) 2024 Vladimir Sadovnikov <sadko4u@gmail.com> |
| 4 | + * |
| 5 | + * This file is part of lsp-plugin-fw |
| 6 | + * Created on: 26 авг. 2024 г. |
| 7 | + * |
| 8 | + * lsp-plugin-fw is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * any later version. |
| 12 | + * |
| 13 | + * lsp-plugin-fw is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with lsp-plugin-fw. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +#ifndef LSP_PLUG_IN_PLUG_FW_CORE_AUDIOBUFFER_H_ |
| 23 | +#define LSP_PLUG_IN_PLUG_FW_CORE_AUDIOBUFFER_H_ |
| 24 | + |
| 25 | +#include <lsp-plug.in/plug-fw/version.h> |
| 26 | + |
| 27 | +#include <lsp-plug.in/common/alloc.h> |
| 28 | +#include <lsp-plug.in/common/types.h> |
| 29 | +#include <lsp-plug.in/dsp/dsp.h> |
| 30 | + |
| 31 | +namespace lsp |
| 32 | +{ |
| 33 | + namespace core |
| 34 | + { |
| 35 | + /** |
| 36 | + * Audio buffer, simple data structure that contains some audio data. |
| 37 | + */ |
| 38 | + class AudioBuffer |
| 39 | + { |
| 40 | + private: |
| 41 | + uint32_t nBufSize; // Size of sanitized buffer in samples |
| 42 | + uint32_t nOffset; // Offset from the beginning of the buffer |
| 43 | + bool bActive; // Activity flag |
| 44 | + bool bClean; // Port is clean |
| 45 | + float *pBuffer; // Buffer data |
| 46 | + |
| 47 | + private: |
| 48 | + void set_clean_state(bool clean); |
| 49 | + |
| 50 | + public: |
| 51 | + explicit AudioBuffer() |
| 52 | + { |
| 53 | + nBufSize = 0; |
| 54 | + nOffset = 0; |
| 55 | + bActive = false; |
| 56 | + bClean = false; |
| 57 | + pBuffer = NULL; |
| 58 | + } |
| 59 | + |
| 60 | + AudioBuffer(const AudioBuffer &) = delete; |
| 61 | + AudioBuffer(AudioBuffer &&) = delete; |
| 62 | + |
| 63 | + ~AudioBuffer() |
| 64 | + { |
| 65 | + if (pBuffer != NULL) |
| 66 | + { |
| 67 | + nBufSize = 0; |
| 68 | + free(pBuffer); |
| 69 | + } |
| 70 | + bActive = false; |
| 71 | + }; |
| 72 | + |
| 73 | + AudioBuffer & operator = (const AudioBuffer &) = delete; |
| 74 | + AudioBuffer & operator = (AudioBuffer &&) = delete; |
| 75 | + |
| 76 | + public: |
| 77 | + /** |
| 78 | + * Get pointer to the beginning of the buffer |
| 79 | + * @return pointer to the beginning of the buffer |
| 80 | + */ |
| 81 | + inline float *data() |
| 82 | + { |
| 83 | + return pBuffer; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get pointer to the buffer data respective to the offset |
| 88 | + * @return pointer to the buffer data |
| 89 | + */ |
| 90 | + inline float *buffer() |
| 91 | + { |
| 92 | + return (pBuffer != NULL) ? &pBuffer[nOffset] : NULL; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Check that buffer is marked as clean and contains zeros |
| 97 | + * @return true if buffer is marked as clean and contains zeros |
| 98 | + */ |
| 99 | + inline bool is_clean() const |
| 100 | + { |
| 101 | + return bClean; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Mark buffer as being clean |
| 106 | + */ |
| 107 | + inline void set_clean() |
| 108 | + { |
| 109 | + set_clean_state(true); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Mark buffer as being not clean |
| 114 | + */ |
| 115 | + inline void set_dirty() |
| 116 | + { |
| 117 | + set_clean_state(false); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Check if buffer is active |
| 122 | + * @return true if buffer is active |
| 123 | + */ |
| 124 | + inline bool active() const |
| 125 | + { |
| 126 | + return bActive; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Set activity of the buffer |
| 131 | + * @param active activity of the buffer |
| 132 | + */ |
| 133 | + inline void set_active(bool active) |
| 134 | + { |
| 135 | + bActive = active; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Get actual buffer size |
| 140 | + * @return actual buffer size |
| 141 | + */ |
| 142 | + inline size_t size() const |
| 143 | + { |
| 144 | + return nBufSize; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Set buffer size |
| 149 | + * @param size buffer size |
| 150 | + */ |
| 151 | + void set_size(size_t size); |
| 152 | + |
| 153 | + /** |
| 154 | + * Get current offset |
| 155 | + * @return current offset |
| 156 | + */ |
| 157 | + inline size_t offset() const |
| 158 | + { |
| 159 | + return nOffset; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Set current offset |
| 164 | + * @param offset current offset |
| 165 | + */ |
| 166 | + inline void set_offset(size_t offset) |
| 167 | + { |
| 168 | + nOffset = offset; |
| 169 | + } |
| 170 | + }; |
| 171 | + } /* namespace core */ |
| 172 | +} /* namespace lsp */ |
| 173 | + |
| 174 | +#endif /* LSP_PLUG_IN_PLUG_FW_CORE_AUDIOBUFFER_H_ */ |
0 commit comments