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.

185 lines
4.3KB

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