oboeru-56-sings
Version:
Singing system using Tone.js for OBOERU-56
131 lines (119 loc) • 2.14 kB
JavaScript
const samplers = {};
const notes = [
// 母音
"a",
"i",
"u",
"e",
"o",
// カ行
"ka",
"ki",
"ku",
"ke",
"ko",
// サ行
"sa",
"shi",
"su",
"se",
"so",
// タ行
"ta",
"chi",
"tsu",
"te",
"to",
// ナ行
"na",
"ni",
"nu",
"ne",
"no",
// ハ行
"ha",
"hi",
"fu",
"he",
"ho",
// マ行
"ma",
"mi",
"mu",
"me",
"mo",
// ヤ行
"ya",
"yu",
"yo",
// ラ行
"ra",
"ri",
"ru",
"re",
"ro",
// ワ行
"wa",
"wo",
"n",
];
export default async function initSamplers() {
for (const note of notes) {
try {
samplers[note] = new Tone.Sampler({
urls: {
C4: `/${note}.wav`,
},
release: 1,
baseUrl: "date",
}).toDestination();
Tone.loaded().then(() => {
return;
});
} catch (error) {
console.error(`サンプラーの初期化に失敗しました: ${note}`, error);
}
}
}
export async function loadMelodyConfig() {
try {
const response = await fetch("melodyConfig.json");
if (!response.ok) throw new Error("メロディ設定の読み込みに失敗しました");
return await response.json();
} catch (error) {
console.error(error);
return {};
}
}
export async function playText(text) {
const melodyConfig = await loadMelodyConfig();
text.split("").forEach((char, index) => {
const sampler = samplers[char];
if (sampler) {
const { pitch, duration } = melodyConfig[char] || {
pitch: "C4",
duration: "0.5",
};
setTimeout(() => {
sampler.triggerAttackRelease(pitch, duration);
}, index * 1000);
} else {
console.warn(`サンプラーが見つかりません: ${char}`);
}
});
}
export async function play(Text) {
const textInput = document.getElementById("textInput").value;
if (!textInput) {
alert("テキストを入力してください");
return;
}
const synth = new Tone.Synth().toDestination();
await Tone.start();
await playText(textInput);
}
/*
window.onload = async function () {
await initSamplers();
};
*/