-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (158 loc) · 5.68 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
const Tone = require("tone")
const polytone = require("./scripts/polytone")
const monotone = require("./scripts/monotone")
const rest = require("./scripts/rest")
const errors = require("./errors")
const Token = require("./definitions").Token
const Error = require("./definitions").Error
// Set up PolySynth for playing polytones
const polysynth = new Tone.PolySynth(5, Tone.Synth).toMaster()
/**
* Sets BPM of the piece via the Transport global of ToneJS
* @param {number} bpm : BPM to be set
*/
function setBPM(bpm) {
Tone.Transport.bpm.value = bpm
}
/**
* Tokenizes MuUp string and validates token prefixes
* @param {string} muup : Entire MuUp text to be converted
* @returns {object} : Object with prefix check result and list of token objects with type and stripped tone strings as attributes
*/
function tokenizeAndValidatePrefix(muup) {
var validate_result = {
good: false,
tokens: []
}
// Get list of individual prefixed tone strings after trimming leading and trailing whitespaces
var separated_list = muup.split(",").map((token) => {
return token.replace(/^\s+|\s+$/g, '')
})
// Prefix validator function
function validate_prefix(element) {
const first_two_chars = element.substring(0, 2)
const last_char = element[element.length - 1]
validate_result.tokens.push(new Token(element))
// Check if first two characters match appropriate prefix
if(first_two_chars == "M(" || first_two_chars == "P(" || first_two_chars == "R(") {
// Check if last character is closing parenthesis
return (last_char == ")")
}
return false
}
// Check if all prefixes are validated
if(separated_list.every(validate_prefix)) {
validate_result.good = true
}
else {
return errors.errorFound(0)
}
return validate_result
}
/**
* Sets the times of all tokens to be played (currently only Polytone)
* @param {Array<Token>} token_list : List of verified Token objects
* @returns {Array<object>} : If valid tone_strings for all tokens, list of playable objects. If not, singleton list of error object.
*/
function getToneObjects(token_list) {
var tone_obj_list = []
// Iterate through every token in the list of tokens
token_list.forEach((token) => {
if(token.token_type == "P") {
var poly_obj = polytone.getPolytoneComponents(token.tone_string)
if(poly_obj instanceof Error) {
tone_obj_list = [poly_obj]
return tone_obj_list
}
else {
var complete_notes = []
if(poly_obj.octaves.length == 1) {
complete_notes = poly_obj.notes.map((note) => {
return note + poly_obj.octaves[0]
})
}
else {
for (let index = 0; index < poly_obj.octaves.length; index++) {
complete_notes.push(poly_obj.notes[index] + poly_obj.octaves[index])
}
}
}
tone_obj_list.push({
note: complete_notes,
duration: poly_obj.durations[0]
})
}
else if(token.token_type == "M") {
var mono_obj = monotone.getMonotoneComponents(token.tone_string)
if(mono_obj instanceof Error) {
tone_obj_list = [mono_obj]
return tone_obj_list
}
else {
if(mono_obj.durations.length != 1) {
for (let index = 0; index < mono_obj.notes.length; index++) {
tone_obj_list.push({
note: mono_obj.notes[index] + mono_obj.octaves[0],
duration: mono_obj.durations[index]
})
}
}
else {
for (let index = 0; index < mono_obj.notes.length; index++) {
tone_obj_list.push({
note: mono_obj.notes[index] + mono_obj.octaves[0],
duration: mono_obj.durations[0]
})
}
}
}
}
else if(token.token_type == "R") {
var rest_obj = rest.getRestComponents(token.tone_string)
if(rest_obj instanceof Error) {
tone_obj_list = [rest_obj]
return tone_obj_list
}
else {
tone_obj_list.push({
note: "rest",
duration: rest_obj.rest_amount
})
}
}
})
return tone_obj_list
}
/**
* Plays audio from complete MuUp string
* @param {string} muup : Complete MuUp string
*/
function playAudio(muup) {
var status = {
instance: "Success",
message: "OK"
}
const token_obj = tokenizeAndValidatePrefix(muup)
if(!(token_obj instanceof Error)) {
const tone_obj_list = getToneObjects(token_obj.tokens)
if(!(tone_obj_list[0] instanceof Error)) {
var time = Tone.now()
tone_obj_list.forEach((obj) => {
if(obj.note != "rest") {
polysynth.triggerAttackRelease(obj.note, obj.duration, time)
}
time += Tone.Time(obj.duration).toSeconds()
})
}
else {
status.instance = "Error"
status.message = tone_obj_list[0].message
}
}
else {
status.instance = "Error"
status.message = token_obj.message
}
return status
}
module.exports = {setBPM, playAudio}