Ardour  9.7-247-gd98f890d3c
scala_file.h
Go to the documentation of this file.
1 /****************************************************************
2  libscala-file, (C) 2020 Mark Conway Wirt
3 
4 Copyright (c) 2020 Mark Conway Wirt
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 ****************************************************************/
24 
25 #pragma once
26 
27 #include <fstream>
28 #include <cmath>
29 #include <vector>
30 
31 
32 // Define this before compilation if you want a stricter adherence
33 // to the specification.
34 
35 // #define SCALA_STRICT
36 
37 #define KBM_NON_ENTRY -1
38 
39 namespace scala {
40 
41 struct degree {
42 
43  double ratio;
44 
45  degree (int n, int d) {
46  // Two inputs: a ratio
47  ratio = static_cast <double> (n) / static_cast <double> (d);
48  }
49 
50  explicit degree (double cents) {
51  // One input: cents
52  ratio = pow(pow(2, 1.0 / 12.0), cents/100.0);
53  }
54 
55  double get_ratio() {
56  // Use to get the value
57  return ratio;
58  }
59 };
60 
61 struct scale {
62 
63  std::string name;
64  std::vector <degree> degrees;
65 
66  scale () {
67  // The first degree is a scala file is always implicit. Make it explicit.
68  degrees.push_back( *(new degree(0.0)));
69  }
70 
71  ~scale(){
72  degrees.clear();
73  std::vector<degree>().swap(degrees);
74  }
75 
76  scale(const scale &s2) {
77  for (size_t i = 0; i < s2.degrees.size(); i++) {
78  degrees.push_back(s2.degrees[i]);
79  }
80  }
81 
82  scale& operator=(const scale &s2){
83  std::vector <degree> new_degrees;
84  for (size_t i = 0; i < s2.degrees.size(); i++) {
85  new_degrees.push_back(s2.degrees[i]);
86  }
87  degrees.swap(new_degrees);
88  new_degrees.clear();
89  return *this;
90  }
91 
92  void add_degree(degree d) {
93  degrees.push_back(d);
94  }
95 
96  double get_ratio(size_t i){
97  return degrees[i].get_ratio();
98  }
99 
100  size_t get_scale_length() {
101  return degrees.size();
102  }
103 
104 };
105 
106 struct kbm {
108  int map_size;
114  std::vector <int> mapping;
115 
116  kbm(){
117  map_size = 0;
118  first_note = 0;
119  last_note = 0;
120  middle_note = 0;
121  reference_note = 0;
122  reference_frequency = 0.0;
123  octave_degree = 0;
124  }
125 
126  ~kbm(){
127  mapping.clear();
128  std::vector<int>().swap(mapping);
129  }
130 
131  kbm( const kbm &k2){
132  for (size_t i = 0; i < k2.mapping.size(); i++) {
133  mapping.push_back(k2.mapping[i]);
134  }
135  map_size = k2.map_size;
136  first_note = k2.first_note;
137  last_note = k2.last_note;
142  }
143 
144  kbm& operator=(const kbm &k2){
145  std::vector <int> new_mapping;
146  for (size_t i = 0; i < k2.mapping.size(); i++) {
147  new_mapping.push_back(k2.mapping[i]);
148  }
149  mapping.swap(new_mapping);
150  new_mapping.clear();
151 
152  map_size = k2.map_size;
153  first_note = k2.first_note;
154  last_note = k2.last_note;
159 
160  return *this;
161  }
162 
163  void add_mapping(int n) {
164  mapping.push_back(n);
165  }
166 
167 };
168 
169 scale read_scl (std::ifstream& input_file);
170 kbm read_kbm (std::ifstream& input_file);
171 
172 } // namespace
static const MIDISequence s2[]
kbm read_kbm(std::ifstream &input_file)
scale read_scl(std::ifstream &input_file)
degree(double cents)
Definition: scala_file.h:50
double ratio
Definition: scala_file.h:43
double get_ratio()
Definition: scala_file.h:55
degree(int n, int d)
Definition: scala_file.h:45
double reference_frequency
Definition: scala_file.h:107
void add_mapping(int n)
Definition: scala_file.h:163
int octave_degree
Definition: scala_file.h:113
int middle_note
Definition: scala_file.h:111
int map_size
Definition: scala_file.h:108
kbm(const kbm &k2)
Definition: scala_file.h:131
int last_note
Definition: scala_file.h:110
int first_note
Definition: scala_file.h:109
kbm & operator=(const kbm &k2)
Definition: scala_file.h:144
std::vector< int > mapping
Definition: scala_file.h:114
int reference_note
Definition: scala_file.h:112
void add_degree(degree d)
Definition: scala_file.h:92
size_t get_scale_length()
Definition: scala_file.h:100
std::string name
Definition: scala_file.h:63
double get_ratio(size_t i)
Definition: scala_file.h:96
scale(const scale &s2)
Definition: scala_file.h:76
scale & operator=(const scale &s2)
Definition: scala_file.h:82
std::vector< degree > degrees
Definition: scala_file.h:64