sequencer
note.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sequencer/assert.hpp>
4 
5 #include <cassert>
6 #include <cmath>
7 
8 namespace sequencer::audio
9 {
10  enum class half_note_t : int
11  {
12  };
13 
14  constexpr int to_int( half_note_t half_note ) noexcept
15  {
16  return static_cast< int >( half_note );
17  }
18 
19  inline double increase_by_half_notes( double frequency, half_note_t half_notes ) noexcept
20  {
21  return frequency * std::pow( 2, to_int( half_notes ) / 12.0 );
22  }
23 
24  inline double increase_by_half_notes( double frequency, double half_notes ) noexcept
25  {
26  return frequency * std::pow( 2, half_notes / 12 );
27  }
28 
29  namespace base_notes
30  {
31  // clang-format off
32  constexpr auto A = 440.0;
33  const auto Ais = increase_by_half_notes(A, half_note_t{1});
34  const auto B = increase_by_half_notes(A, half_note_t{2});
35  const auto C = increase_by_half_notes(A, half_note_t{3});
36  const auto Cis = increase_by_half_notes(A, half_note_t{4});
37  const auto D = increase_by_half_notes(A, half_note_t{5});
38  const auto Dis = increase_by_half_notes(A, half_note_t{6});
39  const auto E = increase_by_half_notes(A, half_note_t{7});
40  const auto F = increase_by_half_notes(A, half_note_t{8});
41  const auto Fis = increase_by_half_notes(A, half_note_t{9});
42  const auto G = increase_by_half_notes(A, half_note_t{10});
43  const auto Gis = increase_by_half_notes(A, half_note_t{11});
44  // clang-format on
45  } // namespace base_notes
46 
47  class note_t
48  {
49  public:
50  constexpr note_t() noexcept = default;
51 
52  constexpr explicit note_t( double base_note, int octave_offset = 0 ) noexcept
53  : base_note_( base_note )
54  {
55  set_octave_offset( octave_offset );
56  }
57 
58  constexpr void set_base_note( double base_note ) noexcept
59  {
60  base_note_ = base_note;
61  }
62 
63  constexpr void set_octave_offset( int octave_offset ) noexcept
64  {
65  octave_multiplier_ = std::pow( 2, octave_offset );
66  }
67 
68  constexpr double frequency() const noexcept
69  {
70  return base_note_ * octave_multiplier_;
71  }
72 
73  private:
74  double base_note_{base_notes::A};
75  double octave_multiplier_{1};
76  };
77 
78  inline double get_harmonic( double frequency, int n )
79  {
80  SEQUENCER_ASSERT( n > 0 )
81  return n * frequency;
82  }
83 } // namespace sequencer::audio
const auto C
Definition: note.hpp:35
Definition: note.hpp:47
note_t
Definition: note.hpp:11
const auto F
Definition: note.hpp:40
const auto Fis
Definition: note.hpp:41
constexpr void set_octave_offset(int octave_offset) noexcept
Definition: note.hpp:63
#define SEQUENCER_ASSERT(cond)
Definition: assert.hpp:8
const auto Dis
Definition: note.hpp:38
double increase_by_half_notes(double frequency, half_note_t half_notes) noexcept
Definition: note.hpp:19
constexpr double frequency() const noexcept
Definition: note.hpp:68
Definition: delay.hpp:12
const auto Gis
Definition: note.hpp:43
constexpr void set_base_note(double base_note) noexcept
Definition: note.hpp:58
const auto Cis
Definition: note.hpp:36
const auto D
Definition: note.hpp:37
const auto Ais
Definition: note.hpp:33
double get_harmonic(double frequency, int n)
Definition: note.hpp:78
const auto G
Definition: note.hpp:42
constexpr note_t(double base_note, int octave_offset=0) noexcept
Definition: note.hpp:52
const auto E
Definition: note.hpp:39
const auto B
Definition: note.hpp:34
constexpr auto A
Definition: note.hpp:32
half_note_t
Definition: note.hpp:10
constexpr int to_int(half_note_t half_note) noexcept
Definition: note.hpp:14