openshot-audio  0.1.7
juce_NormalisableRange.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_NORMALISABLERANGE_H_INCLUDED
30 #define JUCE_NORMALISABLERANGE_H_INCLUDED
31 
32 
33 //==============================================================================
43 template<typename ValueType>
45 {
46 public:
48  NormalisableRange() noexcept : start(), end (1), interval(), skew (static_cast<ValueType> (1)) {}
49 
52  : start (other.start), end (other.end),
53  interval (other.interval), skew (other.skew)
54  {
55  checkInvariants();
56  }
57 
60  {
61  start = other.start;
62  end = other.end;
63  interval = other.interval;
64  skew = other.skew;
65  checkInvariants();
66  return *this;
67  }
68 
70  NormalisableRange (ValueType rangeStart,
71  ValueType rangeEnd,
72  ValueType intervalValue,
73  ValueType skewFactor) noexcept
74  : start (rangeStart), end (rangeEnd),
75  interval (intervalValue), skew (skewFactor)
76  {
77  checkInvariants();
78  }
79 
81  NormalisableRange (ValueType rangeStart,
82  ValueType rangeEnd,
83  ValueType intervalValue) noexcept
84  : start (rangeStart), end (rangeEnd),
85  interval (intervalValue), skew (static_cast<ValueType> (1))
86  {
87  checkInvariants();
88  }
89 
91  NormalisableRange (ValueType rangeStart,
92  ValueType rangeEnd) noexcept
93  : start (rangeStart), end (rangeEnd),
94  interval(), skew (static_cast<ValueType> (1))
95  {
96  checkInvariants();
97  }
98 
102  ValueType convertTo0to1 (ValueType v) const noexcept
103  {
104  ValueType proportion = (v - start) / (end - start);
105 
106  if (skew != static_cast<ValueType> (1))
107  proportion = std::pow (proportion, skew);
108 
109  return proportion;
110  }
111 
115  ValueType convertFrom0to1 (ValueType proportion) const noexcept
116  {
117  if (skew != static_cast<ValueType> (1) && proportion > ValueType())
118  proportion = std::exp (std::log (proportion) / skew);
119 
120  return start + (end - start) * proportion;
121  }
122 
125  ValueType snapToLegalValue (ValueType v) const noexcept
126  {
127  if (interval > ValueType())
128  v = start + interval * std::floor ((v - start) / interval + static_cast<ValueType> (0.5));
129 
130  if (v <= start || end <= start)
131  return start;
132 
133  if (v >= end)
134  return end;
135 
136  return v;
137  }
138 
140  ValueType start;
141 
143  ValueType end;
144 
146  ValueType interval;
147 
157  ValueType skew;
158 
159 private:
160  void checkInvariants() const
161  {
162  jassert (end > start);
163  jassert (interval >= ValueType());
164  jassert (skew > ValueType());
165  }
166 };
167 
168 
169 #endif // JUCE_NORMALISABLERANGE_H_INCLUDED
#define noexcept
Definition: juce_CompilerSupport.h:141
NormalisableRange(ValueType rangeStart, ValueType rangeEnd) noexcept
Definition: juce_NormalisableRange.h:91
ValueType skew
Definition: juce_NormalisableRange.h:157
NormalisableRange(ValueType rangeStart, ValueType rangeEnd, ValueType intervalValue) noexcept
Definition: juce_NormalisableRange.h:81
NormalisableRange() noexcept
Definition: juce_NormalisableRange.h:48
NormalisableRange & operator=(const NormalisableRange &other) noexcept
Definition: juce_NormalisableRange.h:59
Definition: juce_NormalisableRange.h:44
ValueType snapToLegalValue(ValueType v) const noexcept
Definition: juce_NormalisableRange.h:125
ValueType interval
Definition: juce_NormalisableRange.h:146
ValueType convertFrom0to1(ValueType proportion) const noexcept
Definition: juce_NormalisableRange.h:115
#define jassert(a)
Definition: juce_PlatformDefs.h:146
ValueType end
Definition: juce_NormalisableRange.h:143
ValueType start
Definition: juce_NormalisableRange.h:140
ValueType convertTo0to1(ValueType v) const noexcept
Definition: juce_NormalisableRange.h:102
NormalisableRange(const NormalisableRange &other) noexcept
Definition: juce_NormalisableRange.h:51
NormalisableRange(ValueType rangeStart, ValueType rangeEnd, ValueType intervalValue, ValueType skewFactor) noexcept
Definition: juce_NormalisableRange.h:70