| 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 |
#include <lsp-plug.in/mm/IOutAudioStream.h> |
| 23 |
#include <lsp-plug.in/mm/sample.h> |
| 24 |
#include <lsp-plug.in/common/alloc.h> |
| 25 |
#include <lsp-plug.in/stdlib/string.h> |
| 26 |
#include <stdlib.h> |
| 27 |
|
| 28 |
namespace lsp |
| 29 |
{ |
| 30 |
namespace mm |
| 31 |
{ |
| 32 |
|
| 33 |
IOutAudioStream::IOutAudioStream() |
| 34 |
{ |
| 35 |
nOffset = -1; |
| 36 |
pBuffer = NULL; |
| 37 |
nBufSize = 0; |
| 38 |
nErrorCode = STATUS_CLOSED; |
| 39 |
sFormat.srate = 0; |
| 40 |
sFormat.channels = 0; |
| 41 |
sFormat.frames = -1; |
| 42 |
sFormat.format = SFMT_NONE; |
| 43 |
pUserData = NULL; |
| 44 |
pDeleter = NULL; |
| 45 |
} |
| 46 |
|
| 47 |
IOutAudioStream::~IOutAudioStream() |
| 48 |
{ |
| 49 |
do_close(); |
| 50 |
set_user_data(NULL); |
| 51 |
} |
| 52 |
|
| 53 |
void IOutAudioStream::set_user_data(void *data, user_data_deleter_t deleter) |
| 54 |
{ |
| 55 |
if (pDeleter != NULL) |
| 56 |
pDeleter(pUserData); |
| 57 |
pUserData = data; |
| 58 |
pDeleter = deleter; |
| 59 |
} |
| 60 |
|
| 61 |
void IOutAudioStream::do_close() |
| 62 |
{ |
| 63 |
if (pBuffer != NULL) |
| 64 |
{ |
| 65 |
::free(pBuffer); |
| 66 |
pBuffer = NULL; |
| 67 |
} |
| 68 |
nOffset = -1; |
| 69 |
} |
| 70 |
|
| 71 |
status_t IOutAudioStream::ensure_capacity(size_t bytes) |
| 72 |
{ |
| 73 |
// Not enough space in temporary buffer? |
| 74 |
if (nBufSize >= bytes) |
| 75 |
return true; |
| 76 |
|
| 77 |
// Perform buffer re-allocation |
| 78 |
bytes = align_size(bytes, 0x200); |
| 79 |
uint8_t *buf = static_cast<uint8_t *>(::realloc(pBuffer, bytes)); |
| 80 |
if (buf == NULL) |
| 81 |
return false; |
| 82 |
pBuffer = buf; |
| 83 |
nBufSize = bytes; |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
ssize_t IOutAudioStream::direct_write(const void *src, size_t nframes, size_t fmt) |
| 89 |
{ |
| 90 |
return -STATUS_NOT_IMPLEMENTED; |
| 91 |
} |
| 92 |
|
| 93 |
ssize_t IOutAudioStream::conv_write(const void *src, size_t nframes, size_t fmt) |
| 94 |
{ |
| 95 |
if (nOffset < 0) |
| 96 |
return -set_error(STATUS_CLOSED); |
| 97 |
|
| 98 |
size_t rsize = sformat_size_of(fmt) * sFormat.channels; |
| 99 |
if (rsize <= 0) |
| 100 |
return -set_error(STATUS_BAD_FORMAT); |
| 101 |
|
| 102 |
size_t afmt = select_format(fmt); |
| 103 |
size_t fsize = sformat_size_of(afmt) * sFormat.channels; |
| 104 |
if (fsize <= 0) |
| 105 |
return -set_error(STATUS_UNSUPPORTED_FORMAT); |
| 106 |
|
| 107 |
// Perform conversion loop |
| 108 |
const uint8_t *sptr = static_cast<const uint8_t *>(src); |
| 109 |
size_t nwritten = 0; |
| 110 |
|
| 111 |
while (nframes > 0) |
| 112 |
{ |
| 113 |
size_t to_write = (nframes > IO_BUF_SIZE) ? IO_BUF_SIZE : nframes; |
| 114 |
|
| 115 |
// Need to perform encoding? |
| 116 |
if (afmt != fmt) |
| 117 |
{ |
| 118 |
// Check that we have enough place |
| 119 |
size_t atotal = to_write * fsize; |
| 120 |
size_t rtotal = to_write * rsize; |
| 121 |
if (!ensure_capacity(to_write * (fsize + rsize))) |
| 122 |
return -set_error(STATUS_NO_MEM); |
| 123 |
|
| 124 |
// Perform conversion |
| 125 |
::memcpy(&pBuffer[atotal], src, rtotal); |
| 126 |
if (!convert_samples(pBuffer, &pBuffer[atotal], to_write * sFormat.channels, afmt, fmt)) |
| 127 |
return -set_error(STATUS_UNSUPPORTED_FORMAT); |
| 128 |
src = pBuffer; |
| 129 |
} |
| 130 |
else |
| 131 |
src = sptr; |
| 132 |
|
| 133 |
// Call direct write |
| 134 |
ssize_t written = direct_write(src, to_write, afmt); |
| 135 |
if (written < 0) |
| 136 |
{ |
| 137 |
if (nwritten > 0) |
| 138 |
break; |
| 139 |
set_error(-status_t(written)); |
| 140 |
return written; |
| 141 |
} |
| 142 |
|
| 143 |
// Update pointers |
| 144 |
nwritten += written; |
| 145 |
nframes -= written; |
| 146 |
sptr += fsize * written; |
| 147 |
} |
| 148 |
|
| 149 |
set_error(STATUS_OK); |
| 150 |
nOffset += nwritten; |
| 151 |
return nwritten; |
| 152 |
} |
| 153 |
|
| 154 |
size_t IOutAudioStream::select_format(size_t rfmt) |
| 155 |
{ |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
status_t IOutAudioStream::info(audio_stream_t *dst) const |
| 160 |
{ |
| 161 |
if (dst == NULL) |
| 162 |
return STATUS_BAD_ARGUMENTS; |
| 163 |
*dst = sFormat; |
| 164 |
return STATUS_OK; |
| 165 |
} |
| 166 |
|
| 167 |
status_t IOutAudioStream::flush() |
| 168 |
{ |
| 169 |
return set_error(STATUS_OK); |
| 170 |
} |
| 171 |
|
| 172 |
status_t IOutAudioStream::close() |
| 173 |
{ |
| 174 |
do_close(); |
| 175 |
return set_error(STATUS_OK); |
| 176 |
} |
| 177 |
|
| 178 |
wssize_t IOutAudioStream::position() |
| 179 |
{ |
| 180 |
return (nOffset >= 0) ? nOffset : -set_error(STATUS_CLOSED); |
| 181 |
} |
| 182 |
|
| 183 |
wssize_t IOutAudioStream::seek(wsize_t nframes) |
| 184 |
{ |
| 185 |
return -set_error(STATUS_NOT_SUPPORTED); |
| 186 |
} |
| 187 |
|
| 188 |
ssize_t IOutAudioStream::write(const uint8_t *dst, size_t nframes) |
| 189 |
{ |
| 190 |
return conv_write(dst, nframes, SFMT_U8 | SFMT_CPU); |
| 191 |
} |
| 192 |
|
| 193 |
ssize_t IOutAudioStream::write(const int8_t *dst, size_t nframes) |
| 194 |
{ |
| 195 |
return conv_write(dst, nframes, SFMT_S8 | SFMT_CPU); |
| 196 |
} |
| 197 |
|
| 198 |
ssize_t IOutAudioStream::write(const uint16_t *dst, size_t nframes) |
| 199 |
{ |
| 200 |
return conv_write(dst, nframes, SFMT_U16 | SFMT_CPU); |
| 201 |
} |
| 202 |
|
| 203 |
ssize_t IOutAudioStream::write(const int16_t *dst, size_t nframes) |
| 204 |
{ |
| 205 |
return conv_write(dst, nframes, SFMT_S16 | SFMT_CPU); |
| 206 |
} |
| 207 |
|
| 208 |
ssize_t IOutAudioStream::write(const uint32_t *dst, size_t nframes) |
| 209 |
{ |
| 210 |
return conv_write(dst, nframes, SFMT_U32 | SFMT_CPU); |
| 211 |
} |
| 212 |
|
| 213 |
ssize_t IOutAudioStream::write(const int32_t *dst, size_t nframes) |
| 214 |
{ |
| 215 |
return conv_write(dst, nframes, SFMT_S32 | SFMT_CPU); |
| 216 |
} |
| 217 |
|
| 218 |
ssize_t IOutAudioStream::write(const f32_t *dst, size_t nframes) |
| 219 |
{ |
| 220 |
return conv_write(dst, nframes, SFMT_F32 | SFMT_CPU); |
| 221 |
} |
| 222 |
|
| 223 |
ssize_t IOutAudioStream::write(const f64_t *dst, size_t nframes) |
| 224 |
{ |
| 225 |
return conv_write(dst, nframes, SFMT_F64 | SFMT_CPU); |
| 226 |
} |
| 227 |
} /* namespace mm */ |
| 228 |
} /* namespace lsp */ |