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.

176 lines
4.4KB

  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 "Util.h"
  18. #include <vector>
  19. #include <cassert>
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <err.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #ifdef HAVE_SCHEDULER
  30. #include <sched.h>
  31. #endif
  32. prng_t prng_state = 0x1234;
  33. Config config;
  34. float *denormalkillbuf;
  35. /*
  36. * Transform the velocity according the scaling parameter (velocity sensing)
  37. */
  38. float VelF(float velocity, unsigned char scaling)
  39. {
  40. float x;
  41. x = powf(VELOCITY_MAX_SCALE, (64.0f - scaling) / 64.0f);
  42. if((scaling == 127) || (velocity > 0.99f))
  43. return 1.0f;
  44. else
  45. return powf(velocity, x);
  46. }
  47. /*
  48. * Get the detune in cents
  49. */
  50. float getdetune(unsigned char type,
  51. unsigned short int coarsedetune,
  52. unsigned short int finedetune)
  53. {
  54. float det = 0.0f, octdet = 0.0f, cdet = 0.0f, findet = 0.0f;
  55. //Get Octave
  56. int octave = coarsedetune / 1024;
  57. if(octave >= 8)
  58. octave -= 16;
  59. octdet = octave * 1200.0f;
  60. //Coarse and fine detune
  61. int cdetune = coarsedetune % 1024;
  62. if(cdetune > 512)
  63. cdetune -= 1024;
  64. int fdetune = finedetune - 8192;
  65. switch(type) {
  66. // case 1: is used for the default (see below)
  67. case 2:
  68. cdet = fabs(cdetune * 10.0f);
  69. findet = fabs(fdetune / 8192.0f) * 10.0f;
  70. break;
  71. case 3:
  72. cdet = fabs(cdetune * 100);
  73. findet = powf(10, fabs(fdetune / 8192.0f) * 3.0f) / 10.0f - 0.1f;
  74. break;
  75. case 4:
  76. cdet = fabs(cdetune * 701.95500087f); //perfect fifth
  77. findet =
  78. (powf(2, fabs(fdetune / 8192.0f) * 12.0f) - 1.0f) / 4095 * 1200;
  79. break;
  80. //case ...: need to update N_DETUNE_TYPES, if you'll add more
  81. default:
  82. cdet = fabs(cdetune * 50.0f);
  83. findet = fabs(fdetune / 8192.0f) * 35.0f; //almost like "Paul's Sound Designer 2"
  84. break;
  85. }
  86. if(finedetune < 8192)
  87. findet = -findet;
  88. if(cdetune < 0)
  89. cdet = -cdet;
  90. det = octdet + cdet + findet;
  91. return det;
  92. }
  93. bool fileexists(const char *filename)
  94. {
  95. struct stat tmp;
  96. int result = stat(filename, &tmp);
  97. if(result >= 0)
  98. return true;
  99. return false;
  100. }
  101. void set_realtime()
  102. {
  103. #ifdef HAVE_SCHEDULER
  104. sched_param sc;
  105. sc.sched_priority = 60;
  106. //if you want get "sched_setscheduler undeclared" from compilation,
  107. //you can safely remove the folowing line:
  108. sched_setscheduler(0, SCHED_FIFO, &sc);
  109. //if (err==0) printf("Real-time");
  110. #endif
  111. }
  112. void os_sleep(long length)
  113. {
  114. usleep(length);
  115. }
  116. std::string legalizeFilename(std::string filename)
  117. {
  118. for(int i = 0; i < (int) filename.size(); ++i) {
  119. char c = filename[i];
  120. if(!(isdigit(c) || isalpha(c) || (c == '-') || (c == ' ')))
  121. filename[i] = '_';
  122. }
  123. return filename;
  124. }
  125. void invSignal(float *sig, size_t len)
  126. {
  127. for(size_t i = 0; i < len; ++i)
  128. sig[i] *= -1.0f;
  129. }
  130. float SYNTH_T::numRandom()
  131. {
  132. return RND;
  133. }
  134. float interpolate(const float *data, size_t len, float pos)
  135. {
  136. assert(len > (size_t)pos + 1);
  137. const int l_pos = (int)pos,
  138. r_pos = l_pos + 1;
  139. const float leftness = pos - l_pos;
  140. return data[l_pos] * leftness + data[r_pos] * (1.0f - leftness);
  141. }
  142. float cinterpolate(const float *data, size_t len, float pos)
  143. {
  144. const int l_pos = ((int)pos) % len,
  145. r_pos = (l_pos + 1) % len;
  146. const float leftness = pos - l_pos;
  147. return data[l_pos] * leftness + data[r_pos] * (1.0f - leftness);
  148. }