Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions src/renderer/live-retune/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
// .current = { cents, source }, change event 'songcents:change'), e.g. +45c on a sharp record, so
// a standard-tuned guitar plays in tune with the record.
//
// "Follow song" adds the current chart's tuning when every string is offset by the same whole
// number of semitones (Eb standard = -1 on all strings, D standard = -2, ...), so a guitar kept
// in E standard plays along with a down-tuned song without retuning. Uneven tunings (Drop D)
// can't be matched by a whole-guitar shift and are left alone. Off by default: a player who
// physically retunes would otherwise be shifted twice.
//
// The control is appended to #player-controls; the v3 player re-homes plugin controls from there
// into its Plugins popover.
(function () {
Expand All @@ -31,7 +37,7 @@
const st = Math.max(-MAX_ST, Math.min(MAX_ST, Math.round(Number(s.semitones) || 0)));
const c = Math.max(-100, Math.min(100, Math.round(Number(s.cents) || 0)));
const mode = MODES.some((m) => m.id === s.mode) ? s.mode : 'balanced';
return { semitones: st, cents: c, mode, match: !!s.match };
return { semitones: st, cents: c, mode, match: !!s.match, follow: !!s.follow };
}

const state = load();
Expand All @@ -50,7 +56,24 @@
const cur = window.__songCents && window.__songCents.current;
return state.match && cur && typeof cur.cents === 'number' ? Math.round(cur.cents) : 0;
}
const totalCents = () => state.semitones * 100 + state.cents + matchCents();
// The song's uniform tuning offset in semitones when "Follow song" is on (0 when off, when no
// song is loaded, or when the strings aren't all shifted by the same amount).
function songTuningSt(tuning) {
if (!Array.isArray(tuning) || tuning.length === 0) return 0;
const first = Number(tuning[0]);
if (!Number.isInteger(first) || first === 0) return 0;
if (!tuning.every((t) => Number(t) === first)) return 0;
return Math.max(-MAX_ST, Math.min(MAX_ST, first));
}
function followSt() {
if (!state.follow) return 0;
const hw = window.highway || window._slopsmithHighway;
let info = null;
try { info = hw && typeof hw.getSongInfo === 'function' ? hw.getSongInfo() : null; } catch (_) { info = null; }
return songTuningSt(info && info.tuning);
}
const shiftSt = () => Math.max(-MAX_ST, Math.min(MAX_ST, state.semitones + followSt()));
const totalCents = () => shiftSt() * 100 + state.cents + matchCents();
const windowMs = () => (MODES.find((m) => m.id === state.mode) || MODES[1]).ms;

function save() {
Expand All @@ -61,7 +84,7 @@
const a = api();
if (!a || typeof a.setLivePitch !== 'function') { supported = false; return; }
Promise.resolve(a.setLivePitch({
semitones: state.semitones,
semitones: shiftSt(),
cents: state.cents + matchCents(),
windowMs: windowMs(),
})).catch(() => {});
Expand Down Expand Up @@ -97,6 +120,16 @@
if (Number(sl.value) !== state.cents) sl.value = String(state.cents);
cl.textContent = (state.cents > 0 ? '+' : '') + state.cents + 'c';
if (md.value !== state.mode) md.value = state.mode;
const fb = document.getElementById('lr-follow');
if (fb) {
const fst = followSt();
fb.textContent = 'Follow song' + (state.follow && fst ? ` ${fst > 0 ? '+' : ''}${fst} st` : '');
fb.setAttribute('aria-pressed', state.follow ? 'true' : 'false');
fb.style.background = state.follow ? 'rgba(14,116,144,.45)' : '';
fb.style.color = state.follow ? '#e0f2fe' : '';
fb.title = 'Shift by the song’s tuning when every string is down (or up) by the same amount '
+ '(Eb, D, C# standard…), so an E-standard guitar plays along without retuning. Drop tunings are left alone.';
}
const mb = document.getElementById('lr-match');
if (mb) {
const cur = window.__songCents && window.__songCents.current;
Expand Down Expand Up @@ -178,11 +211,13 @@
const reset = btn('0', 'Back to standard (no shift)', () => set({ semitones: 0, cents: 0 }));
const match = btn('Match song', '', () => set({ match: !state.match }));
match.id = 'lr-match';
const follow = btn('Follow song', '', () => set({ follow: !state.follow }));
follow.id = 'lr-follow';
const info = document.createElement('span');
info.id = 'lr-info';
info.className = 'tabular-nums text-gray-400';

wrap.append(name, down, st, up, sl, cl, md, reset, match, info);
wrap.append(name, down, st, up, sl, cl, md, reset, follow, match, info);
bar.append(wrap);
render();
}
Expand Down Expand Up @@ -210,7 +245,7 @@
render();
}

