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.cpp 6.0KB

10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Util.cpp - 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. #include "globals.h"
  18. #include "Util.h"
  19. #include <vector>
  20. #include <cassert>
  21. #include <cmath>
  22. #include <cstdio>
  23. #include <fstream>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #ifdef HAVE_SCHEDULER
  31. #include <sched.h>
  32. #endif
  33. #ifndef errx
  34. #include <err.h>
  35. #endif
  36. #include <rtosc/rtosc.h>
  37. prng_t prng_state = 0x1234;
  38. /*
  39. * Transform the velocity according the scaling parameter (velocity sensing)
  40. */
  41. float VelF(float velocity, unsigned char scaling)
  42. {
  43. float x;
  44. x = powf(VELOCITY_MAX_SCALE, (64.0f - scaling) / 64.0f);
  45. if((scaling == 127) || (velocity > 0.99f))
  46. return 1.0f;
  47. else
  48. return powf(velocity, x);
  49. }
  50. /*
  51. * Get the detune in cents
  52. */
  53. float getdetune(unsigned char type,
  54. unsigned short int coarsedetune,
  55. unsigned short int finedetune)
  56. {
  57. float det = 0.0f, octdet = 0.0f, cdet = 0.0f, findet = 0.0f;
  58. //Get Octave
  59. int octave = coarsedetune / 1024;
  60. if(octave >= 8)
  61. octave -= 16;
  62. octdet = octave * 1200.0f;
  63. //Coarse and fine detune
  64. int cdetune = coarsedetune % 1024;
  65. if(cdetune > 512)
  66. cdetune -= 1024;
  67. int fdetune = finedetune - 8192;
  68. switch(type) {
  69. // case 1: is used for the default (see below)
  70. case 2:
  71. cdet = fabs(cdetune * 10.0f);
  72. findet = fabs(fdetune / 8192.0f) * 10.0f;
  73. break;
  74. case 3:
  75. cdet = fabs(cdetune * 100.0f);
  76. findet = powf(10, fabs(fdetune / 8192.0f) * 3.0f) / 10.0f - 0.1f;
  77. break;
  78. case 4:
  79. cdet = fabs(cdetune * 701.95500087f); //perfect fifth
  80. findet =
  81. (powf(2, fabs(fdetune / 8192.0f) * 12.0f) - 1.0f) / 4095 * 1200;
  82. break;
  83. //case ...: need to update N_DETUNE_TYPES, if you'll add more
  84. default:
  85. cdet = fabs(cdetune * 50.0f);
  86. findet = fabs(fdetune / 8192.0f) * 35.0f; //almost like "Paul's Sound Designer 2"
  87. break;
  88. }
  89. if(finedetune < 8192)
  90. findet = -findet;
  91. if(cdetune < 0)
  92. cdet = -cdet;
  93. det = octdet + cdet + findet;
  94. return det;
  95. }
  96. bool fileexists(const char *filename)
  97. {
  98. struct stat tmp;
  99. int result = stat(filename, &tmp);
  100. if(result >= 0)
  101. return true;
  102. return false;
  103. }
  104. void set_realtime()
  105. {
  106. #ifdef HAVE_SCHEDULER
  107. sched_param sc;
  108. sc.sched_priority = 60;
  109. //if you want get "sched_setscheduler undeclared" from compilation,
  110. //you can safely remove the folowing line:
  111. sched_setscheduler(0, SCHED_FIFO, &sc);
  112. //if (err==0) printf("Real-time");
  113. #endif
  114. }
  115. void os_sleep(long length)
  116. {
  117. usleep(length);
  118. }
  119. //!< maximum lenght a pid has on any POSIX system
  120. //!< this is an estimation, but more than 12 looks insane
  121. constexpr std::size_t max_pid_len = 12;
  122. //!< safe pid lenght guess, posix conform
  123. std::size_t os_guess_pid_length()
  124. {
  125. const char* pid_max_file = "/proc/sys/kernel/pid_max";
  126. if(-1 == access(pid_max_file, R_OK)) {
  127. return max_pid_len;
  128. }
  129. else {
  130. std::ifstream is(pid_max_file);
  131. if(!is.good())
  132. return max_pid_len;
  133. else {
  134. std::string s;
  135. is >> s;
  136. for(const auto& c : s)
  137. if(c < '0' || c > '9')
  138. return max_pid_len;
  139. return std::min(s.length(), max_pid_len);
  140. }
  141. }
  142. }
  143. //!< returns pid padded, posix conform
  144. std::string os_pid_as_padded_string()
  145. {
  146. char result_str[max_pid_len << 1];
  147. std::fill_n(result_str, max_pid_len, '0');
  148. std::size_t written = snprintf(result_str + max_pid_len, max_pid_len,
  149. "%d", (int)getpid());
  150. // the below pointer should never cause segfaults:
  151. return result_str + max_pid_len + written - os_guess_pid_length();
  152. }
  153. std::string legalizeFilename(std::string filename)
  154. {
  155. for(int i = 0; i < (int) filename.size(); ++i) {
  156. char c = filename[i];
  157. if(!(isdigit(c) || isalpha(c) || (c == '-') || (c == ' ')))
  158. filename[i] = '_';
  159. }
  160. return filename;
  161. }
  162. void invSignal(float *sig, size_t len)
  163. {
  164. for(size_t i = 0; i < len; ++i)
  165. sig[i] *= -1.0f;
  166. }
  167. float SYNTH_T::numRandom()
  168. {
  169. return RND;
  170. }
  171. float interpolate(const float *data, size_t len, float pos)
  172. {
  173. assert(len > (size_t)pos + 1);
  174. const int l_pos = (int)pos,
  175. r_pos = l_pos + 1;
  176. const float leftness = pos - l_pos;
  177. return data[l_pos] * leftness + data[r_pos] * (1.0f - leftness);
  178. }
  179. float cinterpolate(const float *data, size_t len, float pos)
  180. {
  181. const unsigned int i_pos = pos,
  182. l_pos = i_pos % len,
  183. r_pos = l_pos + 1 < len ? l_pos + 1 : 0;
  184. const float leftness = pos - i_pos;
  185. return data[l_pos] * leftness + data[r_pos] * (1.0f - leftness);
  186. }
  187. char *rtosc_splat(const char *path, std::set<std::string> v)
  188. {
  189. char argT[v.size()+1];
  190. rtosc_arg_t arg[v.size()];
  191. unsigned i=0;
  192. for(auto vv : v) {
  193. argT[i] = 's';
  194. arg[i].s = vv.c_str();
  195. i++;
  196. }
  197. argT[v.size()] = 0;
  198. size_t len = rtosc_amessage(0, 0, path, argT, arg);
  199. char *buf = new char[len];
  200. rtosc_amessage(buf, len, path, argT, arg);
  201. return buf;
  202. }