| 1 |
/* |
| 2 |
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2024 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 27 окт. 2022 г. |
| 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/fmt/lspc/InAudioStream.h> |
| 23 |
|
| 24 |
namespace lsp |
| 25 |
{ |
| 26 |
namespace lspc |
| 27 |
{ |
| 28 |
InAudioStream::InAudioStream(AudioReader *in, const mm::audio_stream_t *fmt, bool free) |
| 29 |
{ |
| 30 |
pReader = in; |
| 31 |
bDelete = free; |
| 32 |
sFormat = *fmt; |
| 33 |
nOffset = 0; |
| 34 |
} |
| 35 |
|
| 36 |
InAudioStream::~InAudioStream() |
| 37 |
{ |
| 38 |
do_close(); |
| 39 |
} |
| 40 |
|
| 41 |
status_t InAudioStream::do_close() |
| 42 |
{ |
| 43 |
nOffset = -1; |
| 44 |
if (pReader == NULL) |
| 45 |
return STATUS_OK; |
| 46 |
|
| 47 |
status_t res = pReader->close(); |
| 48 |
if (bDelete) |
| 49 |
delete pReader; |
| 50 |
pReader = NULL; |
| 51 |
return res; |
| 52 |
} |
| 53 |
|
| 54 |
status_t InAudioStream::close() |
| 55 |
{ |
| 56 |
return set_error(do_close()); |
| 57 |
} |
| 58 |
|
| 59 |
size_t InAudioStream::select_format(size_t fmt) |
| 60 |
{ |
| 61 |
return mm::SFMT_F32_CPU; |
| 62 |
} |
| 63 |
|
| 64 |
wssize_t InAudioStream::skip(wsize_t nframes) |
| 65 |
{ |
| 66 |
if (pReader == NULL) |
| 67 |
return -set_error(STATUS_CLOSED); |
| 68 |
ssize_t nskipped = pReader->skip_frames(nframes); |
| 69 |
set_error((nskipped < 0) ? status_t(-nskipped) : STATUS_OK); |
| 70 |
|
| 71 |
return nskipped; |
| 72 |
} |
| 73 |
|
| 74 |
ssize_t InAudioStream::direct_read(void *dst, size_t nframes, size_t fmt) |
| 75 |
{ |
| 76 |
if (dst == NULL) |
| 77 |
return -set_error(STATUS_BAD_ARGUMENTS); |
| 78 |
if (pReader == NULL) |
| 79 |
return -set_error(STATUS_CLOSED); |
| 80 |
if (fmt != sFormat.format) |
| 81 |
return -set_error(STATUS_BAD_STATE); |
| 82 |
|
| 83 |
ssize_t nread = pReader->read_frames(reinterpret_cast<float *>(dst), nframes); |
| 84 |
if (nread < 0) |
| 85 |
set_error(status_t(-nread)); |
| 86 |
else if (nread == 0) |
| 87 |
return -set_error(STATUS_EOF); |
| 88 |
|
| 89 |
set_error(STATUS_OK); |
| 90 |
return nread; |
| 91 |
} |
| 92 |
|
| 93 |
status_t InAudioStream::info(mm::audio_stream_t *dst) const |
| 94 |
{ |
| 95 |
if (dst == NULL) |
| 96 |
return STATUS_BAD_ARGUMENTS; |
| 97 |
*dst = sFormat; |
| 98 |
return STATUS_OK; |
| 99 |
} |
| 100 |
|
| 101 |
size_t InAudioStream::sample_rate() const |
| 102 |
{ |
| 103 |
return sFormat.srate; |
| 104 |
} |
| 105 |
|
| 106 |
size_t InAudioStream::channels() const |
| 107 |
{ |
| 108 |
return sFormat.channels; |
| 109 |
} |
| 110 |
|
| 111 |
wssize_t InAudioStream::length() const |
| 112 |
{ |
| 113 |
return sFormat.frames; |
| 114 |
} |
| 115 |
|
| 116 |
size_t InAudioStream::format() const |
| 117 |
{ |
| 118 |
return sFormat.format; |
| 119 |
} |
| 120 |
|
| 121 |
} /* namespace lspc */ |
| 122 |
} /* namespace lsp */ |
| 123 |
|
| 124 |
|