AME
ame_Conversion.hpp
Go to the documentation of this file.
1
10#pragma once
11
12#include "ame_Math.hpp"
13
14#include <algorithm>
15#include <cmath>
16#include <concepts>
17
18namespace ame
19{
23enum class NoteValue
24{
25 N1_DOT = 2880,
26 N1 = 1920,
27 N1_TRIPLET = 1280,
28 N2_DOT = 1440,
29 N2 = 960,
30 N2_TRIPLET = 640,
31 N4_DOT = 720,
32 N4 = 480,
33 N4_TRIPLET = 320,
34 N8_DOT = 360,
35 N8 = 240,
36 N8_TRIPLET = 160,
37 N16_DOT = 180,
38 N16 = 120,
39 N16_TRIPLET = 80,
40 N32_DOT = 90,
41 N32 = 60,
42 N32_TRIPLET = 40,
43 N64_DOT = 45,
44 N64 = 30,
45 N64_TRIPLET = 20,
46 // N128_DOT cannot be expressed as an integer number of ticks
47 N128 = 15,
48 N128_TRIPLET = 10
49};
50
51//==============================================================================
52//Constants
53inline constexpr int32_t Q23_MAX = 8388607;
54inline constexpr int32_t Q23_MIN = -8388608;
55inline constexpr float Q23_ABSMAX = 8388608.0f;
56
57//==============================================================================
58constexpr void floatToQ23 (const float src[], int32_t dest[], const uint32_t blockSize)
59{
60#if defined(__GNUC__) && (__GNUC__ >= 8)
61 #pragma GCC unroll 4
62#endif
63 for (uint32_t i = 0; i < blockSize; ++i)
64 {
65 dest[i] = std::clamp (static_cast<int32_t> (src[i] * Q23_MAX), Q23_MIN, Q23_MAX);
66 }
67}
68
69constexpr void q23ToFloat (const int32_t src[], float dest[], const uint32_t blockSize)
70{
71#if defined(__GNUC__) && (__GNUC__ >= 8)
72 #pragma GCC unroll 4
73#endif
74 for (uint32_t i = 0; i < blockSize; ++i)
75 {
76 dest[i] = static_cast<float> (src[i]) / Q23_ABSMAX;
77 }
78}
79
86constexpr void interleaveSamples (const float** source, float* dest, const uint_fast32_t numSamples, const uint_fast32_t numChannels)
87{
88 for (uint_fast32_t chan = 0; chan < numChannels; ++chan)
89 {
90 auto i = chan;
91 const auto* src = source[chan];
92
93 for (uint_fast32_t j = 0; j < numSamples; ++j)
94 {
95 dest[i] = src[j];
96 i += numChannels;
97 }
98 }
99}
100
107inline void deinterleaveSamples (const float* source, float** dest, const uint_fast32_t numSamples, const uint_fast32_t numChannels)
108{
109 for (uint_fast32_t chan = 0; chan < numChannels; ++chan)
110 {
111 uint_fast32_t i = chan;
112
113 for (uint_fast32_t k = 0; k < numSamples; ++k)
114 {
115 dest[chan][k] = source[i];
116 i += numChannels;
117 }
118 }
119}
120
121//==============================================================================
122// Frequency
123
132inline float semitoneToRatio (const float semitone)
133{
134 return std::pow (2.0f, semitone / 12.0f);
135}
136
141constexpr float freqToPeriod (const float freq) noexcept { return 1.0f / freq; }
142
147constexpr float periodToFreq (const float period) noexcept { return 1.0f / period; }
148
154inline float freqToMidi (const float freq, const float A3Freq = 440.0f)
155{
156 return 12.0f * std::log2f (freq / A3Freq) + 69.0f;
157}
158
164inline float midiToFreq (const float midiNote, const float A3Freq = 440.0f)
165{
166 return A3Freq * std::pow (2.0f, (midiNote - 69.0f) / 12.0f);
167}
168
176inline std::pair<float, float> cartopol (const float real, const float imag)
177{
178 const float amplitude = std::sqrt (real * real + imag * imag);
179 const float angle = std::atan2 (imag, real);
180 return { amplitude, angle };
181}
182
190inline std::pair<float, float> poltocar (const float amplitude, const float angle)
191{
192 const float real = amplitude * cosf (angle);
193 const float imag = amplitude * sinf (angle);
194 return { real, imag };
195}
196
197//==============================================================================
198//time
199
207constexpr float bpmToMs (float bpm) { return 60000.0f / bpm; }
208
216constexpr float msToBpm (float ms) { return 60000.0f / ms; }
217
218//==============================================================================
219//volume
229constexpr float decibelsToGain (const float dB) noexcept
230{
231 return dB > -100.0f
232 ? std::pow (10.0f, dB * 0.05f)
233 : 0.0f; // Outputs 0 if the input is less than -100dB
234}
235
245constexpr float gainToDecibels (const float gain) noexcept
246{
247 return gain > 0.00001f
248 ? std::log10 (gain) * 20.0f
249 : -100.0f; // Minimum output value -100dB
250}
251
252//==============================================================================
258template <std::floating_point FloatType>
259constexpr FloatType deg2rad (FloatType degree) noexcept
260{
261 return degree * (ame::pi<FloatType> / static_cast<FloatType> (180.0));
262}
263
269template <std::floating_point FloatType>
270constexpr FloatType rad2deg (FloatType radian) noexcept
271{
272 return radian * (static_cast<FloatType> (180.0) / ame::pi<FloatType>);
273}
274} // namespace ame
std::pair< float, float > poltocar(const float amplitude, const float angle)
極座表→直交座長変換.
Definition: ame_Conversion.hpp:190
constexpr float bpmToMs(float bpm)
Convert BPM to ms.
Definition: ame_Conversion.hpp:207
constexpr float msToBpm(float ms)
Convert ms to BPM.
Definition: ame_Conversion.hpp:216
constexpr void interleaveSamples(const float **source, float *dest, const uint_fast32_t numSamples, const uint_fast32_t numChannels)
Split channel→Interleave conversion.
Definition: ame_Conversion.hpp:86
constexpr FloatType rad2deg(FloatType radian) noexcept
Radian to Degree.
Definition: ame_Conversion.hpp:270
void deinterleaveSamples(const float *source, float **dest, const uint_fast32_t numSamples, const uint_fast32_t numChannels)
Interleave→Split channel conversion.
Definition: ame_Conversion.hpp:107
constexpr float gainToDecibels(const float gain) noexcept
Convert a gain level into a dBFS value.
Definition: ame_Conversion.hpp:245
float midiToFreq(const float midiNote, const float A3Freq=440.0f)
Convert MIDI note number to frequency.
Definition: ame_Conversion.hpp:164
NoteValue
Enum that associates note values with MIDI ticks.
Definition: ame_Conversion.hpp:24
@ N4
4n = 480 tick
float freqToMidi(const float freq, const float A3Freq=440.0f)
Convert frequency to MIDI note number.
Definition: ame_Conversion.hpp:154
float semitoneToRatio(const float semitone)
Convert semitones to frequency ratio.
Definition: ame_Conversion.hpp:132
constexpr float freqToPeriod(const float freq) noexcept
Convert frequency to period.
Definition: ame_Conversion.hpp:141
std::pair< float, float > cartopol(const float real, const float imag)
直交座標→極座標変換.
Definition: ame_Conversion.hpp:176
constexpr float periodToFreq(const float period) noexcept
Convert period to frequency.
Definition: ame_Conversion.hpp:147
constexpr FloatType deg2rad(FloatType degree) noexcept
Degree to Radians.
Definition: ame_Conversion.hpp:259
constexpr float decibelsToGain(const float dB) noexcept
Convert a dBFS value to to its equivalent gain level.
Definition: ame_Conversion.hpp:229
Math functions.