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.

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