-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model_prev.js
170 lines (149 loc) · 6.98 KB
/
Model_prev.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
/**
* @class Model
*
* Manages the data of the application.
*/
class Model {
constructor() {
this.fullRange = ["C1","C#1","D1","D#1","E1","F1","F#1","G1","G#1","A1","A#1","B1","C2","C#2","D2","D#2","E2","F2","F#2","G2","G#2","A2","A#2","B2","C3","C#3","D3","D#3","E3","F3","F#3","G3","G#3","A3","A#3","B3","C4","C#4","D4","D#4","E4","F4","F#4","G4","G#4","A4","A#4","B4","C5","C#5","D5","D#5","E5","F5","F#5","G5","G#5","A5","A#5","B5","C6","C#6","D6","D#6","E6","F6","F#6","G6","G#6","A6","A#6","B6","C7","C#7","D7","D#7","E7","F7","F#7","G7","G#7","A7","A#7","B7","C8", "C#8","D8","D#8","E8","F8","F#8","G8","G#8","A8","A#8","B8"]
this.todos = JSON.parse(localStorage.getItem('todos')) || []
this.nowPlaying = false
}
bindTodoListChanged(callback) {
this.onTodoListChanged = callback
}
_commit(todos) {
this.onTodoListChanged(todos)
localStorage.setItem('todos', JSON.stringify(todos))
}
bindAudioChanged(callback) {
this.onAudioChanged = callback
}
get activeTodo() {
if (this.nowPlaying === false) { return null }
const idIndex = this.todos.indexOf(this.todos.find(todo => todo.id === this.nowPlaying))
return this.todos[idIndex]
}
todoPlaySetup = (id) => {
console.log('playing to do id', id)
console.log('to dues', this.todos)
const idIndex = this.todos.indexOf(this.todos.find(todo => todo.id === id)) // could make this a getter
console.log('idIndex', idIndex)
// if that particular to do is not playing, turn all todos off and then turn that one on, and set nowPlaying to that ID
if (!this.todos[idIndex].playing) {
this.todos = this.todos.map(todo => {
return {...todo, playing: false }
})
this.todos[idIndex].playing = true
this.nowPlaying = id
console.log('nowPlaying', this.nowPlaying)
// if that particular to do *is* playing, turn it off and set nowPlaying to false
} else if (this.todos[idIndex].playing) {
console.log('in else if...')
this.todos[idIndex].playing = false
this.nowPlaying = false
}
this.onTodoListChanged(this.todos)
this.onAudioChanged()
}
addTodo(todoText) {
// validate null inputs
// let parens = todoText.match(/\((.*?)\)/i) //https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets
// parens = parens !== null ? parens[0] : '(c d e f g a b)'
// let noteNameArray = parens.match(/([a-g])([b#])?/gi)
let noteNameArray = todoText.match(/([a-g])([b#])?/gi)
console.log('noteNameArray', noteNameArray)
noteNameArray = noteNameArray.map(name => name.charAt(0).toUpperCase() + name.slice(1))
let tempo = todoText.match(/t[\d]{2,3}/gi)
console.log('tempo match', tempo)
// ^[DE][0-9]{2,3}$
tempo = tempo !== null ? tempo[0] : 't120'
let percent = todoText.match(/%\d\d/gi)
percent = percent !== null ? percent[0] : '%25'
if (percent === '00') { percent = '%100' }
const loMatch = todoText.match(/lo([a-g])([b#])?[1-8]/gi) || ['loC3']
const hiMatch = todoText.match(/hi([a-g])([b#])?[1-8]/gi) || ['hiB3']
console.log('loMatch', loMatch)
console.log('hiMatch', hiMatch)
// const duration = todoText.match(/d\d\d/gi)
// create to do text from regular expression matches
// to assess range (probably in audio) convert flats to sharps
// console.log('parens', parens)
console.log('noteNameArray', noteNameArray)
console.log('tempo', tempo)
console.log('percent', percent)
// console.log('duration', duration)
// note you'll need to validate the lo and hi ranges
const lo = this.parseLowRange(loMatch)
console.log('lo', lo)
const hi = this.parseHighRange(hiMatch, lo)
console.log('hi', hi)
const todo = {
id: this.todos.length > 0 ? this.todos[this.todos.length - 1].id + 1 : 1,
text: todoText,
tempo: parseInt(tempo.slice(1), 10),
percent: parseInt(percent.slice(1), 10),
noteString: noteNameArray.join('.'),
lo: lo,
hi: hi,
pitchSet: this.createPitchSet(noteNameArray, lo, hi),
playing: false
}
console.log('todo', todo)
this.todos.push(todo)
this._commit(this.todos)
}
parseLowRange = (loPitch) => {
loPitch = loPitch[0].slice(2)
loPitch = loPitch.charAt(0).toUpperCase() + loPitch.slice(1)
loPitch = this.convertFlatToSharp(loPitch.slice(0,-1)) + loPitch.slice(-1)
return loPitch
}
parseHighRange = (hiPitch, loPitch) => {
hiPitch = hiPitch[0].slice(2)
hiPitch = hiPitch.charAt(0).toUpperCase() + hiPitch.slice(1)
hiPitch = this.convertFlatToSharp(hiPitch.slice(0,-1)) + hiPitch.slice(-1)
if (this.fullRange.indexOf(hiPitch) <= this.fullRange.indexOf(loPitch)) {
hiPitch = loPitch
}
return hiPitch
}
convertFlatToSharp = (noteName) => {
const conversion = {'Cb':'B', 'Db':'C#', 'Eb':'D#', 'Fb':'E', 'Gb':'F#', 'Ab':'G#', 'Bb':'A#'}
if (Object.keys(conversion).includes(noteName)) {
return conversion[noteName]
}
else { return noteName }
}
createPitchSet(noteNameArray, lo, hi) {
// needs to function on add or edit
// read Lo and Hi from add to do, or from state?
const noteNameArraySharped = noteNameArray.map(noteName => this.convertFlatToSharp(noteName))
let pitchSet = []
let adjustedRangeLow = this.fullRange.slice(this.fullRange.indexOf(lo))
let adjustedRange = adjustedRangeLow.slice(0, adjustedRangeLow.indexOf(hi)+1)
for (let k=0; k < adjustedRange.length; k++){
if (noteNameArraySharped.indexOf(adjustedRange[k].slice(0,-1)) >-1 ) {
pitchSet.push(adjustedRange[k])
}
}
return pitchSet
}
editTodo(id, updatedText) {
// you want to compare each new thing from the text against the old,
// change it if it is new, leave the same if same,
// leave at previous value if absent
// in other words: search for each thing again,
// if you find it, replace it on the todo
// so you want a function for each regex search & parse
// and skip each thing if the return value from that function is null
this.todos = this.todos.map(todo =>
todo.id === id ? {id: todo.id, text: updatedText} : todo
)
this._commit(this.todos)
}
deleteTodo(id) {
this.todos = this.todos.filter(todo => todo.id !== id)
this._commit(this.todos)
}
}