-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
318 lines (268 loc) · 11.6 KB
/
Copy pathscript.js
File metadata and controls
318 lines (268 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
let keyboardEffectTimer = null;
// 直前の和音の平均ピッチを保持する変数(初期値は真ん中のC付近)
let lastAveragePitch = 60;
// --- 鍵盤生成関数(initKeyboard)の修正 ---
function initKeyboard() {
const visualPiano = document.getElementById('visual-piano');
if (!visualPiano) return;
const notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
let whiteKeyCount = 0;
const whiteKeyWidth = 40;
visualPiano.innerHTML = '';
// オクターブ2から5まで生成
for (let oct = 2; oct <= 5; oct++) {
notes.forEach((note) => {
// C6より上の音はいらないのでストップ
if (oct === 5 && note !== "C") return;
const isBlack = note.includes('#');
const el = document.createElement('div');
el.className = isBlack ? 'black-key' : 'white-key';
el.dataset.note = note + oct;
if (isBlack) {
el.style.left = (whiteKeyCount * whiteKeyWidth - 13) + "px";
} else {
el.style.left = (whiteKeyCount * whiteKeyWidth) + "px";
whiteKeyCount++;
}
visualPiano.appendChild(el);
});
}
visualPiano.style.width = (whiteKeyCount * whiteKeyWidth) + "px";
}
// ページ読み込み完了時に実行
document.addEventListener('DOMContentLoaded', initKeyboard);
// --- 1. 音源設定 ---
const reverb = new Tone.Reverb(1.5).toDestination();
let effectTimer = null; // タイマー管理用変数
// 【本物のピアノ】
const piano = new Tone.Sampler({
urls: { "A1": "A1.mp3", "A2": "A2.mp3", "A3": "A3.mp3", "A4": "A4.mp3" },
baseUrl: "https://tonejs.github.io/audio/salamander/",
onload: () => console.log("Piano Ready")
}).connect(reverb);
// 【本物のギター】
const guitar = new Tone.Sampler({
urls: {
"A2": "A2.mp3",
"E2": "E2.mp3",
"G3": "G3.mp3"
},
baseUrl: "https://raw.githubusercontent.com/nbrosowsky/tonejs-instruments/master/samples/guitar-acoustic/",
onload: () => console.log("Guitar Ready")
}).connect(reverb);
// --- synth(第2音源)をサンプラーに変更 ---
const synth = new Tone.Sampler({
urls: {
"A0": "A0.mp3",
"C1": "C1.mp3",
"D#1": "Ds1.mp3",
"F#1": "Fs1.mp3",
"A1": "A1.mp3"
},
// ファイル名が確実に一致する別のリポジトリ
baseUrl: "https://tonejs.github.io/audio/salamander/",
onload: () => console.log("Second Sampler Ready")
}).connect(reverb);
// 最初はピアノをセット
let currentInstrument = piano;
// 楽器切り替え関数(ここが重要です!)
function setInstrument(type) {
// 全ての音を一度止める
piano.releaseAll();
guitar.releaseAll();
synth.releaseAll();
if (type === 'piano') {
currentInstrument = piano;
} else if (type === 'guitar') {
currentInstrument = guitar;
} else {
currentInstrument = synth;
}
console.log("Switched to:", type);
}
// --- 1. 音源設定 (Tone.js) ---
// 起動時にエラーが出ないよう、PolySynthを定義。
// 最初のタッチイベントで context を再開させます。
const polySynth = new Tone.PolySynth(Tone.Synth, {
oscillator: { type: "triangle" },
envelope: { attack: 0.05, decay: 0.3, sustain: 0.4, release: 0.8 }
}).toDestination();
// --- 0. ピアノ鍵盤の自動生成 (C2〜D4に変更) ---
const visualPiano = document.getElementById('visual-piano');
const notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
let whiteKeyCount = 0;
const whiteKeyWidth = 40;
visualPiano.innerHTML = ''; // 既存の鍵盤をクリア
for (let oct = 2; oct <= 4; oct++) { // 2から4までに変更
notes.forEach((note) => {
// D4より上の音はいらないのでストップ
if (oct === 4 && !["C", "C#", "D"].includes(note)) return;
const isBlack = note.includes('#');
const el = document.createElement('div');
el.className = isBlack ? 'black-key' : 'white-key';
el.dataset.note = note + oct;
if (isBlack) {
el.style.left = (whiteKeyCount * whiteKeyWidth - 13) + "px";
} else {
el.style.left = (whiteKeyCount * whiteKeyWidth) + "px";
whiteKeyCount++;
}
visualPiano.appendChild(el);
});
}
visualPiano.style.minWidth = (whiteKeyCount * whiteKeyWidth) + "px";
// --- 2. データ定義の拡張 ---
const CHORD_INTERVALS = {
"Major": [0, 4, 7],
"m": [0, 3, 7],
"7": [0, 4, 7, 10],
"m7": [0, 3, 7, 10],
"M7": [0, 4, 7, 11], // メジャーセブンを追加
"sus4": [0, 5, 7] // サスフォーを追加
};
const NOTE_MAP = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
// --- 3. フリックロジック ---
let startX, startY;
const keys = document.querySelectorAll('.ime-key');
keys.forEach(key => {
// マウス操作でも確認できるように 'mousedown' も追加(デバッグ用)
const startAction = (e) => {
const touch = e.touches ? e.touches[0] : e;
startX = touch.clientX;
startY = touch.clientY;
// ブラウザの音声再生制限を解除(重要)
if (Tone.context.state !== 'running') {
Tone.context.resume();
}
};
const endAction = (e) => {
const touch = e.changedTouches ? e.changedTouches[0] : e;
const diffX = touch.clientX - startX;
const diffY = touch.clientY - startY;
let quality = "Major";
const threshold = 30;
// 斜め判定を含む8方向認識
if (diffY < -threshold && diffX < -threshold) {
quality = "M7"; // 左上:M7
} else if (diffY < -threshold && diffX > threshold) {
quality = "sus4"; // 右上:sus4
} else if (diffY < -threshold) {
quality = "m"; // 上:m
} else if (diffX > threshold) {
quality = "7"; // 右:7
} else if (diffY > threshold) {
quality = "m7"; // 下:m7
}
let root = key.dataset.root;
if (!/\d/.test(root)) root += "4";
playChord(root, quality);
};
key.addEventListener('touchstart', startAction, {passive: false});
key.addEventListener('touchend', endAction, {passive: false});
// PCブラウザでのテスト用
key.addEventListener('mousedown', startAction);
key.addEventListener('mouseup', endAction);
});
// --- 3. 発音ロジックの改良(ボイスリーディング対応版) ---
function playChord(root, quality) {
if (keyboardEffectTimer) clearTimeout(keyboardEffectTimer);
const octaveSelect = document.getElementById('octave-select');
// UIで選んだオクターブ (3, 4, 5)
const selectedOctave = octaveSelect ? parseInt(octaveSelect.value) : 4;
// 表示系エフェクトのリセット
const displayBox = document.querySelector('.display-main');
if (displayBox) displayBox.classList.remove('effect-off');
document.querySelectorAll('.white-key, .black-key').forEach(k => {
k.classList.remove('key-active', 'key-active-off');
});
const rootName = root.replace(/[0-9]/g, '');
const rootIdx = NOTE_MAP.indexOf(rootName);
if (rootIdx === -1) return;
// 1. 和音の構成音(音名)を計算
const chordNotesNames = CHORD_INTERVALS[quality].map(interval => {
return NOTE_MAP[(rootIdx + interval) % 12];
});
// 2. 最適なボイシング(MIDI番号)を計算
// selectedOctaveが3なら、MIDI番号48(C3)付近を基準に和音を作る
const bestMidiNotes = getBestInversion(chordNotesNames, selectedOctave);
// 3. ベース音の準備(音は鳴らすが、光らせないリスト)
// 選択オクターブの1オクターブ下をベースとする
const bassMidi = (NOTE_MAP.indexOf(rootName) + (selectedOctave + 1) * 12);
const playNotes = [bassMidi, ...bestMidiNotes].map(n => Tone.Frequency(n, "midi").toNote());
// 4. 発音処理
currentInstrument.releaseAll();
if (currentInstrument === guitar) {
currentInstrument.triggerAttackRelease(playNotes[0], "2n", Tone.now());
bestMidiNotes.forEach((midi, i) => {
const note = Tone.Frequency(midi, "midi").toNote();
currentInstrument.triggerAttackRelease(note, "2n", Tone.now() + 0.05 + (i * 0.05));
});
} else {
currentInstrument.triggerAttackRelease(playNotes, "2n");
}
// --- 表示処理 ---
const display = document.getElementById('current-chord');
if (display) display.innerText = rootName + (quality === "Major" ? "" : quality);
// 【重要】和音の構成音(bestMidiNotes)だけを光らせる
bestMidiNotes.forEach(midi => {
// MIDI番号から音名+オクターブを取得 (例: 48 -> "C3")
const noteWithOct = Tone.Frequency(midi, "midi").toNote();
// 画面上の鍵盤(C2〜D4)にマッピングするための調整
// MIDI番号48はC3ですが、画面の構成に合わせてオクターブ値を-1して検索します
const name = noteWithOct.replace(/[0-9]/g, '');
const oct = parseInt(noteWithOct.replace(/[^0-9]/g, ''));
const displayNote = name + (oct - 1); // 画面のデータ属性(data-note)に合わせる
const el = document.querySelector(`[data-note="${displayNote}"]`);
if (el) el.classList.add('key-active');
});
keyboardEffectTimer = setTimeout(() => {
document.querySelectorAll('.key-active').forEach(k => k.classList.add('key-active-off'));
}, 5000);
// 7. スクロール(新しく光った鍵盤へ自動フォーカス)
// 少し遅らせて実行することで、描画タイミングとのズレを防ぎます
setTimeout(() => {
const activeKeys = document.querySelectorAll('.key-active');
if (activeKeys.length > 0) {
// 最初(一番左側)の活動鍵盤を中央付近に表示
activeKeys[0].scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'center'
});
}
}, 50);
}
/**
* ターゲットオクターブを考慮した最短距離計算
*/
function getBestInversion(chordNotes, targetOctave) {
// MIDI番号のオフセットを調整(画面上のLow=3のとき、MIDIの48付近から始まるように)
const octaveOffset = (targetOctave + 1) * 12;
const targetCenter = octaveOffset + 6;
let patterns = [];
for (let i = 0; i < chordNotes.length; i++) {
let pattern = [];
for (let j = 0; j < chordNotes.length; j++) {
let noteIdx = (i + j) % chordNotes.length;
let midi = NOTE_MAP.indexOf(chordNotes[noteIdx]) + octaveOffset;
if (j > 0 && midi <= pattern[j-1]) midi += 12;
pattern.push(midi);
}
patterns.push(pattern);
}
let bestPattern = patterns[0];
let minScore = Infinity;
patterns.forEach(pattern => {
const avg = pattern.reduce((a, b) => a + b) / pattern.length;
const distFromLast = Math.abs(avg - lastAveragePitch);
const distFromTarget = Math.abs(avg - targetCenter);
// 直前の音を優先しつつ、ユーザーが選んだオクターブの範囲内に収まるようにスコアリング
const score = (distFromLast * 0.7) + (distFromTarget * 0.3);
if (score < minScore) {
minScore = score;
bestPattern = pattern;
}
});
lastAveragePitch = bestPattern.reduce((a, b) => a + b) / bestPattern.length;
return bestPattern;
}