ソルフェージュ練習アプリ
以下のボタンを押すとランダムで5音が表示されます。
<!DOCTYPE html>
<html>
<head>
<title>ソルフェージュ練習アプリ</title>
</head>
<body>
<h1>ソルフェージュ練習アプリ</h1>
<p>以下のボタンを押すとランダムで5音が表示されます。</p>
<button onclick="generateNotes()">音を選ぶ</button><br><br>
<p id="notes"></p>
<button onclick="playNotes()">再生</button>
<script>
//ドレミファソラシドに対応するファイル名のリスト
const notes = ["do.mp3", "re.mp3", "mi.mp3", "fa.mp3", "so.mp3", "la.mp3", "si.mp3", "do2.mp3"];
//ランダムに5つの音を選び文字列として画面に表示
function generateNotes() {
let selectedNotes = [];
for (let i = 0; i < 5; i++) {
let index = Math.floor(Math.random() * notes.length);
selectedNotes.push(notes[index]);
}
document.getElementById("notes").textContent = "選ばれた音:" + selectedNotes.join(", ");
}
//選ばれた5つの音を順に再生
function playNotes() {
let audio;
let index = 0;
let interval = setInterval(function() {
if (index >= 5) {
clearInterval(interval);
} else {
audio = new Audio(selectedNotes[index]);
audio.play();
index++;
}
}, 1000);
}
</script>
</body>
</html>