-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy path1-oscillator.cpp
More file actions
150 lines (123 loc) · 5.45 KB
/
1-oscillator.cpp
File metadata and controls
150 lines (123 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// The MIT License (MIT)
//
// Copyright (c) 2024 Roth Michaels
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <mp-units/ext/format.h>
#ifdef MP_UNITS_IMPORT_STD
import std;
#else
#include <algorithm>
#include <cassert>
#include <iostream>
#endif
#ifdef MP_UNITS_MODULES
import mp_units;
#else
#include <mp-units/format.h>
#include <mp-units/systems/angular.h>
#include <mp-units/systems/isq.h>
#include <mp-units/systems/si.h>
#endif
#include "audio.h"
#include "wrapped_third_party_audio_api.h"
namespace {
using namespace mp_units;
//! A DSP generator class that generates sample values for a sine wave oscillator.
class sine_wave_osc {
public:
sine_wave_osc(const audio::musical_context& context, QuantityOf<isq::frequency> auto freq) :
m_context{context}, m_frequency{freq}
{
std::cout << MP_UNITS_STD_FMT::format(
"Created oscillator with starting frequency {} ({}) for sample rate {} at tempo {}\n", freq, m_frequency,
context.current_sample_rate, context.current_tempo);
}
quantity<si::hertz, float> get_frequency() const { return m_frequency; }
void set_frequency(QuantityOf<isq::frequency> auto freq)
{
m_frequency = freq;
std::cout << MP_UNITS_STD_FMT::format("Setting frequency to {} ({})\n", freq, m_frequency);
}
void set_period(QuantityOf<isq::time> auto period)
{
m_frequency = 1.f / value_cast<float>(period);
std::cout << MP_UNITS_STD_FMT::format("Setting period to {} (i.e. frequency to {})\n", period, m_frequency);
}
void set_period(QuantityOf<audio::beat_count> auto period)
{
std::cout << MP_UNITS_STD_FMT::format("Setting period to {} -- ", period);
set_period(value_cast<float>(period) / m_context.current_tempo);
}
quantity<audio::sample_value, float> operator()()
{
auto out = angular::sin(m_phase.quantity_from_zero());
m_phase += m_step;
return out;
}
void reset() { m_phase = phase_t{0.f * angular::radian}; }
private:
using phase_t = quantity_point<angular::radian, mp_units::default_point_origin(angular::radian), float>;
void update_step() { m_step = (m_frequency / m_context.current_sample_rate) * angular::revolution; }
audio::musical_context m_context;
quantity<si::hertz, float> m_frequency;
phase_t m_phase{0.f * angular::radian};
quantity<angular::radian, float> m_step;
};
} // namespace
int main()
{
using namespace mp_units::si::unit_symbols;
const auto context = audio::get_musical_context();
// Sine oscillators are sometimes used as a "low-frequency oscillator"
// (LFO) that runs at a frequency below the range of human hearing and
// is used a source of modulation for other paramters in an audio
// algorithm.
auto sin_gen = sine_wave_osc{context, 1 * Hz};
// Depending on the use-case sometimes an LFO will be set with a frequency in Hz
sin_gen.set_frequency(13 * Hz);
// for some use-cases it is more convient for a user to set the period
sin_gen.set_period(42 * s);
// and in some other use-cases setting the period in musical note duration is more intuitive
sin_gen.set_period(1 * audio::half_note);
// Our oscillator can be used to generate sample values for a buffer
// of audio samples. In this example we will create a buffer with
// duration equal to 2 measures of 4/4 music (i.e. 2 whole notes at
// the current tempo):
const auto beats = 2 * audio::whole_note;
const auto buffer_duration = value_cast<float>(beats) / context.current_tempo;
const auto buffer_size = (buffer_duration * context.current_sample_rate).in(audio::sample);
std::cout << MP_UNITS_STD_FMT::format("\nCreating buffer with size:\n\t{}\n\t{}\n\t{}\n\n", beats, buffer_duration,
buffer_size);
using buffer_t = std::vector<quantity<audio::sample_value, float>>;
auto buffer_1 = buffer_t(std::size_t(buffer_size.numerical_value_in(audio::sample)));
std::cout << MP_UNITS_STD_FMT::format("Filling buffer with values from LFO @ {}", sin_gen.get_frequency());
std::generate(begin(buffer_1), end(buffer_1), sin_gen);
assert(buffer_1.size() > 0u);
std::cout << MP_UNITS_STD_FMT::format("\nLFO Values:\n[{}", buffer_1[0u]);
for (std::size_t i = 1u; i < buffer_1.size(); ++i) {
std::cout << MP_UNITS_STD_FMT::format(", {}", buffer_1[i]);
}
std::cout << "]\n\n";
// generated values should be the same after resetting oscillator
sin_gen.reset();
auto buffer_2 = buffer_t(buffer_1.size());
std::generate(begin(buffer_2), end(buffer_2), sin_gen);
return buffer_1 == buffer_2;
}