-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
190 lines (133 loc) · 3.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
module.exports = class Tune {
constructor() {
// the scale as ratios
this.scale = [];
// i/o modes
this.mode = {
output: 'frequency',
input: 'step'
};
// ET major
this.etmajor = [ 261.62558,
293.664764,
329.627563,
349.228241,
391.995422,
440,
493.883301,
523.25116
];
// Root frequency.
this.root = this.mtof(60); // * Math.pow(2,(60-69)/12);
// default is a major scale
this.createScale(0,2,4,5,7,9,11);
}
/* Return data in the mode you are in (freq, ratio, or midi) */
note(input,octave) {
let newvalue;
if (this.mode.output === 'frequency') {
newvalue = this.frequency(input,octave);
} else if (this.mode.output === 'ratio') {
newvalue = this.ratio(input,octave);
} else if (this.mode.output === 'MIDI') {
newvalue = this.MIDI(input,octave);
} else {
newvalue = this.frequency(input,octave);
}
return newvalue;
}
/* Return freq data */
frequency(stepIn, octaveIn) {
if (this.mode.input === 'midi' || this.mode.input === 'MIDI' ) {
this.stepIn += 60;
}
// what octave is our input
let octave = Math.floor(stepIn/this.scale.length);
if (octaveIn) {
octave += octaveIn;
}
// which scale degree (0 - scale length) is our input
let scaleDegree = stepIn % this.scale.length;
while (scaleDegree < 0) {
scaleDegree += this.scale.length;
}
let ratio = this.scale[scaleDegree];
let freq = this.root * ratio;
freq = freq*(Math.pow(2,octave));
// truncate irrational numbers
freq = Math.floor(freq*100000000000)/100000000000;
return freq;
}
/* Force return ratio data */
ratio(stepIn, octaveIn) {
if (this.mode.input === 'midi' || this.mode.input === 'MIDI' ) {
this.stepIn += 60;
}
// what octave is our input
let octave = Math.floor(stepIn/this.scale.length);
if (octaveIn) {
octave += octaveIn;
}
// which scale degree (0 - scale length) is our input
let scaleDegree = stepIn % this.scale.length;
// what ratio is our input to our key
let ratio = Math.pow(2,octave)*this.scale[scaleDegree];
ratio = Math.floor(ratio*100000000000)/100000000000;
return ratio;
}
/* Force return adjusted MIDI data */
MIDI(stepIn,octaveIn) {
let newvalue = this.frequency(stepIn,octaveIn);
let n = 69 + 12*Math.log(newvalue/440)/Math.log(2);
n = Math.floor(n*1000000000)/1000000000;
return n;
}
createScale() {
let newScale = [];
for (let i=0;i<arguments.length;i++) {
newScale.push( this.mtof( 60 + arguments[i] ) );
}
this.loadScaleFromFrequencies(newScale);
}
createJIScale() {
this.scale = [];
for (let i=0;i<arguments.length;i++) {
this.scale.push(arguments[i]);
}
}
loadScaleFromFrequencies(freqs) {
this.scale = [];
for (let i=0;i<freqs.length-1;i++) {
this.scale.push(freqs[i]/freqs[0]);
}
}
/* Load a new scale */
loadScale(name){
/* load the scale */
let freqs = this.scales[name].frequencies;
this.loadScaleFromFrequencies(freqs);
}
/* Search the names of tunings
Returns an array of names of tunings */
search(letters) {
let possible = [];
for (let key in this.scales) {
if (key.toLowerCase().indexOf(letters.toLowerCase()) !== -1) {
possible.push(key);
}
}
return possible;
}
/* Return a collection of notes as an array */
chord(midis) {
let output = [];
for (let i=0;i<midis.length;i++) {
output.push(this.note(midis[i]));
}
return output;
}
mtof(midi) {
return Math.pow(2, ((midi-69)/12)) * 440;
};
}