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.2KB

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