HART  0.1.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_whitenoise.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <memory>
5#include <random>
6#include <string>
7
9#include "signals/hart_signal.hpp"
10
11namespace hart
12{
13
14/// @brief Produces deterministic white noise
15/// @details Outputs a signal uniformly distrubuted between -1.0 and +1.0, thus peaking below 0dB
16/// @ingroup Signals
17template<typename SampleType>
18class WhiteNoise : public Signal<SampleType>
19{
20public:
21
22 /// @brief Creates a Signal that produces white noise
23 /// @param randomSeed Seed for the RNG
24 /// @details Two signals with the same seed are guaranteed to produce the identical audio
26 m_randomSeed (randomSeed)
27 {
28 reset();
29 }
30
31 bool supportsNumChannels (size_t /* numChannels */) const override { return true; };
32
33 void prepare (double /*sampleRateHz*/, size_t numOutputChannels, size_t /*maxBlockSizeFrames*/) override
34 {
35 this->setNumChannels (numOutputChannels);
36 }
37
38 void renderNextBlock (AudioBuffer<SampleType>& output) override
39 {
40 for (size_t frame = 0; frame < output.getNumFrames(); ++frame)
41 for (size_t channel = 0; channel < this->getNumChannels(); ++channel)
42 output[channel][frame] = m_uniformRealDistribution (m_randomNumberGenerator);
43 }
44
45 /// @copybrief Signal::reset()
46 /// @details After resetting, WhiteNoise is guaranteed to produce identical audio to the one produced after instantiation
47 void reset() override
48 {
49 m_randomNumberGenerator = std::mt19937 (m_randomSeed);
50 m_uniformRealDistribution.reset();
51 }
52
53 void represent (std::ostream& stream) const override
54 {
55 stream << "WhiteNoise (" << m_randomSeed << ")";
56 }
57
59
60private:
61 const uint_fast32_t m_randomSeed;
62 std::mt19937 m_randomNumberGenerator;
63 std::uniform_real_distribution<SampleType> m_uniformRealDistribution {(SampleType) -1, (SampleType) 1};
64};
65
67
68} // namespace hart
Base class for signals.
Produces deterministic white noise.
void renderNextBlock(AudioBuffer< SampleType > &output) override
Renders next block audio for the signal.
void prepare(double, size_t numOutputChannels, size_t) override
Prepare the signal for rendering.
void represent(std::ostream &stream) const override
Makes a text representation of this Signal for test failure outputs.
bool supportsNumChannels(size_t) const override
Tells the host whether this Signal is capable of generating audio for a certain amount of cchannels.
WhiteNoise(uint_fast32_t randomSeed=CLIConfig::getInstance().getRandomSeed())
Creates a Signal that produces white noise.
void reset() override
Resets the Signal to initial state.
#define HART_SIGNAL_DEFINE_COPY_AND_MOVE(ClassName)
Defines hart::Signal::copy() and hart::Signal::move() methods.
#define HART_SIGNAL_DECLARE_ALIASES_FOR(ClassName)
uint_fast32_t getRandomSeed()
static CLIConfig & getInstance()