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 2.8KB

10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "Config.h"
  23. #include "../globals.h"
  24. //Velocity Sensing function
  25. extern float VelF(float velocity, unsigned char scaling);
  26. bool fileexists(const char *filename);
  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_sleep(long length);
  38. std::string legalizeFilename(std::string filename);
  39. extern float *denormalkillbuf; /**<the buffer to add noise in order to avoid denormalisation*/
  40. extern class Config config;
  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. T stringTo(const char *x)
  51. {
  52. std::string str = x != NULL ? x : "0"; //should work for the basic float/int
  53. std::stringstream ss(str);
  54. T ans;
  55. ss >> ans;
  56. return ans;
  57. }
  58. template<class T>
  59. T limit(T val, T min, T max)
  60. {
  61. return val < min ? min : (val > max ? max : val);
  62. }
  63. //Random number generator
  64. typedef uint32_t prng_t;
  65. extern prng_t prng_state;
  66. // Portable Pseudo-Random Number Generator
  67. inline prng_t prng_r(prng_t &p)
  68. {
  69. return p = p * 1103515245 + 12345;
  70. }
  71. inline prng_t prng(void)
  72. {
  73. return prng_r(prng_state) & 0x7fffffff;
  74. }
  75. inline void sprng(prng_t p)
  76. {
  77. prng_state = p;
  78. }
  79. /*
  80. * The random generator (0.0f..1.0f)
  81. */
  82. #ifndef INT32_MAX
  83. # define INT32_MAX (2147483647)
  84. #endif
  85. #define RND (float(prng()) / float(INT32_MAX))
  86. //Linear Interpolation
  87. float interpolate(const float *data, size_t len, float pos);
  88. //Linear circular interpolation
  89. float cinterpolate(const float *data, size_t len, float pos);
  90. #endif