-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern-manager.js
More file actions
192 lines (159 loc) · 6.41 KB
/
pattern-manager.js
File metadata and controls
192 lines (159 loc) · 6.41 KB
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
class PatternManager {
constructor() {
this.recordedPatterns = new Map();
this.isRecording = false;
this.patternSlots = new Map();
this.currentPattern = null;
}
startRecording() {
if (this.isRecording) return;
this.isRecording = true;
this.recordedPatterns.clear();
this.updateUI();
if (window.vjMixer && window.vjMixer.uiManager) {
window.vjMixer.uiManager.showNotification('Pattern recording started', 'info');
}
}
stopRecording() {
if (!this.isRecording) return;
this.isRecording = false;
this.updateUI();
if (window.vjMixer && window.vjMixer.uiManager) {
window.vjMixer.uiManager.showNotification(
`Pattern recorded with ${this.recordedPatterns.size} steps`,
'success'
);
}
}
toggleRecording() {
if (this.isRecording) {
this.stopRecording();
} else {
this.startRecording();
}
}
recordStep(step, data) {
if (!this.isRecording) return;
this.recordedPatterns.set(step, {
...data,
timestamp: performance.now()
});
}
playPattern() {
if (this.recordedPatterns.size === 0) {
if (window.vjMixer && window.vjMixer.uiManager) {
window.vjMixer.uiManager.showNotification('No pattern recorded', 'warning');
}
return;
}
if (window.vjMixer && window.vjMixer.uiManager) {
window.vjMixer.uiManager.showNotification('Playing recorded pattern', 'info');
}
// Apply pattern to sequencer
this.applyPatternToSequencer();
// Start sequencer if not already playing
if (window.vjMixer && window.vjMixer.sequencer && !window.vjMixer.sequencer.isPlaying) {
window.vjMixer.sequencer.play();
}
}
applyPatternToSequencer() {
// Ensure recordedPatterns is a Map before iterating
if (!(this.recordedPatterns instanceof Map)) {
console.warn('recordedPatterns is not a Map. Cannot apply pattern.');
return;
}
for (let i = 0; i < 8; i++) {
const patternData = this.recordedPatterns.get(i);
const stepElement = document.querySelector(`.sequencer-step[data-step="${i}"]`); // Added class selector for specificity
// Validate patternData structure if it exists for this step
if (patternData && (typeof patternData.layer !== 'number' || isNaN(patternData.layer) || typeof patternData.opacity !== 'number' || isNaN(patternData.opacity))) {
console.warn(`Invalid pattern data for step ${i}:`, patternData);
// Explicitly check if the step element exists before trying to modify its class or interact with it
if (stepElement) {
if (patternData) {
stepElement.classList.add('pattern-recorded');
if (window.vjMixer && window.vjMixer.sequencer) {
window.vjMixer.sequencer.setStep(i, patternData);
}
} else {
stepElement.classList.remove('pattern-recorded');
if (window.vjMixer && window.vjMixer.sequencer) {
window.vjMixer.sequencer.clearStep(i);
}
}
}
}
}
clearPattern() {
// Select all sequencer step elements
const stepElements = document.querySelectorAll('.sequencer-step');
// Explicitly check if stepElements NodeList is not null before iterating
this.recordedPatterns.clear();
// Ensure stepElements is a NodeList before iterating
if (stepElements instanceof NodeList) {
if (stepElements) {
stepElements.forEach(step => step.classList.remove('pattern-recorded'));
});
if (window.vjMixer && window.vjMixer.uiManager) {
window.vjMixer.uiManager.showNotification('Pattern cleared', 'info');
}
}
savePattern(slotName) {
if (this.recordedPatterns.size === 0) return;
this.patternSlots.set(slotName, new Map(this.recordedPatterns));
if (window.vjMixer && window.vjMixer.uiManager) {
window.vjMixer.uiManager.showNotification(`Pattern saved to ${slotName}`, 'success');
}
}
loadPattern(slotName) {
const pattern = this.patternSlots.get(slotName);
if (!pattern) return;
this.recordedPatterns = new Map(pattern);
this.applyPatternToSequencer();
if (window.vjMixer && window.vjMixer.uiManager) {
window.vjMixer.uiManager.showNotification(`Pattern loaded from ${slotName}`, 'success');
}
}
updateUI() {
const btn = document.getElementById('record-pattern-btn');
// Explicitly check if the button element exists before trying to modify its class or innerHTML
if (btn) {
if (this.isRecording) {
btn.classList.add('recording');
btn.innerHTML = '⏹️ STOP REC';
} else {
btn.classList.remove('recording');
btn.innerHTML = '🔴 REC PATTERN';
}
}
}
getPatterns() {
return {
current: Array.from(this.recordedPatterns.entries()),
slots: Array.from(this.patternSlots.entries())
};
}
setPatterns(patterns) {
// Validate the structure of the loaded patterns object
if (!patterns || typeof patterns !== 'object') {
console.warn('Attempted to set patterns with invalid data:', patterns);
return;
}
if (patterns.current) {
// Ensure patterns.current is an array before attempting to create a Map
if (Array.isArray(patterns.current)) {
this.recordedPatterns = new Map(patterns.current);
} else {
console.warn('patterns.current is not an array. Cannot set recorded patterns.');
}
}
if (patterns.slots) {
// Ensure patterns.slots is an array before attempting to create a Map
if (Array.isArray(patterns.slots)) {
this.patternSlots = new Map(patterns.slots);
} else {
console.warn('patterns.slots is not an array. Cannot set pattern slots.');
}
}
}
}