window.__liveRetune = { state, set, apply };
window.__liveRetune = { state, set, apply, songTuningSt, followSt, shiftSt };
window.addEventListener('songcents:change', () => { apply(); render(); });

const bus = window.feedBack || window.slopsmith;
Expand Down
85 changes: 85 additions & 0 deletions tests/live-retune-follow-song.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Live Retune "Follow song": an E-standard guitar is shifted by the song's tuning when every
// string is offset by the same whole number of semitones (Eb standard -> -1 st). Runs the real
// plugin script in a vm with a stub engine bridge and highway.

'use strict';

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');

const SRC = fs.readFileSync(path.join(__dirname, '..', 'src', 'renderer', 'live-retune', 'screen.js'), 'utf8');

function run({ saved = {}, tuning = [0, 0, 0, 0, 0, 0] } = {}) {
const calls = [];
const store = { liveRetune: JSON.stringify(saved) };
const song = { tuning };
const window = {
feedBackDesktop: {
audio: {
setLivePitch: (p) => { calls.push(p); return Promise.resolve(); },
getLivePitch: () => Promise.resolve({ supported: true, cents: 0, windowMs: 14, latencyMs: 0 }),
},
},
highway: { getSongInfo: () => song },
addEventListener: () => {},
};
const ctx = {
window,
document: { getElementById: () => null, createElement: () => ({}) },
localStorage: { getItem: (k) => store[k] ?? null, setItem: (k, v) => { store[k] = v; } },
setInterval: () => 0,
Promise,
};
vm.runInNewContext(SRC, ctx);
const lr = window.__liveRetune;
return { lr, calls, song, store, last: () => calls[calls.length - 1] };
}

test('only whole-guitar tunings are followed', () => {
const { lr } = run();
assert.equal(lr.songTuningSt([-1, -1, -1, -1, -1, -1]), -1, 'Eb standard');
assert.equal(lr.songTuningSt([-2, -2, -2, -2, -2, -2]), -2, 'D standard');
assert.equal(lr.songTuningSt([1, 1, 1, 1]), 1, 'up-tuned bass');
assert.equal(lr.songTuningSt([-2, 0, 0, 0, 0, 0]), 0, 'Drop D is left alone');
assert.equal(lr.songTuningSt([0, 0, 0, 0, 0, 0]), 0, 'E standard');
assert.equal(lr.songTuningSt([-7, -7, -7, -7, -7, -7]), -5, 'clamped to the engine range');
assert.equal(lr.songTuningSt(null), 0);
});

test('Follow song shifts an E-standard guitar to an Eb-standard song', () => {
const { calls, last } = run({ saved: { follow: true }, tuning: [-1, -1, -1, -1, -1, -1] });
assert.ok(calls.length > 0, 'applied on load');
assert.equal(last().semitones, -1);
});

test('Follow off (the default) never shifts', () => {
const { last } = run({ saved: {}, tuning: [-1, -1, -1, -1, -1, -1] });
assert.equal(last().semitones, 0);
});

test('manual semitones stack on the followed tuning', () => {
const { lr, last } = run({ saved: { follow: true, semitones: -1 }, tuning: [-1, -1, -1, -1, -1, -1] });
assert.equal(last().semitones, -2);
lr.set({ semitones: 0 });
assert.equal(last().semitones, -1);
});

test('a new song re-reads the tuning', () => {
const { lr, song, last } = run({ saved: { follow: true }, tuning: [-1, -1, -1, -1, -1, -1] });
assert.equal(last().semitones, -1);
song.tuning = [-2, -2, -2, -2, -2, -2];
lr.apply();
assert.equal(last().semitones, -2);
song.tuning = [-2, 0, 0, 0, 0, 0]; // Drop D
lr.apply();
assert.equal(last().semitones, 0);
});

test('the choice is remembered', () => {
const { lr, store } = run({ saved: {} });
lr.set({ follow: true });
assert.equal(JSON.parse(store.liveRetune).follow, true);
});
Loading