diff options
Diffstat (limited to 'source/core')
-rw-r--r-- | source/core/StarAudio.cpp | 20 | ||||
-rw-r--r-- | source/core/StarAudio.hpp | 9 |
2 files changed, 29 insertions, 0 deletions
diff --git a/source/core/StarAudio.cpp b/source/core/StarAudio.cpp index 4f07d05..2dfdaee 100644 --- a/source/core/StarAudio.cpp +++ b/source/core/StarAudio.cpp @@ -13,6 +13,26 @@ namespace Star { +float const DefaultPerceptualRangeDb = 40.f; +float const DefaultPerceptualBoostRangeDb = 6.f; +// https://github.com/discord/perceptual +float perceptualToAmplitude(float perceptual, float normalizedMax, float range, float boostRange) { + if (perceptual == 0.f) return 0.f; + float dB = perceptual > normalizedMax + ? ((perceptual - normalizedMax) / normalizedMax) * boostRange + : (perceptual / normalizedMax) * range - range; + return normalizedMax * pow(10.f, dB / 20.f); +} + +float amplitudeToPerceptual(float amp, float normalizedMax, float range, float boostRange) { + if (amp == 0.f) return 0.f; + float const dB = 20.f * log10(amp / normalizedMax); + float perceptual = dB > 0.f + ? dB / boostRange + 1 + : (range + dB) / range; + return normalizedMax * perceptual; +} + namespace { struct WaveData { ByteArrayPtr byteArray; diff --git a/source/core/StarAudio.hpp b/source/core/StarAudio.hpp index cf8f91f..571aaf3 100644 --- a/source/core/StarAudio.hpp +++ b/source/core/StarAudio.hpp @@ -4,6 +4,15 @@ namespace Star { +extern float const DefaultPerceptualRangeDb; +extern float const DefaultPerceptualBoostRangeDb; + +float perceptualToAmplitude(float perceptual, float normalizedMax = 1.f, + float range = DefaultPerceptualRangeDb, float boostRange = DefaultPerceptualBoostRangeDb); + +float amplitudeToPerceptual(float amp, float normalizedMax = 1.f, + float range = DefaultPerceptualRangeDb, float boostRange = DefaultPerceptualBoostRangeDb); + STAR_CLASS(CompressedAudioImpl); STAR_CLASS(UncompressedAudioImpl); STAR_CLASS(Audio); |