Ardour  9.2-541-gc1841a13dd
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::vector <degree> degrees;
64 
65  scale () {
66  // The first degree is a scala file is always implicit. Make it explicit.
67  degrees.push_back( *(new degree(0.0)));
68  }
69 
70  ~scale(){
71  degrees.clear();
72  std::vector<degree>().swap(degrees);
73  }
74 
75  scale(const scale &s2) {
76  for (size_t i = 0; i < s2.degrees.size(); i++) {
77  degrees.push_back(s2.degrees[i]);
78  }
79  }
80 
81  scale& operator=(const scale &s2){
82  std::vector <degree> new_degrees;
83  for (size_t i = 0; i < s2.degrees.size(); i++) {
84  new_degrees.push_back(s2.degrees[i]);
85  }
86  degrees.swap(new_degrees);
87  new_degrees.clear();
88  return *this;
89  }
90 
91  void add_degree(degree d) {
92  degrees.push_back(d);
93  }
94 
95  double get_ratio(size_t i){
96  return degrees[i].get_ratio();
97  }
98 
99  size_t get_scale_length() {
100  return degrees.size();
101  }
102 
103 };
104 
105 struct kbm {
107  int map_size;
113  std::vector <int> mapping;
114 
115  kbm(){
116  map_size = 0;
117  first_note = 0;
118  last_note = 0;
119  middle_note = 0;
120  reference_note = 0;
121  reference_frequency = 0.0;
122  octave_degree = 0;
123  }
124 
125  ~kbm(){
126  mapping.clear();
127  std::vector<int>().swap(mapping);
128  }
129 
130  kbm( const kbm &k2){
131  for (size_t i = 0; i < k2.mapping.size(); i++) {
132  mapping.push_back(k2.mapping[i]);
133  }
134  map_size = k2.map_size;
135  first_note = k2.first_note;
136  last_note = k2.last_note;
141  }
142 
143  kbm& operator=(const kbm &k2){
144  std::vector <int> new_mapping;
145  for (size_t i = 0; i < k2.mapping.size(); i++) {
146  new_mapping.push_back(k2.mapping[i]);
147  }
148  mapping.swap(new_mapping);
149  new_mapping.clear();
150 
151  map_size = k2.map_size;
152  first_note = k2.first_note;
153  last_note = k2.last_note;
158 
159  return *this;
160  }
161 
162  void add_mapping(int n) {
163  mapping.push_back(n);
164  }
165 
166 };
167 
168 scale read_scl (std::ifstream& input_file);
169 kbm read_kbm (std::ifstream& input_file);
170 
171 } // 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:106
void add_mapping(int n)
Definition: scala_file.h:162
int octave_degree
Definition: scala_file.h:112
int middle_note
Definition: scala_file.h:110
int map_size
Definition: scala_file.h:107
kbm(const kbm &k2)
Definition: scala_file.h:130
int last_note
Definition: scala_file.h:109
int first_note
Definition: scala_file.h:108
kbm & operator=(const kbm &k2)
Definition: scala_file.h:143
std::vector< int > mapping
Definition: scala_file.h:113
int reference_note
Definition: scala_file.h:111
void add_degree(degree d)
Definition: scala_file.h:91
size_t get_scale_length()
Definition: scala_file.h:99
double get_ratio(size_t i)
Definition: scala_file.h:95
scale(const scale &s2)
Definition: scala_file.h:75
scale & operator=(const scale &s2)
Definition: scala_file.h:81
std::vector< degree > degrees
Definition: scala_file.h:63