Jamba C++ API  4.1.0
SampleRateBasedClock.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019 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 
115  TSamples getNextBarSampleCount(TSamples iCurrentSampleCount, double iTempo, int32 iTimeSigNumerator = 4, int32 iTimeSigDenominator = 4) const
116  {
117  auto sampleCountFor1Bar =
118  static_cast<TSamples>(getSampleCountFor1Bar(iTempo, iTimeSigNumerator, iTimeSigDenominator));
119  auto barMultiples = iCurrentSampleCount / sampleCountFor1Bar;
120  auto barBoundary = barMultiples * sampleCountFor1Bar;
121  if(barBoundary == iCurrentSampleCount)
122  return iCurrentSampleCount;
123  else
124  return barBoundary + sampleCountFor1Bar;
125  }
126 
127  // getSampleRate
128  SampleRate getSampleRate() const
129  {
130  return fSampleRate;
131  }
132 
133  // setSampleRate
134  void setSampleRate(SampleRate iSampleRate)
135  {
136  fSampleRate = iSampleRate;
137  }
138 
139  // getRateLimiter
140  RateLimiter getRateLimiter(uint32 iMillis) const
141  {
142  return RateLimiter{getSampleCountFor(iMillis)};
143  }
144 
145 private:
146  SampleRate fSampleRate;
147 };
148 
149 }
150 }
RateLimiter getRateLimiter(uint32 iMillis) const
Definition: SampleRateBasedClock.h:140
SampleRate fSampleRate
Definition: SampleRateBasedClock.h:146
uint32 fSampleCount
Definition: SampleRateBasedClock.h:72
Keeps track of the time in number of samples processed vs sample rate.
Definition: SampleRateBasedClock.h:49
Definition: Clock.h:22
SampleRate getSampleRate() const
Definition: SampleRateBasedClock.h:128
uint32 getTimeForSampleCount(uint32 iSampleCount) const
Definition: SampleRateBasedClock.h:99
uint32 getSampleCountFor(uint32 iMillis) const
Definition: SampleRateBasedClock.h:82
SampleRateBasedClock(SampleRate iSampleRate)
Definition: SampleRateBasedClock.h:76
uint32 getSampleCountFor1Bar(double iTempo, int32 iTimeSigNumerator=4, int32 iTimeSigDenominator=4) const
Definition: SampleRateBasedClock.h:88
bool shouldUpdate(uint32 numSamples)
Calls this method when a new batch of samples is processed and returns true if the limit (in samples)...
Definition: SampleRateBasedClock.h:59
void setSampleRate(SampleRate iSampleRate)
Definition: SampleRateBasedClock.h:134
TSamples getNextBarSampleCount(TSamples iCurrentSampleCount, double iTempo, int32 iTimeSigNumerator=4, int32 iTimeSigDenominator=4) const
Given the current sample count (which can be extracted from ProcessData::processContext->projectTimeS...
Definition: SampleRateBasedClock.h:115
uint32 fRateLimitInSamples
Definition: SampleRateBasedClock.h:71
RateLimiter(uint32 iRateLimitInSamples=0)
Definition: SampleRateBasedClock.h:52
The purpose of this class is to deal with timing based on the sample rate and do the proper conversio...
Definition: SampleRateBasedClock.h:34