1 /* 2 ZynAddSubFX - a software synthesizer 3 4 Util.h - Miscellaneous functions 5 Copyright (C) 2002-2005 Nasca Octavian Paul 6 Author: Nasca Octavian Paul 7 8 This program is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License 10 as published by the Free Software Foundation; either version 2 11 of the License, or (at your option) any later version. 12 */ 13 14 #ifndef UTIL_H 15 #define UTIL_H 16 17 #include <string> 18 #include <sstream> 19 #include <stdint.h> 20 #include <algorithm> 21 #include <set> 22 23 #include <rtosc/ports.h> 24 #include <rtosc/port-sugar.h> 25 26 namespace zyn { 27 28 extern bool isPlugin; 29 bool fileexists(const char *filename); 30 31 using std::min; 32 using std::max; 33 34 /** 35 * Copy string to another memory location, including the terminating zero byte 36 * @param dest Destination memory location 37 * @param src Source string 38 * @param buffersize Maximal number of bytes that you can write to @p dest 39 * @return A pointer to @p dest 40 * @warning @p dest and @p src shall not overlap 41 * @warning if buffersize is larger than strlen(src)+1, unused bytes in @p dest 42 * are not overwritten. Secure information may be released. Don't use this if 43 * you want to send the string somewhere else, e.g. via IPC. 44 */ 45 char *fast_strcpy(char *dest, const char *src, size_t buffersize); 46 47 //Velocity Sensing function 48 extern float VelF(float velocity, unsigned char scaling); 49 50 #define N_DETUNE_TYPES 4 //the number of detune types 51 extern float getdetune(unsigned char type, 52 unsigned short int coarsedetune, 53 unsigned short int finedetune); 54 55 /**Try to set current thread to realtime priority program priority 56 * \todo see if the right pid is being sent 57 * \todo see if this is having desired effect, if not then look at 58 * pthread_attr_t*/ 59 void set_realtime(); 60 61 /**Os independent sleep in microsecond*/ 62 void os_usleep(long length); 63 64 //! returns pid padded to maximum pid length, posix conform 65 std::string os_pid_as_padded_string(); 66 67 std::string legalizeFilename(std::string filename); 68 69 void invSignal(float *sig, size_t len); 70 71 template<class T> 72 std::string stringFrom(T x) 73 { 74 std::stringstream ss; 75 ss << x; 76 return ss.str(); 77 } 78 79 template<class T> 80 std::string to_s(T x) 81 { 82 return stringFrom(x); 83 } 84 85 template<class T> 86 T stringTo(const char *x) 87 { 88 std::string str = x != NULL ? x : "0"; //should work for the basic float/int 89 std::stringstream ss(str); 90 T ans; 91 ss >> ans; 92 return ans; 93 } 94 95 96 97 template<class T> 98 T limit(T val, T min, T max) 99 { 100 return val < min ? min : (val > max ? max : val); 101 } 102 103 template<class T> 104 bool inRange(T val, T min, T max) 105 { 106 return val >= min && val <= max; 107 } 108 109 template<class T> 110 T array_max(const T *data, size_t len) 111 { 112 T max = 0; 113 114 for(unsigned i = 0; i < len; ++i) 115 if(max < data[i]) 116 max = data[i]; 117 return max; 118 } 119 120 //Random number generator 121 122 typedef uint32_t prng_t; 123 extern prng_t prng_state; 124 125 // Portable Pseudo-Random Number Generator 126 inline prng_t prng_r(prng_t &p) 127 { 128 return p = p * 1103515245 + 12345; 129 } 130 131 inline prng_t prng(void) 132 { 133 return prng_r(prng_state) & 0x7fffffff; 134 } 135 136 inline void sprng(prng_t p) 137 { 138 prng_state = p; 139 } 140 141 /* 142 * The random generator (0.0f..1.0f) 143 */ 144 #ifndef INT32_MAX_FLOAT 145 #define INT32_MAX_FLOAT 0x7fffff80 /* the float mantissa is only 24-bit */ 146 #endif 147 #define RND (prng() / (INT32_MAX_FLOAT * 1.0f)) 148 149 //Linear Interpolation 150 float interpolate(const float *data, size_t len, float pos); 151 152 //Linear circular interpolation 153 float cinterpolate(const float *data, size_t len, float pos); 154 155 template<class T> 156 static inline void nullify(T &t) {delete t; t = NULL; } 157 template<class T> 158 static inline void arrayNullify(T &t) {delete [] t; t = NULL; } 159 160 char *rtosc_splat(const char *path, std::set<std::string>); 161 162 /** 163 * Port macros - these produce easy and regular port definitions for common 164 * types 165 */ 166 #define rParamZyn(name, ...) \ 167 {STRINGIFY(name) "::i", rProp(parameter) rMap(min, 0) rMap(max, 127) DOC(__VA_ARGS__), NULL, rParamICb(name)} 168 169 #define rPresetType \ 170 {"preset-type:", rProp(internal) rDoc("clipboard type of object"), 0, \ 171 [](const char *, rtosc::RtData &d){ \ 172 rObject *obj = (rObject*)d.obj; \ 173 d.reply(d.loc, "s", obj->type);}} 174 175 #define rPaste \ 176 rPresetType, \ 177 {"paste:b", rProp(internal) rDoc("paste port"), 0, \ 178 [](const char *m, rtosc::RtData &d){ \ 179 printf("rPaste...\n"); \ 180 rObject &paste = **(rObject **)rtosc_argument(m,0).b.data; \ 181 rObject &o = *(rObject*)d.obj;\ 182 o.paste(paste);}} 183 184 #define rArrayPaste \ 185 {"paste-array:bi", rProp(internal) rDoc("array paste port"), 0, \ 186 [](const char *m, rtosc::RtData &d){ \ 187 printf("rArrayPaste...\n"); \ 188 rObject &paste = **(rObject **)rtosc_argument(m,0).b.data; \ 189 int field = rtosc_argument(m,1).i; \ 190 rObject &o = *(rObject*)d.obj;\ 191 o.pasteArray(paste,field);}} 192 193 } 194 195 #define rUnit(x) rMap(unit, x) 196 197 #endif 198