Ardour  9.7-336-gb784ad01ee
ardour/ardour/scale.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2026 Paul Davis <paul@linuxaudiosystems.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #pragma once
20 
21 #include <map>
22 #include <string>
23 #include <vector>
24 
25 #include "pbd/signals.h"
26 #include "pbd/xml++.h"
27 
29 #include "ardour/types.h"
30 
31 namespace ARDOUR {
32 
34 {
35  public:
36  enum Name {
73  Algerian
74  };
75 
76  /* In theory we should be able to overload the singuler name, but at
77  least gcc is unable to resolve whether a single std::string as the
78  second argument is meant to be an initializer list or not. So we
79  provide two versions, one plural one singular.
80 
81  The singular version should be used where there is only one accepted
82  name for a scale; the plural (initializer list) version should be
83  used where there is more than one.
84  */
85  static void register_scale (TuningSystem, std::string const &, MusicalModeType, std::vector<float> const &);
86  static void register_scales (TuningSystem, std::vector<std::string> const &, MusicalModeType, std::vector<float> const &);
87 
88  MusicalMode (TuningSystem, std::string const & name, MusicalModeType type, std::vector<float> const & elements);
89  MusicalMode (MusicalMode const & other);
90  MusicalMode (std::string const & name);
91  MusicalMode (std::ifstream& file); /* Read from a Scala file */
92  MusicalMode (XMLNode const &);
93 
94  virtual ~MusicalMode() {}
95 
96  MusicalMode& operator= (MusicalMode const & other);
97 
98  virtual std::string name() const { return _name; }
99  TuningSystem tuning() const { return _tuning; }
100  MusicalModeType type() const { return _type; }
101  int size() const { return _elements.size(); }
102  std::vector<float> const & elements() const { return _elements; }
103  int ring_id () const;
104 
105  std::vector<int> as_midi (int scale_root) const;
106  void set_name (std::string const & str);
107 
110 
111  static std::multimap<TuningSystem,MusicalMode> modes_by_tuning;
112  static std::map<std::string,MusicalMode> modes_by_name;
113  static std::map<int,MusicalMode> modes_by_id;
114 
115  static std::map<TuningSystem,int> tone_equivalent_ratio;
116  static std::map<TuningSystem,int> tones_per_equivalent;
117 
118  XMLNode& get_state () const;
119  int set_state (XMLNode const &);
120  static char const * xml_node_name;
121 
122  protected:
124  mutable int _ring_id;
125  std::string _name;
127  std::vector<float> _elements;
128 
129  static void init ();
130 
131  std::vector<int> absolute_pitch_as_midi (int root) const;
132  std::vector<int> pitch_class_as_midi (int root) const;
133  std::vector<int> wholetone_steps_as_midi (int root) const;
134  std::vector<int> ratio_steps_as_midi (int root) const;
135  std::vector<int> ratio_from_root_as_midi (int root) const;
136  std::vector<int> midi_note_as_midi (int root) const;
137 };
138 
139 
141 {
142  public:
143  MusicalKey (float root, MusicalMode const &);
144  MusicalKey (float root, std::string const &);
145  MusicalKey (XMLNode const &);
146  MusicalKey (MusicalKey const & other);
147 
148  std::string name() const;
149  std::string mode_name() const;
150  float root() const { return _root; }
151  float nth (unsigned int n) const;
152  bool in_key (int midi_note) const;
153  int closest_midi_note (int midi_note) const;
154  int lower_midi_note (int midi_note) const;
155  int higher_midi_note (int midi_note) const;
157 
158  XMLNode& get_state () const;
159  int set_state (XMLNode const &);
160 
161  private:
162  float _root;
163  mutable std::vector<int> _midi_notes;
164 };
165 
166 } // namespace
float nth(unsigned int n) const
int conform_midi_note(int midi_note, ARDOUR::KeyEnforcementPolicy) const
std::string mode_name() const
int higher_midi_note(int midi_note) const
int set_state(XMLNode const &)
bool in_key(int midi_note) const
MusicalKey(MusicalKey const &other)
MusicalKey(XMLNode const &)
int lower_midi_note(int midi_note) const
int closest_midi_note(int midi_note) const
MusicalKey(float root, std::string const &)
XMLNode & get_state() const
std::string name() const
MusicalKey(float root, MusicalMode const &)
std::vector< int > _midi_notes
MusicalMode(std::ifstream &file)
std::vector< int > ratio_steps_as_midi(int root) const
static std::multimap< TuningSystem, MusicalMode > modes_by_tuning
PBD::Signal< void()> NameChanged
int ring_id() const
MusicalMode(TuningSystem, std::string const &name, MusicalModeType type, std::vector< float > const &elements)
MusicalMode(std::string const &name)
static void init()
void set_name(std::string const &str)
static std::map< TuningSystem, int > tones_per_equivalent
MusicalModeType type() const
static void register_scale(TuningSystem, std::string const &, MusicalModeType, std::vector< float > const &)
MusicalMode(XMLNode const &)
std::vector< float > const & elements() const
static std::map< TuningSystem, int > tone_equivalent_ratio
XMLNode & get_state() const
std::vector< int > absolute_pitch_as_midi(int root) const
std::vector< float > _elements
PBD::Signal< void()> Changed
static void register_scales(TuningSystem, std::vector< std::string > const &, MusicalModeType, std::vector< float > const &)
std::vector< int > as_midi(int scale_root) const
static std::map< int, MusicalMode > modes_by_id
int set_state(XMLNode const &)
std::vector< int > wholetone_steps_as_midi(int root) const
TuningSystem tuning() const
static char const * xml_node_name
std::vector< int > midi_note_as_midi(int root) const
virtual std::string name() const
static std::map< std::string, MusicalMode > modes_by_name
std::vector< int > pitch_class_as_midi(int root) const
MusicalMode(MusicalMode const &other)
std::vector< int > ratio_from_root_as_midi(int root) const
Definition: xml++.h:114
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBARDOUR_API