|
| 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-dsp-units |
| 6 | + * Created on: 8 мая 2024 г. |
| 7 | + * |
| 8 | + * lsp-dsp-units 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-dsp-units 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-dsp-units. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +#ifndef LSP_PLUG_IN_DSP_UNITS_SHARED_AUDIOSTREAM_H_ |
| 23 | +#define LSP_PLUG_IN_DSP_UNITS_SHARED_AUDIOSTREAM_H_ |
| 24 | + |
| 25 | +#include <lsp-plug.in/dsp-units/version.h> |
| 26 | + |
| 27 | +#include <lsp-plug.in/common/status.h> |
| 28 | +#include <lsp-plug.in/common/types.h> |
| 29 | +#include <lsp-plug.in/ipc/SharedMem.h> |
| 30 | +#include <lsp-plug.in/runtime/LSPString.h> |
| 31 | + |
| 32 | +namespace lsp |
| 33 | +{ |
| 34 | + namespace dspu |
| 35 | + { |
| 36 | + /** |
| 37 | + * Shared audio stream FIFO with single producer and multiple consumers. |
| 38 | + */ |
| 39 | + class LSP_DSP_UNITS_PUBLIC AudioStream |
| 40 | + { |
| 41 | + protected: |
| 42 | + enum stream_flags_t |
| 43 | + { |
| 44 | + SS_INITIALIZED = 0x000000c3, |
| 45 | + SS_UPDATED = 0x00009600, |
| 46 | + SS_TERMINATED = 0x005a0000, |
| 47 | + |
| 48 | + SS_INIT_MASK = 0x000000ff, |
| 49 | + SS_UPD_MASK = 0x0000ff00, |
| 50 | + SS_TERM_MASK = 0x00ff0000 |
| 51 | + }; |
| 52 | + |
| 53 | + typedef struct alloc_params_t |
| 54 | + { |
| 55 | + size_t nChannels; |
| 56 | + size_t nHdrBytes; |
| 57 | + size_t nChannelBytes; |
| 58 | + size_t nSegmentBytes; |
| 59 | + } alloc_params_t; |
| 60 | + |
| 61 | + typedef struct sh_header_t |
| 62 | + { |
| 63 | + uint32_t nMagic; // Magic number |
| 64 | + uint32_t nVersion; // Version of the buffer |
| 65 | + uint32_t nFlags; // Stream flags |
| 66 | + uint32_t nChannels; // Number of channels |
| 67 | + uint32_t nLength; // Number of samples per channel |
| 68 | + volatile uint32_t nMaxBlkSize; // Maximum block size written |
| 69 | + volatile uint32_t nHead; // Position of the head of the buffer |
| 70 | + volatile uint32_t nCounter; // Auto-incrementing counter for each change |
| 71 | + } sh_header_t; |
| 72 | + |
| 73 | + typedef struct channel_t |
| 74 | + { |
| 75 | + uint32_t nPosition; // Read/Write position |
| 76 | + uint32_t nCount; // Number of samples read/written |
| 77 | + float *pData; // Pointer to channel data |
| 78 | + } channel_t; |
| 79 | + |
| 80 | + protected: |
| 81 | + ipc::SharedMem hMem; // Shared memory descriptor |
| 82 | + sh_header_t *pHeader; // Header of the shared buffer |
| 83 | + channel_t *vChannels; // Pointer to channel descriptions |
| 84 | + uint32_t nChannels; // Number of channels |
| 85 | + uint32_t nHead; // Head position |
| 86 | + uint32_t nAvail; // Number of samples available |
| 87 | + uint32_t nBlkSize; // Block size |
| 88 | + uint32_t nCounter; // Counter |
| 89 | + bool bWriteMode; // Stream is opened for writing |
| 90 | + bool bIO; // I/O mode (begin() called) |
| 91 | + bool bUnderrun; // Underrun detected |
| 92 | + |
| 93 | + protected: |
| 94 | + bool check_channels_synchronized(); |
| 95 | + status_t open_internal(); |
| 96 | + status_t create_internal(size_t channels, const alloc_params_t *params); |
| 97 | + |
| 98 | + protected: |
| 99 | + static bool calc_params(alloc_params_t *params, size_t channels, size_t length); |
| 100 | + |
| 101 | + public: |
| 102 | + AudioStream(); |
| 103 | + AudioStream(const AudioStream &) = delete; |
| 104 | + AudioStream(AudioStream &&) = delete; |
| 105 | + ~AudioStream(); |
| 106 | + |
| 107 | + AudioStream & operator = (const AudioStream &) = delete; |
| 108 | + AudioStream & operator = (AudioStream &&) = delete; |
| 109 | + |
| 110 | + /** Construct object |
| 111 | + * |
| 112 | + */ |
| 113 | + void construct(); |
| 114 | + |
| 115 | + /** Destroy object |
| 116 | + * |
| 117 | + */ |
| 118 | + void destroy(); |
| 119 | + |
| 120 | + |
| 121 | + public: |
| 122 | + /** |
| 123 | + * Open named audio stream for reading |
| 124 | + * @param id identifier of the named audio stream |
| 125 | + * @return status of operation |
| 126 | + */ |
| 127 | + status_t open(const char *id); |
| 128 | + |
| 129 | + /** |
| 130 | + * Open named audio stream for reading |
| 131 | + * @param id identifier of the named audio stream |
| 132 | + * @return status of operation |
| 133 | + */ |
| 134 | + status_t open(const LSPString *id); |
| 135 | + |
| 136 | + /** |
| 137 | + * Create and open named audio stream for writing |
| 138 | + * @param id identifier of the named audio stream |
| 139 | + * @param channels number of audio channels |
| 140 | + * @param length length of each channel |
| 141 | + * @param persist use persistent shared memory segment |
| 142 | + * @return status of operation |
| 143 | + */ |
| 144 | + status_t create(const char *id, size_t channels, size_t length); |
| 145 | + |
| 146 | + /** |
| 147 | + * Create and open named audio stream for writing |
| 148 | + * @param id identifier of the named audio stream |
| 149 | + * @param channels number of audio channels |
| 150 | + * @param length length of each channel |
| 151 | + * @param persit use persistent shared memory segment |
| 152 | + * @return status of operation |
| 153 | + */ |
| 154 | + status_t create(const LSPString *id, size_t channels, size_t length); |
| 155 | + |
| 156 | + /** |
| 157 | + * Create and open named audio stream for writing |
| 158 | + * @param name pointer to store the name of the shared segment |
| 159 | + * @param postfix postfix for the shared segment to add |
| 160 | + * @param channels number of audio channels |
| 161 | + * @param length length of each channel |
| 162 | + * @param persit use persistent shared memory segment |
| 163 | + * @return status of operation |
| 164 | + */ |
| 165 | + status_t allocate(LSPString *name, const char *postfix, size_t channels, size_t length); |
| 166 | + |
| 167 | + /** |
| 168 | + * Create and open named audio stream for writing |
| 169 | + * @param name pointer to store the name of the shared segment |
| 170 | + * @param postfix postfix for the shared segment to add |
| 171 | + * @param channels number of audio channels |
| 172 | + * @param length length of each channel |
| 173 | + * @param persit use persistent shared memory segment |
| 174 | + * @return status of operation |
| 175 | + */ |
| 176 | + status_t allocate(LSPString *name, const LSPString *postfix, size_t channels, size_t length); |
| 177 | + |
| 178 | + /** |
| 179 | + * Close |
| 180 | + * @return status of operation |
| 181 | + */ |
| 182 | + status_t close(); |
| 183 | + |
| 184 | + public: |
| 185 | + /** |
| 186 | + * Return number of channels |
| 187 | + * @return number of channels |
| 188 | + */ |
| 189 | + size_t channels() const; |
| 190 | + |
| 191 | + /** |
| 192 | + * Get number of samples per channel |
| 193 | + * @return number of samples per channel |
| 194 | + */ |
| 195 | + size_t length() const; |
| 196 | + |
| 197 | + /** |
| 198 | + * Begin I/O operation on the stream |
| 199 | + * @param block_size the desired block size that will be read or written, zero value means infinite block size |
| 200 | + * @return status of operation |
| 201 | + */ |
| 202 | + status_t begin(ssize_t block_size = 0); |
| 203 | + |
| 204 | + /** |
| 205 | + * Read contents of specific channel |
| 206 | + * Should be called between begin() and end() calls |
| 207 | + * |
| 208 | + * @param channel number of channel |
| 209 | + * @param dst destination buffer to store data |
| 210 | + * @param samples number of samples to read |
| 211 | + * @return status of operation |
| 212 | + */ |
| 213 | + status_t read(size_t channel, float *dst, size_t samples); |
| 214 | + |
| 215 | + /** |
| 216 | + * Read contents of specific channel |
| 217 | + * Should be called between begin() and end() calls |
| 218 | + * |
| 219 | + * @param channel number of channel |
| 220 | + * @param dst destination buffer to store data |
| 221 | + * @param samples number of samples to read |
| 222 | + * @return status of operation |
| 223 | + */ |
| 224 | + status_t write(size_t channel, const float *src, size_t samples); |
| 225 | + |
| 226 | + /** |
| 227 | + * End I/O operations on the stream |
| 228 | + * @return status of operation |
| 229 | + */ |
| 230 | + status_t end(); |
| 231 | + }; |
| 232 | + |
| 233 | + } /* namespace dspu */ |
| 234 | +} /* namespace lsp */ |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | +#endif /* LSP_PLUG_IN_DSP_UNITS_SHARED_SHAREDAUDIOSTREAM_H_ */ |
0 commit comments