-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
240 lines (195 loc) · 9.8 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voice to STL Model</title>
</head>
<body>
<h1>Voice to STL Model</h1>
<button id="record-btn">Start Recording</button>
<p id="status"></p>
<img id="loading" src="https://c.tenor.com/BbfKJfP43RwAAAAC/tenor.gif" style="display: none; max-width: 200px;">
<a id="download-link" href="#" download style="display: none;">Download Model</a>
<a id="prepare-print-button" href="#" style="display: none;">Prepare Print</a>
<div id="renderer-container" style="display: none;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/loaders/STLLoader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
let mediaRecorder;
let audioChunks = [];
let recording = false;
let scene, camera, renderer, loader, controls;
let stl_file_path;
const ZOO_DEV_STL_RENDER_SCALE_ADJUSTMENT = 5;
window.onload = async function () {
try {
const permissionObj = await navigator.permissions.query({ name: 'microphone' });
console.log(permissionObj.state);
} catch (error) {
console.log('Got error:', error);
}
};
async function doMakePrintableModelRequest() {
document.getElementById('loading').style.display = 'block';
const jsonData = { "file_path": stl_file_path };
try {
const response = await fetch('/make-printable-model', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
});
const data = await response.json();
document.getElementById('loading').style.display = 'none';
document.getElementById('status').innerText = data.message;
if (data.message.includes('error')) {
return;
}
document.getElementById('download-link').href = `/download/${data.file_path}`;
document.getElementById('download-link').style.display = 'block';
document.getElementById('renderer-container').style.display = 'block';
playAudio(data.message);
renderSTL(`/download/${data.file_path}`, 1);
} catch (error) {
console.error('Error:', error);
}
}
document.getElementById('prepare-print-button').addEventListener('click', () => {
playAudio("Starting to prepare printable model");
doMakePrintableModelRequest();
});
document.getElementById('record-btn').addEventListener('click', () => {
if (recording) {
mediaRecorder.stop();
document.getElementById('record-btn').innerText = "Start Recording";
document.getElementById('status').innerText = "Processing...";
recording = false;
} else {
startRecording();
}
});
async function startRecording() {
console.log("Requesting microphone access...");
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
console.log("Microphone access granted.");
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = event => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = async () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
const formData = new FormData();
formData.append('audio', audioBlob, 'recording.wav');
try {
const response = await fetch('/record', {
method: 'POST',
body: formData
});
const data = await response.json();
document.getElementById('status').innerText = data.text;
if (data.text.includes('error')) {
return;
}
playAudio(`Making ${data.text}`);
document.getElementById('loading').style.display = 'block';
const jsonData = { "prompt": data.text };
const makeModelResponse = await fetch('/make-model', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
});
const makeModelData = await makeModelResponse.json();
document.getElementById('loading').style.display = 'none';
document.getElementById('status').innerText = makeModelData.message;
if (makeModelData.message.includes('error')) {
return;
}
stl_file_path = makeModelData.file_path;
document.getElementById('download-link').href = `/download/${makeModelData.file_path}`;
document.getElementById('download-link').style.display = 'block';
document.getElementById('renderer-container').style.display = 'block';
document.getElementById('prepare-print-button').style.display = 'block';
playAudio(makeModelData.message);
renderSTL(`/download/${makeModelData.file_path}`, ZOO_DEV_STL_RENDER_SCALE_ADJUSTMENT);
setTimeout(() => {
playAudio("Now making the model printable.");
}, 1000);
doMakePrintableModelRequest();
} catch (error) {
console.error('Error:', error);
}
};
mediaRecorder.start();
document.getElementById('record-btn').innerText = "Stop Recording";
document.getElementById('status').innerText = "Recording...";
audioChunks = [];
recording = true;
} catch (error) {
console.error('Error accessing microphone:', error);
document.getElementById('status').innerText = "Microphone access denied.";
}
}
function playAudio(text) {
const msg = new SpeechSynthesisUtterance(text);
window.speechSynthesis.speak(msg);
}
function initThreeJS() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0xf0f0f0);
const canvasWidth = 500; // window.innerWidth;
camera = new THREE.PerspectiveCamera(50, canvasWidth / 500, 0.1, 1000);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(canvasWidth, 500);
document.getElementById('renderer-container').appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0x404040); // Soft white light
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);
const gridHelper = new THREE.GridHelper(10, 20);
scene.add(gridHelper);
loader = new THREE.STLLoader();
controls = new THREE.OrbitControls(camera, renderer.domElement);
}
function renderSTL(url, scale) {
loader.load(url, geometry => {
// first clear the scene
scene.remove(scene.children[scene.children.length - 1]);
const material = new THREE.MeshStandardMaterial({ color: 0x606060, roughness: 0.75, metalness: 0.1 });
const mesh = new THREE.Mesh(geometry, material);
mesh.scale.set(scale, scale, scale);
scene.add(mesh);
// Compute the bounding box of the geometry to dynamically adjust the camera
const boundingBox = new THREE.Box3().setFromObject(mesh);
const center = boundingBox.getCenter(new THREE.Vector3());
const size = boundingBox.getSize(new THREE.Vector3());
mesh.position.copy(center).negate();
const maxDim = Math.max(size.x, size.y, size.z);
const fov = camera.fov * (Math.PI / 180);
let cameraZ = Math.abs(maxDim / 2 / Math.tan(fov / 2));
cameraZ *= 2; // Ensure we can see the whole object
camera.position.set(center.x, center.y, cameraZ);
camera.lookAt(center);
controls.target.set(center.x, center.y, center.z);
controls.update();
animate();
});
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
initThreeJS();
// document.getElementById('renderer-container').style.display = 'block';
// renderSTL('/download/8da3966d-98ce-404a-a5a0-c532b3a15eea.stl', ZOO_DEV_STL_RENDER_SCALE_ADJUSTMENT);
// renderSTL('/download/export.stl', 1);
</script>
</body>
</html>