// Ses: kodla üretilen mehter (kös, davul, nakkare, zil, zurna) + efektler (top, çöküş, ok, kılıç).
// YouTube linki verilirse müzik YouTube'dan çalar, efektler yine buradan.
import { createYoutube } from './youtube.js';
// Hicaz dizisi (Re'den): yarım ses adımları
const D4 = 293.66;
const note = (semi) => D4 * 2 ** (semi / 12);
// Zurna ezgisi: [başlangıç adımı (8'lik), uzunluk, yarım ses]; 4 ölçü x 16 adım. Kendi bestemiz.
const MELODY = [
[0, 4, 7], [4, 2, 7], [6, 2, 8], [8, 4, 7], [12, 4, 5],
[16, 4, 4], [20, 2, 5], [22, 2, 4], [24, 4, 1], [28, 4, 0],
[32, 2, 0], [34, 2, 4], [36, 2, 5], [38, 2, 7], [40, 4, 8], [44, 4, 7],
[48, 2, 5], [50, 2, 4], [52, 2, 5], [54, 2, 7], [56, 4, 4], [60, 4, 0],
// ikinci kısım: tiz
[64, 4, 12], [68, 2, 10], [70, 2, 12], [72, 4, 8], [76, 4, 7],
[80, 2, 8], [82, 2, 10], [84, 2, 8], [86, 2, 7], [88, 4, 5], [92, 4, 4],
[96, 2, 4], [98, 2, 5], [100, 2, 7], [102, 2, 8], [104, 4, 7], [108, 4, 5],
[112, 2, 4], [114, 2, 1], [116, 2, 4], [118, 2, 5], [120, 8, 0],
];
const LOOP = 128;
export function createAudio({ volume = 0.6, bpm = 100, youtube } = {}) {
const ctx = new (window.AudioContext || window.webkitAudioContext)();
const master = ctx.createGain();
master.gain.value = volume;
master.connect(ctx.destination);
const music = ctx.createGain();
music.connect(master);
const sfx = ctx.createGain();
sfx.gain.value = 0.9;
sfx.connect(master);
const noiseBuf = ctx.createBuffer(1, ctx.sampleRate * 2, ctx.sampleRate);
const nd = noiseBuf.getChannelData(0);
for (let i = 0; i < nd.length; i++) nd[i] = Math.random() * 2 - 1;
function noise(t, dur, { type = 'lowpass', freq = 1000, q = 0.7, gain = 0.5, attack = 0.002, out = sfx, sweep } = {}) {
const src = ctx.createBufferSource();
src.buffer = noiseBuf;
const f = ctx.createBiquadFilter();
f.type = type;
f.frequency.setValueAtTime(freq, t);
if (sweep) f.frequency.exponentialRampToValueAtTime(sweep, t + dur);
f.Q.value = q;
const g = ctx.createGain();
g.gain.setValueAtTime(0.0001, t);
g.gain.exponentialRampToValueAtTime(gain, t + attack);
g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
src.connect(f).connect(g).connect(out);
src.start(t, Math.random());
src.stop(t + dur + 0.05);
}
function tone(t, dur, { freq = 100, to, type = 'sine', gain = 0.5, attack = 0.003, out = sfx } = {}) {
const o = ctx.createOscillator();
o.type = type;
o.frequency.setValueAtTime(freq, t);
if (to) o.frequency.exponentialRampToValueAtTime(to, t + dur);
const g = ctx.createGain();
g.gain.setValueAtTime(0.0001, t);
g.gain.exponentialRampToValueAtTime(gain, t + attack);
g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
o.connect(g).connect(out);
o.start(t);
o.stop(t + dur + 0.05);
}
// ---------- Mehter ----------
const kos = (t) => { tone(t, 1.1, { freq: 62, to: 38, gain: 0.9, out: music }); noise(t, 0.25, { freq: 300, gain: 0.25, out: music }); };
const dum = (t) => { tone(t, 0.45, { freq: 95, to: 52, gain: 0.75, out: music }); noise(t, 0.08, { freq: 900, gain: 0.2, out: music }); };
const tek = (t) => { noise(t, 0.07, { type: 'highpass', freq: 2200, gain: 0.35, out: music }); tone(t, 0.06, { freq: 420, to: 300, gain: 0.15, out: music }); };
const nakkare = (t, hi) => tone(t, 0.12, { freq: hi ? 330 : 240, to: hi ? 260 : 190, type: 'triangle', gain: 0.22, out: music });
const zil = (t) => { noise(t, 0.9, { type: 'bandpass', freq: 7200, q: 0.6, gain: 0.16, out: music }); noise(t, 0.35, { type: 'highpass', freq: 9000, gain: 0.1, out: music }); };
function zurna(t, dur, freq) {
const g = ctx.createGain();
g.gain.setValueAtTime(0.0001, t);
g.gain.exponentialRampToValueAtTime(0.11, t + 0.025);
g.gain.setValueAtTime(0.11, t + dur * 0.8);
g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
const bp = ctx.createBiquadFilter();
bp.type = 'bandpass';
bp.frequency.value = 1500;
bp.Q.value = 0.9;
const lp = ctx.createBiquadFilter();
lp.type = 'lowpass';
lp.frequency.value = 4200;
const lfo = ctx.createOscillator();
lfo.frequency.value = 5.6;
const lfoG = ctx.createGain();
lfoG.gain.value = freq * 0.012;
lfo.connect(lfoG);
for (const [type, mul, lvl] of [['sawtooth', 1, 1], ['square', 2, 0.35]]) {
const o = ctx.createOscillator();
o.type = type;
o.frequency.setValueAtTime(freq * mul * 0.985, t);
o.frequency.exponentialRampToValueAtTime(freq * mul, t + 0.04); // zurnanın kayarak girişi
lfoG.connect(o.frequency);
const og = ctx.createGain();
og.gain.value = lvl;
o.connect(og).connect(bp);
o.start(t);
o.stop(t + dur + 0.05);
}
bp.connect(lp).connect(g).connect(music);
lfo.start(t);
lfo.stop(t + dur + 0.05);
}
const mehter = (() => {
const step = 60 / bpm / 2; // 8'lik
let timer = null;
let nextTime = 0;
let i = 0;
let startTime = 0;
function play(n, t) {
const s = n % 16;
if (s === 0) kos(t);
if (s === 0 || s === 6 || s === 8) dum(t);
if (s === 4 || s === 12 || s === 14) tek(t);
if (s === 2 || s === 10) nakkare(t, false);
if (s === 3 || s === 11 || s === 15) nakkare(t, true);
if (s === 4 || s === 12) zil(t);
const m = n % LOOP;
for (const [st, len, semi] of MELODY) if (st === m) zurna(t, len * step * 0.95, note(semi));
}
function schedule() {
while (nextTime < ctx.currentTime + 0.15) {
play(i++, nextTime);
nextTime += step;
}
}
return {
start() {
if (timer) return;
nextTime = ctx.currentTime + 0.05;
startTime = nextTime;
i = 0;
timer = setInterval(schedule, 25);
},
stop() { clearInterval(timer); timer = null; },
phase: () => (ctx.currentTime - startTime) / (step * 2),
running: () => !!timer,
};
})();
// ---------- Efektler ----------
const now = () => ctx.currentTime + 0.01;
const fx = {
cannon() {
const t = now();
tone(t, 1.2, { freq: 70, to: 28, gain: 1 });
noise(t, 1.4, { freq: 600, sweep: 120, gain: 0.9 });
noise(t, 0.12, { type: 'highpass', freq: 1500, gain: 0.4 });
},
impact(big = false) {
const t = now();
noise(t, big ? 2.2 : 1.1, { freq: big ? 500 : 800, sweep: 90, gain: big ? 0.9 : 0.55 });
tone(t, 0.6, { freq: 55, to: 30, gain: big ? 0.8 : 0.4 });
for (let k = 0; k < (big ? 8 : 3); k++) noise(t + 0.1 + Math.random() * (big ? 1.4 : 0.5), 0.15, { type: 'bandpass', freq: 1200 + Math.random() * 1500, q: 2, gain: 0.25 });
},
arrow() {
const t = now();
noise(t, 0.35, { type: 'bandpass', freq: 3000, sweep: 900, q: 3, gain: 0.25, attack: 0.05 });
},
clash() {
const t = now();
for (const f of [1870, 2630, 3810]) tone(t, 0.4, { freq: f, type: 'square', gain: 0.05 });
noise(t, 0.08, { type: 'highpass', freq: 4000, gain: 0.3 });
},
horn() {
const t = now();
const seq = [0, 4, 7, 12, 12];
seq.forEach((s, k) => zurna(t + k * 0.22, k === seq.length - 1 ? 1.4 : 0.2, note(s)));
kos(t);
kos(t + 0.9);
},
cheer() {
const t = now();
for (let k = 0; k < 6; k++) noise(t + k * 0.05, 1.6, { type: 'bandpass', freq: 500 + k * 180, q: 1.5, gain: 0.15, attack: 0.2 });
},
};
// ---------- YouTube ----------
let ytUrl = youtube?.url || '';
const ytBpm = Number(youtube?.bpm) || bpm;
let yt = null;
let nowPlaying = '';
let onTrackCb = () => {};
const getYt = () => yt || (yt = createYoutube({
volume,
onTitle: (title) => { nowPlaying = title; onTrackCb(title); },
onError: (msg) => console.warn('[youtube]', msg),
}));
const beat = { pulse: 0, phase: 0 };
let mode = 'none';
const api = {
fx,
get mode() { return mode; },
get playing() {
if (mode === 'youtube') return !!yt?.playing;
return mode !== 'none' && ctx.state === 'running';
},
get needsGesture() {
if (mode === 'youtube') return getYt().needsGesture || ctx.state !== 'running';
return ctx.state !== 'running';
},
onTrack(cb) { onTrackCb = cb; },
async setYoutube(url) {
ytUrl = url || '';
if (ytUrl) {
mehter.stop();
mode = 'youtube';
nowPlaying = 'YouTube yükleniyor…';
onTrackCb(nowPlaying);
await getYt().play(ytUrl);
} else {
yt?.stop();
mode = 'none';
await api.start();
}
},
async start() {
if (mode === 'youtube') {
getYt().resume();
try { await ctx.resume(); } catch { /* izin yok */ }
return;
}
try { await ctx.resume(); } catch { /* izin yok */ }
if (mode !== 'none') return;
if (ytUrl) return api.setYoutube(ytUrl);
mode = 'mehter';
mehter.start();
nowPlaying = 'Mehter Marşı (otomatik)';
onTrackCb(nowPlaying);
},
next() { if (mode === 'youtube') getYt().next(); },
update(dt) {
const prev = beat.phase;
if (mode === 'mehter') beat.phase = mehter.phase();
else beat.phase += ((mode === 'youtube' && yt?.playing ? ytBpm : bpm) / 60) * dt;
if (Math.floor(beat.phase) !== Math.floor(prev)) beat.pulse = 1;
beat.pulse = Math.max(0, beat.pulse - dt * 4);
return beat;
},
};
return api;
}
Lisans bilgisi projenin LICENSE dosyasında.