Spaces:
Running
Running
Update templates/index.html
Browse files- templates/index.html +75 -58
templates/index.html
CHANGED
@@ -47,8 +47,12 @@
|
|
47 |
height: 40px;
|
48 |
border-radius: 10%;
|
49 |
}
|
50 |
-
.result-
|
51 |
margin-top: 20px;
|
|
|
|
|
|
|
|
|
52 |
padding: 10px 20px;
|
53 |
background-color: #4caf50;
|
54 |
border: none;
|
@@ -56,6 +60,7 @@
|
|
56 |
color: white;
|
57 |
cursor: pointer;
|
58 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.4);
|
|
|
59 |
}
|
60 |
.result-button:hover {
|
61 |
background-color: #388e3c;
|
@@ -67,31 +72,48 @@
|
|
67 |
<div class="chart">
|
68 |
<canvas id="speechChart"></canvas>
|
69 |
</div>
|
|
|
70 |
<button class="record-button" id="recordButton" onclick="toggleRecording()">
|
71 |
<div class="record-icon" id="recordIcon"></div>
|
72 |
</button>
|
73 |
-
|
74 |
-
<
|
75 |
-
<
|
76 |
-
|
77 |
-
|
78 |
-
</button>
|
79 |
-
<button class="result-button" id="resultButton" onclick="showResults()">
|
80 |
-
フィードバック画面を表示
|
81 |
-
</button>
|
82 |
-
</div>
|
83 |
-
</form>
|
84 |
|
85 |
<script>
|
86 |
let isRecording = false;
|
87 |
let mediaRecorder;
|
88 |
let audioChunks = [];
|
89 |
-
let isRecordingStopped = false; //自動停止を管理する
|
90 |
let recordingInterval;
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
async function toggleRecording() {
|
93 |
const recordButton = document.getElementById('recordButton');
|
94 |
-
|
95 |
if (!isRecording) {
|
96 |
// 録音開始
|
97 |
isRecording = true;
|
@@ -100,47 +122,46 @@
|
|
100 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
101 |
mediaRecorder = new MediaRecorder(stream);
|
102 |
audioChunks = [];
|
103 |
-
|
104 |
mediaRecorder.ondataavailable = event => {
|
105 |
if (event.data.size > 0) {
|
106 |
audioChunks.push(event.data);
|
107 |
}
|
108 |
};
|
|
|
109 |
mediaRecorder.onstop = () => {
|
110 |
sendAudioChunks([...audioChunks]);
|
111 |
audioChunks = [];
|
112 |
};
|
113 |
-
|
114 |
mediaRecorder.start();
|
115 |
-
|
116 |
recordingInterval = setInterval(() => {
|
117 |
-
if(
|
118 |
-
isRecordingStopped = true;
|
119 |
mediaRecorder.stop();
|
120 |
}
|
121 |
-
},10000);
|
122 |
} catch (error) {
|
123 |
console.error('マイクへのアクセスに失敗しました:', error);
|
124 |
isRecording = false;
|
125 |
recordButton.classList.remove('recording');
|
126 |
}
|
127 |
} else {
|
128 |
-
//
|
129 |
isRecording = false;
|
130 |
recordButton.classList.remove('recording');
|
131 |
clearInterval(recordingInterval);
|
132 |
-
if (mediaRecorder && mediaRecorder.state
|
133 |
mediaRecorder.stop();
|
134 |
}
|
135 |
}
|
136 |
}
|
137 |
-
|
138 |
-
function sendAudioChunks(chunks){
|
139 |
const audioBlob = new Blob(chunks, { type: 'audio/wav' });
|
140 |
const reader = new FileReader();
|
141 |
reader.onloadend = () => {
|
142 |
-
const base64String = reader.result.split(',')[1]; // Base64
|
143 |
-
// サーバーへ音声データを送信
|
144 |
fetch('/upload_audio', {
|
145 |
method: 'POST',
|
146 |
headers: {
|
@@ -150,46 +171,42 @@
|
|
150 |
})
|
151 |
.then(response => response.json())
|
152 |
.then(data => {
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
})
|
155 |
.catch(error => {
|
156 |
console.error('エラー:', error);
|
|
|
|
|
|
|
157 |
});
|
158 |
};
|
159 |
reader.readAsDataURL(audioBlob);
|
160 |
-
|
161 |
-
isRecordingStopped = false;
|
162 |
-
mediaRecorder.start();
|
163 |
}
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
function showResults() {
|
|
|
166 |
window.location.href = 'feedback.html';
|
167 |
}
|
168 |
-
|
169 |
-
// Chart.js の初期化
|
170 |
-
const ctx = document.getElementById('speechChart').getContext('2d');
|
171 |
-
const speechChart = new Chart(ctx, {
|
172 |
-
type: 'doughnut',
|
173 |
-
data: {
|
174 |
-
labels: ['自分', '他の人'],
|
175 |
-
datasets: [{
|
176 |
-
data: [30, 70],
|
177 |
-
backgroundColor: ['#4caf50', '#757575'],
|
178 |
-
}],
|
179 |
-
},
|
180 |
-
options: {
|
181 |
-
responsive: true,
|
182 |
-
plugins: {
|
183 |
-
legend: {
|
184 |
-
display: true,
|
185 |
-
position: 'bottom',
|
186 |
-
labels: {
|
187 |
-
color: 'white'
|
188 |
-
}
|
189 |
-
}
|
190 |
-
}
|
191 |
-
}
|
192 |
-
});
|
193 |
</script>
|
194 |
</body>
|
195 |
-
</html>
|
|
|
47 |
height: 40px;
|
48 |
border-radius: 10%;
|
49 |
}
|
50 |
+
.result-buttons {
|
51 |
margin-top: 20px;
|
52 |
+
display: flex;
|
53 |
+
gap: 10px;
|
54 |
+
}
|
55 |
+
.result-button {
|
56 |
padding: 10px 20px;
|
57 |
background-color: #4caf50;
|
58 |
border: none;
|
|
|
60 |
color: white;
|
61 |
cursor: pointer;
|
62 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.4);
|
63 |
+
transition: background-color 0.2s ease;
|
64 |
}
|
65 |
.result-button:hover {
|
66 |
background-color: #388e3c;
|
|
|
72 |
<div class="chart">
|
73 |
<canvas id="speechChart"></canvas>
|
74 |
</div>
|
75 |
+
|
76 |
<button class="record-button" id="recordButton" onclick="toggleRecording()">
|
77 |
<div class="record-icon" id="recordIcon"></div>
|
78 |
</button>
|
79 |
+
|
80 |
+
<div class="result-buttons">
|
81 |
+
<button class="result-button" id="historyButton" onclick="showHistory()">会話履歴を表示</button>
|
82 |
+
<button class="result-button" id="feedbackButton" onclick="showResults()">フィードバック画面を表示</button>
|
83 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
<script>
|
86 |
let isRecording = false;
|
87 |
let mediaRecorder;
|
88 |
let audioChunks = [];
|
|
|
89 |
let recordingInterval;
|
90 |
+
|
91 |
+
// Chart.js の初期化
|
92 |
+
const ctx = document.getElementById('speechChart').getContext('2d');
|
93 |
+
const speechChart = new Chart(ctx, {
|
94 |
+
type: 'doughnut',
|
95 |
+
data: {
|
96 |
+
labels: ['自分', '他の人'],
|
97 |
+
datasets: [{
|
98 |
+
data: [30, 70],
|
99 |
+
backgroundColor: ['#4caf50', '#757575'],
|
100 |
+
}],
|
101 |
+
},
|
102 |
+
options: {
|
103 |
+
responsive: true,
|
104 |
+
plugins: {
|
105 |
+
legend: {
|
106 |
+
display: true,
|
107 |
+
position: 'bottom',
|
108 |
+
labels: { color: 'white' }
|
109 |
+
}
|
110 |
+
}
|
111 |
+
}
|
112 |
+
});
|
113 |
+
|
114 |
async function toggleRecording() {
|
115 |
const recordButton = document.getElementById('recordButton');
|
116 |
+
|
117 |
if (!isRecording) {
|
118 |
// 録音開始
|
119 |
isRecording = true;
|
|
|
122 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
123 |
mediaRecorder = new MediaRecorder(stream);
|
124 |
audioChunks = [];
|
125 |
+
|
126 |
mediaRecorder.ondataavailable = event => {
|
127 |
if (event.data.size > 0) {
|
128 |
audioChunks.push(event.data);
|
129 |
}
|
130 |
};
|
131 |
+
|
132 |
mediaRecorder.onstop = () => {
|
133 |
sendAudioChunks([...audioChunks]);
|
134 |
audioChunks = [];
|
135 |
};
|
136 |
+
|
137 |
mediaRecorder.start();
|
138 |
+
// 10秒ごとに自動で停止して送信
|
139 |
recordingInterval = setInterval(() => {
|
140 |
+
if (mediaRecorder && mediaRecorder.state === "recording") {
|
|
|
141 |
mediaRecorder.stop();
|
142 |
}
|
143 |
+
}, 10000);
|
144 |
} catch (error) {
|
145 |
console.error('マイクへのアクセスに失敗しました:', error);
|
146 |
isRecording = false;
|
147 |
recordButton.classList.remove('recording');
|
148 |
}
|
149 |
} else {
|
150 |
+
// 手動停止
|
151 |
isRecording = false;
|
152 |
recordButton.classList.remove('recording');
|
153 |
clearInterval(recordingInterval);
|
154 |
+
if (mediaRecorder && mediaRecorder.state === "recording") {
|
155 |
mediaRecorder.stop();
|
156 |
}
|
157 |
}
|
158 |
}
|
159 |
+
|
160 |
+
function sendAudioChunks(chunks) {
|
161 |
const audioBlob = new Blob(chunks, { type: 'audio/wav' });
|
162 |
const reader = new FileReader();
|
163 |
reader.onloadend = () => {
|
164 |
+
const base64String = reader.result.split(',')[1]; // Base64エンコードされた音声データ
|
|
|
165 |
fetch('/upload_audio', {
|
166 |
method: 'POST',
|
167 |
headers: {
|
|
|
171 |
})
|
172 |
.then(response => response.json())
|
173 |
.then(data => {
|
174 |
+
if (data.error) {
|
175 |
+
alert('エラー: ' + data.error);
|
176 |
+
console.error(data.details);
|
177 |
+
} else if (data.rate !== undefined) {
|
178 |
+
// 解析結果が返ってきた場合はチャートを更新
|
179 |
+
speechChart.data.datasets[0].data = [data.rate, 100 - data.rate];
|
180 |
+
speechChart.update();
|
181 |
+
alert('音声の解析が完了しました。自分の音声: ' + data.rate.toFixed(2) + '%, 他の人: ' + (100 - data.rate).toFixed(2) + '%');
|
182 |
+
} else {
|
183 |
+
alert('音声がバックエンドに送信されました。');
|
184 |
+
}
|
185 |
+
// 録音が継続中であれば、再度録音を開始(自動録音の連続処理)
|
186 |
+
if (isRecording && mediaRecorder && mediaRecorder.state === "inactive") {
|
187 |
+
mediaRecorder.start();
|
188 |
+
}
|
189 |
})
|
190 |
.catch(error => {
|
191 |
console.error('エラー:', error);
|
192 |
+
if (isRecording && mediaRecorder && mediaRecorder.state === "inactive") {
|
193 |
+
mediaRecorder.start();
|
194 |
+
}
|
195 |
});
|
196 |
};
|
197 |
reader.readAsDataURL(audioBlob);
|
|
|
|
|
|
|
198 |
}
|
199 |
+
|
200 |
+
function showHistory() {
|
201 |
+
// 会話履歴表示の画面があれば、そのページへ遷移する例
|
202 |
+
// window.location.href = 'history.html';
|
203 |
+
alert('会話履歴を表示する機能は未実装です。');
|
204 |
+
}
|
205 |
+
|
206 |
function showResults() {
|
207 |
+
// フィードバック画面へ遷移
|
208 |
window.location.href = 'feedback.html';
|
209 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
</script>
|
211 |
</body>
|
212 |
+
</html>
|