8#include "signals/hart_signal.hpp"
19template<
typename SampleType>
43 double startFrequencyHz = 20.0,
44 double endFrequencyHz = 20.0e3,
47 double initialPhaseRadians = 0.0
49 m_durationSeconds (durationSeconds),
50 m_startFrequencyHz (startFrequencyHz),
51 m_endFrequencyHz (endFrequencyHz),
54 m_initialPhaseRadians (
wrapPhase (initialPhaseRadians
)),
55 m_currentPhaseRadians (m_initialPhaseRadians),
57 m_isFixedFrequency (
floatsEqual (m_startFrequencyHz
, m_endFrequencyHz
)),
58 m_frequencyRatio (m_endFrequencyHz / m_startFrequencyHz)
60 if (durationSeconds < 0)
63 if (startFrequencyHz <= 0 || endFrequencyHz <= 0)
73 return SineSweep (durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
82 return SineSweep (m_durationSeconds, startFrequencyHz, m_endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
91 return SineSweep (m_durationSeconds, m_startFrequencyHz, endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
100 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, type, m_loop, m_initialPhaseRadians);
112 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, loop, m_initialPhaseRadians);
121 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, m_loop, initialPhaseRadians);
126 void prepare (
double sampleRateHz, size_t , size_t )
override
128 m_sampleRateHz = sampleRateHz;
129 m_durationFrames =
roundToSizeT (m_durationSeconds * m_sampleRateHz
);
134 if (m_generateSilence)
136 fillWithSilence (output);
140 for (size_t frame = 0; frame < output.getNumFrames(); ++frame)
142 const SampleType value = (SampleType) std::sin (m_currentPhaseRadians);
144 for (size_t channel = 0; channel < output.getNumChannels(); ++channel)
145 output[channel][frame] = value;
149 if (m_posFrames == m_durationFrames)
154 m_reverseFrequencyDirection = ! m_reverseFrequencyDirection;
160 fillWithSilence (output, frame + 1);
162 m_generateSilence =
true;
167 const double currentFrequencyHz = frequencyAtFrame (m_posFrames, m_reverseFrequencyDirection);
168 m_currentPhaseRadians +=
hart::twoPi * currentFrequencyHz / m_sampleRateHz;
169 m_currentPhaseRadians =
wrapPhase (m_currentPhaseRadians
);
176 m_currentPhaseRadians = m_initialPhaseRadians;
178 m_reverseFrequencyDirection =
false;
183 stream <<
"SineSweep ("
185 << m_durationSeconds <<
"_s, "
187 << m_startFrequencyHz <<
"_Hz, "
188 << m_endFrequencyHz <<
"_Hz, "
189 << (m_type ==
SweepType::linear ?
", SweepType::linear" :
"SweepType::log")
190 << (m_loop ==
Loop::yes ?
", Loop::yes)" :
", Loop::no)");
194 const double m_durationSeconds;
195 const double m_startFrequencyHz;
196 const double m_endFrequencyHz;
200 double m_sampleRateHz = 0.0;
201 size_t m_durationFrames = 0;
202 size_t m_posFrames = 0;
203 const double m_initialPhaseRadians;
204 double m_currentPhaseRadians;
205 bool m_generateSilence;
206 const bool m_isFixedFrequency;
207 const double m_frequencyRatio;
208 bool m_reverseFrequencyDirection =
false;
210 void fillWithSilence (AudioBuffer<SampleType>& output, size_t startingFrame = 0)
212 if (startingFrame >= output.getNumFrames())
215 for (size_t channel = 0; channel < output.getNumChannels(); ++channel)
216 std::fill (output[channel] + startingFrame, output[channel] + output.getNumFrames(), (SampleType) 0);
219 double frequencyAtFrame (size_t offsetFrames,
bool reverseFrequencyDirection)
const
221 if (m_isFixedFrequency)
222 return m_startFrequencyHz;
224 hassert (offsetFrames < m_durationFrames);
225 const double offsetSeconds =
static_cast<
double> (offsetFrames) / m_sampleRateHz;
227 double portion = offsetSeconds / m_durationSeconds;
228 portion = reverseFrequencyDirection ? 1.0 - portion : portion;
231 return m_startFrequencyHz + (m_endFrequencyHz - m_startFrequencyHz) * portion;
234 return m_startFrequencyHz * std::pow (m_frequencyRatio, portion);
void renderNextBlock(AudioBuffer< SampleType > &output) override
Renders next block audio for the signal.
SineSweep withDuration(double durationSeconds)
Returns a new SineSweep instance with specified duration.
SineSweep withEndFrequency(double endFrequencyHz)
Returns a new SineSweep instance with specified end frequency.
SweepType
Determines how to change the frequency.
@ linear
Linear sweep, for a white noise-like spectrum.
@ log
Logarithmic sweep, for a pink noise-like spectrum.
SineSweep(double durationSeconds=CLIConfig::getInstance().getDefaultRenderDurationSeconds(), double startFrequencyHz=20.0, double endFrequencyHz=20.0e3, SweepType type=SweepType::log, Loop loop=Loop::no, double initialPhaseRadians=0.0)
Creates a sine sweep signal.
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 channels.
SineSweep withType(SweepType type)
Returns a new SineSweep instance with specified sweep type.
void prepare(double sampleRateHz, size_t, size_t) override
Prepare the signal for rendering.
SineSweep withLoop(Loop loop)
Returns a new SineSweep instance with specified loop preference.
void reset() override
Resets the Signal to initial state.
SineSweep withPhase(double initialPhaseRadians)
Returns a new SineSweep instance with specified initial phase.
SineSweep withStartFrequency(double startFrequencyHz)
Returns a new SineSweep instance with specified start frequency.
Thrown when an inappropriate value is encountered.
#define HART_THROW_OR_RETURN_VOID(ExceptionType, message)
Throws an exception if HART_DO_NOT_THROW_EXCEPTIONS is set, prints a message and returns otherwise.
#define hassert(condition)
Triggers a HartAssertException if the condition is false
std::ostream & secPrecision(std::ostream &stream)
Sets number of decimal places for values in seconds.
std::ostream & hzPrecision(std::ostream &stream)
Sets number of decimal places for values in hertz.
constexpr double twoPi
2 * pi
SampleType wrapPhase(const SampleType phaseRadians)
Keeps phase in 0..twoPi range.
static size_t roundToSizeT(SampleType x)
Rounds a floating point value to a size_t value.
static SampleType floatsEqual(SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
Compares two floating point numbers within a given tolerance.
Loop
Helper values for something that could loop, like a Signal.
#define HART_SIGNAL_DECLARE_ALIASES_FOR(ClassName)
Holds values set by the user via CLI interface.
double getDefaultRenderDurationSeconds() const
static CLIConfig & getInstance()
Get the singleton instance.