|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 | + * (C) 2022 Vladimir Sadovnikov <[email protected]> |
| 4 | + * |
| 5 | + * This file is part of lsp-plugin-fw |
| 6 | + * Created on: 18 дек. 2022 г. |
| 7 | + * |
| 8 | + * lsp-plugin-fw 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-plugin-fw 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-plugin-fw. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +#ifndef LSP_PLUG_IN_PLUG_FW_CORE_SAMPLEPLAYER_H_ |
| 23 | +#define LSP_PLUG_IN_PLUG_FW_CORE_SAMPLEPLAYER_H_ |
| 24 | + |
| 25 | +#include <lsp-plug.in/plug-fw/version.h> |
| 26 | + |
| 27 | +#include <lsp-plug.in/dsp-units/sampling/SamplePlayer.h> |
| 28 | +#include <lsp-plug.in/ipc/ITask.h> |
| 29 | +#include <lsp-plug.in/plug-fw/plug.h> |
| 30 | + |
| 31 | + |
| 32 | +namespace lsp |
| 33 | +{ |
| 34 | + namespace core |
| 35 | + { |
| 36 | + /** |
| 37 | + * Sample player class for playing audio files on demand |
| 38 | + */ |
| 39 | + class SamplePlayer |
| 40 | + { |
| 41 | + private: |
| 42 | + SamplePlayer & operator = (const SamplePlayer &); |
| 43 | + |
| 44 | + protected: |
| 45 | + class LoadTask: public ipc::ITask |
| 46 | + { |
| 47 | + private: |
| 48 | + SamplePlayer *pCore; |
| 49 | + |
| 50 | + public: |
| 51 | + explicit LoadTask(SamplePlayer *core); |
| 52 | + virtual ~LoadTask(); |
| 53 | + |
| 54 | + public: |
| 55 | + virtual status_t run(); |
| 56 | + }; |
| 57 | + |
| 58 | + class GCTask: public ipc::ITask |
| 59 | + { |
| 60 | + private: |
| 61 | + SamplePlayer *pCore; |
| 62 | + |
| 63 | + public: |
| 64 | + explicit GCTask(SamplePlayer *core); |
| 65 | + virtual ~GCTask(); |
| 66 | + |
| 67 | + public: |
| 68 | + virtual status_t run(); |
| 69 | + }; |
| 70 | + |
| 71 | + private: |
| 72 | + const meta::plugin_t *pMetadata; |
| 73 | + plug::IWrapper *pWrapper; |
| 74 | + LoadTask sLoadTask; |
| 75 | + GCTask sGCTask; |
| 76 | + |
| 77 | + dspu::SamplePlayer vPlayers[2]; |
| 78 | + dspu::Playback vPlaybacks[2]; |
| 79 | + plug::IPort *pOut[2]; |
| 80 | + size_t nSampleRate; |
| 81 | + |
| 82 | + dspu::Sample *pLoaded; // Loaded sample |
| 83 | + dspu::Sample *pGCList; // Garbage collection |
| 84 | + |
| 85 | + wssize_t nPlayPosition; // Playback position |
| 86 | + wssize_t nFileLength; // Length of the file |
| 87 | + |
| 88 | + char sFileName[PATH_MAX]; // Actual file name |
| 89 | + char sReqFileName[PATH_MAX]; // Requested file name |
| 90 | + wsize_t nReqPosition; // Requested playback position |
| 91 | + bool bReqRelease; // Release request |
| 92 | + size_t nUpdateReq; // Update request counter |
| 93 | + size_t nUpdateResp; // Update response counter |
| 94 | + |
| 95 | + protected: |
| 96 | + static void destroy_sample(dspu::Sample * &sample); |
| 97 | + static void destroy_samples(dspu::Sample *gc_list); |
| 98 | + |
| 99 | + static plug::IPort *find_out_port(const char *id, plug::IPort **ports, size_t count); |
| 100 | + |
| 101 | + protected: |
| 102 | + void connect_outputs(plug::IPort **ports, size_t count); |
| 103 | + status_t load_sample(); |
| 104 | + status_t perform_gc(); |
| 105 | + void play_current_sample(wsize_t position); |
| 106 | + void process_async_requests(); |
| 107 | + void process_gc_tasks(); |
| 108 | + void process_playback(size_t samples); |
| 109 | + |
| 110 | + public: |
| 111 | + explicit SamplePlayer(const meta::plugin_t *meta); |
| 112 | + ~SamplePlayer(); |
| 113 | + |
| 114 | + void init(plug::IWrapper *wrapper, plug::IPort **ports, size_t count); |
| 115 | + void destroy(); |
| 116 | + |
| 117 | + public: |
| 118 | + void set_sample_rate(size_t sample_rate); |
| 119 | + void process(size_t samples); |
| 120 | + void play_sample(const char *file, wsize_t position, bool release); |
| 121 | + void play_sample(wsize_t position, bool release); |
| 122 | + |
| 123 | + inline wssize_t position() const { return nPlayPosition; } |
| 124 | + inline wssize_t sample_length() const { return nFileLength; } |
| 125 | + inline char *requested_file_name() { return sReqFileName; } |
| 126 | + }; |
| 127 | + |
| 128 | + } /* namespace core */ |
| 129 | +} /* namespace lsp */ |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +#endif /* LSP_PLUG_IN_PLUG_FW_CORE_SAMPLEPLAYER_H_ */ |
0 commit comments