sequencer
oscillator.hpp
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include <iostream>
7 namespace sequencer::audio
8 {
10  {
11  sine,
12  triangular,
13  square,
14  saw,
15  pulse,
16  ramp,
17  noise
18  };
19 
21  {
22  public:
23  template < class FM >
24  double operator()( double t, FM fm ) const noexcept
25  {
26  switch ( wave_form_ )
27  {
29  return signal( wave_form::sine, t, frequency_ + fm( t ) );
31  return signal( wave_form::triangular, t, frequency_ + fm( t ) );
33  return signal( wave_form::square, t, frequency_ + fm( t ) );
35  return signal( wave_form::saw, t, frequency_ + fm( t ) );
37  return signal( wave_form::pulse_t{pulse_length_}, t, frequency_ + fm( t ) );
39  return signal( wave_form::ramp_t{1.0}, t, frequency_ + fm( t ) );
41  return amplitude_ * wave_form::random();
42  }
43  }
44 
45  double operator()( double t ) const noexcept
46  {
47  return ( *this )( t, []( auto ) { return 0; } );
48  }
49 
51  {
52  wave_form_ = wave_form;
53  }
54 
55  double frequency() const noexcept
56  {
57  return frequency_;
58  }
59 
60  void set_frequency( double frequency ) noexcept
61  {
62  frequency_ = frequency;
63  }
64 
65  void set_amplitude( double amplitude ) noexcept
66  {
67  amplitude_ = amplitude;
68  }
69 
70  void set_phase( double phase ) noexcept
71  {
72  phase_ = phase;
73  }
74 
75  void set_pulse_length( double pulse_length ) noexcept
76  {
77  pulse_length_ = pulse_length;
78  }
79 
80  private:
81  template < class F >
82  double signal( F f, double t, double freq ) const
83  {
84  return amplitude_ * f( t * freq + phase_ / freq );
85  }
86 
88  copyable_atomic< double > frequency_{100};
89  copyable_atomic< double > amplitude_{1};
90  copyable_atomic< double > phase_{0};
91  copyable_atomic< double > pulse_length_{50};
92  };
93 } // namespace sequencer::audio
double operator()(double t) const noexcept
Definition: oscillator.hpp:45
const auto F
Definition: note.hpp:40
void set_phase(double phase) noexcept
Definition: oscillator.hpp:70
Definition: oscillator.hpp:20
void set_frequency(double frequency) noexcept
Definition: oscillator.hpp:60
double frequency() const noexcept
Definition: oscillator.hpp:55
oscillator_wave_form
Definition: oscillator.hpp:9
Definition: wave_form.hpp:117
Definition: delay.hpp:12
double operator()(double t, FM fm) const noexcept
Definition: oscillator.hpp:24
void set_wave_form(oscillator_wave_form wave_form)
Definition: oscillator.hpp:50
void set_amplitude(double amplitude) noexcept
Definition: oscillator.hpp:65
Definition: wave_form.hpp:20
double random()
Definition: wave_form.hpp:134
void set_pulse_length(double pulse_length) noexcept
Definition: oscillator.hpp:75