Spaces:
Sleeping
Sleeping
Update index.html
Browse files- index.html +82 -53
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,25 +72,29 @@
|
|
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 |
<script>
|
77 |
let isRecording = false;
|
78 |
let mediaRecorder;
|
79 |
let audioChunks = [];
|
80 |
-
|
81 |
-
|
|
|
82 |
const ctx = document.getElementById('speechChart').getContext('2d');
|
83 |
const speechChart = new Chart(ctx, {
|
84 |
type: 'doughnut',
|
85 |
data: {
|
86 |
labels: ['自分', '他の人'],
|
87 |
datasets: [{
|
88 |
-
// 初期値は仮のデータとして [30, 70] を設定
|
89 |
data: [30, 70],
|
90 |
backgroundColor: ['#4caf50', '#757575'],
|
91 |
}],
|
@@ -96,18 +105,15 @@
|
|
96 |
legend: {
|
97 |
display: true,
|
98 |
position: 'bottom',
|
99 |
-
labels: {
|
100 |
-
color: 'white'
|
101 |
-
}
|
102 |
}
|
103 |
}
|
104 |
}
|
105 |
});
|
106 |
-
|
107 |
async function toggleRecording() {
|
108 |
const recordButton = document.getElementById('recordButton');
|
109 |
-
|
110 |
-
|
111 |
if (!isRecording) {
|
112 |
// 録音開始
|
113 |
isRecording = true;
|
@@ -116,67 +122,90 @@
|
|
116 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
117 |
mediaRecorder = new MediaRecorder(stream);
|
118 |
audioChunks = [];
|
119 |
-
|
120 |
mediaRecorder.ondataavailable = event => {
|
121 |
if (event.data.size > 0) {
|
122 |
audioChunks.push(event.data);
|
123 |
}
|
124 |
};
|
125 |
-
|
126 |
mediaRecorder.onstop = () => {
|
127 |
-
|
128 |
-
|
129 |
-
reader.onloadend = () => {
|
130 |
-
// Base64エンコードされた文字列を取得
|
131 |
-
const base64String = reader.result.split(',')[1];
|
132 |
-
// サーバーへ音声データを送信
|
133 |
-
fetch('/upload_audio', {
|
134 |
-
method: 'POST',
|
135 |
-
headers: {
|
136 |
-
'Content-Type': 'application/json',
|
137 |
-
},
|
138 |
-
body: JSON.stringify({ audio_data: base64String }),
|
139 |
-
})
|
140 |
-
.then(response => response.json())
|
141 |
-
.then(data => {
|
142 |
-
if(data.error) {
|
143 |
-
alert('エラー: ' + data.error);
|
144 |
-
console.error(data.details);
|
145 |
-
return;
|
146 |
-
}
|
147 |
-
// data.rate はパーセンテージ(例: 40なら自分の音声が40%)\n
|
148 |
-
// 円グラフのデータを更新:自分の音声をdata.rate、他の音声を(100 - data.rate)として設定
|
149 |
-
speechChart.data.datasets[0].data = [data.rate, 100 - data.rate];
|
150 |
-
speechChart.update();
|
151 |
-
alert('音声の解析が完了しました。自分の音声: ' + data.rate.toFixed(2) + '%, 他の人: ' + (100 - data.rate).toFixed(2) + '%');
|
152 |
-
})
|
153 |
-
.catch(error => {
|
154 |
-
console.error('エラー:', error);
|
155 |
-
});
|
156 |
-
};
|
157 |
-
reader.readAsDataURL(audioBlob);
|
158 |
};
|
159 |
-
|
160 |
mediaRecorder.start();
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
} catch (error) {
|
162 |
console.error('マイクへのアクセスに失敗しました:', error);
|
163 |
isRecording = false;
|
164 |
recordButton.classList.remove('recording');
|
165 |
}
|
166 |
} else {
|
167 |
-
//
|
168 |
isRecording = false;
|
169 |
recordButton.classList.remove('recording');
|
170 |
-
|
|
|
171 |
mediaRecorder.stop();
|
172 |
}
|
173 |
}
|
174 |
}
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
function showResults() {
|
177 |
-
//
|
178 |
-
|
179 |
-
alert('結果は円グラフ上に反映されています。');
|
180 |
}
|
181 |
</script>
|
182 |
</body>
|
|
|
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 |
}],
|
|
|
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: {
|
168 |
+
'Content-Type': 'application/json',
|
169 |
+
},
|
170 |
+
body: JSON.stringify({ audio_data: base64String }),
|
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>
|