Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
pitch.hpp
Go to the documentation of this file.
1/**
2 * @file pitch.hpp
3 * @brief Pitch as octaves and ratios: from an interval to a frequency and back
4 *
5 * An interval is a number of octaves, continuous, or the frequency ratio it
6 * stands for: r = 2^octaves, so 1 octave is 2:1 and log2(3/2) octaves is a
7 * just fifth. A frequency is a reference moved by an interval, in the
8 * reference's own unit, Hz or cycles per frame. Nothing here holds a note,
9 * a scale, a division of the octave or a reference pitch: a tuning that
10 * maps keys or degrees to intervals sits above these functions
11 * (sound-byte-libs#146).
12 *
13 * Plain math, no clamping: a non-finite input gives a non-finite output, and
14 * octaves_from_ratio() needs a ratio above 0.
15 *
16 * @code
17 * const float f = sbl::dsp::math::frequency(tune, pitch_octaves); // tune * 2^octaves
18 * const float fifth = sbl::dsp::math::octaves_from_ratio(1.5f); // 0.585 octaves
19 * @endcode
20 */
21
22#ifndef SBL_DSP_MATH_PITCH_HPP_
23#define SBL_DSP_MATH_PITCH_HPP_
24
25#include <cmath>
26
27namespace sbl::dsp::math {
28
29/**
30 * @brief The frequency ratio of an interval: 2^octaves.
31 *
32 * @param octaves Interval in octaves
33 * @return Frequency ratio, above 0 for a finite input
34 */
35inline float ratio_from_octaves(float octaves) { return std::exp2(octaves); }
36
37/**
38 * @brief The interval of a frequency ratio, in octaves: log2(ratio).
39 *
40 * @param ratio Frequency ratio, above 0
41 * @return Interval in octaves
42 */
43inline float octaves_from_ratio(float ratio) { return std::log2(ratio); }
44
45/**
46 * @brief A reference frequency moved by an interval in octaves.
47 *
48 * @param reference Reference frequency, in Hz or cycles per frame
49 * @param octaves Interval in octaves
50 * @return reference * 2^octaves, in the reference's unit
51 */
52inline float frequency(float reference, float octaves) { return reference * std::exp2(octaves); }
53
54} // namespace sbl::dsp::math
55
56#endif // SBL_DSP_MATH_PITCH_HPP_
Math for the audio stack.
Definition crossfade.hpp:18
float octaves_from_ratio(float ratio)
The interval of a frequency ratio, in octaves: log2(ratio).
Definition pitch.hpp:43
float frequency(float reference, float octaves)
A reference frequency moved by an interval in octaves.
Definition pitch.hpp:52
float ratio_from_octaves(float octaves)
The frequency ratio of an interval: 2^octaves.
Definition pitch.hpp:35