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.

192 lines
4.8KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Util.h - Miscellaneous functions
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #ifndef UTIL_H
  18. #define UTIL_H
  19. #include <string>
  20. #include <sstream>
  21. #include <stdint.h>
  22. #include <algorithm>
  23. #include <set>
  24. #include "Config.h"
  25. #include "../globals.h"
  26. using std::min;
  27. using std::max;
  28. //Velocity Sensing function
  29. extern float VelF(float velocity, unsigned char scaling);
  30. bool fileexists(const char *filename);
  31. #define N_DETUNE_TYPES 4 //the number of detune types
  32. extern float getdetune(unsigned char type,
  33. unsigned short int coarsedetune,
  34. unsigned short int finedetune);
  35. /**Try to set current thread to realtime priority program priority
  36. * \todo see if the right pid is being sent
  37. * \todo see if this is having desired effect, if not then look at
  38. * pthread_attr_t*/
  39. void set_realtime();
  40. /**Os independent sleep in microsecond*/
  41. void os_sleep(long length);
  42. //! returns pid padded to maximum pid lenght, posix conform
  43. std::string os_pid_as_padded_string();
  44. std::string legalizeFilename(std::string filename);
  45. extern float *denormalkillbuf; /**<the buffer to add noise in order to avoid denormalisation*/
  46. extern class Config config;
  47. void invSignal(float *sig, size_t len);
  48. template<class T>
  49. std::string stringFrom(T x)
  50. {
  51. std::stringstream ss;
  52. ss << x;
  53. return ss.str();
  54. }
  55. template<class T>
  56. std::string to_s(T x)
  57. {
  58. return stringFrom(x);
  59. }
  60. template<class T>
  61. T stringTo(const char *x)
  62. {
  63. std::string str = x != NULL ? x : "0"; //should work for the basic float/int
  64. std::stringstream ss(str);
  65. T ans;
  66. ss >> ans;
  67. return ans;
  68. }
  69. template<class T>
  70. T limit(T val, T min, T max)
  71. {
  72. return val < min ? min : (val > max ? max : val);
  73. }
  74. template<class T>
  75. bool inRange(T val, T min, T max)
  76. {
  77. return val >= min && val <= max;
  78. }
  79. template<class T>
  80. T array_max(const T *data, size_t len)
  81. {
  82. T max = 0;
  83. for(unsigned i = 0; i < len; ++i)
  84. if(max < data[i])
  85. max = data[i];
  86. return max;
  87. }
  88. //Random number generator
  89. typedef uint32_t prng_t;
  90. extern prng_t prng_state;
  91. // Portable Pseudo-Random Number Generator
  92. inline prng_t prng_r(prng_t &p)
  93. {
  94. return p = p * 1103515245 + 12345;
  95. }
  96. inline prng_t prng(void)
  97. {
  98. return prng_r(prng_state) & 0x7fffffff;
  99. }
  100. inline void sprng(prng_t p)
  101. {
  102. prng_state = p;
  103. }
  104. /*
  105. * The random generator (0.0f..1.0f)
  106. */
  107. #ifndef INT32_MAX
  108. #define INT32_MAX (2147483647)
  109. #endif
  110. #define RND (prng() / (INT32_MAX * 1.0f))
  111. //Linear Interpolation
  112. float interpolate(const float *data, size_t len, float pos);
  113. //Linear circular interpolation
  114. float cinterpolate(const float *data, size_t len, float pos);
  115. template<class T>
  116. static inline void nullify(T &t) {delete t; t = NULL; }
  117. template<class T>
  118. static inline void arrayNullify(T &t) {delete [] t; t = NULL; }
  119. char *rtosc_splat(const char *path, std::set<std::string>);
  120. /**
  121. * Port macros - these produce easy and regular port definitions for common
  122. * types
  123. */
  124. #define rParamZyn(name, ...) \
  125. {STRINGIFY(name) "::i", rProp(parameter) rMap(min, 0) rMap(max, 127) DOC(__VA_ARGS__), NULL, rParamICb(name)}
  126. #define rSelf(type) \
  127. {"self", rProp(internal) rMap(class, type) rDoc("port metadata"), 0, \
  128. [](const char *, rtosc::RtData &d){ \
  129. d.reply(d.loc, "b", sizeof(d.obj), &d.obj);}}\
  130. #define rPaste \
  131. {"preset-type", rProp(internal), 0, \
  132. [](const char *, rtosc::RtData &d){ \
  133. rObject *obj = (rObject*)d.obj; \
  134. d.reply(d.loc, "s", obj->type);}},\
  135. {"paste:b", rProp(internal) rDoc("paste port"), 0, \
  136. [](const char *m, rtosc::RtData &d){ \
  137. printf("rPaste...\n"); \
  138. rObject &paste = **(rObject **)rtosc_argument(m,0).b.data; \
  139. rObject &o = *(rObject*)d.obj;\
  140. o.paste(paste);}}
  141. #define rArrayPaste \
  142. {"paste-array:bi", rProp(internal) rDoc("array paste port"), 0, \
  143. [](const char *m, rtosc::RtData &d){ \
  144. printf("rArrayPaste...\n"); \
  145. rObject &paste = **(rObject **)rtosc_argument(m,0).b.data; \
  146. int field = rtosc_argument(m,1).i; \
  147. rObject &o = *(rObject*)d.obj;\
  148. o.pasteArray(paste,field);}}
  149. #endif