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_eel.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "ysfx.hpp"
  18. #include "ysfx_api_eel.hpp"
  19. #include "ysfx_utils.hpp"
  20. #include <cstring>
  21. #include <cstdlib>
  22. #include <cstddef>
  23. #include "WDL/ptrlist.h"
  24. #include "WDL/assocarray.h"
  25. #include "WDL/mutex.h"
  26. #ifndef EELSCRIPT_NO_STDIO
  27. # define EEL_STRING_STDOUT_WRITE(x,len) { fwrite(x,len,1,stdout); fflush(stdout); }
  28. #endif
  29. //TODO: thread-safety considerations with strings
  30. #define EEL_STRING_MAXUSERSTRING_LENGTH_HINT ysfx_string_max_length
  31. static ysfx::mutex atomic_mutex;
  32. #define EEL_ATOMIC_SET_SCOPE(opaque) ysfx::mutex *mutex = ((opaque) ? &((ysfx_t *)(opaque))->atomic_mutex : &atomic_mutex);
  33. #define EEL_ATOMIC_ENTER mutex->lock()
  34. #define EEL_ATOMIC_LEAVE mutex->unlock()
  35. #include "WDL/eel2/eel_strings.h"
  36. #include "WDL/eel2/eel_misc.h"
  37. #include "WDL/eel2/eel_fft.h"
  38. #include "WDL/eel2/eel_mdct.h"
  39. #include "WDL/eel2/eel_atomic.h"
  40. //------------------------------------------------------------------------------
  41. void ysfx_api_init_eel()
  42. {
  43. EEL_string_register();
  44. EEL_fft_register();
  45. EEL_mdct_register();
  46. EEL_string_register();
  47. EEL_misc_register();
  48. EEL_atomic_register();
  49. }
  50. //------------------------------------------------------------------------------
  51. void ysfx_eel_string_initvm(NSEEL_VMCTX vm)
  52. {
  53. eel_string_initvm(vm);
  54. }
  55. //------------------------------------------------------------------------------
  56. eel_string_context_state *ysfx_eel_string_context_new()
  57. {
  58. return new eel_string_context_state;
  59. }
  60. void ysfx_eel_string_context_free(eel_string_context_state *state)
  61. {
  62. delete state;
  63. }
  64. void ysfx_eel_string_context_update_named_vars(eel_string_context_state *state, NSEEL_VMCTX vm)
  65. {
  66. state->update_named_vars(vm);
  67. }
  68. //------------------------------------------------------------------------------
  69. static_assert(
  70. ysfx_string_max_length == EEL_STRING_MAXUSERSTRING_LENGTH_HINT,
  71. "string max lengths do not match");
  72. bool ysfx_string_access(ysfx_t *fx, ysfx_real id, bool for_write, void (*access)(void *, WDL_FastString &), void *userdata)
  73. {
  74. void *opaque = fx;
  75. eel_string_context_state *ctx = EEL_STRING_GET_CONTEXT_POINTER(opaque);
  76. EEL_STRING_MUTEXLOCK_SCOPE
  77. EEL_STRING_STORAGECLASS *wr = nullptr;
  78. ctx->GetStringForIndex(id, &wr, for_write);
  79. if (!wr)
  80. return false;
  81. access(userdata, *wr);
  82. return true;
  83. }
  84. bool ysfx_string_get(ysfx_t *fx, ysfx_real id, std::string &txt)
  85. {
  86. return ysfx_string_access(fx, id, false, [](void *ud, WDL_FastString &str) {
  87. ((std::string *)ud)->assign(str.Get(), (uint32_t)str.GetLength());
  88. }, &txt);
  89. }
  90. bool ysfx_string_set(ysfx_t *fx, ysfx_real id, const std::string &txt)
  91. {
  92. return ysfx_string_access(fx, id, true, [](void *ud, WDL_FastString &str) {
  93. const std::string *txt = (const std::string *)ud;
  94. size_t size = txt->size();
  95. if (size > ysfx_string_max_length)
  96. size = ysfx_string_max_length;
  97. str.SetRaw(txt->data(), (int)size);
  98. }, (void *)&txt);
  99. }
  100. void ysfx_string_lock(ysfx_t *fx)
  101. {
  102. fx->string_mutex.lock();
  103. }
  104. void ysfx_string_unlock(ysfx_t *fx)
  105. {
  106. fx->string_mutex.unlock();
  107. }
  108. const char *ysfx_string_access_unlocked(ysfx_t *fx, ysfx_real id, WDL_FastString **fs, bool for_write)
  109. {
  110. return fx->string_ctx->GetStringForIndex(id, fs, for_write);
  111. }
  112. //------------------------------------------------------------------------------
  113. // NOTE(jpc) implement this? I guess probably not.
  114. // DSP and UI should not mutex each other.
  115. void NSEEL_HOSTSTUB_EnterMutex()
  116. {
  117. }
  118. void NSEEL_HOSTSTUB_LeaveMutex()
  119. {
  120. }