Jamba  3.0.2
SampleRateBasedClock.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/base/ftypes.h>
21 #include <pluginterfaces/vst/vsttypes.h>
22 #include <cmath>
23 #include <chrono>
24 
25 namespace pongasoft {
26 namespace VST {
27 
28 using namespace Steinberg;
29 using namespace Steinberg::Vst;
30 
35 {
36 public:
50  {
51  public:
52  explicit RateLimiter(uint32 iRateLimitInSamples = 0) : fRateLimitInSamples{iRateLimitInSamples}, fSampleCount{0}
53  {}
54 
59  bool shouldUpdate(uint32 numSamples)
60  {
61  fSampleCount += numSamples;
62  if(fSampleCount >= fRateLimitInSamples)
63  {
64  fSampleCount -= fRateLimitInSamples;
65  return true;
66  }
67  return false;
68  }
69 
70  private:
72  uint32 fSampleCount;
73  };
74 
75  // Constructor
76  explicit SampleRateBasedClock(SampleRate iSampleRate) : fSampleRate{iSampleRate}
77  {
78 
79  }
80 
81  // getSampleCountFor: how many samples for iMillis seconds
82  uint32 getSampleCountFor(uint32 iMillis) const
83  {
84  return static_cast<uint32>(ceil(fSampleRate * iMillis / 1000.0));
85  }
86 
87  // getSampleCountFor1Bar: how many samples for 1 bar (given the tempo and optional time signature)
88  uint32 getSampleCountFor1Bar(double iTempo, int32 iTimeSigNumerator = 4, int32 iTimeSigDenominator = 4) const
89  {
90  auto oneBarInSeconds = 240.0 / iTempo;
91  if(iTimeSigNumerator != iTimeSigDenominator && iTimeSigDenominator != 0)
92  {
93  oneBarInSeconds *= (static_cast<double>(iTimeSigNumerator) / static_cast<double>(iTimeSigDenominator));
94  }
95  return static_cast<uint32>(ceil(fSampleRate * oneBarInSeconds));
96  }
97 
98  // getTimeForSampleCount: inverse of getSampleCountFor
99  uint32 getTimeForSampleCount(uint32 iSampleCount) const
100  {
101  return static_cast<uint32>(ceil(iSampleCount * 1000.0 / fSampleRate));
102  }
103 
104  // getSampleRate
105  SampleRate getSampleRate() const
106  {
107  return fSampleRate;
108  }
109 
110  // setSampleRate
111  void setSampleRate(SampleRate iSampleRate)
112  {
113  fSampleRate = iSampleRate;
114  }
115 
116  // getRateLimiter
117  RateLimiter getRateLimiter(uint32 iMillis) const
118  {
119  return RateLimiter{getSampleCountFor(iMillis)};
120  }
121 
122 private:
123  SampleRate fSampleRate;
124 };
125 
126 }
127 }
SampleRate getSampleRate() const
Definition: SampleRateBasedClock.h:105
RateLimiter(uint32 iRateLimitInSamples=0)
Definition: SampleRateBasedClock.h:52
uint32 getSampleCountFor(uint32 iMillis) const
Definition: SampleRateBasedClock.h:82
Definition: SampleRateBasedClock.h:49
Definition: Clock.h:22
SampleRateBasedClock(SampleRate iSampleRate)
Definition: SampleRateBasedClock.h:76
uint32 fSampleCount
Definition: SampleRateBasedClock.h:72
bool shouldUpdate(uint32 numSamples)
Definition: SampleRateBasedClock.h:59
void setSampleRate(SampleRate iSampleRate)
Definition: SampleRateBasedClock.h:111
uint32 getTimeForSampleCount(uint32 iSampleCount) const
Definition: SampleRateBasedClock.h:99
RateLimiter getRateLimiter(uint32 iMillis) const
Definition: SampleRateBasedClock.h:117
uint32 getSampleCountFor1Bar(double iTempo, int32 iTimeSigNumerator=4, int32 iTimeSigDenominator=4) const
Definition: SampleRateBasedClock.h:88
Definition: SampleRateBasedClock.h:34
SampleRate fSampleRate
Definition: SampleRateBasedClock.h:123
uint32 fRateLimitInSamples
Definition: SampleRateBasedClock.h:71