HART  0.2.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" // wrapPhase(), floatsEqual(), roundToSizeT(), Loop
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>
20 public Signal<SampleType, SineSweep<SampleType>>
21{
22public:
23 /// @brief Determines how to change the frequency
24 enum class SweepType
25 {
26 linear, ///< Linear sweep, for a white noise-like spectrum
27 log ///< Logarithmic sweep, for a pink noise-like spectrum
28 };
29
30 /// @brief Creates a sine sweep signal
31 /// @param durationSeconds Duration of sine sweep
32 /// @param startFrequencyHz Start frequency of the sine sweep
33 /// @param endFrequencyHz End frequency of the sine sweep
34 /// @param type Linear or Log frequency sweep, see @ref SweepType
35 /// @param loop If Loop::no is selected, the Signal will produce silence after duration;
36 /// if Loop::yes is selected, the signal will keep on going back and forth between
37 /// start and end frequencies (up and down) indefinitely.
38 /// @see hart::Loop
39 /// @param initialPhaseRadians Initial phase of the signal
41 double durationSeconds = 1.0,
42 double startFrequencyHz = 20.0,
43 double endFrequencyHz = 20.0e3,
44 SweepType type = SweepType::log,
45 Loop loop = Loop::no,
46 double initialPhaseRadians = 0.0
47 ):
48 m_durationSeconds (durationSeconds),
49 m_startFrequencyHz (startFrequencyHz),
50 m_endFrequencyHz (endFrequencyHz),
51 m_type (type),
52 m_loop (loop),
53 m_initialPhaseRadians (wrapPhase (initialPhaseRadians)),
54 m_currentPhaseRadians (m_initialPhaseRadians),
55 m_generateSilence (floatsEqual (durationSeconds, 0.0)),
56 m_isFixedFrequency (floatsEqual (m_startFrequencyHz, m_endFrequencyHz)),
57 m_frequencyRatio (m_endFrequencyHz / m_startFrequencyHz)
58 {
59 if (durationSeconds < 0)
60 HART_THROW_OR_RETURN_VOID (hart::ValueError, "Duration cannot be negative");
61
62 if (startFrequencyHz <= 0 || endFrequencyHz <= 0)
63 HART_THROW_OR_RETURN_VOID (hart::ValueError, "Frequencies must be positive");
64 }
65
66 /// @brief Returns a new SineSweep instance with specified duration
67 /// @details Handy if you want to skip specifying some arguments in the constructor
68 /// @param durationSeconds Duration of sine sweep
69 /// @return A new SineSweep instance with a specified parameter
70 SineSweep withDuration (double durationSeconds)
71 {
72 return SineSweep (durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
73 }
74
75 /// @brief Returns a new SineSweep instance with specified start frequency
76 /// @details Handy if you want to skip specifying some arguments in the constructor
77 /// @param startFrequencyHz Start frequency of the sine sweep
78 /// @return A new SineSweep instance with a specified parameter
79 SineSweep withStartFrequency (double startFrequencyHz)
80 {
81 return SineSweep (m_durationSeconds, startFrequencyHz, m_endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
82 }
83
84 /// @brief Returns a new SineSweep instance with specified end frequency
85 /// @details Handy if you want to skip specifying some arguments in the constructor
86 /// @param endFrequencyHz End frequency of the sine sweep
87 /// @return A new SineSweep instance with a specified parameter
88 SineSweep withEndFrequency (double endFrequencyHz)
89 {
90 return SineSweep (m_durationSeconds, m_startFrequencyHz, endFrequencyHz, m_type, m_loop, m_initialPhaseRadians);
91 }
92
93 /// @brief Returns a new SineSweep instance with specified sweep type
94 /// @details Handy if you want to skip specifying some arguments in the constructor
95 /// @param type Linear or Log frequency sweep, see @ref SweepType
96 /// @return A new SineSweep instance with a specified parameter
98 {
99 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, type, m_loop, m_initialPhaseRadians);
100 }
101
102 /// @brief Returns a new SineSweep instance with specified loop preference
103 /// @details Handy if you want to skip specifying some arguments in the constructor
104 /// @param loop If Loop::no is selected, the Signal will produce silence after duration;
105 /// if Loop::yes is selected, the signal will keep on going back and forth between
106 /// start and end frequencies (up and down) indefinitely.
107 /// @see hart::Loop
108 /// @return A new SineSweep instance with a specified parameter
110 {
111 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, loop, m_initialPhaseRadians);
112 }
113
114 /// @brief Returns a new SineSweep instance with specified initial phase
115 /// @details Handy if you want to skip specifying some arguments in the constructor
116 /// @param initialPhaseRadians Initial phase of the signal
117 /// @return A new SineSweep instance with a specified parameter
118 SineSweep withPhase (double initialPhaseRadians)
119 {
120 return SineSweep (m_durationSeconds, m_startFrequencyHz, m_endFrequencyHz, m_type, m_loop, initialPhaseRadians);
121 }
122
123 bool supportsNumChannels (size_t /*numChannels*/) const override { return true; }
124
125 void prepare (double sampleRateHz, size_t /*numOutputChannels*/, size_t /*maxBlockSizeFrames*/) override
126 {
127 m_sampleRateHz = sampleRateHz;
128 m_durationFrames = roundToSizeT (m_durationSeconds * m_sampleRateHz);
129 }
130
131 void renderNextBlock (AudioBuffer<SampleType>& output) override
132 {
133 if (m_generateSilence)
134 {
135 fillWithSilence (output);
136 return;
137 }
138
139 for (size_t frame = 0; frame < output.getNumFrames(); ++frame)
140 {
141 const SampleType value = (SampleType) std::sin (m_currentPhaseRadians);
142
143 for (size_t channel = 0; channel < output.getNumChannels(); ++channel)
144 output[channel][frame] = value;
145
146 ++m_posFrames;
147
148 if (m_posFrames == m_durationFrames)
149 {
150 if (m_loop == Loop::yes)
151 {
152 // Reverse frequency direction
153 m_reverseFrequencyDirection = ! m_reverseFrequencyDirection;
154 m_posFrames = 0;
155 }
156 else
157 {
158 // Stop generating the sine
159 fillWithSilence (output, frame + 1);
160 m_posFrames = 0;
161 m_generateSilence = true;
162 break;
163 }
164 }
165
166 const double currentFrequencyHz = frequencyAtFrame (m_posFrames, m_reverseFrequencyDirection);
167 m_currentPhaseRadians += hart::twoPi * currentFrequencyHz / m_sampleRateHz;
168 m_currentPhaseRadians = wrapPhase (m_currentPhaseRadians);
169 }
170 }
171
172 void reset() override
173 {
174 m_posFrames = 0;
175 m_currentPhaseRadians = m_initialPhaseRadians;
176 m_generateSilence = floatsEqual (m_durationSeconds, 0.0);
177 m_reverseFrequencyDirection = false;
178 }
179
180 void represent (std::ostream& stream) const override
181 {
182 stream << "SineSweep ("
183 << secPrecision
184 << m_durationSeconds << "_s, "
185 << hzPrecision
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)");
190 }
191
192private:
193 const double m_durationSeconds;
194 const double m_startFrequencyHz;
195 const double m_endFrequencyHz;
196 const SweepType m_type;
197 const Loop m_loop;
198
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;
208
209 void fillWithSilence (AudioBuffer<SampleType>& output, size_t startingFrame = 0)
210 {
211 if (startingFrame >= output.getNumFrames())
212 return;
213
214 for (size_t channel = 0; channel < output.getNumChannels(); ++channel)
215 std::fill (output[channel] + startingFrame, output[channel] + output.getNumFrames(), (SampleType) 0);
216 }
217
218 double frequencyAtFrame (size_t offsetFrames, bool reverseFrequencyDirection) const
219 {
220 if (m_isFixedFrequency)
221 return m_startFrequencyHz;
222
223 hassert (offsetFrames < m_durationFrames);
224 const double offsetSeconds = static_cast<double> (offsetFrames) / m_sampleRateHz;
225
226 double portion = offsetSeconds / m_durationSeconds;
227 portion = reverseFrequencyDirection ? 1.0 - portion : portion;
228
229 if (m_type == SweepType::linear)
230 return m_startFrequencyHz + (m_endFrequencyHz - m_startFrequencyHz) * portion;
231
232 // SweepType::logaritmic
233 return m_startFrequencyHz * std::pow (m_frequencyRatio, portion);
234 }
235};
236
238
239} // namespace hart
Container for audio data.
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 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)