| 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 |
1x
1x
1x
1x
2x
42x
69x
41x
42x
7x
35x
4x
30x
2x
28x
28x
2x
2x
90x
90x
1x
26x
26x
1x
45x
45x
45x
3x
7x
6x
35x
35x
35x
35x
3x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
| 'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var noteParser = require('note-parser');
var intervalNotation = require('interval-notation');
var tonalEncoding = require('tonal-encoding');
/**
* Functions to deal with pitches (either notes or intervals).
*
* This functions are very low level and more developer friendly of this functions
* are exposed in the note and interval packages. It's unlikely you need them.
* That's why __this module is NOT exported in the tonal package__.
*
* @private
* @module pitch
*/
/**
* Create a pitch
* @param {Integer} fifths - the number of fifths from C or from P1
* @param {Integer} focts - the number of encoded octaves
* @param {Integer} dir - (Optional) Only required for intervals. Can be 1 or -1
* @return {Pitch}
*/
function pitch (fifths, focts, dir) {
return dir ? ['tnlp', [fifths, focts], dir] : ['tnlp', [fifths, focts]]
}
/**
* Test if an object is a pitch
* @param {Pitch}
* @return {Boolean}
*/
function isPitch (p) { return Array.isArray(p) && p[0] === 'tnlp' }
/**
* Encode a pitch
* @param {Integer} step
* @param {Integer} alt
* @param {Integer} oct
* @param {Integer} dir - (Optional)
*/
function encode$1 (s, a, o, dir) {
return dir ? ['tnlp', tonalEncoding.encode(s, a, o), dir] : ['tnlp', tonalEncoding.encode(s, a, o)]
}
/**
* Decode a pitch
* @param {Pitch} the pitch
* @return {Array} An array with [step, alt, oct]
*/
function decode$1 (p) {
return tonalEncoding.decode.apply(null, p[1])
}
/**
* Get pitch type
* @param {Pitch}
* @return {String} 'ivl' or 'note' or null if not a pitch
*/
function pType (p) {
return !isPitch(p) ? null : p[2] ? 'ivl' : 'note'
}
/**
* Test if is a pitch note (with or without octave)
* @param {Pitch}
* @return {Boolean}
*/
function isNotePitch (p) { return pType(p) === 'note' }
/**
* Test if is an interval
* @param {Pitch}
* @return {Boolean}
*/
function isIvlPitch (p) { return pType(p) === 'ivl' }
/**
* Test if is a pitch class (a pitch note without octave)
* @param {Pitch}
* @return {Boolean}
*/
function isPC (p) { return isPitch(p) && p[1].length === 1 }
/**
* Get direction of a pitch (even for notes)
* @param {Pitch}
* @return {Integer} 1 or -1
*/
function dir (p) { return p[2] === -1 ? -1 : 1 }
/**
* Get encoded fifths from pitch.
* @param {Pitch}
* @return {Integer}
*/
function fifths (p) { return p[2] === -1 ? -p[1][0] : p[1][0] }
/**
* Get encoded octaves from pitch.
* @param {Pitch}
* @return {Integer}
*/
function focts (p) { return p[2] === -1 ? -p[1][1] : p[1][1] }
/**
* Get height of a pitch.
* @param {Pitch}
* @return {Integer}
*/
function height (p) { return fifths(p) * 7 + focts(p) * 12 }
/**
* Get chroma of a pitch. The chroma is a number between 0 and 11 to represent
* the position of a pitch inside an octave. Is the numeric equivlent of a
* pitch class.
*
* @param {Pitch}
* @return {Integer}
*/
function chr (p) {
var f = fifths(p);
return 7 * f - 12 * Math.floor(f * 7 / 12)
}
// memoize parsers
function memoize (fn) {
var cache = {};
return function (str) {
Iif (typeof str !== 'string') return null
return cache[str] || (cache[str] = fn(str))
}
}
/**
* Parse a note
* @function
* @param {String} str
* @return {Pitch} the pitch or null if not valid note string
*/
var parseNote = memoize(function (s) {
var p = noteParser.parse(s);
return p ? encode$1(p.step, p.alt, p.oct) : null
});
/**
* Parse an interval
* @function
* @param {String} str
* @return {Pitch} the pitch or null if not valid interval string
*/
var parseIvl = memoize(function (s) {
var p = intervalNotation.parse(s);
Iif (!p) return null
return p ? encode$1(p.simple - 1, p.alt, p.oct, p.dir) : null
});
/**
* Parse a note or an interval
* @param {String} str
* @return {Pitch} the pitch or null if not valid pitch string
*/
function parsePitch (s) { return parseNote(s) || parseIvl(s) }
/**
* Ensure the given object is a note pitch. If is a string, it will be
* parsed. If not a note pitch or valid note string, it returns null.
* @param {Pitch|String}
* @return {Pitch}
*/
function asNotePitch (p) { return isNotePitch(p) ? p : parseNote(p) }
/**
* Ensure the given object is a interval pitch. If is a string, it will be
* parsed. If not a interval pitch or valid interval string, it returns null.
* @param {Pitch|String}
* @return {Pitch}
*/
function asIvlPitch (p) { return isIvlPitch(p) ? p : parseIvl(p) }
/**
* Ensure the given object is a pitch. If is a string, it will be
* parsed. If not a pitch or valid pitch string, it returns null.
* @param {Pitch|String}
* @return {Pitch}
*/
function asPitch (p) { return isPitch(p) ? p : parsePitch(p) }
/**
* Convert a note pitch to string representation
* @param {Pitch}
* @return {String}
*/
function strNote (p) {
if (!isNotePitch(p)) return null
return noteParser.build.apply(null, decode$1(p))
}
/**
* Convert a interval pitch to string representation
* @param {Pitch}
* @return {String}
*/
function strIvl (p) {
Iif (!isIvlPitch(p)) return null
// decode to [step, alt, oct]
var d = decode$1(p);
// d = [step, alt, oct]
var num = d[0] + 1 + 7 * d[2];
return p[2] * num + intervalNotation.altToQ(num, d[1])
}
/**
* Convert a pitch to string representation (either notes or intervals)
* @param {Pitch}
* @return {String}
*/
function strPitch (p) { return strNote(p) || strIvl(p) }
// A function that creates a decorator
// The returned function can _decorate_ other functions to parse and build
// string representations
function decorator (is, parse$$1, str) {
return function (fn) {
return function (v) {
var i = is(v);
// if the value is in pitch notation no conversion
if (i) return fn(v)
// else parse the pitch
var p = parse$$1(v);
// if parsed, apply function and back to string
return p ? str(fn(p)) : null
}
}
}
/**
* Decorate a function to work internally with note pitches, even if the
* parameters are provided as strings. Also it converts back the result
* to string if a note pitch is returned.
* @function
* @param {Function} fn
* @return {Function} the decorated function
*/
var noteFn = decorator(isNotePitch, parseNote, strNote);
/**
* Decorate a function to work internally with interval pitches, even if the
* parameters are provided as strings. Also it converts back the result
* to string if a interval pitch is returned.
* @function
* @param {Function} fn
* @return {Function} the decorated function
*/
var ivlFn = decorator(isIvlPitch, parseIvl, strIvl);
/**
* Decorate a function to work internally with pitches, even if the
* parameters are provided as strings. Also it converts back the result
* to string if a pitch is returned.
* @function
* @param {Function} fn
* @return {Function} the decorated function
*/
var pitchFn = decorator(isPitch, parsePitch, strPitch);
exports.pitch = pitch;
exports.isPitch = isPitch;
exports.encode = encode$1;
exports.decode = decode$1;
exports.pType = pType;
exports.isNotePitch = isNotePitch;
exports.isIvlPitch = isIvlPitch;
exports.isPC = isPC;
exports.dir = dir;
exports.fifths = fifths;
exports.focts = focts;
exports.height = height;
exports.chr = chr;
exports.parseNote = parseNote;
exports.parseIvl = parseIvl;
exports.parsePitch = parsePitch;
exports.asNotePitch = asNotePitch;
exports.asIvlPitch = asIvlPitch;
exports.asPitch = asPitch;
exports.strNote = strNote;
exports.strIvl = strIvl;
exports.strPitch = strPitch;
exports.noteFn = noteFn;
exports.ivlFn = ivlFn;
exports.pitchFn = pitchFn;
|