Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ysfx_api_file.hpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.
  14. //
  15. // SPDX-License-Identifier: Apache-2.0
  16. //
  17. #pragma once
  18. #include "ysfx.h"
  19. #include "ysfx_utils.hpp"
  20. #include "WDL/eel2/ns-eel.h"
  21. #include "WDL/eel2/ns-eel-int.h"
  22. #include <vector>
  23. #include <memory>
  24. struct ysfx_file_t {
  25. virtual ~ysfx_file_t() {}
  26. virtual int32_t avail() = 0;
  27. virtual void rewind() = 0;
  28. virtual bool var(ysfx_real *var) = 0;
  29. virtual uint32_t mem(uint32_t offset, uint32_t length) = 0;
  30. virtual uint32_t string(std::string &str) = 0;
  31. virtual bool riff(uint32_t &nch, ysfx_real &samplerate) = 0;
  32. virtual bool is_text() = 0;
  33. virtual bool is_in_write_mode() = 0;
  34. std::unique_ptr<ysfx::mutex> m_mutex{new ysfx::mutex};
  35. };
  36. using ysfx_file_u = std::unique_ptr<ysfx_file_t>;
  37. //------------------------------------------------------------------------------
  38. struct ysfx_raw_file_t final : ysfx_file_t {
  39. ysfx_raw_file_t(NSEEL_VMCTX vm, const char *filename);
  40. int32_t avail() override;
  41. void rewind() override;
  42. bool var(ysfx_real *var) override;
  43. uint32_t mem(uint32_t offset, uint32_t length) override;
  44. uint32_t string(std::string &str) override;
  45. bool riff(uint32_t &, ysfx_real &) override { return false; }
  46. bool is_text() override { return false; }
  47. bool is_in_write_mode() override { return false; }
  48. NSEEL_VMCTX m_vm = nullptr;
  49. ysfx::FILE_u m_stream;
  50. };
  51. //------------------------------------------------------------------------------
  52. struct ysfx_text_file_t final : ysfx_file_t {
  53. ysfx_text_file_t(NSEEL_VMCTX vm, const char *filename);
  54. int32_t avail() override;
  55. void rewind() override;
  56. bool var(ysfx_real *var) override;
  57. uint32_t mem(uint32_t offset, uint32_t length) override;
  58. uint32_t string(std::string &str) override;
  59. bool riff(uint32_t &, ysfx_real &) override { return false; }
  60. bool is_text() override { return true; }
  61. bool is_in_write_mode() override { return false; }
  62. NSEEL_VMCTX m_vm = nullptr;
  63. ysfx::FILE_u m_stream;
  64. std::string m_buf;
  65. };
  66. //------------------------------------------------------------------------------
  67. struct ysfx_audio_file_t final : ysfx_file_t {
  68. ysfx_audio_file_t(NSEEL_VMCTX vm, const ysfx_audio_format_t &fmt, const char *filename);
  69. int32_t avail() override;
  70. void rewind() override;
  71. bool var(ysfx_real *var) override;
  72. uint32_t mem(uint32_t offset, uint32_t length) override;
  73. uint32_t string(std::string &str) override;
  74. bool riff(uint32_t &nch, ysfx_real &samplerate) override;
  75. bool is_text() override { return false; }
  76. bool is_in_write_mode() override { return false; }
  77. NSEEL_VMCTX m_vm = nullptr;
  78. ysfx_audio_format_t m_fmt{};
  79. std::unique_ptr<ysfx_audio_reader_t, void (*)(ysfx_audio_reader_t *)> m_reader;
  80. enum { buffer_size = 256 };
  81. std::unique_ptr<ysfx_real[]> m_buf{new ysfx_real[buffer_size]};
  82. };
  83. //------------------------------------------------------------------------------
  84. struct ysfx_serializer_t final : ysfx_file_t {
  85. explicit ysfx_serializer_t(NSEEL_VMCTX vm);
  86. void begin(bool write, std::string &buffer);
  87. void end();
  88. int32_t avail() override;
  89. void rewind() override;
  90. bool var(ysfx_real *var) override;
  91. uint32_t mem(uint32_t offset, uint32_t length) override;
  92. uint32_t string(std::string &str) override;
  93. bool riff(uint32_t &, ysfx_real &) override { return false; }
  94. bool is_text() override { return false; }
  95. bool is_in_write_mode() override { return m_write == 1; }
  96. NSEEL_VMCTX m_vm{};
  97. int m_write = -1;
  98. std::string *m_buffer = nullptr;
  99. size_t m_pos = 0;
  100. };
  101. using ysfx_serializer_u = std::unique_ptr<ysfx_serializer_t>;
  102. //------------------------------------------------------------------------------
  103. void ysfx_api_init_file();