adding various improvements to the experience
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,20 @@
|
||||
#include "../BoomCast_Waveshare_Arduino/boomcast_detector.h"
|
||||
#include <cstdio>
|
||||
#include <cstdint>
|
||||
|
||||
int main() {
|
||||
BoomDetector detector(16000, 533, 32000, 16000, 48000,
|
||||
0.01f, 0.02f, 2.0f, 0.075f, 5.0f, 0.16f);
|
||||
int16_t sample;
|
||||
uint64_t frame = 0;
|
||||
while (std::fread(&sample, sizeof(sample), 1, stdin) == 1) {
|
||||
++frame;
|
||||
auto result = detector.process(float(sample) / 32768.0f);
|
||||
if (result == BoomDetector::Result::Trigger) std::printf("T %llu\n", (unsigned long long)frame);
|
||||
if (result == BoomDetector::Result::TailDone) {
|
||||
std::printf("E %llu\n", (unsigned long long)frame);
|
||||
detector.reset();
|
||||
}
|
||||
}
|
||||
std::printf("B %.8f\n", detector.baseline());
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Compare the firmware's detector with the unchanged browser AudioWorklet."""
|
||||
from array import array
|
||||
from pathlib import Path
|
||||
import math
|
||||
import random
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
REFERENCE = ROOT.parent / "django/frontend/src/scripts/burst-listener-worklet.js"
|
||||
CPP = Path(__file__).with_name("detector_trace.cpp")
|
||||
JS = Path(__file__).with_name("worklet_trace.cjs")
|
||||
RATE = 16000
|
||||
|
||||
|
||||
def samples(kind):
|
||||
rng = random.Random(71)
|
||||
length = RATE * 13
|
||||
values = [0] * length
|
||||
if kind in {"kitchen", "impulse", "repeated", "overlap"}:
|
||||
for i in range(length):
|
||||
values[i] = round(rng.uniform(-0.012, 0.012) * 32767)
|
||||
def bang(at, duration=.05, amplitude=.8):
|
||||
start = round(at * RATE)
|
||||
for j in range(round(duration * RATE)):
|
||||
if start + j < length:
|
||||
values[start + j] = round(amplitude * 32767 * math.sin(j * .28))
|
||||
if kind == "kitchen":
|
||||
for at in (2, 4, 7):
|
||||
bang(at, .15, .12) # below the browser's peak gate
|
||||
if kind == "impulse": bang(4)
|
||||
if kind == "repeated":
|
||||
bang(4)
|
||||
bang(4.3)
|
||||
bang(10)
|
||||
if kind == "overlap":
|
||||
bang(4, .2)
|
||||
bang(4.1, .2, .5)
|
||||
return values
|
||||
|
||||
|
||||
class DetectorParity(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.directory = tempfile.TemporaryDirectory()
|
||||
cls.exe = Path(cls.directory.name) / "detector_trace"
|
||||
subprocess.run(["g++", "-std=c++17", "-O2", str(CPP), "-o", str(cls.exe)], check=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.directory.cleanup()
|
||||
|
||||
def test_reference_traces(self):
|
||||
for kind in ("silence", "kitchen", "impulse", "repeated", "overlap"):
|
||||
with self.subTest(kind=kind):
|
||||
path = Path(self.directory.name) / f"{kind}.pcm"
|
||||
raw = array('h', samples(kind))
|
||||
if sys.byteorder != 'little': raw.byteswap()
|
||||
path.write_bytes(raw.tobytes())
|
||||
firmware = subprocess.check_output([str(self.exe)], input=path.read_bytes(), text=False).decode().splitlines()
|
||||
browser = subprocess.check_output(["node", str(JS), str(REFERENCE), str(path)], text=True).splitlines()
|
||||
self.assertEqual(firmware[:-1], browser[:-1])
|
||||
self.assertAlmostEqual(float(firmware[-1][2:]), float(browser[-1][2:]), delta=0.00002)
|
||||
if kind == "silence" or kind == "kitchen": self.assertFalse(firmware[:-1])
|
||||
else: self.assertIn("T ", firmware[0])
|
||||
|
||||
|
||||
if __name__ == "__main__": unittest.main()
|
||||
@@ -0,0 +1,31 @@
|
||||
const fs = require('fs');
|
||||
const vm = require('vm');
|
||||
const source = fs.readFileSync(process.argv[2], 'utf8');
|
||||
let Processor;
|
||||
class AudioWorkletProcessor {
|
||||
constructor() { this.port = { postMessage(message) { messages.push({ type: message.type, frame: processor.frame }); } }; }
|
||||
}
|
||||
let messages = [];
|
||||
vm.runInNewContext(source, {
|
||||
sampleRate: 16000, AudioWorkletProcessor,
|
||||
registerProcessor(name, implementation) { Processor = implementation; },
|
||||
Float32Array, Math,
|
||||
});
|
||||
const processor = new Processor();
|
||||
const bytes = fs.readFileSync(process.argv[3]);
|
||||
for (let offset = 0; offset < bytes.length;) {
|
||||
const length = Math.min(128, (bytes.length - offset) / 2);
|
||||
const channel = new Float32Array(length);
|
||||
for (let i = 0; i < length; i++) channel[i] = bytes.readInt16LE(offset + i * 2) / 32768;
|
||||
messages = [];
|
||||
processor.process([[channel]]);
|
||||
for (const message of messages) {
|
||||
if (message.type === 'trigger') process.stdout.write(`T ${message.frame}\n`);
|
||||
if (message.type === 'clip') {
|
||||
process.stdout.write(`E ${message.frame}\n`);
|
||||
processor.reset();
|
||||
}
|
||||
}
|
||||
offset = processor.frame * 2;
|
||||
}
|
||||
process.stdout.write(`B ${processor.baseline.toFixed(8)}\n`);
|
||||
Reference in New Issue
Block a user