Files
ploxion-metronome/app.blade.php

128 lines
9.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{{-- PL0XION metronome — le métronome du xerboxion (von Neumann, mode forge).
WebAudio : clic précis (scheduler lookahead), BPM 40-240, tap-tempo, mesure
(accent sur le 1), témoin visuel des temps. S'enregistre sur window.jOSSound
-> volume/start-stop depuis le 🎛 Mixer. §4.4 : unmount stoppe le scheduler +
ferme l'AudioContext. 100% local. window.jOSApps['metronome']. mount->{unmount}.
Composé sur les BIONS runtime (window.jOSb : beep + sound). --}}
@include('ploxions._bions')
<style>
.mn { display:flex; flex-direction:column; height:100%; background:var(--xr-bg,#0a0c11); color:var(--xr-text,#e9edf4); font-family:ui-sans-serif,system-ui,sans-serif; align-items:center; justify-content:center; gap:18px; padding:18px; }
.mn__bpm { font-size:clamp(48px,16vmin,96px); font-weight:800; line-height:1; font-variant-numeric:tabular-nums; color:#fbbf24; text-shadow:0 0 24px rgba(251,191,36,.35); }
.mn__bpm small { font-size:.26em; color:rgba(200,210,230,.55); font-weight:600; letter-spacing:.1em; }
.mn__beats { display:flex; gap:10px; }
.mn__dot { width:18px; height:18px; border-radius:50%; background:rgba(120,140,180,.25); transition:transform .05s, background .05s, box-shadow .05s; }
.mn__dot.on { background:#fbbf24; transform:scale(1.35); box-shadow:0 0 16px rgba(251,191,36,.8); }
.mn__dot.accent.on { background:#f87171; box-shadow:0 0 16px rgba(248,113,113,.9); }
.mn__slider { width:min(86%,360px); accent-color:#fbbf24; }
.mn__row { display:flex; gap:8px; align-items:center; flex-wrap:wrap; justify-content:center; }
.mn__b { cursor:pointer; border-radius:9px; border:1px solid rgba(251,191,36,.4); background:rgba(60,48,16,.4); color:#ffe9b0; font-size:.82rem; padding:8px 13px; font-weight:600; }
.mn__b:hover { background:rgba(90,72,24,.5); }
.mn__b.play { background:#fbbf24; color:#1a1304; border:0; font-weight:800; padding:10px 26px; font-size:.95rem; }
.mn__b.sm { padding:6px 10px; font-size:.74rem; }
.mn__seg { display:inline-flex; border:1px solid rgba(251,191,36,.3); border-radius:8px; overflow:hidden; }
.mn__seg button { cursor:pointer; border:0; background:transparent; color:#ffe9b0; font-size:.74rem; padding:6px 11px; }
.mn__seg button.on { background:rgba(251,191,36,.4); color:#1a1304; font-weight:700; }
.mn__lab { font-size:.66rem; color:rgba(190,200,225,.6); }
</style>
<script>
(function () {
'use strict';
var LIVE = null; // posé au mount : { bpm, playing }
function describe() {
var bpm = LIVE ? LIVE.bpm() : 100, playing = LIVE ? LIVE.playing() : false;
return { v: 1, id: 'metronome', title: 'Métronome', icon: '🥁', items: [
{ p: 1, kind: 'stat', label: playing ? '▶ en marche' : '⏸ arrêté', value: bpm + ' BPM' },
{ p: 2, kind: 'action', label: playing ? 'stop' : 'démarrer', act: { port: 'ploxion.metronome.cmd', payload: { cmd: 'toggle' } } }
] };
}
function mount(container, ctx) {
var bpm = 100, beats = 4, playing = false, vol = 0.5, ac = null, timer = 0, soundUnreg = null;
var nextTime = 0, beatIdx = 0, taps = [];
container.innerHTML =
'<div class="mn">'
+ ' <div class="mn__bpm"><span data-bpm>100</span> <small>BPM</small></div>'
+ ' <div class="mn__beats" data-beats></div>'
+ ' <input class="mn__slider" type="range" min="40" max="240" value="100" data-slider>'
+ ' <div class="mn__row"><button class="mn__b sm" data-dec>−</button><button class="mn__b play" data-play>▶ démarrer</button><button class="mn__b sm" data-inc>+</button></div>'
+ ' <div class="mn__row"><button class="mn__b" data-tap>tap tempo</button>'
+ ' <span class="mn__lab">mesure</span><span class="mn__seg" data-sig>'
+ [2,3,4,6].map(function (n) { return '<button data-n="' + n + '"' + (n === 4 ? ' class="on"' : '') + '>' + n + '/4</button>'; }).join('') + '</span></div>'
+ '</div>';
var elBpm = container.querySelector('[data-bpm]'), elBeats = container.querySelector('[data-beats]'), elSlider = container.querySelector('[data-slider]'), elPlay = container.querySelector('[data-play]');
function renderBeats() {
elBeats.innerHTML = '';
for (var i = 0; i < beats; i++) { var d = document.createElement('div'); d.className = 'mn__dot' + (i === 0 ? ' accent' : ''); elBeats.appendChild(d); }
}
function setBpm(v) { bpm = Math.max(40, Math.min(240, Math.round(v))); elBpm.textContent = bpm; elSlider.value = bpm; if (playing) pulse(); }
// bion ♪ onde : le clic (accent = plus aigu)
function click(accent, when) { if (ac && window.jOSb) jOSb.beep(ac, { freq: accent ? 1500 : 900, vol: vol, when: when, dur: 0.05 }); }
function flash(i) { var dots = elBeats.children; for (var k = 0; k < dots.length; k++) dots[k].classList.toggle('on', k === i);
setTimeout(function () { if (dots[i]) dots[i].classList.remove('on'); }, Math.min(150, 30000 / bpm)); }
// scheduler lookahead (timing tight)
function scheduler() {
while (nextTime < ac.currentTime + 0.1) {
var accent = (beatIdx % beats) === 0;
click(accent, nextTime);
(function (bi, t) { var d = (t - ac.currentTime) * 1000; setTimeout(function () { flash(bi % beats); }, Math.max(0, d)); })(beatIdx, nextTime);
nextTime += 60 / bpm; beatIdx++;
}
}
function start() {
if (playing) return;
ac = ac || new (window.AudioContext || window.webkitAudioContext)();
if (ac.state === 'suspended') ac.resume();
playing = true; beatIdx = 0; nextTime = ac.currentTime + 0.06;
pulse(); // avant le 1er scheduler : le temps 0 tombe à nextTime
timer = setInterval(scheduler, 25);
elPlay.textContent = '■ arrêter'; elPlay.style.background = '#f87171';
}
function stop() {
playing = false; if (timer) { clearInterval(timer); timer = 0; } pulse();
elPlay.textContent = '▶ démarrer'; elPlay.style.background = '';
for (var k = 0; k < elBeats.children.length; k++) elBeats.children[k].classList.remove('on');
}
function toggle() { playing ? stop() : start(); }
// ⚡ l'horloge musicale (lot PULSE, docs/dev/30-son-et-flow.md) : le métronome qui bat EST l'horloge exacte — le temps beatIdx
// tombe à nextTime (temps de l'AudioContext) ; l'horloge compte des mesures de 4 temps ; rien si le bion manque ou xb:pulse=off
function pulse() { try { var P = window.jOSPulse; if (!P || !P.declarer) return; if (!playing || !ac) { P.declarer('metronome', null); return; } P.declarer('metronome', { nom: 'métronome', bpm: bpm, ac: ac, at: nextTime, temps0: beatIdx }); } catch (e) {} }
elSlider.addEventListener('input', function () { setBpm(+elSlider.value); });
container.querySelector('[data-dec]').addEventListener('click', function () { setBpm(bpm - 1); });
container.querySelector('[data-inc]').addEventListener('click', function () { setBpm(bpm + 1); });
elPlay.addEventListener('click', toggle);
container.querySelector('[data-tap]').addEventListener('click', function () {
var now = Date.now(); taps.push(now); taps = taps.filter(function (t) { return now - t < 2500; });
if (taps.length >= 2) { var ivs = 0; for (var i = 1; i < taps.length; i++) ivs += taps[i] - taps[i - 1]; setBpm(60000 / (ivs / (taps.length - 1))); }
});
container.querySelector('[data-sig]').addEventListener('click', function (e) { var b = e.target.closest('[data-n]'); if (!b) return;
beats = +b.getAttribute('data-n'); container.querySelectorAll('[data-sig] button').forEach(function (x) { x.classList.toggle('on', x === b); }); renderBeats(); });
renderBeats(); setBpm(100);
// bion ♪ onde : s'enregistre au MIXER
soundUnreg = window.jOSb ? jOSb.sound({
id: 'metronome', name: 'Métronome', icon: '🥁', kind: 'métronome',
label: function () { return bpm + ' BPM'; }, sub: function () { return beats + '/4'; },
isPaused: function () { return !playing; }, toggle: toggle,
getVolume: function () { return vol; }, setVolume: function (v) { vol = Math.max(0, Math.min(1, v)); }
}) : null;
var offCmd = null;
if (window.jOSPorts && jOSPorts.on) { offCmd = jOSPorts.on('ploxion.metronome.cmd', function (p) {
var cmd = p && p.cmd; if (cmd === 'toggle') toggle(); else if (cmd === 'play') start(); else if (cmd === 'stop') stop();
}); }
LIVE = { bpm: function () { return bpm; }, playing: function () { return playing; } };
return { unmount: function () {
LIVE = null; if (offCmd) { try { offCmd(); } catch (e) {} }
stop(); if (soundUnreg) try { soundUnreg(); } catch (e) {}
if (ac) try { ac.close(); } catch (e) {} ac = null;
container.innerHTML = '';
} };
}
window.jOSApps = window.jOSApps || {};
window.jOSApps['metronome'] = { manifest: { id: 'metronome', name: 'Métronome', icon: '🥁' }, mount: mount, describe: describe };
})();
</script>