| 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_IOUTAUDIOSTREAM_H_ |
| 23 |
#define LSP_PLUG_IN_MM_IOUTAUDIOSTREAM_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/status.h> |
| 27 |
#include <lsp-plug.in/mm/types.h> |
| 28 |
|
| 29 |
namespace lsp |
| 30 |
{ |
| 31 |
namespace mm |
| 32 |
{ |
| 33 |
class IOutAudioStream |
| 34 |
{ |
| 35 |
public: |
| 36 |
static const size_t IO_BUF_SIZE = 0x1000; |
| 37 |
|
| 38 |
protected: |
| 39 |
wssize_t nOffset; // Offset from beginning, negative if closed |
| 40 |
status_t nErrorCode; // Last error code |
| 41 |
uint8_t *pBuffer; // Buffer for sample format conversion |
| 42 |
size_t nBufSize; // Size of buffer |
| 43 |
audio_stream_t sFormat; // Audio stream format |
| 44 |
void *pUserData; // User data |
| 45 |
user_data_deleter_t pDeleter; // User data deleter |
| 46 |
|
| 47 |
protected: |
| 48 |
void do_close(); |
| 49 |
|
| 50 |
inline status_t set_error(status_t error) { return nErrorCode = error; } |
| 51 |
|
| 52 |
/** |
| 53 |
* Ensure that internal buffer has enough capacity to keep specified number of bytes |
| 54 |
* @param bytes number of bytes |
| 55 |
* @return status of operation |
| 56 |
*/ |
| 57 |
status_t ensure_capacity(size_t bytes); |
| 58 |
|
| 59 |
/** |
| 60 |
* Perform direct write of sample data |
| 61 |
* |
| 62 |
* @param src buffer with frame data. |
| 63 |
* @param nframes number of frames to write |
| 64 |
* @param fmt actual sample format that should be written |
| 65 |
* @return number of frames actually written or negative error code |
| 66 |
*/ |
| 67 |
virtual ssize_t direct_write(const void *src, size_t nframes, size_t fmt); |
| 68 |
|
| 69 |
/** |
| 70 |
* Perform write with conversion |
| 71 |
* @param src source buffer with frame data |
| 72 |
* @param nframes number of frames to write |
| 73 |
* @param fmt sample format |
| 74 |
* @return status if operation |
| 75 |
*/ |
| 76 |
virtual ssize_t conv_write(const void *src, size_t nframes, size_t fmt); |
| 77 |
|
| 78 |
/** |
| 79 |
* Select preferred sample format depending on the actual sample format |
| 80 |
* @param rfmt requested format |
| 81 |
* @return preferred sample format, by default 0 (unsupported format) |
| 82 |
*/ |
| 83 |
virtual size_t select_format(size_t rfmt); |
| 84 |
|
| 85 |
public: |
| 86 |
explicit IOutAudioStream(); |
| 87 |
IOutAudioStream (const IOutAudioStream &) = delete; |
| 88 |
IOutAudioStream (IOutAudioStream &&) = delete; |
| 89 |
virtual ~IOutAudioStream(); |
| 90 |
|
| 91 |
IOutAudioStream & operator = (const IOutAudioStream &) = delete; |
| 92 |
IOutAudioStream & operator = (IOutAudioStream &&) = delete; |
| 93 |
|
| 94 |
public: |
| 95 |
/** |
| 96 |
* Check whether audio stream is closed |
| 97 |
* @return true if audio stream is closed |
| 98 |
*/ |
| 99 |
inline bool is_closed() const { return nOffset < 0; } |
| 100 |
|
| 101 |
/** Get last I/O error code |
| 102 |
* |
| 103 |
* @return last I/O error code |
| 104 |
*/ |
| 105 |
inline status_t last_error() const { return nErrorCode; }; |
| 106 |
|
| 107 |
/** |
| 108 |
* Obtain the information about audio stream |
| 109 |
* @param dst destination to store the audio stream information |
| 110 |
* @return status of operation |
| 111 |
*/ |
| 112 |
status_t info(audio_stream_t *dst) const; |
| 113 |
|
| 114 |
/** |
| 115 |
* Get sample rate |
| 116 |
* @return sample rate |
| 117 |
*/ |
| 118 |
inline size_t sample_rate() const { return sFormat.srate; } |
| 119 |
|
| 120 |
/** |
| 121 |
* Get number of channels |
| 122 |
* @return number of channels |
| 123 |
*/ |
| 124 |
inline size_t channels() const { return sFormat.channels; } |
| 125 |
|
| 126 |
/** |
| 127 |
* Get overall duration |
| 128 |
* @return overall number of frames available in audio stream, |
| 129 |
* negative if number of frames can not be estimated |
| 130 |
*/ |
| 131 |
inline wssize_t length() const { return sFormat.frames; } |
| 132 |
|
| 133 |
/** |
| 134 |
* Get sample format |
| 135 |
* @return sample format |
| 136 |
*/ |
| 137 |
inline size_t format() const { return sFormat.format; } |
| 138 |
|
| 139 |
/** |
| 140 |
* Flush audio stream |
| 141 |
* @return status of operations |
| 142 |
*/ |
| 143 |
virtual status_t flush(); |
| 144 |
|
| 145 |
/** |
| 146 |
* Close audio stream |
| 147 |
* @return status of operations |
| 148 |
*/ |
| 149 |
virtual status_t close(); |
| 150 |
|
| 151 |
/** |
| 152 |
* Get actual position in frames from beginning of the stream |
| 153 |
* @return number of frames from beginning of the stream or negative error code |
| 154 |
*/ |
| 155 |
virtual wssize_t position(); |
| 156 |
|
| 157 |
/** |
| 158 |
* Try to seek stream at the specified position |
| 159 |
* @return actual position in frames from beginnin of the stream |
| 160 |
* or negative error code on error (STATUS_NOT_SUPPORTED if stream |
| 161 |
* positioning is not supported) |
| 162 |
*/ |
| 163 |
virtual wssize_t seek(wsize_t nframes); |
| 164 |
|
| 165 |
/** |
| 166 |
* Different reads |
| 167 |
* @param dst destination buffer to perform read |
| 168 |
* @param nframes number of frames to read |
| 169 |
* @return number of frames actually read or negative status fo operation |
| 170 |
*/ |
| 171 |
virtual ssize_t write(const uint8_t *dst, size_t nframes); |
| 172 |
virtual ssize_t write(const int8_t *dst, size_t nframes); |
| 173 |
virtual ssize_t write(const uint16_t *dst, size_t nframes); |
| 174 |
virtual ssize_t write(const int16_t *dst, size_t nframes); |
| 175 |
virtual ssize_t write(const uint32_t *dst, size_t nframes); |
| 176 |
virtual ssize_t write(const int32_t *dst, size_t nframes); |
| 177 |
virtual ssize_t write(const f32_t *dst, size_t nframes); |
| 178 |
virtual ssize_t write(const f64_t *dst, size_t nframes); |
| 179 |
|
| 180 |
/** |
| 181 |
* Strict type reads |
| 182 |
* @param dst destination buffer to perform read |
| 183 |
* @param nframes number of frames to read |
| 184 |
*/ |
| 185 |
inline ssize_t write_u8(const void *dst, size_t nframes) { return write(static_cast<const uint8_t *>(dst), nframes); } |
| 186 |
inline ssize_t write_s8(const void *dst, size_t nframes) { return write(static_cast<const int8_t *>(dst), nframes); } |
| 187 |
inline ssize_t write_u16(const void *dst, size_t nframes) { return write(static_cast<const uint16_t *>(dst), nframes); } |
| 188 |
inline ssize_t write_s16(const void *dst, size_t nframes) { return write(static_cast<const int16_t *>(dst), nframes); } |
| 189 |
inline ssize_t write_u32(const void *dst, size_t nframes) { return write(static_cast<const uint32_t *>(dst), nframes); } |
| 190 |
inline ssize_t write_s32(const void *dst, size_t nframes) { return write(static_cast<const int32_t *>(dst), nframes); } |
| 191 |
inline ssize_t write_f32(const void *dst, size_t nframes) { return write(static_cast<const f32_t *>(dst), nframes); } |
| 192 |
inline ssize_t write_f64(const void *dst, size_t nframes) { return write(static_cast<const f64_t *>(dst), nframes); } |
| 193 |
|
| 194 |
public: |
| 195 |
/** |
| 196 |
* Set user data. If previous user data has been set, it will be removed |
| 197 |
* if the deleter was provided |
| 198 |
* @param data data to set |
| 199 |
* @param deleter deleter to remove the data |
| 200 |
*/ |
| 201 |
void set_user_data(void *data, user_data_deleter_t deleter = NULL); |
| 202 |
|
| 203 |
/** Get user data |
| 204 |
* @return user data currently bound to the object |
| 205 |
*/ |
| 206 |
inline void *user_data() { return pUserData; } |
| 207 |
}; |
| 208 |
|
| 209 |
} /* namespace mm */ |
| 210 |
} /* namespace lsp */ |
| 211 |
|
| 212 |
#endif /* LSP_PLUG_IN_MM_IOUTAUDIOSTREAM_H_ */ |