7#include "signals/hart_signal.hpp"
18template<
typename SampleType>
46 double durationSeconds = 1.0,
47 double startFrequencyHz = 20.0,
48 double endFrequencyHz = 20.0e3,
51 double initialPhaseRadians = 0.0
53 m_durationSeconds (durationSeconds),
54 m_startFrequencyHz (startFrequencyHz),
55 m_endFrequencyHz (endFrequencyHz),
58 m_initialPhaseRadians (
wrapPhase (initialPhaseRadians
)),
59 m_currentPhaseRadians (m_initialPhaseRadians),
61 m_isFixedFrequency (
floatsEqual (m_startFrequencyHz
, m_endFrequencyHz
)),
62 m_frequencyRatio (m_endFrequencyHz / m_startFrequencyHz)
64 if (durationSeconds < 0)
67 if (startFrequencyHz <= 0 || endFrequencyHz <= 0)
77 return SineSweep (durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
86 return SineSweep (m_durationSeconds, startFrequencyHz, m_endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
95 return SineSweep (m_durationSeconds, m_startFrequencyHz, endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
104 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, type, m_loop, m_initialPhaseRadians);
115 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, loop, m_initialPhaseRadians);
124 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, m_loop, initialPhaseRadians);
129 void prepare (
double sampleRateHz, size_t , size_t )
override
131 m_sampleRateHz = sampleRateHz;
132 m_durationFrames =
roundToSizeT (m_durationSeconds * m_sampleRateHz
);
137 if (m_generateSilence)
139 fillWithSilence (output);
143 for (size_t frame = 0; frame < output.getNumFrames(); ++frame)
145 const SampleType value = (SampleType) std::sin (m_currentPhaseRadians);
147 for (size_t channel = 0; channel < output.getNumChannels(); ++channel)
148 output[channel][frame] = value;
152 if (m_posFrames == m_durationFrames)
154 if (m_loop ==
Loop::yes)
157 m_reverseFrequencyDirection = ! m_reverseFrequencyDirection;
163 fillWithSilence (output, frame + 1);
165 m_generateSilence =
true;
170 const double currentFrequencyHz = frequencyAtFrame (m_posFrames, m_reverseFrequencyDirection);
171 m_currentPhaseRadians +=
hart::twoPi * currentFrequencyHz / m_sampleRateHz;
172 m_currentPhaseRadians =
wrapPhase (m_currentPhaseRadians
);
179 m_currentPhaseRadians = m_initialPhaseRadians;
181 m_reverseFrequencyDirection =
false;
186 stream <<
"SineSweep ("
188 << m_durationSeconds <<
"_s, "
190 << m_startFrequencyHz <<
"_Hz, "
191 << m_endFrequencyHz <<
"_Hz, "
192 << (m_type ==
SweepType::linear ?
", SweepType::linear" :
"SweepType::log")
193 << (m_loop ==
Loop::yes ?
", Loop::yes)" :
", Loop::no)");
199 const double m_durationSeconds;
200 const double m_startFrequencyHz;
201 const double m_endFrequencyHz;
205 double m_sampleRateHz = 0.0;
206 size_t m_durationFrames = 0;
207 size_t m_posFrames = 0;
208 const double m_initialPhaseRadians;
209 double m_currentPhaseRadians;
210 bool m_generateSilence;
211 const bool m_isFixedFrequency;
212 const double m_frequencyRatio;
213 bool m_reverseFrequencyDirection =
false;
215 void fillWithSilence (
AudioBuffer<SampleType>& output, size_t startingFrame = 0)
217 if (startingFrame >= output.getNumFrames())
220 for (size_t channel = 0; channel < output.getNumChannels(); ++channel)
221 std::fill (output[channel] + startingFrame, output[channel] + output.getNumFrames(), (SampleType) 0);
224 double frequencyAtFrame (size_t offsetFrames,
bool reverseFrequencyDirection)
const
226 if (m_isFixedFrequency)
227 return m_startFrequencyHz;
229 hassert (offsetFrames < m_durationFrames);
230 const double offsetSeconds =
static_cast<
double> (offsetFrames) / m_sampleRateHz;
232 double portion = offsetSeconds / m_durationSeconds;
233 portion = reverseFrequencyDirection ? 1.0 - portion : portion;
236 return m_startFrequencyHz + (m_endFrequencyHz - m_startFrequencyHz) * portion;
239 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=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 cchannels.
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.
Loop
Determines what to do after frequency sweep is done.
@ no
Stop after finishing one sweep.
@ yes
Keep on sweeping back and forth.
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.
#define HART_SIGNAL_DEFINE_COPY_AND_MOVE(ClassName)
Defines hart::Signal::copy() and hart::Signal::move() methods.
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.
#define HART_THROW_OR_RETURN_VOID(ExceptionType, message)
#define hassert(condition)
#define HART_SIGNAL_DECLARE_ALIASES_FOR(ClassName)