| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 6 нояб. 2018 г. |
| 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_FMT_LSPC_AUDIOREADER_H_ |
| 23 |
#define LSP_PLUG_IN_FMT_LSPC_AUDIOREADER_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/types.h> |
| 27 |
#include <lsp-plug.in/common/status.h> |
| 28 |
#include <lsp-plug.in/fmt/lspc/File.h> |
| 29 |
#include <lsp-plug.in/fmt/lspc/lspc.h> |
| 30 |
|
| 31 |
namespace lsp |
| 32 |
{ |
| 33 |
namespace lspc |
| 34 |
{ |
| 35 |
/** |
| 36 |
* This is helper class for reading audio content from LSPC files. |
| 37 |
*/ |
| 38 |
class AudioReader |
| 39 |
{ |
| 40 |
private: |
| 41 |
enum flags_t |
| 42 |
{ |
| 43 |
F_OPENED = 1 << 0, |
| 44 |
F_CLOSE_READER = 1 << 1, |
| 45 |
F_CLOSE_FILE = 1 << 2, |
| 46 |
F_REV_BYTES = 1 << 3, |
| 47 |
F_DROP_READER = 1 << 4 |
| 48 |
}; |
| 49 |
|
| 50 |
typedef struct buffer_t |
| 51 |
{ |
| 52 |
uint8_t *vData; // Pointer to the data |
| 53 |
size_t nSize; // Size of data stored in buffer |
| 54 |
size_t nOff; // Offset to the beginning of non-read data |
| 55 |
} buffer_t; |
| 56 |
|
| 57 |
typedef void (*decode_func_t)(float *vp, const void *src, size_t ns); |
| 58 |
|
| 59 |
private: |
| 60 |
audio_parameters_t sParams; |
| 61 |
File *pFD; |
| 62 |
ChunkReader *pRD; |
| 63 |
size_t nFlags; |
| 64 |
size_t nBPS; // Bytes per sample |
| 65 |
size_t nFrameSize; // Size of frame |
| 66 |
size_t nBytesLeft; |
| 67 |
buffer_t sBuf; |
| 68 |
decode_func_t pDecode; |
| 69 |
float *pFBuffer; // frame buffer |
| 70 |
|
| 71 |
protected: |
| 72 |
static void decode_u8(float *vp, const void *src, size_t ns); |
| 73 |
static void decode_s8(float *vp, const void *src, size_t ns); |
| 74 |
static void decode_u16(float *vp, const void *src, size_t ns); |
| 75 |
static void decode_s16(float *vp, const void *src, size_t ns); |
| 76 |
static void decode_u24le(float *vp, const void *src, size_t ns); |
| 77 |
static void decode_u24be(float *vp, const void *src, size_t ns); |
| 78 |
static void decode_s24le(float *vp, const void *src, size_t ns); |
| 79 |
static void decode_s24be(float *vp, const void *src, size_t ns); |
| 80 |
static void decode_u32(float *vp, const void *src, size_t ns); |
| 81 |
static void decode_s32(float *vp, const void *src, size_t ns); |
| 82 |
static void decode_f32(float *vp, const void *src, size_t ns); |
| 83 |
static void decode_f64(float *vp, const void *src, size_t ns); |
| 84 |
|
| 85 |
protected: |
| 86 |
status_t read_audio_header(ChunkReader *rd); |
| 87 |
status_t apply_params(const audio_parameters_t *p); |
| 88 |
status_t fill_buffer(); |
| 89 |
|
| 90 |
public: |
| 91 |
explicit AudioReader(); |
| 92 |
AudioReader(const AudioReader &) = delete; |
| 93 |
AudioReader(AudioReader &&) = delete; |
| 94 |
~AudioReader(); |
| 95 |
|
| 96 |
AudioReader & operator = (const AudioReader &) = delete; |
| 97 |
AudioReader & operator = (AudioReader &&) = delete; |
| 98 |
|
| 99 |
public: |
| 100 |
/** |
| 101 |
* Find first audio chunk and open it for reading |
| 102 |
* @param lspc opened LSPC file |
| 103 |
* @param auto_close automatically close reader on close() |
| 104 |
* @return status of operation |
| 105 |
*/ |
| 106 |
status_t open(File *lspc, bool auto_close = false); |
| 107 |
|
| 108 |
/** |
| 109 |
* Find audio chunk by ID and read it |
| 110 |
* @param lspc opened LSPC file |
| 111 |
* @param uid unique chunk identifier |
| 112 |
* @param auto_close automatically close reader on close() |
| 113 |
* @return status of operation |
| 114 |
*/ |
| 115 |
status_t open(File *lspc, uint32_t uid, bool auto_close = false); |
| 116 |
|
| 117 |
/** |
| 118 |
* Find first audio chunk by magic and open it as audio stream with specified parameters |
| 119 |
* @param lspc opened LSPC file |
| 120 |
* @param magic chunk magic identifier |
| 121 |
* @param params audio parameters |
| 122 |
* @param auto_close automatically close reader on close() |
| 123 |
* @return status of operation |
| 124 |
*/ |
| 125 |
status_t open_raw_magic(File *lspc, const audio_parameters_t *params, uint32_t magic, bool auto_close = false); |
| 126 |
|
| 127 |
/** |
| 128 |
* Open audio chunk by UID as audio stream with specified parameters |
| 129 |
* @param lspc opened LSPC file |
| 130 |
* @param uid unique chunk identifier |
| 131 |
* @param params audio parameters |
| 132 |
* @param auto_close automatically close reader on close() |
| 133 |
* @return status of operation |
| 134 |
*/ |
| 135 |
status_t open_raw_uid(File *lspc, const audio_parameters_t *params, uint32_t uid, bool auto_close = false); |
| 136 |
|
| 137 |
/** |
| 138 |
* Open audio chunk as sub-stream of audio stream with specified parameters |
| 139 |
* @param rd chunk reader |
| 140 |
* @param params audio parameers |
| 141 |
* @param auto_close automatically close reader on close() |
| 142 |
* @return status of operation |
| 143 |
*/ |
| 144 |
status_t open_raw(ChunkReader *rd, const audio_parameters_t *params, bool auto_close = false); |
| 145 |
|
| 146 |
/** |
| 147 |
* Close audio reader |
| 148 |
* @return status of operation |
| 149 |
*/ |
| 150 |
status_t close(); |
| 151 |
|
| 152 |
/** |
| 153 |
* Read sample data from chunk |
| 154 |
* |
| 155 |
* @param data array of pointers to store data, must match number of channels in the stream |
| 156 |
* @param frames number of frames to read |
| 157 |
* @return number of frames actually read or error code as a negative value (0 or STATUS_EOF if there is no data) |
| 158 |
*/ |
| 159 |
ssize_t read_samples(float **data, size_t frames); |
| 160 |
|
| 161 |
/** |
| 162 |
* Read frames from chunk |
| 163 |
* @param data buffer to store data |
| 164 |
* @param frames number of frames to read |
| 165 |
* @return actual number of frames read or error code as a negative value (0 or STATUS_EOF if there is no data) |
| 166 |
*/ |
| 167 |
ssize_t read_frames(float *data, size_t frames); |
| 168 |
|
| 169 |
/** |
| 170 |
* Skip number of frames |
| 171 |
* @param frames number of frames to skip |
| 172 |
* @return number of frames actually skipped |
| 173 |
*/ |
| 174 |
ssize_t skip_frames(size_t frames); |
| 175 |
|
| 176 |
/** |
| 177 |
* Obtain current audio parameters of the stream |
| 178 |
* @param dst pointer to store audio parameters |
| 179 |
* @return status of operation |
| 180 |
*/ |
| 181 |
status_t get_parameters(audio_parameters_t *dst) const; |
| 182 |
|
| 183 |
/** |
| 184 |
* Get current chunk identifier |
| 185 |
* @return current chunk identifier |
| 186 |
*/ |
| 187 |
uint32_t unique_id() const; |
| 188 |
|
| 189 |
/** |
| 190 |
* Get current chunk magic |
| 191 |
* @return current chunk magic |
| 192 |
*/ |
| 193 |
uint32_t magic() const; |
| 194 |
}; |
| 195 |
} |
| 196 |
|
| 197 |
} /* namespace lsp */ |
| 198 |
|
| 199 |
#endif /* LSP_PLUG_IN_FMT_LSPC_AUDIOREADER_H_ */ |