-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dd337f
commit 042ccbc
Showing
6 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#pragma once | ||
|
||
#include <grit/audio/noise/airwindows_vinyl_dither.hpp> | ||
#include <grit/audio/noise/dither.hpp> | ||
#include <grit/audio/noise/white_noise.hpp> | ||
#include <grit/audio/noise/white_noise.hpp> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#pragma once | ||
|
||
#include <etl/algorithm.hpp> | ||
#include <etl/array.hpp> | ||
#include <etl/cmath.hpp> | ||
#include <etl/concepts.hpp> | ||
#include <etl/random.hpp> | ||
|
||
namespace grit { | ||
|
||
template<etl::floating_point Float, typename URNG = etl::xoshiro128plusplus> | ||
struct AirWindowsVinylDither | ||
{ | ||
using value_type = Float; | ||
using seed_type = typename URNG::result_type; | ||
|
||
AirWindowsVinylDither() = default; | ||
explicit AirWindowsVinylDither(seed_type seed); | ||
|
||
auto setDeRez(Float deRez) -> void; | ||
[[nodiscard]] auto getDeRez() const -> Float; | ||
|
||
[[nodiscard]] auto operator()(Float x) -> Float; | ||
auto reset() -> void; | ||
|
||
private: | ||
auto advanceNoise() -> Float; | ||
|
||
Float _deRez{0}; | ||
Float _inScale{0}; | ||
Float _outScale{0}; | ||
|
||
URNG _rng{42}; | ||
etl::uniform_real_distribution<Float> _dist{Float(-0.5), Float(0.5)}; | ||
|
||
Float _nsOdd{0}; | ||
Float _prev{0}; | ||
etl::array<Float, 16> _ns{}; | ||
}; | ||
|
||
template<etl::floating_point Float, typename URNG> | ||
AirWindowsVinylDither<Float, URNG>::AirWindowsVinylDither(seed_type seed) : _rng{seed} | ||
{} | ||
|
||
template<etl::floating_point Float, typename URNG> | ||
auto AirWindowsVinylDither<Float, URNG>::setDeRez(Float deRez) -> void | ||
{ | ||
_deRez = deRez; | ||
|
||
auto scaleFactor = Float(32768.0); | ||
if (deRez > Float(0)) { | ||
scaleFactor *= etl::pow(Float(1) - deRez, Float(6)); | ||
} | ||
if (scaleFactor < Float(0.0001)) { | ||
scaleFactor = Float(0.0001); | ||
} | ||
|
||
auto outScale = scaleFactor; | ||
if (outScale < Float(8)) { | ||
outScale = Float(8); | ||
} | ||
|
||
_inScale = scaleFactor; | ||
_outScale = Float(1) / outScale; | ||
} | ||
|
||
template<etl::floating_point Float, typename URNG> | ||
auto AirWindowsVinylDither<Float, URNG>::getDeRez() const -> Float | ||
{ | ||
return _deRez; | ||
} | ||
|
||
template<etl::floating_point Float, typename URNG> | ||
auto AirWindowsVinylDither<Float, URNG>::operator()(Float x) -> Float | ||
{ | ||
auto const y = x * _inScale; | ||
|
||
auto absSample = advanceNoise(); | ||
absSample += y; | ||
|
||
if (_nsOdd > 0) { | ||
_nsOdd -= Float(0.97); | ||
} | ||
if (_nsOdd < 0) { | ||
_nsOdd += Float(0.97); | ||
} | ||
|
||
_nsOdd -= (_nsOdd * _nsOdd * _nsOdd * Float(0.475)); | ||
_nsOdd += _prev; | ||
|
||
absSample += (_nsOdd * Float(0.475)); | ||
auto const floor = etl::floor(absSample); | ||
|
||
_prev = floor - y; | ||
return floor * _outScale; | ||
} | ||
|
||
template<etl::floating_point Float, typename URNG> | ||
auto AirWindowsVinylDither<Float, URNG>::reset() -> void | ||
{ | ||
_nsOdd = Float(0); | ||
_prev = Float(0); | ||
etl::fill(_ns.begin(), _ns.end(), Float(0)); | ||
} | ||
|
||
template<etl::floating_point Float, typename URNG> | ||
auto AirWindowsVinylDither<Float, URNG>::advanceNoise() -> Float | ||
{ | ||
auto absSample = _dist(_rng); | ||
_ns[0] += absSample; | ||
_ns[0] *= Float(0.5); | ||
absSample -= _ns[0]; | ||
|
||
for (auto i{1U}; i < _ns.size(); ++i) { | ||
absSample += _dist(_rng); | ||
_ns[i] += absSample; | ||
_ns[i] *= Float(0.5); | ||
absSample -= _ns[i]; | ||
} | ||
|
||
return absSample; | ||
} | ||
|
||
} // namespace grit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "airwindows_vinyl_dither.hpp" | ||
|
||
#include <catch2/catch_approx.hpp> | ||
#include <catch2/catch_template_test_macros.hpp> | ||
|
||
TEMPLATE_TEST_CASE("grit/audio/noise: AirWindowsVinylDither", "", float, double) | ||
{ | ||
using Float = TestType; | ||
|
||
auto proc = grit::AirWindowsVinylDither<Float>{42}; | ||
proc.reset(); | ||
REQUIRE(proc(Float(0.0)) == Catch::Approx(Float(0.0))); | ||
|
||
proc.setDeRez(Float(0.5)); | ||
REQUIRE(proc.getDeRez() == Catch::Approx(Float(0.5))); | ||
REQUIRE(proc(Float(0.125)) != Float(0.0)); | ||
REQUIRE(proc(Float(0.111)) != Float(0.0)); | ||
REQUIRE(proc(Float(0.113)) != Float(0.0)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters