| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 16 апр. 2020 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib 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-runtime-lib 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-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_MM_IINAUDIOSTREAM_H_ |
| 23 |
#define LSP_PLUG_IN_MM_IINAUDIOSTREAM_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
|
| 27 |
#include <lsp-plug.in/common/status.h> |
| 28 |
#include <lsp-plug.in/mm/types.h> |
| 29 |
|
| 30 |
namespace lsp |
| 31 |
{ |
| 32 |
namespace mm |
| 33 |
{ |
| 34 |
/** |
| 35 |
* Interface for input audio stream |
| 36 |
*/ |
| 37 |
class IInAudioStream |
| 38 |
{ |
| 39 |
public: |
| 40 |
static const size_t IO_BUF_SIZE = 0x1000; |
| 41 |
|
| 42 |
protected: |
| 43 |
wssize_t nOffset; // Offset from beginning, negative if closed |
| 44 |
status_t nErrorCode; // Last error code |
| 45 |
uint8_t *pBuffer; // Buffer for sample format conversion |
| 46 |
size_t nBufSize; // Size of buffer |
| 47 |
void *pUserData; // User data |
| 48 |
user_data_deleter_t pDeleter; // User data deleter |
| 49 |
|
| 50 |
protected: |
| 51 |
void do_close(); |
| 52 |
|
| 53 |
inline status_t set_error(status_t error) { return nErrorCode = error; } |
| 54 |
|
| 55 |
/** |
| 56 |
* Ensure that internal buffer has enough capacity to keep specified number of bytes |
| 57 |
* @param bytes number of bytes |
| 58 |
* @return status of operation |
| 59 |
*/ |
| 60 |
bool ensure_capacity(size_t bytes); |
| 61 |
|
| 62 |
/** |
| 63 |
* Perform direct read of sample data into the buffer |
| 64 |
* |
| 65 |
* @param dst buffer to store samples |
| 66 |
* @param nframes number of frames to read |
| 67 |
* @param fmt sample format selected for read |
| 68 |
* @return number of frames actually read or negative error code |
| 69 |
*/ |
| 70 |
virtual ssize_t direct_read(void *dst, size_t nframes, size_t fmt); |
| 71 |
|
| 72 |
/** |
| 73 |
* Select actual sample format for direct read |
| 74 |
* @param fmt the requested |
| 75 |
* @return actual sample format, by defaul 0 (not supported) |
| 76 |
*/ |
| 77 |
virtual size_t select_format(size_t fmt); |
| 78 |
|
| 79 |
/** |
| 80 |
* Perform read with conversion |
| 81 |
* @param dst destination buffer to perform read |
| 82 |
* @param nframes number of frames to read |
| 83 |
* @param fmt the requested sample format |
| 84 |
* @return number of frames to read or negative error code |
| 85 |
*/ |
| 86 |
virtual ssize_t conv_read(void *dst, size_t nframes, size_t fmt); |
| 87 |
|
| 88 |
public: |
| 89 |
explicit IInAudioStream(); |
| 90 |
IInAudioStream(const IInAudioStream &) = delete; |
| 91 |
IInAudioStream(IInAudioStream &&) = delete; |
| 92 |
virtual ~IInAudioStream(); |
| 93 |
|
| 94 |
IInAudioStream & operator = (const IInAudioStream &) = delete; |
| 95 |
IInAudioStream & operator = (IInAudioStream &&) = delete; |
| 96 |
|
| 97 |
public: |
| 98 |
/** |
| 99 |
* Check whether audio stream is closed |
| 100 |
* @return true if audio stream is closed |
| 101 |
*/ |
| 102 |
inline bool is_closed() const { return nOffset < 0; } |
| 103 |
|
| 104 |
/** Get last I/O error code |
| 105 |
* |
| 106 |
* @return last I/O error code |
| 107 |
*/ |
| 108 |
inline status_t last_error() const { return nErrorCode; }; |
| 109 |
|
| 110 |
/** |
| 111 |
* Obtain the information about audio stream |
| 112 |
* @param dst destination to store the audio stream information |
| 113 |
* @return status of operation |
| 114 |
*/ |
| 115 |
virtual status_t info(audio_stream_t *dst) const; |
| 116 |
|
| 117 |
/** |
| 118 |
* Get sample rate |
| 119 |
* @return sample rate |
| 120 |
*/ |
| 121 |
virtual size_t sample_rate() const; |
| 122 |
|
| 123 |
/** |
| 124 |
* Get number of channels |
| 125 |
* @return number of channels |
| 126 |
*/ |
| 127 |
virtual size_t channels() const; |
| 128 |
|
| 129 |
/** |
| 130 |
* Get overall duration |
| 131 |
* @return overall number of frames available in audio stream, |
| 132 |
* negative if number of frames can not be estimated |
| 133 |
*/ |
| 134 |
virtual wssize_t length() const; |
| 135 |
|
| 136 |
/** |
| 137 |
* Get sample format |
| 138 |
* @return sample format |
| 139 |
*/ |
| 140 |
virtual size_t format() const; |
| 141 |
|
| 142 |
/** |
| 143 |
* Close audio stream |
| 144 |
* @return status of operations |
| 145 |
*/ |
| 146 |
virtual status_t close(); |
| 147 |
|
| 148 |
/** |
| 149 |
* Skip amount of frames |
| 150 |
* @param nframes number of frames to skip |
| 151 |
* @return number of frames skipped or negative status code |
| 152 |
*/ |
| 153 |
virtual wssize_t skip(wsize_t nframes); |
| 154 |
|
| 155 |
/** |
| 156 |
* Get actual position in frames from beginning of the stream |
| 157 |
* @return number of frames from beginning of the stream or negative error code |
| 158 |
*/ |
| 159 |
virtual wssize_t position(); |
| 160 |
|
| 161 |
/** |
| 162 |
* Try to seek stream at the specified position |
| 163 |
* @param offset the offset from the beginning of the stream |
| 164 |
* @return actual position in frames from beginnig of the stream |
| 165 |
* or negative error code on error (STATUS_NOT_SUPPORTED if stream |
| 166 |
* positioning is not supported) |
| 167 |
*/ |
| 168 |
virtual wssize_t seek(wsize_t offset); |
| 169 |
|
| 170 |
/** |
| 171 |
* Different reads |
| 172 |
* @param dst destination buffer to perform read |
| 173 |
* @param nframes number of frames to read |
| 174 |
* @return number of frames actually read or negative status fo operation |
| 175 |
*/ |
| 176 |
virtual ssize_t read(uint8_t *dst, size_t nframes); |
| 177 |
virtual ssize_t read(int8_t *dst, size_t nframes); |
| 178 |
virtual ssize_t read(uint16_t *dst, size_t nframes); |
| 179 |
virtual ssize_t read(int16_t *dst, size_t nframes); |
| 180 |
virtual ssize_t read(uint32_t *dst, size_t nframes); |
| 181 |
virtual ssize_t read(int32_t *dst, size_t nframes); |
| 182 |
virtual ssize_t read(f32_t *dst, size_t nframes); |
| 183 |
virtual ssize_t read(f64_t *dst, size_t nframes); |
| 184 |
|
| 185 |
/** |
| 186 |
* Strict type reads |
| 187 |
* @param dst destination buffer to perform read |
| 188 |
* @param nframes number of frames to read |
| 189 |
*/ |
| 190 |
inline ssize_t read_u8(void *dst, size_t nframes) { return read(static_cast<uint8_t *>(dst), nframes); } |
| 191 |
inline ssize_t read_s8(void *dst, size_t nframes) { return read(static_cast<int8_t *>(dst), nframes); } |
| 192 |
inline ssize_t read_u16(void *dst, size_t nframes) { return read(static_cast<uint16_t *>(dst), nframes); } |
| 193 |
inline ssize_t read_s16(void *dst, size_t nframes) { return read(static_cast<int16_t *>(dst), nframes); } |
| 194 |
inline ssize_t read_u32(void *dst, size_t nframes) { return read(static_cast<uint32_t *>(dst), nframes); } |
| 195 |
inline ssize_t read_s32(void *dst, size_t nframes) { return read(static_cast<int32_t *>(dst), nframes); } |
| 196 |
inline ssize_t read_f32(void *dst, size_t nframes) { return read(static_cast<f32_t *>(dst), nframes); } |
| 197 |
inline ssize_t read_f64(void *dst, size_t nframes) { return read(static_cast<f64_t *>(dst), nframes); } |
| 198 |
|
| 199 |
public: |
| 200 |
/** |
| 201 |
* Set user data. If previous user data has been set, it will be removed |
| 202 |
* if the deleter was provided |
| 203 |
* @param data data to set |
| 204 |
* @param deleter deleter to remove the data |
| 205 |
*/ |
| 206 |
void set_user_data(void *data, user_data_deleter_t deleter = NULL); |
| 207 |
|
| 208 |
/** Get user data |
| 209 |
* @return user data currently bound to the object |
| 210 |
*/ |
| 211 |
inline void *user_data() { return pUserData; } |
| 212 |
}; |
| 213 |
|
| 214 |
} /* namespace mm */ |
| 215 |
} /* namespace lsp */ |
| 216 |
|
| 217 |
#endif /* LSP_PLUG_IN_MM_IINAUDIOSTREAM_H_ */ |