1 /* 2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> 3 * (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com> 4 * 5 * This file is part of lsp-plugins 6 * Created on: 12 янв. 2016 г. 7 * 8 * lsp-plugins is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Lesser General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * any later version. 12 * 13 * lsp-plugins is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License 19 * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>. 20 */ 21 22 #ifndef CORE_UNITS_H_ 23 #define CORE_UNITS_H_ 24 25 #include <core/stdlib/math.h> 26 27 // Some physical constants 28 #define MAX_SAMPLE_RATE 192000 /* Maximum supported sample rate [samples / s] */ 29 #define DEFAULT_SAMPLE_RATE 48000 /* Default sample rate [samples / s] */ 30 #define DECIBEL_INF_THRESH 1.19209289551e-40 /* Float value that is equal -INF dB */ 31 #define AIR_ADIABATIC_INDEX 1.4 /* Adiabatic index for the Air */ 32 #define AIR_MOLAR_MASS 28.98 /* Molar mass of the air [g/mol] */ 33 #define GAS_CONSTANT 8.3144598 /* Gas constant [ j/(mol * K) } */ 34 #define TEMP_ABS_ZERO -273.15 /* Temperature of the absolute zero [ C ] */ 35 #define MAX_SOUND_SPEED 500 /* Maximum speed of the sound [ m/s ] */ 36 #define CMP_TOLERANCE 1e-6 /* Float compare tolerance */ 37 #define AMPLIFICATION_THRESH 1e-8 /* Amplification threshold */ 38 #define SOUND_SPEED_M_S 340.29f /* Sound speed [ m / s ] */ 39 #define SPEC_FREQ_MIN 10.0f /* Minimum frequency [ Hz ] */ 40 #define SPEC_FREQ_MAX 24000.0f /* Maximum frequency [ Hz ] */ 41 #define BPM_MIN 1.0f /* Minimum BPM */ 42 #define BPM_MAX 1000.0f /* Maximum BPM */ 43 #define BPM_DEFAULT 120.0f /* Default BPM */ 44 #define DEFAULT_TICKS_PER_BEAT 1920.0f /* Default tick per beat resolution */ 45 #define MIDI_EVENTS_MAX 4096 /* Maximum number of MIDI events per buffer */ 46 #define OSC_BUFFER_MAX 0x100000 /* Maximum size of the OSC messaging buffer (bytes) */ 47 #define OSC_PACKET_MAX 0x10000 /* Maximum size of the OSC packet (bytes) */ 48 #define GOLDEN_RATIO 1.618 /* Golden ratio */ 49 #define R_GOLDEN_RATIO 0.618 /* Reverse golden ratio */ 50 51 52 // Gain levels in decibels 53 #define GAIN_AMP_P_96_DB 3.98107e+4 /* +96 dB */ 54 #define GAIN_AMP_P_84_DB 1.5849e+4 /* +84 dB */ 55 #define GAIN_AMP_P_72_DB 3981.073 /* +72 dB */ 56 #define GAIN_AMP_P_60_DB 1000.0 /* +60 dB */ 57 #define GAIN_AMP_P_48_DB 251.18861 /* +48 dB */ 58 #define GAIN_AMP_P_36_DB 63.09575 /* +36 dB */ 59 #define GAIN_AMP_P_24_DB 15.84893 /* +24 dB */ 60 #define GAIN_AMP_P_12_DB 3.98107 /* +12 dB */ 61 #define GAIN_AMP_P_6_DB 1.99526 /* +6 dB */ 62 #define GAIN_AMP_P_3_DB 1.412536 /* +3 dB */ 63 #define GAIN_AMP_0_DB 1.0 /* 0 dB */ 64 #define GAIN_AMP_M_3_DB 0.707946 /* -3 dB */ 65 #define GAIN_AMP_M_6_DB 0.50118 /* -6 dB */ 66 #define GAIN_AMP_M_9_DB 0.354813 /* -9 dB */ 67 #define GAIN_AMP_M_12_DB 0.25119 /* -12 dB */ 68 #define GAIN_AMP_M_18_DB 0.12589 /* -18 dB */ 69 #define GAIN_AMP_M_24_DB 0.06310 /* -24 dB */ 70 #define GAIN_AMP_M_36_DB 0.01585 /* -36 dB */ 71 #define GAIN_AMP_M_48_DB 3.98107e-3 /* -48 dB */ 72 #define GAIN_AMP_M_60_DB 0.001 /* -60 dB */ 73 #define GAIN_AMP_M_72_DB 2.5119e-4 /* -72 dB */ 74 #define GAIN_AMP_M_84_DB 6.309575e-5 /* -84 dB */ 75 #define GAIN_AMP_M_96_DB 1.5849e-5 /* -96 dB */ 76 #define GAIN_AMP_M_108_DB 3.98107e-6 /* -60 dB */ 77 #define GAIN_AMP_M_120_DB 1e-6 /* -120 dB */ 78 #define GAIN_AMP_M_132_DB 2.5119e-7 /* -132 dB */ 79 #define GAIN_AMP_M_144_DB 6.309575e-8 /* -144 dB */ 80 #define GAIN_AMP_M_INF_DB 0.0 /* -inf dB */ 81 82 #define GAIN_AMP_MIN 1e-6 83 #define GAIN_AMP_MAX 1e+6 84 85 #define GAIN_AMP_M_20_DB 0.1 /* -20 dB */ 86 #define GAIN_AMP_M_40_DB 0.01 /* -40 dB */ 87 #define GAIN_AMP_M_80_DB 0.0001 /* -80 dB */ 88 #define GAIN_AMP_M_100_DB 0.00001 /* -100 dB */ 89 #define GAIN_AMP_M_140_DB 0.0000001 /* -140 dB */ 90 #define GAIN_AMP_M_160_DB 0.00000001 /* -160 dB */ 91 #define GAIN_AMP_P_20_DB 10.0 /* +20 dB */ 92 #define GAIN_AMP_P_40_DB 100.0 /* +40 dB */ 93 #define GAIN_AMP_P_80_DB 10000.0 /* +80 dB */ 94 95 #define GAIN_AMP_N_12_DB GAIN_AMP_M_12_DB 96 #define GAIN_AMP_N_24_DB GAIN_AMP_M_24_DB 97 #define GAIN_AMP_N_36_DB GAIN_AMP_M_36_DB 98 #define GAIN_AMP_N_48_DB GAIN_AMP_M_48_DB 99 #define GAIN_AMP_N_60_DB GAIN_AMP_M_60_DB 100 #define GAIN_AMP_N_72_DB GAIN_AMP_M_72_DB 101 102 //#define GAIN_AMP_M_0_1_DB 0.988553094657 103 #define GAIN_AMP_S_0_1_DB 0.01157945426 104 #define GAIN_AMP_S_0_5_DB 0.05925372518 105 #define GAIN_AMP_S_1_DB 0.1220184543 106 107 // Float saturation limits 108 #define FLOAT_SAT_P_NAN 0.0f 109 #define FLOAT_SAT_N_NAN 0.0f 110 #define FLOAT_SAT_P_INF 1e+10f 111 #define FLOAT_SAT_N_INF -1e+10f 112 #define FLOAT_SAT_P_NAN_I 0 113 #define FLOAT_SAT_N_NAN_I 0 114 #define FLOAT_SAT_P_INF_I 0x501502f9 115 #define FLOAT_SAT_N_INF_I 0xd01502f9 116 117 namespace lsp 118 { 119 /** Convert temperature from Celsium degrees to sound speed [m/s] 120 * 121 * @param temp temperature [ Celsium degrees ] 122 * @return sound speed [m/s] 123 */ sound_speed(float temp)124 inline float sound_speed(float temp) 125 { 126 return sqrtf(AIR_ADIABATIC_INDEX * GAS_CONSTANT * (temp - TEMP_ABS_ZERO) * 1000 /* g/kg */ / AIR_MOLAR_MASS); 127 } 128 129 /** Convert samples [samp] to time [s] 130 * 131 * @param sr sample rate [samp/s] 132 * @param samples number of samples [samp] 133 * @return time [s] 134 */ samples_to_seconds(float sr,float samples)135 inline float samples_to_seconds(float sr, float samples) 136 { 137 return samples / sr; 138 } 139 140 /** Convert time [s] to samples [samp] 141 * 142 * @param sr sample rate [samp/s] 143 * @param time [s] 144 * @return samples [samp] 145 */ seconds_to_samples(float sr,float time)146 inline float seconds_to_samples(float sr, float time) 147 { 148 return time * sr; 149 } 150 151 /** Convert samples [samp] to milliseconds [ms] 152 * 153 * @param sr sample rate 154 * @param samples number of samples 155 * @return milliseconds 156 */ samples_to_millis(float sr,float samples)157 inline float samples_to_millis(float sr, float samples) 158 { 159 return (samples / sr) * 1000.0f; 160 } 161 162 /** Convert samples [samp] to distance [m] 163 * 164 * @param sr sample rate [samp/s] 165 * @param speed sound speed [m/s] 166 * @param samples number of samples [samp] 167 * @return distance [m] 168 */ samples_to_meters(float sr,float speed,float samples)169 inline float samples_to_meters(float sr, float speed, float samples) 170 { 171 return (samples * speed) / sr; 172 } 173 174 /** Convert samples [samp] to distance [cm] 175 * 176 * @param sr sample rate [samp/s] 177 * @param speed sound speed [m/s] 178 * @param samples number of samples [samp] 179 * @return distance [cm] 180 */ samples_to_centimeters(float sr,float speed,float samples)181 inline float samples_to_centimeters(float sr, float speed, float samples) 182 { 183 return ((samples * speed) / sr) * 100.0f; 184 } 185 186 /** Convert time [ms] to samples [samp] 187 * 188 * @param sr sample rate [samp/s] 189 * @param time time [ms] 190 * @return samples [samp] 191 */ millis_to_samples(float sr,float time)192 inline float millis_to_samples(float sr, float time) 193 { 194 return (time * 0.001f) * sr; 195 } 196 197 /** Convert decibels to gain value 198 * 199 * @param db decibels 200 * @return gain 201 */ db_to_gain(float db)202 inline float db_to_gain(float db) 203 { 204 return expf(db * M_LN10 * 0.05f); 205 } 206 207 /** Convert decibels to power value 208 * 209 * @param db decibels 210 * @return power 211 */ db_to_power(float db)212 inline float db_to_power(float db) 213 { 214 return expf(db * M_LN10 * 0.1f); 215 } 216 217 /** Convert gain value to decibels 218 * 219 * @param gain gain value 220 * @return decibels 221 */ gain_to_db(float gain)222 inline float gain_to_db(float gain) 223 { 224 return (20.0f / M_LN10) * logf(gain); 225 } 226 227 /** Convert powerr value to decibels 228 * 229 * @param pwr power value 230 * @return decibels 231 */ power_to_db(float pwr)232 inline float power_to_db(float pwr) 233 { 234 return (10.0f / M_LN10) * logf(pwr); 235 } 236 } 237 238 #endif /* CORE_UNITS_H_ */ 239