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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
*
* This file is part of lsp-runtime-lib
* Created on: 16 апр. 2020 г.
*
* lsp-runtime-lib 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-runtime-lib 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-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef LSP_PLUG_IN_MM_IINAUDIOSTREAM_H_
#define LSP_PLUG_IN_MM_IINAUDIOSTREAM_H_
#include <lsp-plug.in/runtime/version.h>
#include <lsp-plug.in/common/status.h>
#include <lsp-plug.in/mm/types.h>
namespace lsp
{
namespace mm
{
/**
* Interface for input audio stream
*/
class IInAudioStream
{
private:
IInAudioStream & operator = (const IInAudioStream &);
public:
static const size_t IO_BUF_SIZE = 0x1000;
protected:
wssize_t nOffset; // Offset from beginning, negative if closed
status_t nErrorCode; // Last error code
uint8_t *pBuffer; // Buffer for sample format conversion
size_t nBufSize; // Size of buffer
protected:
void do_close();
inline status_t set_error(status_t error) { return nErrorCode = error; }
/**
* Ensure that internal buffer has enough capacity to keep specified number of bytes
* @param bytes number of bytes
* @return status of operation
*/
bool ensure_capacity(size_t bytes);
/**
* Perform direct read of sample data into the buffer
*
* @param dst buffer to store samples
* @param nframes number of frames to read
* @param fmt sample format selected for read
* @return number of frames actually read or negative error code
*/
virtual ssize_t direct_read(void *dst, size_t nframes, size_t fmt);
/**
* Select actual sample format for direct read
* @param fmt the requested
* @return actual sample format, by defaul 0 (not supported)
*/
virtual size_t select_format(size_t fmt);
/**
* Perform read with conversion
* @param dst destination buffer to perform read
* @param nframes number of frames to read
* @param fmt the requested sample format
* @return number of frames to read or negative error code
*/
virtual ssize_t conv_read(void *dst, size_t nframes, size_t fmt);
public:
explicit IInAudioStream();
virtual ~IInAudioStream();
public:
/**
* Check whether audio stream is closed
* @return true if audio stream is closed
*/
inline bool is_closed() const { return nOffset < 0; }
/** Get last I/O error code
*
* @return last I/O error code
*/
inline status_t last_error() const { return nErrorCode; };
/**
* Obtain the information about audio stream
* @param dst destination to store the audio stream information
* @return status of operation
*/
virtual status_t info(audio_stream_t *dst) const;
/**
* Get sample rate
* @return sample rate
*/
virtual size_t sample_rate() const;
/**
* Get number of channels
* @return number of channels
*/
virtual size_t channels() const;
/**
* Get overall duration
* @return overall number of frames available in audio stream,
* negative if number of frames can not be estimated
*/
virtual wssize_t length() const;
/**
* Get sample format
* @return sample format
*/
virtual size_t format() const;
/**
* Close audio stream
* @return status of operations
*/
virtual status_t close();
/**
* Skip amount of frames
* @param nframes number of frames to skip
* @return number of frames skipped or negative status code
*/
virtual wssize_t skip(wsize_t nframes);
/**
* Get actual position in frames from beginning of the stream
* @return number of frames from beginning of the stream or negative error code
*/
virtual wssize_t position();
/**
* Try to seek stream at the specified position
* @param offset the offset from the beginning of the stream
* @return actual position in frames from beginnig of the stream
* or negative error code on error (STATUS_NOT_SUPPORTED if stream
* positioning is not supported)
*/
virtual wssize_t seek(wsize_t offset);
/**
* Different reads
* @param dst destination buffer to perform read
* @param nframes number of frames to read
* @return number of frames actually read or negative status fo operation
*/
virtual ssize_t read(uint8_t *dst, size_t nframes);
virtual ssize_t read(int8_t *dst, size_t nframes);
virtual ssize_t read(uint16_t *dst, size_t nframes);
virtual ssize_t read(int16_t *dst, size_t nframes);
virtual ssize_t read(uint32_t *dst, size_t nframes);
virtual ssize_t read(int32_t *dst, size_t nframes);
virtual ssize_t read(f32_t *dst, size_t nframes);
virtual ssize_t read(f64_t *dst, size_t nframes);
/**
* Strict type reads
* @param dst destination buffer to perform read
* @param nframe number of frames to read
*/
inline ssize_t read_u8(void *dst, size_t nframes) { return read(static_cast<uint8_t *>(dst), nframes); }
inline ssize_t read_s8(void *dst, size_t nframes) { return read(static_cast<int8_t *>(dst), nframes); }
inline ssize_t read_u16(void *dst, size_t nframes) { return read(static_cast<uint16_t *>(dst), nframes); }
inline ssize_t read_s16(void *dst, size_t nframes) { return read(static_cast<int16_t *>(dst), nframes); }
inline ssize_t read_u32(void *dst, size_t nframes) { return read(static_cast<uint32_t *>(dst), nframes); }
inline ssize_t read_s32(void *dst, size_t nframes) { return read(static_cast<int32_t *>(dst), nframes); }
inline ssize_t read_f32(void *dst, size_t nframes) { return read(static_cast<f32_t *>(dst), nframes); }
inline ssize_t read_f64(void *dst, size_t nframes) { return read(static_cast<f64_t *>(dst), nframes); }
};
} /* namespace mm */
} /* namespace lsp */
#endif /* LSP_PLUG_IN_MM_IINAUDIOSTREAM_H_ */
|