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.

Util.h 4.6KB

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