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.

WatchPoint.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. WatchPoint.cpp - Synthesis State Watcher
  4. Copyright (C) 2015-2015 Mark McCurry
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of version 2 of the GNU General Public License
  7. as published by the Free Software Foundation.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License (version 2 or later) for more details.
  12. You should have received a copy of the GNU General Public License (version 2)
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "WatchPoint.h"
  17. #include <cstring>
  18. #include <rtosc/thread-link.h>
  19. namespace zyncarla {
  20. WatchPoint::WatchPoint(WatchManager *ref, const char *prefix, const char *id)
  21. :active(false), samples_left(0), reference(ref)
  22. {
  23. identity[0] = 0;
  24. if(prefix)
  25. strncpy(identity, prefix, 128);
  26. if(id)
  27. strncat(identity, id, 128);
  28. }
  29. bool WatchPoint::is_active(void)
  30. {
  31. //Either the watchpoint is already active or the watchpoint manager has
  32. //received another activation this frame
  33. if(active)
  34. return true;
  35. if(reference && reference->active(identity)) {
  36. active = true;
  37. samples_left = 1;
  38. return true;
  39. }
  40. return false;
  41. }
  42. FloatWatchPoint::FloatWatchPoint(WatchManager *ref, const char *prefix, const char *id)
  43. :WatchPoint(ref, prefix, id)
  44. {}
  45. VecWatchPoint::VecWatchPoint(WatchManager *ref, const char *prefix, const char *id)
  46. :WatchPoint(ref, prefix, id)
  47. {}
  48. WatchManager::WatchManager(thrlnk *link)
  49. :write_back(link), new_active(false)
  50. {
  51. memset(active_list, 0, sizeof(active_list));
  52. memset(sample_list, 0, sizeof(sample_list));
  53. memset(data_list, 0, sizeof(data_list));
  54. memset(deactivate, 0, sizeof(deactivate));
  55. }
  56. void WatchManager::add_watch(const char *id)
  57. {
  58. //Don't add duplicate watchs
  59. for(int i=0; i<MAX_WATCH; ++i)
  60. if(!strcmp(active_list[i], id))
  61. return;
  62. //Apply to a free slot
  63. for(int i=0; i<MAX_WATCH; ++i) {
  64. if(!active_list[i][0]) {
  65. strncpy(active_list[i], id, 128);
  66. new_active = true;
  67. sample_list[i] = 0;
  68. break;
  69. }
  70. }
  71. }
  72. void WatchManager::del_watch(const char *id)
  73. {
  74. //Queue up the delete
  75. for(int i=0; i<MAX_WATCH; ++i)
  76. if(!strcmp(active_list[i], id))
  77. return (void) (deactivate[i] = true);
  78. }
  79. void WatchManager::tick(void)
  80. {
  81. //Try to send out any vector stuff
  82. for(int i=0; i<MAX_WATCH; ++i) {
  83. if(sample_list[i]) {
  84. char arg_types[MAX_SAMPLE+1] = {0};
  85. rtosc_arg_t arg_val[MAX_SAMPLE];
  86. for(int j=0; j<sample_list[i]; ++j) {
  87. arg_types[j] = 'f';
  88. arg_val[j].f = data_list[i][j];
  89. }
  90. write_back->writeArray(active_list[i], arg_types, arg_val);
  91. deactivate[i] = true;
  92. }
  93. }
  94. //Cleanup internal data
  95. new_active = false;
  96. //Clear deleted slots
  97. for(int i=0; i<MAX_WATCH; ++i) {
  98. if(deactivate[i]) {
  99. memset(active_list[i], 0, 128);
  100. sample_list[i] = 0;
  101. deactivate[i] = false;
  102. }
  103. }
  104. }
  105. bool WatchManager::active(const char *id) const
  106. {
  107. assert(this);
  108. assert(id);
  109. if(new_active || true)
  110. for(int i=0; i<MAX_WATCH; ++i)
  111. if(!strcmp(active_list[i], id))
  112. return true;
  113. return false;
  114. }
  115. int WatchManager::samples(const char *id) const
  116. {
  117. for(int i=0; i<MAX_WATCH; ++i)
  118. if(!strcmp(active_list[i], id))
  119. return sample_list[i];
  120. return 0;
  121. }
  122. void WatchManager::satisfy(const char *id, float f)
  123. {
  124. //printf("trying to satisfy '%s'\n", id);
  125. if(write_back)
  126. write_back->write(id, "f", f);
  127. del_watch(id);
  128. }
  129. void WatchManager::satisfy(const char *id, float *f, int n)
  130. {
  131. int selected = -1;
  132. for(int i=0; i<MAX_WATCH; ++i)
  133. if(!strcmp(active_list[i], id))
  134. selected = i;
  135. if(selected == -1)
  136. return;
  137. //FIXME buffer overflow
  138. for(int i=0; i<n; ++i)
  139. data_list[selected][sample_list[selected]++] = f[i];
  140. }
  141. }