Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/fmt/lspc/AudioWriter.h

raw
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_AUDIOWRITER_H_
23 #define LSP_PLUG_IN_FMT_LSPC_AUDIOWRITER_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26 #include <lsp-plug.in/runtime/LSPString.h>
27 #include <lsp-plug.in/common/types.h>
28 #include <lsp-plug.in/common/status.h>
29 #include <lsp-plug.in/fmt/lspc/File.h>
30 #include <lsp-plug.in/io/Path.h>
31 #include <lsp-plug.in/fmt/lspc/lspc.h>
32
33 namespace lsp
34 {
35 namespace lspc
36 {
37 /**
38 * This is helper class for writing audio content to LSPC files.
39 */
40 class AudioWriter
41 {
42 private:
43 enum flags_t
44 {
45 F_OPENED = 1 << 0,
46 F_CLOSE_WRITER = 1 << 1,
47 F_CLOSE_FILE = 1 << 2,
48 F_REV_BYTES = 1 << 3,
49 F_DROP_WRITER = 1 << 4,
50 F_INTEGER_SAMPLE = 1 << 5,
51 F_DROP_FILE = 1 << 6
52 };
53
54 typedef void (* encode_func_t)(void *dst, const float *src, size_t ns);
55
56 protected:
57 audio_parameters_t sParams;
58 File *pFD;
59 ChunkWriter *pWD;
60 size_t nFlags;
61 size_t nBPS; // Bytes per sample
62 size_t nFrameChannels; // Size of frame in channels
63 encode_func_t pEncode;
64 float *pBuffer;
65 uint8_t *pFBuffer; // frame buffer
66
67 protected:
68 static void encode_u8(void *vp, const float *src, size_t ns);
69 static void encode_s8(void *vp, const float *src, size_t ns);
70 static void encode_u16(void *vp, const float *src, size_t ns);
71 static void encode_s16(void *vp, const float *src, size_t ns);
72 static void encode_u24le(void *vp, const float *src, size_t ns);
73 static void encode_u24be(void *vp, const float *src, size_t ns);
74 static void encode_s24le(void *vp, const float *src, size_t ns);
75 static void encode_s24be(void *vp, const float *src, size_t ns);
76 static void encode_u32(void *vp, const float *src, size_t ns);
77 static void encode_s32(void *vp, const float *src, size_t ns);
78 static void encode_f32(void *vp, const float *src, size_t ns);
79 static void encode_f64(void *vp, const float *src, size_t ns);
80
81 protected:
82 status_t parse_parameters(const audio_parameters_t *p);
83 status_t free_resources();
84 status_t write_header(ChunkWriter *wr);
85
86 public:
87 explicit AudioWriter();
88 AudioWriter(const AudioWriter &) = delete;
89 AudioWriter(AudioWriter &&) = delete;
90 ~AudioWriter();
91
92 AudioWriter & operator = (const AudioWriter &) = delete;
93 AudioWriter & operator = (AudioWriter &&) = delete;
94
95 public:
96 /**
97 * Create new LSPC file and open for writing, write header to LSPC file
98 * @param path UTF-8 path to the LSPC file
99 * @param params audio parameters
100 * @return status of operation
101 */
102 status_t create(const char *path, const audio_parameters_t *params);
103
104 /**
105 * Create new LSPC file and open for writing, write header to LSPC file
106 * @param path path to the LSPC file
107 * @param params audio parameters
108 * @return status of operation
109 */
110 status_t create(const LSPString *path, const audio_parameters_t *params);
111
112 /**
113 * Create new LSPC file and open for writing, write header to LSPC file
114 * @param path path to the LSPC file
115 * @param params audio parameters
116 * @return status of operation
117 */
118 status_t create(const io::Path *path, const audio_parameters_t *params);
119
120 /**
121 * Create chunk in LSPC file with magic=LSPC_CHUNK_AUDIO and write header
122 * @param lspc LSPC file
123 * @param params audio stream parameters
124 * @param auto_close automatically close file on close()
125 * @return status of operation
126 */
127 status_t open(File *lspc, const audio_parameters_t *params, bool auto_close = false);
128
129 /**
130 * Create chunk in LSPC file with magic=LSPC_CHUNK_AUDIO, do not write header
131 * @param lspc LSPC file
132 * @param params audio stream parameters
133 * @param auto_close automatically close file on close()
134 * @return status of operation
135 */
136 status_t open_raw(File *lspc, const audio_parameters_t *params, bool auto_close = false);
137
138 /**
139 * Create chunk in LSPC file with specified magic and write header
140 * @param lspc LSPC file
141 * @param magic specified chunk magic
142 * @param params audio stream parameters
143 * @param auto_close automatically close file on close()
144 * @return status of operation
145 */
146 status_t open(File *lspc, uint32_t magic, const audio_parameters_t *params, bool auto_close = false);
147
148 /**
149 * Create chunk in LSPC file with specified magic, do not write header
150 * @param lspc LSPC file
151 * @param magic specified chunk magic
152 * @param params audio stream parameters
153 * @param auto_close automatically close file on close()
154 * @return status of operation
155 */
156 status_t open_raw(File *lspc, uint32_t magic, const audio_parameters_t *params, bool auto_close = false);
157
158 /**
159 * Write header to already opened chunk
160 * @param wr chunk writer
161 * @param params audio stream parameters
162 * @param auto_close automatically close chunk writer on close()
163 * @return status of operation
164 */
165 status_t open(ChunkWriter *wr, const audio_parameters_t *params, bool auto_close = false);
166
167 /**
168 * Open chunk writer as a raw data stream. Does not emit audio header into stream contents.
169 * @param wr chunk writer
170 * @param params audio stream parameters
171 * @param auto_close automatically close chunk writer on close()
172 * @return status of operation
173 */
174 status_t open_raw(ChunkWriter *wr, const audio_parameters_t *params, bool auto_close = false);
175
176 /**
177 * Close audio writer
178 * @return status of operation
179 */
180 status_t close();
181
182 /**
183 * Write sample data to chunk
184 *
185 * @param data array of pointers to store data, must match number of channels in the stream
186 * @param frames number of frames to write
187 * @return status of operation
188 */
189 status_t write_samples(const float **data, size_t frames);
190
191 /**
192 * Write frames to chunk
193 * @param data buffer to store data
194 * @param frames number of frames to write
195 * @return status of operation
196 */
197 status_t write_frames(const float *data, size_t frames);
198
199 /**
200 * Obtain current audio parameters of the stream
201 * @param dst pointer to store audio parameters
202 * @return status of operation
203 */
204 status_t get_parameters(audio_parameters_t *dst) const;
205
206 /**
207 * Get current chunk identifier
208 * @return current chunk identifier
209 */
210 uint32_t unique_id() const;
211
212 /**
213 * Get current chunk magic
214 * @return current chunk magic
215 */
216 uint32_t magic() const;
217 };
218 }
219
220 } /* namespace lsp */
221
222 #endif /* LSP_PLUG_IN_FMT_LSPC_AUDIOWRITER_H_ */