7#include "signals/hart_signal.hpp"
18template<
typename SampleType>
41 double durationSeconds = 1.0,
42 double startFrequencyHz = 20.0,
43 double endFrequencyHz = 20.0e3,
46 double initialPhaseRadians = 0.0
48 m_durationSeconds (durationSeconds),
49 m_startFrequencyHz (startFrequencyHz),
50 m_endFrequencyHz (endFrequencyHz),
53 m_initialPhaseRadians (
wrapPhase (initialPhaseRadians
)),
54 m_currentPhaseRadians (m_initialPhaseRadians),
56 m_isFixedFrequency (
floatsEqual (m_startFrequencyHz
, m_endFrequencyHz
)),
57 m_frequencyRatio (m_endFrequencyHz / m_startFrequencyHz)
59 if (durationSeconds < 0)
62 if (startFrequencyHz <= 0 || endFrequencyHz <= 0)
72 return SineSweep (durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
81 return SineSweep (m_durationSeconds, startFrequencyHz, m_endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
90 return SineSweep (m_durationSeconds, m_startFrequencyHz, endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
99 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, type, m_loop, m_initialPhaseRadians);
111 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, loop, m_initialPhaseRadians);
120 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, m_loop, initialPhaseRadians);
125 void prepare (
double sampleRateHz, size_t , size_t )
override
127 m_sampleRateHz = sampleRateHz;
128 m_durationFrames =
roundToSizeT (m_durationSeconds * m_sampleRateHz
);
133 if (m_generateSilence)
135 fillWithSilence (output);
139 for (size_t frame = 0; frame < output.getNumFrames(); ++frame)
141 const SampleType value = (SampleType) std::sin (m_currentPhaseRadians);
143 for (size_t channel = 0; channel < output.getNumChannels(); ++channel)
144 output[channel][frame] = value;
148 if (m_posFrames == m_durationFrames)
153 m_reverseFrequencyDirection = ! m_reverseFrequencyDirection;
159 fillWithSilence (output, frame + 1);
161 m_generateSilence =
true;
166 const double currentFrequencyHz = frequencyAtFrame (m_posFrames, m_reverseFrequencyDirection);
167 m_currentPhaseRadians +=
hart::twoPi * currentFrequencyHz / m_sampleRateHz;
168 m_currentPhaseRadians =
wrapPhase (m_currentPhaseRadians
);
175 m_currentPhaseRadians = m_initialPhaseRadians;
177 m_reverseFrequencyDirection =
false;
182 stream <<
"SineSweep ("
184 << m_durationSeconds <<
"_s, "
186 << m_startFrequencyHz <<
"_Hz, "
187 << m_endFrequencyHz <<
"_Hz, "
188 << (m_type ==
SweepType::linear ?
", SweepType::linear" :
"SweepType::log")
189 << (m_loop ==
Loop::yes ?
", Loop::yes)" :
", Loop::no)");
193 const double m_durationSeconds;
194 const double m_startFrequencyHz;
195 const double m_endFrequencyHz;
199 double m_sampleRateHz = 0.0;
200 size_t m_durationFrames = 0;
201 size_t m_posFrames = 0;
202 const double m_initialPhaseRadians;
203 double m_currentPhaseRadians;
204 bool m_generateSilence;
205 const bool m_isFixedFrequency;
206 const double m_frequencyRatio;
207 bool m_reverseFrequencyDirection =
false;
209 void fillWithSilence (
AudioBuffer<SampleType>& output, size_t startingFrame = 0)
211 if (startingFrame >= output.getNumFrames())
214 for (size_t channel = 0; channel < output.getNumChannels(); ++channel)
215 std::fill (output[channel] + startingFrame, output[channel] + output.getNumFrames(), (SampleType) 0);
218 double frequencyAtFrame (size_t offsetFrames,
bool reverseFrequencyDirection)
const
220 if (m_isFixedFrequency)
221 return m_startFrequencyHz;
223 hassert (offsetFrames < m_durationFrames);
224 const double offsetSeconds =
static_cast<
double> (offsetFrames) / m_sampleRateHz;
226 double portion = offsetSeconds / m_durationSeconds;
227 portion = reverseFrequencyDirection ? 1.0 - portion : portion;
230 return m_startFrequencyHz + (m_endFrequencyHz - m_startFrequencyHz) * portion;
233 return m_startFrequencyHz * std::pow (m_frequencyRatio, portion);
Container for audio data.
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=1.0, 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)