Add initial bits for Qt6 support
[carla.git] / source / modules / ysfx / sources / ysfx_api_file.hpp
blobf94e7ebb5ec8bc56c60defc8d7319ff55e3600d1
1 // Copyright 2021 Jean Pierre Cimalando
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // SPDX-License-Identifier: Apache-2.0
18 #pragma once
19 #include "ysfx.h"
20 #include "ysfx_utils.hpp"
21 #include "WDL/eel2/ns-eel.h"
22 #include "WDL/eel2/ns-eel-int.h"
23 #include <vector>
24 #include <memory>
26 struct ysfx_file_t {
27 virtual ~ysfx_file_t() {}
29 virtual int32_t avail() = 0;
30 virtual void rewind() = 0;
31 virtual bool var(ysfx_real *var) = 0;
32 virtual uint32_t mem(uint32_t offset, uint32_t length) = 0;
33 virtual uint32_t string(std::string &str) = 0;
34 virtual bool riff(uint32_t &nch, ysfx_real &samplerate) = 0;
35 virtual bool is_text() = 0;
36 virtual bool is_in_write_mode() = 0;
38 std::unique_ptr<ysfx::mutex> m_mutex{new ysfx::mutex};
41 using ysfx_file_u = std::unique_ptr<ysfx_file_t>;
43 //------------------------------------------------------------------------------
45 struct ysfx_raw_file_t final : ysfx_file_t {
46 ysfx_raw_file_t(NSEEL_VMCTX vm, const char *filename);
48 int32_t avail() override;
49 void rewind() override;
50 bool var(ysfx_real *var) override;
51 uint32_t mem(uint32_t offset, uint32_t length) override;
52 uint32_t string(std::string &str) override;
53 bool riff(uint32_t &, ysfx_real &) override { return false; }
54 bool is_text() override { return false; }
55 bool is_in_write_mode() override { return false; }
57 NSEEL_VMCTX m_vm = nullptr;
58 ysfx::FILE_u m_stream;
61 //------------------------------------------------------------------------------
63 struct ysfx_text_file_t final : ysfx_file_t {
64 ysfx_text_file_t(NSEEL_VMCTX vm, const char *filename);
66 int32_t avail() override;
67 void rewind() override;
68 bool var(ysfx_real *var) override;
69 uint32_t mem(uint32_t offset, uint32_t length) override;
70 uint32_t string(std::string &str) override;
71 bool riff(uint32_t &, ysfx_real &) override { return false; }
72 bool is_text() override { return true; }
73 bool is_in_write_mode() override { return false; }
75 NSEEL_VMCTX m_vm = nullptr;
76 ysfx::FILE_u m_stream;
77 std::string m_buf;
80 //------------------------------------------------------------------------------
82 struct ysfx_audio_file_t final : ysfx_file_t {
83 ysfx_audio_file_t(NSEEL_VMCTX vm, const ysfx_audio_format_t &fmt, const char *filename);
85 int32_t avail() override;
86 void rewind() override;
87 bool var(ysfx_real *var) override;
88 uint32_t mem(uint32_t offset, uint32_t length) override;
89 uint32_t string(std::string &str) override;
90 bool riff(uint32_t &nch, ysfx_real &samplerate) override;
91 bool is_text() override { return false; }
92 bool is_in_write_mode() override { return false; }
94 NSEEL_VMCTX m_vm = nullptr;
95 ysfx_audio_format_t m_fmt{};
96 std::unique_ptr<ysfx_audio_reader_t, void (*)(ysfx_audio_reader_t *)> m_reader;
97 enum { buffer_size = 256 };
98 std::unique_ptr<ysfx_real[]> m_buf{new ysfx_real[buffer_size]};
101 //------------------------------------------------------------------------------
103 struct ysfx_serializer_t final : ysfx_file_t {
104 explicit ysfx_serializer_t(NSEEL_VMCTX vm);
106 void begin(bool write, std::string &buffer);
107 void end();
109 int32_t avail() override;
110 void rewind() override;
111 bool var(ysfx_real *var) override;
112 uint32_t mem(uint32_t offset, uint32_t length) override;
113 uint32_t string(std::string &str) override;
114 bool riff(uint32_t &, ysfx_real &) override { return false; }
115 bool is_text() override { return false; }
116 bool is_in_write_mode() override { return m_write == 1; }
118 NSEEL_VMCTX m_vm{};
119 int m_write = -1;
120 std::string *m_buffer = nullptr;
121 size_t m_pos = 0;
124 using ysfx_serializer_u = std::unique_ptr<ysfx_serializer_t>;
126 //------------------------------------------------------------------------------
127 void ysfx_api_init_file();