Jamba C++ API  5.0.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 
35 template<typename T>
36 constexpr T bitSet(T a, int bit) { return a | (static_cast<uint64>(1) << bit); }
37 
39 template<typename T>
40 constexpr T bitClear(T a, int bit) { return a & ~(static_cast<uint64>(1) << bit); }
41 
43 template<typename T>
44 constexpr bool bitTest(T a, int bit) { return (a & (static_cast<uint64>(1) << bit)) != 0; }
45 
46 // defines the threshold of silence as constants
47 constexpr Sample32 Sample32SilentThreshold = ((Sample32)2.0e-8);
48 constexpr Sample64 Sample64SilentThreshold = ((Sample64)2.0e-8);
49 
50 // defines the threshold of silence as a templated method
51 template<typename SampleType>
52 SampleType getSampleSilentThreshold() noexcept;
53 template<>
54 constexpr Sample32 getSampleSilentThreshold<Sample32>() noexcept { return Sample32SilentThreshold; }
55 template<>
56 constexpr Sample64 getSampleSilentThreshold<Sample64>() noexcept { return Sample64SilentThreshold; }
57 
58 //------------------------------------------------------------------------
59 // check if sample is silent (lower than threshold) Sample32 version
60 //------------------------------------------------------------------------
61 inline bool isSilent(Sample32 value)
62 {
63  if(value < 0)
64  value = -value;
65  return value <= Sample32SilentThreshold;
66 }
67 
68 //------------------------------------------------------------------------
69 // check if sample is silent (lower than threshold) Sample64 version
70 //------------------------------------------------------------------------
71 inline bool isSilent(Sample64 value)
72 {
73  if(value < 0)
74  value = -value;
75  return value <= Sample64SilentThreshold;
76 }
77 
78 //------------------------------------------------------------------------
79 // dbToSample
80 //------------------------------------------------------------------------
81 template<typename SampleType>
82 inline SampleType dbToSample(double valueInDb)
83 {
84  return static_cast<SampleType>(std::pow(10.0, valueInDb / 20.0));
85 }
86 
87 //------------------------------------------------------------------------
88 // sampleToDb
89 //------------------------------------------------------------------------
90 template<typename SampleType>
91 inline double sampleToDb(SampleType valueInSample)
92 {
93  return std::log10(valueInSample) * 20.0;
94 }
95 
96 }
97 }
Definition: Clock.h:22
SampleType dbToSample(double valueInDb)
Definition: AudioUtils.h:82
constexpr Sample64 getSampleSilentThreshold< Sample64 >() noexcept
Definition: AudioUtils.h:56
SampleType getSampleSilentThreshold() noexcept
bool isSilent(Sample32 value)
Definition: AudioUtils.h:61
constexpr Sample32 Sample32SilentThreshold
Definition: AudioUtils.h:47
constexpr T bitSet(T a, int bit)
Sets bit bit in a
Definition: AudioUtils.h:36
constexpr Sample64 Sample64SilentThreshold
Definition: AudioUtils.h:48
double sampleToDb(SampleType valueInSample)
Definition: AudioUtils.h:91
constexpr T bitClear(T a, int bit)
Clears bit bit in a
Definition: AudioUtils.h:40
constexpr bool bitTest(T a, int bit)
Test if bit bit is set in a
Definition: AudioUtils.h:44