Jamba C++ API  4.2.0
AudioUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
20 #include <pluginterfaces/vst/ivstaudioprocessor.h>
21 
22 #include <cmath>
23 
24 namespace pongasoft {
25 namespace VST {
26 
27 using namespace Steinberg;
28 using namespace Steinberg::Vst;
29 
30 #define BIT_SET(a,b) ((a) |= (static_cast<uint64>(1)<<(b)))
31 #define BIT_CLEAR(a,b) ((a) &= ~(static_cast<uint64>(1)<<(b)))
32 #define BIT_TEST(a,b) (((a) & (static_cast<uint64>(1)<<(b))) != 0)
33 
34 // defines the threshold of silence as constants
35 constexpr Sample32 Sample32SilentThreshold = ((Sample32)2.0e-8);
36 constexpr Sample64 Sample64SilentThreshold = ((Sample64)2.0e-8);
37 
38 // defines the threshold of silence as a templated method
39 template<typename SampleType>
40 SampleType getSampleSilentThreshold() noexcept;
41 template<>
42 inline Sample32 getSampleSilentThreshold<Sample32>() noexcept { return Sample32SilentThreshold; }
43 template<>
44 inline Sample64 getSampleSilentThreshold<Sample64>() noexcept { return Sample64SilentThreshold; }
45 
46 //------------------------------------------------------------------------
47 // check if sample is silent (lower than threshold) Sample32 version
48 //------------------------------------------------------------------------
49 inline bool isSilent(Sample32 value)
50 {
51  if(value < 0)
52  value = -value;
53  return value <= Sample32SilentThreshold;
54 }
55 
56 //------------------------------------------------------------------------
57 // check if sample is silent (lower than threshold) Sample64 version
58 //------------------------------------------------------------------------
59 inline bool isSilent(Sample64 value)
60 {
61  if(value < 0)
62  value = -value;
63  return value <= Sample64SilentThreshold;
64 }
65 
66 //------------------------------------------------------------------------
67 // dbToSample
68 //------------------------------------------------------------------------
69 template<typename SampleType>
70 inline SampleType dbToSample(double valueInDb)
71 {
72  return static_cast<SampleType>(std::pow(10.0, valueInDb / 20.0));
73 }
74 
75 //------------------------------------------------------------------------
76 // sampleToDb
77 //------------------------------------------------------------------------
78 template<typename SampleType>
79 inline double sampleToDb(SampleType valueInSample)
80 {
81  return std::log10(valueInSample) * 20.0;
82 }
83 
84 }
85 }
Definition: Clock.h:22
Sample64 getSampleSilentThreshold< Sample64 >() noexcept
Definition: AudioUtils.h:44
SampleType dbToSample(double valueInDb)
Definition: AudioUtils.h:70
SampleType getSampleSilentThreshold() noexcept
bool isSilent(Sample32 value)
Definition: AudioUtils.h:49
constexpr Sample32 Sample32SilentThreshold
Definition: AudioUtils.h:35
constexpr Sample64 Sample64SilentThreshold
Definition: AudioUtils.h:36
double sampleToDb(SampleType valueInSample)
Definition: AudioUtils.h:79