marvin 0.0.1
Pure C++ audio helper library
 
Loading...
Searching...
No Matches
marvin_Conversions.h
Go to the documentation of this file.
1// ========================================================================================================
2// _______ _______ ______ ___ ___ _______ _______
3// | | | _ | __ \ | |_ _| | |
4// | | | < | |_| |_| |
5// |__|_|__|___|___|___|__|\_____/|_______|__|____|
6//
7// This file is part of the Marvin open source library and is licensed under the terms of the MIT License.
8//
9// ========================================================================================================
10
11#ifndef MARVIN_CONVERSIONS_H
12#define MARVIN_CONVERSIONS_H
14namespace marvin::math {
15
21 template <FloatType T>
22 [[nodiscard]] T msToSeconds(T ms) noexcept {
23 return ms / static_cast<T>(1000.0);
24 }
25
32 template <FloatType T>
33 [[nodiscard]] T secondsToSamples(T seconds, double sampleRate) noexcept {
34 return seconds * static_cast<T>(sampleRate);
35 }
36
43 template <FloatType T>
44 [[nodiscard]] T msToSamples(T ms, double sampleRate) noexcept {
45 const FloatType auto seconds = msToSeconds(ms);
46 return secondsToSamples(seconds, sampleRate);
47 }
48
55 template <FloatType T>
56 [[nodiscard]] T dbToGain(T db, T referenceMinDb = static_cast<T>(-100.0)) {
57 if (db > referenceMinDb) {
58 return std::pow(static_cast<T>(10.0), db * static_cast<T>(0.05));
59 } else {
60 return static_cast<T>(0.0);
61 }
62 }
63
70 template <FloatType T>
71 [[nodiscard]] T gainToDb(T gain, T minusInfDb = static_cast<T>(-100.0)) noexcept {
72 const auto clamped = std::clamp(gain, static_cast<T>(0.0), static_cast<T>(1.0));
73 if (clamped <= 0.0f) return minusInfDb;
74 const auto db = std::max(static_cast<T>(20.0) * std::log10(clamped), minusInfDb);
75 return db;
76 }
77
78} // namespace marvin::math
79
80#endif // INFERNO_MARVIN_CONVERSIONS_H
Contrains T to be either a float or a double.
Definition marvin_Concepts.h:27
Math helper functions and classes.
Definition marvin_Math.h:22
T msToSamples(T ms, double sampleRate) noexcept
Definition marvin_Conversions.h:44
T dbToGain(T db, T referenceMinDb=static_cast< T >(-100.0))
Definition marvin_Conversions.h:56
T msToSeconds(T ms) noexcept
Definition marvin_Conversions.h:22
T gainToDb(T gain, T minusInfDb=static_cast< T >(-100.0)) noexcept
Definition marvin_Conversions.h:71
T secondsToSamples(T seconds, double sampleRate) noexcept
Definition marvin_Conversions.h:33