ploxion-beats/app.blade.php

161 lines
14 KiB
PHP

{{-- PL0XION beats 🥁 — LA BIBLIOTHÈQUE DE BEATS (rs-4, RS-7 : « toute une bibliothèque entière de
tous les beats »). Boîte à rythmes NATIVE : synthés de batterie Web Audio (zéro sample externe,
offline, chill-tekk) + gros catalogue de patterns classiques par genre (house/techno/funk/hiphop/
trap/dnb/breakbeat/disco/afrobeat/reggaeton/rock/jazz…). Clic = joue en boucle. Séquenceur 16 pas
éditable. Émet music.note.on par frappe (Hydra réagit) + music.pattern.active + tsoin. Tempo knob. --}}
<style>
.bts{display:flex;height:100%;background:#080a12;color:#e6ecff;font-family:var(--xr-font-ui,ui-sans-serif,system-ui);overflow:hidden}
.bts__lib{flex:0 0 210px;display:flex;flex-direction:column;border-right:1px solid #1a2340;min-height:0}
.bts__lh{padding:8px 10px;border-bottom:1px solid #1a2340;display:flex;gap:6px;flex-wrap:wrap}
.bts__gsel{flex:1;background:#0b1020;border:1px solid #222a48;color:#cfe;border-radius:7px;padding:5px;font:inherit;font-size:.74rem}
.bts__list{flex:1;overflow:auto;padding:5px}
.bts__it{cursor:pointer;padding:6px 9px;border-radius:8px;font-size:.76rem;display:flex;justify-content:space-between;gap:6px}
.bts__it:hover{background:rgba(143,123,255,.12)} .bts__it.on{background:#8f7bff;color:#08040f;font-weight:700}
.bts__it small{opacity:.6;font-size:.62rem}
.bts__main{flex:1;display:flex;flex-direction:column;min-width:0}
.bts__h{display:flex;gap:9px;align-items:center;padding:9px 12px;border-bottom:1px solid #1a2340;flex-wrap:wrap}
.bts__h b{color:#8f7bff;font-weight:800}
.bts__btn{cursor:pointer;border:0;border-radius:9px;background:#8f7bff;color:#08040f;font:inherit;font-weight:800;font-size:.78rem;padding:8px 14px}
.bts__btn.sec{background:rgba(40,40,70,.7);color:#cdd0f0;border:1px solid #33385c;font-weight:600}
.bts__grid{flex:1;overflow:auto;padding:10px 12px}
.bts__track{display:flex;align-items:center;gap:6px;margin-bottom:5px}
.bts__track .lbl{width:44px;font-size:.66rem;color:#9aa0d0;text-transform:uppercase}
.bts__steps{display:grid;grid-template-columns:repeat(16,1fr);gap:3px;flex:1}
.bts__c{aspect-ratio:1;border-radius:4px;background:#141a30;cursor:pointer;border:1px solid #202844}
.bts__c.on{background:#8f7bff;border-color:#a99cff}
.bts__c.beat{outline:1px solid #2a3358}
.bts__c.play{box-shadow:0 0 0 2px #6affc0}
.bts__knobs{display:flex;gap:9px}
</style>
<script>
(function () {
'use strict';
// ---- catalogue : tracks encodées 'x'=frappe '.'=silence (16 pas) ----
var TR = ['k', 's', 'h', 'o', 'c', 't', 'r']; // kick snare hat openhat clap tom rim
function P(g, n, bpm, k, s, h, o, c, t, r) { return { g: g, n: n, bpm: bpm, k: k, s: s, h: h || '', o: o || '', c: c || '', t: t || '', r: r || '' }; }
var LIB = [
P('house', 'Classic House', 124, 'x...x...x...x...', '....x.......x...', 'x.x.x.x.x.x.x.x.', '..x...x...x...x.', '....x.......x...'),
P('house', 'Deep House', 122, 'x...x...x...x...', '', '..x...x...x...x.', 'x...x...x...x...', '....x.......x...'),
P('house', 'Jackin', 126, 'x..xx...x...x..x', '....x.......x...', 'x.x.x.x.x.x.x.x.', '', '....x.......x...'),
P('techno', 'Detroit', 130, 'x...x...x...x...', '', 'x.x.x.x.x.x.x.x.', '..x...x...x...x.', '', '', '..x...x...x...x.'),
P('techno', 'Peak Time', 134, 'x...x...x...x...', 'x...x...x...x...', '..x...x...x...x.', '....x.......x...', '', 'x...x...x...x...'),
P('techno', 'Hypnotic', 128, 'x...x...x...x...', '', '.x.x.x.x.x.x.x.x', '', '', '', 'x.x.x.x.x.x.x.x.'),
P('funk', 'James Brown', 100, 'x......x..x.....', '....x.......x...', 'x.xxx.xxx.xxx.xx', '', '', '......x.....x..x', '..x.......x.....'),
P('funk', 'Purdie Shuffle', 96, 'x.....x...x.....', '...x....x...x..x', 'x.xx.xx.x.xx.xx.', '', '', '', 'x..x.x..x..x.x..'),
P('hiphop', 'Boom Bap', 90, 'x.....x...x.....', '....x.......x...', 'x.x.x.x.x.x.x.x.', '', '', '', ''),
P('hiphop', 'Lo-Fi', 82, 'x.......x.x.....', '....x.......x...', 'x.x.x.x.x.x.x.x.', '', '', '', '..x...x...x...x.'),
P('trap', 'Trap 1', 140, 'x.....x.....x...', '....x.......x...', 'x.xxx.xxxxx.xxxx', '', '....x.......x...'),
P('trap', 'Drill', 142, 'x...x.....x.x...', '....x.......x...', 'xxxx.xxx.xxxxx.x', '', '....x.......x...'),
P('dnb', 'Amen', 174, 'x......x..x.....', '....x.......x..x', 'x.x.x.x.x.x.x.x.', '', '', '.....x.....x...x'),
P('dnb', 'Liquid', 172, 'x.......x.......', '....x.......x...', 'x.x.x.x.x.x.x.x.', '..x...x...x...x.'),
P('breakbeat', 'Big Beat', 130, 'x..x....x..x....', '....x.......x...', 'x.x.x.x.x.x.x.x.', '', '', 'x..x....x.x.....'),
P('disco', 'Four on Floor', 118, 'x...x...x...x...', '....x.......x...', '..x...x...x...x.', 'x.x.x.x.x.x.x.x.', '....x.......x...'),
P('afrobeat', 'Afrobeat', 112, 'x..x..x...x..x..', '....x.......x...', 'x.xx.xx.x.xx.xx.', '', '', '..x..x....x..x..', 'x..x..x...x..x..'),
P('reggaeton', 'Dembow', 96, 'x...x...x...x...', '..x..x....x..x..', 'x.x.x.x.x.x.x.x.', '', '..x..x....x..x..'),
P('rock', 'Rock Straight', 120, 'x...x...x...x...', '....x.......x...', 'x.x.x.x.x.x.x.x.', '', '', '..........x.x...'),
P('rock', 'Half-Time', 90, 'x.......x.......', '........x.......', 'x.x.x.x.x.x.x.x.'),
P('jazz', 'Swing', 130, 'x.....x...x.....', '', 'x..x..x..x..x..x', '', '', '', '..x.x...x.x...x.'),
P('latin', 'Bossa', 130, 'x.....x...x.....', '', 'x.x.x.x.x.x.x.x.', '', '', '', 'x..x..x.x..x..x.'),
P('pop', 'Pop 4/4', 110, 'x...x...x...x...', '....x.......x...', 'x.x.x.x.x.x.x.x.'),
P('minimal', 'Click', 128, 'x...x...x...x...', '', '....x.......x...', '', '', '', '..x...x...x...x.')
];
var GENRES = ['tous']; LIB.forEach(function (p) { if (GENRES.indexOf(p.g) < 0) GENRES.push(p.g); });
// ---- moteur audio (synthés de batterie, zéro sample) ----
function Engine() {
var AC = window.AudioContext || window.webkitAudioContext; if (!AC) return null;
var ac = new AC(), master = ac.createGain(); master.gain.value = 0.9; master.connect(ac.destination);
var buf = null; function noise() { if (buf) return buf; buf = ac.createBuffer(1, ac.sampleRate * 0.4, ac.sampleRate); var d = buf.getChannelData(0); for (var i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1; return buf; }
function env(g, t, a, d, peak) { g.gain.setValueAtTime(0.0001, t); g.gain.exponentialRampToValueAtTime(peak || 1, t + a); g.gain.exponentialRampToValueAtTime(0.0001, t + a + d); }
function tone(t, f0, f1, dur, type, peak) { var o = ac.createOscillator(), g = ac.createGain(); o.type = type || 'sine'; o.frequency.setValueAtTime(f0, t); if (f1) o.frequency.exponentialRampToValueAtTime(f1, t + dur); env(g, t, 0.002, dur, peak); o.connect(g).connect(master); o.start(t); o.stop(t + dur + 0.05); }
function nz(t, dur, hp, peak) { var s = ac.createBufferSource(); s.buffer = noise(); var f = ac.createBiquadFilter(); f.type = hp ? 'highpass' : 'lowpass'; f.frequency.value = hp || 8000; var g = ac.createGain(); env(g, t, 0.001, dur, peak || 0.6); s.connect(f).connect(g).connect(master); s.start(t); s.stop(t + dur + 0.05); }
return { ac: ac, master: master,
k: function (t) { tone(t, 150, 48, 0.16, 'sine', 1); },
s: function (t) { tone(t, 200, 120, 0.09, 'triangle', 0.5); nz(t, 0.14, 1500, 0.7); },
h: function (t) { nz(t, 0.03, 9000, 0.35); },
o: function (t) { nz(t, 0.18, 8000, 0.3); },
c: function (t) { nz(t, 0.02, 1200, 0.5); nz(t + 0.012, 0.05, 1200, 0.5); },
t: function (t) { tone(t, 180, 90, 0.18, 'sine', 0.7); },
r: function (t) { tone(t, 900, 700, 0.03, 'square', 0.25); } };
}
var LIVE = null; // posé au mount : { cur, playing, bpm }
function describe() {
var c = LIVE ? LIVE.cur() : LIB[0];
var playing = LIVE ? LIVE.playing() : false;
var bpm = LIVE ? LIVE.bpm() : c.bpm;
return { v: 1, id: 'beats', title: 'Beats', icon: '🥁', items: [
{ p: 1, kind: 'stat', label: playing ? '▶ ' + c.n : '⏸ ' + c.n, value: bpm + ' BPM' },
{ p: 2, kind: 'action', label: playing ? 'stop' : 'jouer', act: { port: 'ploxion.beats.cmd', payload: { cmd: playing ? 'stop' : 'play' } } }
] };
}
function mount(container) {
var eng = null, cur = LIB[0], playing = false, step = 0, timer = null, nextT = 0, bpm = cur.bpm, gsel = 'tous';
container.innerHTML = '<div class="bts"><div class="bts__lib"><div class="bts__lh"><select class="bts__gsel" data-g></select></div><div class="bts__list" data-list></div></div>'
+ '<div class="bts__main"><div class="bts__h"><b>🥁 beats</b><button class="bts__btn" data-play>▶ jouer</button><button class="bts__btn sec" data-stop>■</button>'
+ '<span class="bts__ttl" data-ttl style="color:#9aa0d0;font-size:.8rem"></span><div class="bts__knobs" data-knobs></div></div>'
+ '<div class="bts__grid" data-grid></div></div></div>';
var listEl = container.querySelector('[data-list]'), gridEl = container.querySelector('[data-grid]'), ttl = container.querySelector('[data-ttl]'), gEl = container.querySelector('[data-g]');
gEl.innerHTML = GENRES.map(function (g) { return '<option>' + g + '</option>'; }).join('');
function esc(s) { var d = document.createElement('div'); d.textContent = s; return d.innerHTML; }
function renderList() {
var l = LIB.filter(function (p) { return gsel === 'tous' || p.g === gsel; });
listEl.innerHTML = l.map(function (p) { var i = LIB.indexOf(p); return '<div class="bts__it' + (p === cur ? ' on' : '') + '" data-i="' + i + '">' + esc(p.n) + '<small>' + p.g + ' · ' + p.bpm + '</small></div>'; }).join('');
}
function renderGrid() {
ttl.textContent = cur.n + ' · ' + bpm + ' BPM';
gridEl.innerHTML = TR.filter(function (tk) { return cur[tk]; }).map(function (tk) {
var s = (cur[tk] + '................').slice(0, 16);
return '<div class="bts__track"><span class="lbl">' + tk + '</span><div class="bts__steps" data-tk="' + tk + '">'
+ s.split('').map(function (ch, i) { return '<div class="bts__c' + (ch === 'x' ? ' on' : '') + (i % 4 === 0 ? ' beat' : '') + '" data-i="' + i + '"></div>'; }).join('') + '</div></div>';
}).join('');
}
function highlight() { [].forEach.call(gridEl.querySelectorAll('.bts__c'), function (c) { c.classList.toggle('play', +c.getAttribute('data-i') === step); }); }
function schedule() {
var ac = eng.ac, spb = 60 / bpm / 4; // 16e de note
while (nextT < ac.currentTime + 0.1) {
var st = step;
TR.forEach(function (tk) { var pat = cur[tk]; if (pat && pat[st] === 'x') { eng[tk](nextT);
try { window.jOSPorts && jOSPorts.emit('music', 'note.on', { pitch: tk === 'k' ? 36 : tk === 's' ? 38 : 42, velocity: 0.9, drum: tk, ts: Date.now() }); } catch (e) {} } });
var s2 = st; setTimeout(function () { step = s2; highlight(); }, (nextT - ac.currentTime) * 1000);
step = (step + 1) % 16; nextT += spb;
}
}
function play() { if (!eng) eng = Engine(); if (!eng) { ttl.textContent = 'Web Audio indisponible'; return; }
if (eng.ac.state === 'suspended') eng.ac.resume(); playing = true; step = 0; nextT = eng.ac.currentTime + 0.05;
clearInterval(timer); timer = setInterval(schedule, 25);
try { window.jOSPorts && jOSPorts.emit('music', 'pattern.active', { ploxion: 'beats', code: cur.n + ' @' + bpm }); } catch (e) {}
try { (window.__tsoinMark || (window.jOSTsoin && window.jOSTsoin.mark))('music.pattern.eval', 'beat:' + cur.n); } catch (e) {} pulse(); }
function stop() { playing = false; clearInterval(timer); timer = null; highlight(); try { window.jOSPorts && jOSPorts.emit('music', 'session.stop', { ploxion: 'beats' }); } catch (e) {} pulse(); }
// ⚡ l'horloge musicale (lot PULSE, docs/dev/30-son-et-flow.md) : quand beats joue, il EST l'horloge exacte du xerboxion — le pas
// `step` tombe à `nextT` (temps de l'AudioContext), 4 pas par temps ; rien si le bion manque ou xb:pulse=off
function pulse() { try { var P = window.jOSPulse; if (!P || !P.declarer) return; if (!playing || !eng) { P.declarer('beats', null); return; } P.declarer('beats', { nom: 'beats · ' + cur.n, bpm: bpm, ac: eng.ac, at: nextT, temps0: step / 4, noeud: eng.master }); } catch (e) {} }
gEl.addEventListener('change', function () { gsel = gEl.value; renderList(); });
listEl.addEventListener('click', function (e) { var it = e.target.closest('[data-i]'); if (!it) return; cur = LIB[+it.getAttribute('data-i')]; bpm = cur.bpm; if (kn) kn.set('bpm', bpm); renderList(); renderGrid(); if (playing) { step = 0; pulse(); } else play(); });
gridEl.addEventListener('click', function (e) { var c = e.target.closest('.bts__c'); if (!c) return; var row = c.closest('[data-tk]'), tk = row.getAttribute('data-tk'), i = +c.getAttribute('data-i');
var s = (cur[tk] + '................').slice(0, 16).split(''); s[i] = s[i] === 'x' ? '.' : 'x'; cur[tk] = s.join(''); c.classList.toggle('on'); });
container.querySelector('[data-play]').addEventListener('click', play);
container.querySelector('[data-stop]').addEventListener('click', stop);
var kn = null; if (window.jOSKnob) kn = jOSKnob.panel(container.querySelector('[data-knobs]'), [{ key: 'bpm', label: 'BPM', min: 60, max: 190, value: bpm, step: 1, fmt: function (v) { return v.toFixed(0); } }], function (v) { bpm = v.bpm; ttl.textContent = cur.n + ' · ' + (bpm | 0) + ' BPM'; pulse(); }, { shape: 'koin' });
renderList(); renderGrid();
var offCmd = null;
if (window.jOSPorts && jOSPorts.on) { offCmd = jOSPorts.on('ploxion.beats.cmd', function (p) {
var cmd = p && p.cmd; if (cmd === 'play') play(); else if (cmd === 'stop') stop(); else if (cmd === 'toggle') { if (playing) stop(); else play(); }
}); }
LIVE = { cur: function () { return cur; }, playing: function () { return playing; }, bpm: function () { return bpm; } };
return { unmount: function () { LIVE = null; if (offCmd) { try { offCmd(); } catch (e) {} } stop(); try { eng && eng.ac.close(); } catch (e) {} container.innerHTML = ''; } };
}
window.jOSApps = window.jOSApps || {};
window.jOSApps['beats'] = { manifest: { id: 'beats', name: 'Beats', icon: '🥁' }, mount: mount, describe: describe };
})();
</script>