JusTalk / templates /index.html
buletomato25
adduser
f742eba
raw
history blame
8.19 kB
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voice Recorder Interface</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body
class="flex flex-col items-center justify-center h-screen bg-gray-900 text-white"
>
<!-- メンバーを登録ボタン -->
<div class="flex items-center mb-5">
<button
id="registerButton"
onclick="showUserRegister()"
class="px-4 py-2 bg-blue-600 rounded-md hover:bg-blue-700 transition"
>
メンバーを登録
</button>
</div>
<!-- チャート表示部 -->
<div class="chart w-72 h-72 mb-5">
<canvas id="speechChart"></canvas>
</div>
<form
id="recordForm"
action="/submit"
method="POST"
class="flex items-center space-x-2 w-full sm:w-auto"
onsubmit="event.preventDefault();"
>
<input
type="text"
name="name"
placeholder="名前を入力"
class="flex-1 px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 bg-gray-700 text-white"
/>
<!-- 録音ボタン -->
<button
type="button"
class="record-button"
id="recordButton"
onclick="toggleRecording()"
>
<div class="record-icon" id="recordIcon"></div>
</button>
</form>
<!-- 名前入力 -->
<!-- 結果ボタン -->
<div class="flex mt-5">
<button
id="historyButton"
onclick="showTalkdetail()"
class="result-button px-4 py-2 mx-2 bg-green-600 rounded-md hover:bg-green-700 transition"
>
会話履歴を表示
</button>
<button
id="feedbackButton"
onclick="showResults()"
class="result-button px-4 py-2 mx-2 bg-blue-600 rounded-md hover:bg-blue-700 transition"
>
フィードバック画面を表示
</button>
</div>
<style>
.record-button {
width: 80px;
height: 80px;
background-color: transparent;
border-radius: 50%;
border: 4px solid white;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.4);
transition: all 0.2s ease;
}
.record-icon {
width: 60px;
height: 60px;
background-color: #d32f2f;
border-radius: 50%;
transition: all 0.2s ease;
}
.recording .record-icon {
width: 40px;
height: 40px;
border-radius: 10%;
}
</style>
<script>
let isRecording = false;
let mediaRecorder;
let audioChunks = [];
let recordingInterval;
let count_voice = 0;
let before_rate = 0;
// 初期設定:人数と名前を受け取って円グラフを作成
let members = ["aaa", "bbb", "ccc", "その他"];
let voiceData = [50, 20, 20, 10]; // 自分と不明の割合を仮設定
// Chart.js の初期化
const ctx = document.getElementById("speechChart").getContext("2d");
const speechChart = new Chart(ctx, {
type: "doughnut",
data: {
labels: members,
datasets: [
{
data: voiceData,
backgroundColor: ["#4caf50", "#757575"],
},
],
},
options: {
responsive: true,
plugins: {
legend: {
display: true,
position: "bottom",
labels: { color: "white" },
},
},
},
});
//録音ボタン見た目変化
function toggleRecording() {
isRecording = !isRecording;
const recordIcon = document.getElementById("recordIcon");
if (isRecording) {
recordIcon.classList.add("w-10", "h-10", "bg-red-900", "rounded-md");
} else {
recordIcon.classList.remove(
"w-10",
"h-10",
"bg-red-900",
"rounded-md"
);
}
}
async function toggleRecording() {
const recordButton = document.getElementById("recordButton");
if (!isRecording) {
// 録音開始
isRecording = true;
recordButton.classList.add("recording");
try {
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
mediaRecorder = new MediaRecorder(stream);
audioChunks = [];
mediaRecorder.ondataavailable = (event) => {
if (event.data.size > 0) {
audioChunks.push(event.data);
}
};
mediaRecorder.onstop = () => {
sendAudioChunks([...audioChunks]);
audioChunks = [];
};
mediaRecorder.start();
} catch (error) {
console.error("マイクへのアクセスに失敗しました:", error);
isRecording = false;
recordButton.classList.remove("recording");
}
} else {
// 手動停止
isRecording = false;
recordButton.classList.remove("recording");
if (mediaRecorder && mediaRecorder.state === "recording") {
mediaRecorder.stop();
count_voice = 0;
before_rate = 0;
}
}
}
function sendAudioChunks(chunks) {
const audioBlob = new Blob(chunks, { type: "audio/wav" });
const reader = new FileReader();
reader.onloadend = () => {
const base64String = reader.result.split(",")[1]; // Base64エンコードされた音声データ
// フォーム要素を取得
const form = document.getElementById("recordForm");
const nameInput = form.querySelector('input[name="name"]');
const name = nameInput ? nameInput.value : "unknown"; // 名前がない
fetch("/upload_audio", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ audio_data: base64String, name: name }),
})
.then((response) => response.json())
.then((data) => {
if (data.error) {
alert("エラー: " + data.error);
console.error(data.details);
} else if (data.rate !== undefined) {
// 通常モードの場合、解析結果をチャートに反映
if (count_voice === 0) {
speechChart.data.datasets[0].data = [
data.rate,
100 - data.rate,
];
before_rate = data.rate;
} else if (count_voice === 1) {
let tmp_rate = (data.rate + before_rate) / 2;
speechChart.data.datasets[0].data = [
tmp_rate,
100 - tmp_rate,
];
before_rate = tmp_rate;
} else {
let tmp_rate = (data.rate + before_rate * 2) / 3;
speechChart.data.datasets[0].data = [
tmp_rate,
100 - tmp_rate,
];
before_rate = tmp_rate;
}
count_voice++;
speechChart.update();
}
})
.catch((error) => {
console.error("エラー:", error);
});
};
reader.readAsDataURL(audioBlob);
}
function showTalkdetail() {
window.location.href = "talk_detail";
}
function showResults() {
// フィードバック画面へ遷移
window.location.href = "feedback";
}
function showUserRegister() {
// 音声登録画面へ遷移
window.location.href = "userregister";
}
</script>
</body>
</html>