Debian Codegraph @

modules/lsp-runtime-lib/src/main/mm/IInAudioStream.cpp

raw
1 /*
2 * Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2023 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 #include <lsp-plug.in/mm/IInAudioStream.h>
23 #include <lsp-plug.in/mm/sample.h>
24 #include <lsp-plug.in/common/alloc.h>
25 #include <stdlib.h>
26
27 namespace lsp
28 {
29 namespace mm
30 {
31 IInAudioStream::IInAudioStream()
32 {
33 nOffset = -1;
34 pBuffer = NULL;
35 nBufSize = 0;
36 nErrorCode = STATUS_CLOSED;
37 pUserData = NULL;
38 pDeleter = NULL;
39 }
40
41 IInAudioStream::~IInAudioStream()
42 {
43 do_close();
44 set_user_data(NULL);
45 }
46
47 void IInAudioStream::set_user_data(void *data, user_data_deleter_t deleter)
48 {
49 if (pDeleter != NULL)
50 pDeleter(pUserData);
51 pUserData = data;
52 pDeleter = deleter;
53 }
54
55 void IInAudioStream::do_close()
56 {
57 // Free allocated buffer if present
58 if (pBuffer != NULL)
59 {
60 ::free(pBuffer);
61 pBuffer = NULL;
62 }
63
64 nOffset = -1; // Mark as closed
65 }
66
67 status_t IInAudioStream::close()
68 {
69 do_close();
70 return set_error(STATUS_OK);
71 }
72
73 status_t IInAudioStream::info(audio_stream_t *dst) const
74 {
75 if (dst == NULL)
76 return STATUS_BAD_ARGUMENTS;
77
78 dst->srate = sample_rate();
79 dst->channels = channels();
80 dst->frames = length();
81 dst->format = format();
82
83 return STATUS_OK;
84 }
85
86 size_t IInAudioStream::sample_rate() const
87 {
88 return 0;
89 }
90
91 size_t IInAudioStream::channels() const
92 {
93 return 0;
94 }
95
96 wssize_t IInAudioStream::length() const
97 {
98 return -1;
99 }
100
101 size_t IInAudioStream::format() const
102 {
103 return SFMT_NONE;
104 }
105
106 wssize_t IInAudioStream::skip(wsize_t nframes)
107 {
108 if (nframes == 0)
109 {
110 set_error(STATUS_OK);
111 return 0;
112 }
113
114 // Select format and check frame size
115 size_t afmt = select_format(0);
116 size_t asize = sformat_size_of(afmt) * channels();
117 if (asize <= 0)
118 return -set_error(STATUS_UNSUPPORTED_FORMAT);
119
120 // Perform direct read
121 wssize_t nread = 0;
122 do
123 {
124 // Ensure capacity
125 size_t to_read = (nframes > IO_BUF_SIZE) ? IO_BUF_SIZE : nframes;
126 if (!ensure_capacity(to_read * asize))
127 return -set_error(STATUS_NO_MEM);
128
129 // Perform read
130 ssize_t read = direct_read(pBuffer, to_read, 0);
131 if (read < 0)
132 {
133 if (nread > 0)
134 break;
135 set_error(-status_t(nread));
136 return nread;
137 }
138
139 // Update position
140 nframes -= read;
141 nread += read;
142 } while (nframes > 0);
143
144 // Update statistics
145 set_error(STATUS_OK);
146 nOffset += nread;
147 return nread;
148 }
149
150 wssize_t IInAudioStream::position()
151 {
152 return (nOffset >= 0) ? nOffset : -set_error(STATUS_CLOSED);
153 }
154
155 wssize_t IInAudioStream::seek(wsize_t offset)
156 {
157 if (nOffset < 0)
158 return -set_error(STATUS_CLOSED);
159 if (nOffset > wssize_t(offset))
160 return -set_error(STATUS_NOT_SUPPORTED);
161
162 return skip(offset - nOffset);
163 }
164
165 ssize_t IInAudioStream::direct_read(void *dst, size_t nframes, size_t fmt)
166 {
167 return -STATUS_NOT_IMPLEMENTED;
168 }
169
170 size_t IInAudioStream::select_format(size_t fmt)
171 {
172 return 0;
173 }
174
175 bool IInAudioStream::ensure_capacity(size_t bytes)
176 {
177 // Not enough space in temporary buffer?
178 if (nBufSize >= bytes)
179 return true;
180
181 // Perform buffer re-allocation
182 bytes = align_size(bytes, 0x200);
183 uint8_t *buf = static_cast<uint8_t *>(::realloc(pBuffer, bytes));
184 if (buf == NULL)
185 return false;
186 pBuffer = buf;
187 nBufSize = bytes;
188
189 return true;
190 }
191
192 ssize_t IInAudioStream::conv_read(void *dst, size_t nframes, size_t fmt)
193 {
194 if (nOffset < 0)
195 return -set_error(STATUS_CLOSED);
196
197 // Prepare pointers and remember frame size
198 size_t nchan = channels();
199 size_t fsize = sformat_size_of(fmt) * nchan;
200 if (fsize <= 0)
201 return -set_error(STATUS_BAD_FORMAT);
202
203 size_t afmt = select_format(fmt);
204 size_t asize = sformat_size_of(afmt) * nchan;
205 if (asize <= 0)
206 return -set_error(STATUS_UNSUPPORTED_FORMAT);
207
208 uint8_t *dptr = static_cast<uint8_t *>(dst);
209 size_t nread = 0;
210
211 // Perform direct read
212 if (fmt != afmt)
213 {
214 while (nframes > 0)
215 {
216 // Ensure capacity
217 size_t to_read = lsp_min(nframes, IO_BUF_SIZE);
218 if (!ensure_capacity(to_read * asize))
219 return -set_error(STATUS_NO_MEM);
220
221 // Perform direct read
222 ssize_t read = direct_read(pBuffer, to_read, afmt);
223
224 // Analyze read status
225 if (read < 0)
226 {
227 if (nread > 0)
228 break;
229 set_error(-status_t(read));
230 return read;
231 }
232
233 // Data is stored in pBuffer which can be updated, perform sample conversion
234 if (!convert_samples(dptr, pBuffer, read * nchan, fmt, afmt))
235 return -set_error(STATUS_UNSUPPORTED_FORMAT);
236
237 // Update position and pointers
238 nframes -= read;
239 nread += read;
240 dptr += fsize * read;
241 }
242 }
243 else
244 {
245 while (nframes > 0)
246 {
247 // Select number of frames and perform read
248 size_t to_read = lsp_min(nframes, IO_BUF_SIZE);
249 ssize_t read = direct_read(dptr, to_read, afmt);
250
251 // Analyze read status
252 if (read < 0)
253 {
254 if (nread > 0)
255 break;
256 set_error(-status_t(read));
257 return read;
258 }
259
260 // Update position and pointers
261 nframes -= read;
262 nread += read;
263 dptr += fsize * read;
264 }
265 }
266
267 // Update statistics
268 set_error(STATUS_OK);
269 nOffset += nread;
270 return nread;
271 }
272
273 ssize_t IInAudioStream::read(uint8_t *dst, size_t nframes)
274 {
275 return conv_read(dst, nframes, SFMT_U8 | SFMT_CPU);
276 }
277
278 ssize_t IInAudioStream::read(int8_t *dst, size_t nframes)
279 {
280 return conv_read(dst, nframes, SFMT_S8 | SFMT_CPU);
281 }
282
283 ssize_t IInAudioStream::read(uint16_t *dst, size_t nframes)
284 {
285 return conv_read(dst, nframes, SFMT_U16 | SFMT_CPU);
286 }
287
288 ssize_t IInAudioStream::read(int16_t *dst, size_t nframes)
289 {
290 return conv_read(dst, nframes, SFMT_S16 | SFMT_CPU);
291 }
292
293 ssize_t IInAudioStream::read(uint32_t *dst, size_t nframes)
294 {
295 return conv_read(dst, nframes, SFMT_U32 | SFMT_CPU);
296 }
297
298 ssize_t IInAudioStream::read(int32_t *dst, size_t nframes)
299 {
300 return conv_read(dst, nframes, SFMT_S32 | SFMT_CPU);
301 }
302
303 ssize_t IInAudioStream::read(f32_t *dst, size_t nframes)
304 {
305 return conv_read(dst, nframes, SFMT_F32 | SFMT_CPU);
306 }
307
308 ssize_t IInAudioStream::read(f64_t *dst, size_t nframes)
309 {
310 return conv_read(dst, nframes, SFMT_F64 | SFMT_CPU);
311 }
312
313 } /* namespace mm */
314 } /* namespace lsp */