-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-syllables.js
147 lines (127 loc) · 3.67 KB
/
parse-syllables.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
shaveDecimals = function(value, numDecimals) {
var multiplier = Math.pow(10, numDecimals)
return Math.round(multiplier * value) / multiplier
}
Syllables = {
re: {
vowels: /AE|EY|AO|AX|IY|EH|IH|AY|IX|AA|UW|UH|UX|OW|AW|OY/,
consonants: /b|C|d|D|f|g|h|J|k|l|m|n|N|p|r|s|S|t|T|v|w|y|z|Z/,
punctuation: /\\.|!|\\?|,|:/,
divider: /~|_|\\+|=/,
},
parseSyllables: function(tune) {
return this.getSyllables(tune)
},
parseTuneEntries: function(tune) {
var entries = tune.split("\n")
for (var i = 0; i < entries.length; i++) {
entries[i] = this.parseTuneEntry(entries[i])
}
return entries
},
parseTuneEntry: function(tuneEntry) {
var entry = { raw: tuneEntry, duration: 0, pitches: [], sylBreak: false }
// symbol
entry.symbol = tuneEntry.split(" ")[0]
// duration
var matches = tuneEntry.match(/{D ([0-9.-]+)/)
if (matches) entry.duration = parseFloat(matches[1])
// pitches
matches = tuneEntry.match(/P ([^}]+)}/)
if (matches) {
var pitches = matches[1].split(" ")
for (var i = 0; i < pitches.length; i++) {
pitches[i] = pitches[i].split(":")
pitches[i][0] = parseFloat(pitches[i][0])
pitches[i][1] = parseFloat(pitches[i][1])
}
entry.pitches = pitches
}
// type
if (this.re.vowels.test(entry.symbol))
entry.type = "vowel"
else if (entry.pitches.length > 0)
entry.type = "consonant" // any non-vowel with pitch
else if (entry.duration > 0)
entry.type = "pause" // anything with time but no pitch
else
entry.type = "meta" // non-vocal data
entry.toString = function() {
if (this.type === "meta") return this.raw
var s = this.symbol + " {D " + shaveDecimals(this.duration, 1)
if (this.pitches.length > 0) {
s += "; P"
for (var i = 0; i < this.pitches.length; i++) {
s += " " + shaveDecimals(this.pitches[i][0], 1) +
":" + shaveDecimals(this.pitches[i][1], 1)
}
}
s += "}"
return s
}
entry.description = function() {
var p = []
this.pitches.forEach(function(pitch) {
p.push("[" + pitch[0] + ", " + pitch[1] + "]")
})
var s = [
"{",
" raw: " + this.raw,
" type: " + this.type,
" symbol: " + this.symbol,
" duration: " + this.duration,
" pitches: [ " + p.join(", ") + "]",
" sylBreak: " + this.sylBreak,
"}",
].join("\n")
return s
}
return entry
},
markBreaks: function(entries) {
var lastWasConsonant = false
var vowelHasBeenSeen = false
for (var i = 0; i < entries.length; i++) {
var entry = entries[i]
// don't break until we've seen a vowel
if (vowelHasBeenSeen) {
// if the synthesizer marked a syllable break, go with that
if (this.re.divider.test(entry.symbol)) {
entry.sylBreak = true
vowelHasBeenSeen = false
}
// if we hit a vowel break one phoneme back if that was a consonant
// otherwise break here
else if (entry.type == "vowel") {
if (lastWasConsonant) {
entries[i-1].sylBreak = true // keep prior consonant if any
}
else {
entry.sylBreak = true // otherwise break at the vowel
}
}
}
vowelHasBeenSeen = vowelHasBeenSeen || entry.type == "vowel"
lastWasConsonant = entry.type == "consonant"
}
},
getSyllables: function(tune) {
var entries = this.parseTuneEntries(tune)
this.markBreaks(entries)
var syllables = [[]]
var k = 0
for (var i = 0; i < entries.length; i++) {
if (entries[i].sylBreak) syllables[++k] = []
syllables[k].push(entries[i])
}
syllables.flatten = function() {
var flat = []
this.forEach(function(syl) { flat = flat.concat(syl) })
return flat
}
syllables.toString = function() {
return this.flatten().join("\n")
}
return syllables
}
}