HART  0.1.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_sine_sweep.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4
7#include "signals/hart_signal.hpp"
8#include "hart_utils.hpp"
9
10namespace hart {
11
12/// @brief Produces a sine sweep
13/// @details Outputs a signal at unity gain (-1.0..+1.0), linear or log sweep, up or down.
14/// Tip: If you want to get an low-high-low or a high-low-high sweep, set `loop` to Loop::yes,
15/// and duration of host's signal (see @ref AudioTestBuilder::withDuration())
16/// to 2x ```durationSeconds``` of this signal.
17/// @ingroup Signals
18template<typename SampleType>
19class SineSweep : public Signal<SampleType>
20{
21public:
22 /// @brief Determines what to do after frequency sweep is done
23 enum class Loop
24 {
25 no, ///< Stop after finishing one sweep
26 yes ///< Keep on sweeping back and forth
27 };
28
29 /// @brief Determines how to change the frequency
30 enum class SweepType
31 {
32 linear, ///< Linear sweep, for a white noise-like spectrum
33 log ///< Logarithmic sweep, for a pink noise-like spectrum
34 };
35
36 /// @brief Creates a sine sweep signal
37 /// @param durationSeconds Duration of sine sweep
38 /// @param startFrequencyHz Start frequency of the sine sweep
39 /// @param endFrequencyHz End frequency of the sine sweep
40 /// @param type Linear or Log frequency sweep, see @ref SweepType
41 /// @param loop If Loop::no is selected, the Signal will produce silence after duration;
42 /// if Loop::yes is selected, the signal will keep on going back and forth between
43 /// start and end frequencies (up and down) indefinitely.
44 /// @param initialPhaseRadians Initial phase of the signal
46 double durationSeconds = 1.0,
47 double startFrequencyHz = 20.0,
48 double endFrequencyHz = 20.0e3,
49 SweepType type = SweepType::log,
50 Loop loop = Loop::no,
51 double initialPhaseRadians = 0.0
52 ):
53 m_durationSeconds (durationSeconds),
54 m_startFrequencyHz (startFrequencyHz),
55 m_endFrequencyHz (endFrequencyHz),
56 m_type (type),
57 m_loop (loop),
58 m_initialPhaseRadians (wrapPhase (initialPhaseRadians)),
59 m_currentPhaseRadians (m_initialPhaseRadians),
60 m_generateSilence (floatsEqual (durationSeconds, 0.0)),
61 m_isFixedFrequency (floatsEqual (m_startFrequencyHz, m_endFrequencyHz)),
62 m_frequencyRatio (m_endFrequencyHz / m_startFrequencyHz)
63 {
64 if (durationSeconds < 0)
65 HART_THROW_OR_RETURN_VOID (hart::ValueError, "Duration cannot be negative");
66
67 if (startFrequencyHz <= 0 || endFrequencyHz <= 0)
68 HART_THROW_OR_RETURN_VOID (hart::ValueError, "Frequencies must be positive");
69 }
70
71 /// @brief Returns a new SineSweep instance with specified duration
72 /// @details Handy if you want to skip specifying some arguments in the constructor
73 /// @param durationSeconds Duration of sine sweep
74 /// @return A new SineSweep instance with a specified parameter
75 SineSweep withDuration (double durationSeconds)
76 {
77 return SineSweep (durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
78 }
79
80 /// @brief Returns a new SineSweep instance with specified start frequency
81 /// @details Handy if you want to skip specifying some arguments in the constructor
82 /// @param startFrequencyHz Start frequency of the sine sweep
83 /// @return A new SineSweep instance with a specified parameter
84 SineSweep withStartFrequency (double startFrequencyHz)
85 {
86 return SineSweep (m_durationSeconds, startFrequencyHz, m_endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
87 }
88
89 /// @brief Returns a new SineSweep instance with specified end frequency
90 /// @details Handy if you want to skip specifying some arguments in the constructor
91 /// @param endFrequencyHz End frequency of the sine sweep
92 /// @return A new SineSweep instance with a specified parameter
93 SineSweep withEndFrequency (double endFrequencyHz)
94 {
95 return SineSweep (m_durationSeconds, m_startFrequencyHz, endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
96 }
97
98 /// @brief Returns a new SineSweep instance with specified sweep type
99 /// @details Handy if you want to skip specifying some arguments in the constructor
100 /// @param type Linear or Log frequency sweep, see @ref SweepType
101 /// @return A new SineSweep instance with a specified parameter
103 {
104 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, type, m_loop, m_initialPhaseRadians);
105 }
106
107 /// @brief Returns a new SineSweep instance with specified loop preference
108 /// @details Handy if you want to skip specifying some arguments in the constructor
109 /// @param loop If Loop::no is selected, the Signal will produce silence after duration;
110 /// if Loop::yes is selected, the signal will keep on going back and forth between
111 /// start and end frequencies (up and down) indefinitely.
112 /// @return A new SineSweep instance with a specified parameter
114 {
115 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, loop, m_initialPhaseRadians);
116 }
117
118 /// @brief Returns a new SineSweep instance with specified initial phase
119 /// @details Handy if you want to skip specifying some arguments in the constructor
120 /// @param initialPhaseRadians Initial phase of the signal
121 /// @return A new SineSweep instance with a specified parameter
122 SineSweep withPhase (double initialPhaseRadians)
123 {
124 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, m_loop, initialPhaseRadians);
125 }
126
127 bool supportsNumChannels (size_t /*numChannels*/) const override { return true; }
128
129 void prepare (double sampleRateHz, size_t /*numOutputChannels*/, size_t /*maxBlockSizeFrames*/) override
130 {
131 m_sampleRateHz = sampleRateHz;
132 m_durationFrames = roundToSizeT (m_durationSeconds * m_sampleRateHz);
133 }
134
135 void renderNextBlock (AudioBuffer<SampleType>& output) override
136 {
137 if (m_generateSilence)
138 {
139 fillWithSilence (output);
140 return;
141 }
142
143 for (size_t frame = 0; frame < output.getNumFrames(); ++frame)
144 {
145 const SampleType value = (SampleType) std::sin (m_currentPhaseRadians);
146
147 for (size_t channel = 0; channel < output.getNumChannels(); ++channel)
148 output[channel][frame] = value;
149
150 ++m_posFrames;
151
152 if (m_posFrames == m_durationFrames)
153 {
154 if (m_loop == Loop::yes)
155 {
156 // Reverse frequency direction
157 m_reverseFrequencyDirection = ! m_reverseFrequencyDirection;
158 m_posFrames = 0;
159 }
160 else
161 {
162 // Stop generating the sine
163 fillWithSilence (output, frame + 1);
164 m_posFrames = 0;
165 m_generateSilence = true;
166 break;
167 }
168 }
169
170 const double currentFrequencyHz = frequencyAtFrame (m_posFrames, m_reverseFrequencyDirection);
171 m_currentPhaseRadians += hart::twoPi * currentFrequencyHz / m_sampleRateHz;
172 m_currentPhaseRadians = wrapPhase (m_currentPhaseRadians);
173 }
174 }
175
176 void reset() override
177 {
178 m_posFrames = 0;
179 m_currentPhaseRadians = m_initialPhaseRadians;
180 m_generateSilence = floatsEqual (m_durationSeconds, 0.0);
181 m_reverseFrequencyDirection = false;
182 }
183
184 void represent (std::ostream& stream) const override
185 {
186 stream << "SineSweep ("
187 << secPrecision
188 << m_durationSeconds << "_s, "
189 << hzPrecision
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)");
194 }
195
197
198private:
199 const double m_durationSeconds;
200 const double m_startFrequencyHz;
201 const double m_endFrequencyHz;
202 const SweepType m_type;
203 const Loop m_loop;
204
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;
214
215 void fillWithSilence (AudioBuffer<SampleType>& output, size_t startingFrame = 0)
216 {
217 if (startingFrame >= output.getNumFrames())
218 return;
219
220 for (size_t channel = 0; channel < output.getNumChannels(); ++channel)
221 std::fill (output[channel] + startingFrame, output[channel] + output.getNumFrames(), (SampleType) 0);
222 }
223
224 double frequencyAtFrame (size_t offsetFrames, bool reverseFrequencyDirection) const
225 {
226 if (m_isFixedFrequency)
227 return m_startFrequencyHz;
228
229 hassert (offsetFrames < m_durationFrames);
230 const double offsetSeconds = static_cast<double> (offsetFrames) / m_sampleRateHz;
231
232 double portion = offsetSeconds / m_durationSeconds;
233 portion = reverseFrequencyDirection ? 1.0 - portion : portion;
234
235 if (m_type == SweepType::linear)
236 return m_startFrequencyHz + (m_endFrequencyHz - m_startFrequencyHz) * portion;
237
238 // SweepType::logaritmic
239 return m_startFrequencyHz * std::pow (m_frequencyRatio, portion);
240 }
241};
242
244
245} // namespace hart
Base class for signals.
Produces a sine sweep.
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)