-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.html
267 lines (230 loc) · 10.4 KB
/
test.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>Wave Synthesizer</title>
<script src="https://aframe.io/releases/1.4.0/aframe.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/chenzlabs/[email protected]/dist/aframe-dragndrop.min.js"></script>
</head>
<body>
<a-scene vr-mode-ui="enabled: true">
<!-- Camera with Cursor -->
<a-entity position="0 1.6 4">
<a-camera>
<a-cursor raycaster="objects: .clickable" fuse="false" material="color: yellow; shader: flat"></a-cursor>
</a-camera>
</a-entity>
<!-- Light -->
<a-light type="ambient" color="#ffffff"></a-light>
<!-- Synthesizer Blocks -->
<a-entity id="synthesizer" wave-synthesizer></a-entity>
<!-- Plus Block -->
<a-box position="0 0.5 -3" width="0.5" height="0.5" depth="0.5" color="#FFFFFF"></a-box>
<!-- Ground -->
<a-plane rotation="-90 0 0" width="10" height="10" color="#7BC8A4"></a-plane>
<!-- Controller input -->
<a-entity id="controller-left" laser-controls="hand: left" raycaster="objects: .clickable"></a-entity>
<a-entity id="controller-right" laser-controls="hand: right" raycaster="objects: .clickable"></a-entity>
</a-scene>
<script>
AFRAME.registerComponent('wave-synthesizer', {
schema: {},
init: function () {
const sceneEl = this.el.sceneEl;
// Audio context and oscillators
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
let oscillators = {};
let gainNodes = {};
this.currentBlock = null;
// Waveform Visualization
const waveformLines = {};
// Helper to create a block
const createWaveBlock = (type, x, y, z) => {
const block = document.createElement('a-box');
block.setAttribute('position', `${x} ${y} ${z}`);
block.setAttribute('width', '0.5');
block.setAttribute('height', '0.5');
block.setAttribute('depth', '0.5');
block.setAttribute('color', type === 'sine' ? '#0077FF' : type === 'square' ? '#FF7700' : '#00FF77');
block.setAttribute('class', 'clickable draggable');
block.setAttribute('dragndrop', 'enabled: true');
// State and behavior
let isActive = false;
// Hover effect
block.addEventListener('raycaster-intersected', () => {
block.setAttribute('animation__hover', {
property: 'scale',
to: '0.55 0.55 0.55',
dur: 150,
easing: 'easeInOutQuad',
});
});
block.addEventListener('raycaster-intersected-cleared', () => {
block.removeAttribute('animation__hover');
block.setAttribute('scale', '0.5 0.5 0.5');
});
// Toggle wave generation
block.addEventListener('click', () => {
isActive = !isActive;
if (isActive) {
block.setAttribute('color', '#00FF00');
// Add oscillator and gain node
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.type = type;
oscillator.frequency.value = 440; // Set base frequency
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
gainNode.gain.value = 0.5; // Set initial volume
oscillator.start();
oscillators[type] = oscillator;
gainNodes[type] = gainNode;
// Create waveform line
const line = document.createElement('a-entity');
waveformLines[type] = line;
sceneEl.appendChild(line);
} else {
block.setAttribute('color', type === 'sine' ? '#0077FF' : type === 'square' ? '#FF7700' : '#00FF77');
// Stop and remove oscillator
if (oscillators[type]) {
oscillators[type].stop();
delete oscillators[type];
delete gainNodes[type];
}
// Remove waveform line
if (waveformLines[type]) {
sceneEl.removeChild(waveformLines[type]);
delete waveformLines[type];
}
}
updateSummation();
});
// Drag effect for frequency adjustment
block.addEventListener('componentchanged', function (evt) {
if (evt.detail.name === 'position' && oscillators[type]) {
const newPosition = block.getAttribute('position');
const frequency = 440 + newPosition.y * 100; // Adjust frequency by position
oscillators[type].frequency.value = frequency;
updateSummation();
}
});
// Rotation effect for volume adjustment
block.addEventListener('componentchanged', function (evt) {
if (evt.detail.name === 'rotation' && gainNodes[type]) {
const newRotation = block.getAttribute('rotation');
const volume = Math.abs(newRotation.x) / 90; // Adjust volume by rotation
gainNodes[type].gain.value = Math.max(0, Math.min(1, volume)); // Keep volume within 0 to 1 range
updateSummation();
}
});
// Handle mouse and controller input for dragging and rotating
block.addEventListener('mousedown', () => {
this.currentBlock = block; // Use this.currentBlock
});
block.addEventListener('mouseup', () => {
this.currentBlock = null; // Use this.currentBlock
});
sceneEl.appendChild(block);
};
// Update the synthesized wave summation and visualization
const updateSummation = () => {
const activeTypes = Object.keys(oscillators);
const plusBlockPosition = { x: 0, y: 0.5, z: -3 }; // Position of the plus block
activeTypes.forEach(type => {
const block = sceneEl.querySelector(`[color="${type === 'sine' ? '#0077FF' : type === 'square' ? '#FF7700' : '#00FF77'}"]`);
if (block && waveformLines[type]) {
const startPosition = block.getAttribute('position');
waveformLines[type].setAttribute('line', {
start: `${startPosition.x} ${startPosition.y} ${startPosition.z}`,
end: `${plusBlockPosition.x} ${plusBlockPosition.y} ${plusBlockPosition.z}`,
color: block.getAttribute('color')
});
}
});
if (activeTypes.length > 0) {
console.log('Synthesized Wave:', activeTypes.join(' + '));
} else {
console.log('No active waveforms');
}
};
// Create wave blocks
createWaveBlock('sine', -1, 1, -3);
createWaveBlock('square', 0, 1, -3);
createWaveBlock('sawtooth', 1, 1, -3);
},
tick: function (time, timeDelta) {
if (this.currentBlock) {
const controllerElRight = document.querySelector('#controller-right');
const controllerElLeft = document.querySelector('#controller-left');
if (controllerElRight) {
const position = controllerElRight.getAttribute('position');
this.currentBlock.setAttribute('position', {
x: this.currentBlock.getAttribute('position').x,
y: position.y,
z: this.currentBlock.getAttribute('position').z
});
}
if (controllerElLeft) {
const rotation = controllerElLeft.getAttribute('rotation');
this.currentBlock.setAttribute('rotation', rotation);
}
}
}
});
AFRAME.registerComponent('controller-input', {
init: function () {
const leftControllerEl = document.querySelector('#controller-left');
const rightControllerEl = document.querySelector('#controller-right');
let activeBlock = null;
// Handle right controller for dragging (frequency)
rightControllerEl.addEventListener('triggerdown', () => {
const intersectedEl = rightControllerEl.components.raycaster.intersectedEls[0];
if (intersectedEl && intersectedEl.classList.contains('draggable')) {
activeBlock = intersectedEl;
}
});
rightControllerEl.addEventListener('triggerup', () => {
activeBlock = null;
});
// Handle left controller for rotating (volume)
leftControllerEl.addEventListener('triggerdown', () => {
const intersectedEl = leftControllerEl.components.raycaster.intersectedEls[0];
if (intersectedEl && intersectedEl.classList.contains('draggable')) {
activeBlock = intersectedEl;
}
});
leftControllerEl.addEventListener('triggerup', () => {
activeBlock = null;
});
// Update block positions and rotations based on controller input
this.el.sceneEl.addEventListener('axismove', (evt) => {
const { hand } = evt.target;
if (activeBlock) {
if (hand === 'right') {
const position = evt.detail.axis;
activeBlock.setAttribute('position', {
x: activeBlock.getAttribute('position').x,
y: position[1] * 5, // Adjust y for frequency
z: activeBlock.getAttribute('position').z
});
} else if (hand === 'left') {
const rotation = evt.detail.axis;
activeBlock.setAttribute('rotation', {
x: rotation[0] * 360, // Adjust x for volume
y: activeBlock.getAttribute('rotation').y,
z: activeBlock.getAttribute('rotation').z
});
}
}
});
}
});
// Initialize the components
AFRAME.registerComponent('synthesizer', AFRAME.components['wave-synthesizer']);
AFRAME.registerComponent('controller', AFRAME.components['controller-input']);
</script>
</body>
</html>