Preserve notification preview opening notes through audio startup
This commit is contained in:
1 parent
53f1ba9329
commit
65df2029df
5 files changed
+90
-15
No files matched your search
@@ -62,6 +62,15 @@ assert.equal(waveView.getInt16(wav.length - 2, true), 0);
|
||||
assert.equal(builder.melodyWav("off").length, 44);
|
||||
assert.equal(builder.melodyWav("inherit").length, 44);
|
||||
assert.throws(() => builder.melodyWav("invalid"));
|
||||
// The browser warmup must preserve every sample, including the opening notes.
|
||||
const warmedWav = builder.melodyWav("test:d=4,o=5,b=120:a,p,a6", 1000);
|
||||
const leadBytes = rate * 2;
|
||||
assert.equal(warmedWav.length, wav.length + leadBytes);
|
||||
assert.ok(warmedWav.slice(44, 44 + leadBytes).every(byte => byte === 0));
|
||||
assert.deepEqual(warmedWav.slice(44 + leadBytes), wav.slice(44));
|
||||
assert.equal(new DataView(warmedWav.buffer).getUint32(40, true), warmedWav.length - 44);
|
||||
assert.equal(builder.melodyWav("off", 1000).length, 44);
|
||||
for (const lead of [-1, 2001, 0.5, NaN]) assert.throws(() => builder.melodyWav("off", lead));
|
||||
console.log("Sound preview: audible PCM, note pitch, silence, duration, and recipient-free examples passed");
|
||||
const encoded = builder.encodeCommand("notify.stop", "A7");
|
||||
assert.equal(encoded[0], 60);assert.equal(encoded[3], 0x42);
|
||||
|
||||
@@ -18,19 +18,33 @@ except ImportError:
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
TAP_AUDIO = """() => {
|
||||
const audio = document.querySelector('[data-role="preview-audio"]');
|
||||
window.previewAudioStats = { plays: 0, rms: 0 };
|
||||
window.previewAudioStats = { plays: 0, rms: 0, tones: [] };
|
||||
// Attach before playback: late MediaElementSource wiring is silent in WebKit.
|
||||
const context = new AudioContext(), analyser = context.createAnalyser();
|
||||
context.createMediaElementSource(audio).connect(analyser);
|
||||
const context = new AudioContext(), analyser = context.createAnalyser(), gate = context.createGain();
|
||||
context.createMediaElementSource(audio).connect(gate); gate.connect(analyser);
|
||||
analyser.connect(context.destination);
|
||||
const samples = new Float32Array(analyser.fftSize);
|
||||
let collectingAfter = Infinity;
|
||||
setInterval(() => {
|
||||
// Analyser windows can retain the previous play's final samples.
|
||||
if (context.currentTime < collectingAfter) return;
|
||||
analyser.getFloatTimeDomainData(samples);
|
||||
const rms = Math.sqrt(samples.reduce((sum, v) => sum + v * v, 0) / samples.length);
|
||||
previewAudioStats.rms = Math.max(previewAudioStats.rms, rms);
|
||||
if (rms > 0.05) {
|
||||
let crossings = 0;
|
||||
for (let i = 1; i < samples.length; i++) if (samples[i] > 0 && samples[i - 1] <= 0) crossings++;
|
||||
previewAudioStats.tones.push({hz: crossings * context.sampleRate / samples.length, ms: audio.currentTime * 1000});
|
||||
}
|
||||
}, 10);
|
||||
audio.addEventListener('playing', () => {
|
||||
previewAudioStats.plays++;
|
||||
previewAudioStats.rms = 0; previewAudioStats.tones = [];
|
||||
const now = context.currentTime;
|
||||
collectingAfter = now + Math.max((window.previewMuteStartupMs || 0) / 1000, samples.length / context.sampleRate);
|
||||
gate.gain.cancelScheduledValues(now);
|
||||
gate.gain.setValueAtTime(window.previewMuteStartupMs ? 0 : 1, now);
|
||||
gate.gain.setValueAtTime(1, now + (window.previewMuteStartupMs || 0) / 1000);
|
||||
context.resume();
|
||||
});
|
||||
}"""
|
||||
@@ -87,7 +101,7 @@ class NotificationAudioTest(unittest.TestCase):
|
||||
self.assertEqual(self.page.locator('[data-role="error"]').inner_text(), "")
|
||||
self.page.wait_for_function("document.querySelector('[data-role=preview-sound]').textContent === 'Cycle finished'")
|
||||
self.assertEqual(self.page.evaluate("previewAudioStats.plays"), index)
|
||||
self.assertTrue(self.page.evaluate("document.querySelector('audio').paused && !document.querySelector('audio').hasAttribute('src')"))
|
||||
self.assertTrue(self.page.evaluate("document.querySelector('audio').paused && document.querySelector('audio').ended"))
|
||||
|
||||
def test_timer_waits_for_audio_and_stop_cancels_pending_start(self):
|
||||
self.page.evaluate("""() => {
|
||||
@@ -128,6 +142,38 @@ class NotificationAudioTest(unittest.TestCase):
|
||||
self.assertEqual(self.page.locator('[data-role="preview-sound"]').inner_text(), "Stopped")
|
||||
self.assertTrue(self.page.locator('[data-action="preview"]').is_enabled())
|
||||
|
||||
def test_cold_output_preserves_all_opening_notes_on_repeated_starts(self):
|
||||
# A successful play() does not tell us when a cold output becomes audible.
|
||||
# Drop its first 750 ms, enough to lose several unprotected opening notes.
|
||||
self.page.evaluate("window.previewMuteStartupMs = 750")
|
||||
for attempt in range(3):
|
||||
self.page.evaluate("previewAudioStats.rms = 0; previewAudioStats.tones = []")
|
||||
self.click("preview")
|
||||
self.page.wait_for_function("previewAudioStats.plays === " + str(attempt + 1))
|
||||
self.page.wait_for_function("document.querySelector('audio').currentTime >= 0.3")
|
||||
self.assertEqual(self.page.locator('[data-role="preview-time"]').inner_text(), "0 / 910 ms")
|
||||
self.assertEqual(self.page.locator('[data-role="preview-sound"]').inner_text(), "Warming up audio...")
|
||||
self.assertEqual(self.page.evaluate("previewAudioStats.rms"), 0)
|
||||
for indicator in ("vibration", "led", "gpio", "screen"):
|
||||
self.assertEqual(self.page.locator('[data-indicator="' + indicator + '"]').get_attribute("data-on"), "false")
|
||||
self.audible()
|
||||
self.page.wait_for_function("document.querySelector('[data-role=preview-sound]').textContent === 'Cycle finished'")
|
||||
tones = self.page.evaluate("previewAudioStats.tones")
|
||||
# Check every pitch, especially the initial C/E/G and final C6,
|
||||
# rather than treating any later sound as proof of a complete tune.
|
||||
for hz in (523.25, 659.25, 783.99, 1046.50):
|
||||
self.assertTrue(any(abs(tone["hz"] - hz) < 25 for tone in tones), (attempt, hz, tones))
|
||||
self.assertEqual(self.page.evaluate("previewAudioStats.plays"), attempt + 1)
|
||||
|
||||
def test_stop_during_warmup_never_plays_the_first_note(self):
|
||||
self.click("preview")
|
||||
self.page.wait_for_function("document.querySelector('audio').currentTime >= 0.2")
|
||||
self.click("preview-stop")
|
||||
self.page.wait_for_timeout(1100)
|
||||
self.assertEqual(self.page.evaluate("previewAudioStats.rms"), 0)
|
||||
self.assertEqual(self.page.locator('[data-role="preview-sound"]').inner_text(), "Stopped")
|
||||
self.assertEqual(self.page.locator('[data-role="error"]').inner_text(), "")
|
||||
|
||||
def test_off_inherit_stop_and_playback_errors(self):
|
||||
for value in ("off", "inherit"):
|
||||
self.page.locator('input[name="sound"]').fill(value)
|
||||
|
||||
Reference in new issue
Block a user