LibWeb/WebAudio: Define and partially implement AnalyserNode

https://webaudio.github.io/web-audio-api/#AnalyserNode

Most of the interface is naively implemented. Container types
probably need adjusted (Vector<double> is used for all the processing).
A Fourier Transform is needed, but that's waiting on either a 3rd
party library or a complex number type.

There are lots of simple miscellaneous filters that need to be applied.
It could be reasonable to implement from scratch, supposing that
it can be parallelized. It might be hard to find one library with
everything. Not my call though.

Some additional scaffolding around blocks and render quanta is
probably needed before this is developed much further, which
probably comes in at the level of the AudioNode.

Co-authored-by: Tim Ledbetter <tim.ledbetter@ladybird.org>
This commit is contained in:
Noah Bright 2024-09-18 09:09:18 -04:00 committed by Andreas Kling
commit 6c6bf322ea
Notes: github-actions[bot] 2025-01-17 09:15:57 +00:00
16 changed files with 911 additions and 5 deletions

View file

@ -0,0 +1,183 @@
<!DOCTYPE html>
<html>
<head>
<title>
Test Constructor: AnalyserNode
</title>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../../../webaudio/resources/audit-util.js"></script>
<script src="../../../webaudio/resources/audit.js"></script>
<script src="../../../webaudio/resources/audionodeoptions.js"></script>
</head>
<body>
<script id="layout-test-code">
let context;
let audit = Audit.createTaskRunner();
audit.define('initialize', (task, should) => {
context = initializeContext(should);
task.done();
});
audit.define('invalid constructor', (task, should) => {
testInvalidConstructor(should, 'AnalyserNode', context);
task.done();
});
audit.define('default constructor', (task, should) => {
let prefix = 'node0';
let node = testDefaultConstructor(should, 'AnalyserNode', context, {
prefix: prefix,
numberOfInputs: 1,
numberOfOutputs: 1,
channelCount: 2,
channelCountMode: 'max',
channelInterpretation: 'speakers'
});
testDefaultAttributes(should, node, prefix, [
{name: 'fftSize', value: 2048},
{name: 'frequencyBinCount', value: 1024},
{name: 'minDecibels', value: -100}, {name: 'maxDecibels', value: -30},
{name: 'smoothingTimeConstant', value: 0.8}
]);
task.done();
});
audit.define('test AudioNodeOptions', (task, should) => {
testAudioNodeOptions(should, context, 'AnalyserNode');
task.done();
});
audit.define('constructor with options', (task, should) => {
let options = {
fftSize: 32,
maxDecibels: 1,
minDecibels: -13,
// Choose a value that can be represented the same as a float and as a
// double.
smoothingTimeConstant: 0.125
};
let node;
should(
() => {
node = new AnalyserNode(context, options);
},
'node1 = new AnalyserNode(c, ' + JSON.stringify(options) + ')')
.notThrow();
should(node instanceof AnalyserNode, 'node1 instanceof AnalyserNode')
.beEqualTo(true);
should(node.fftSize, 'node1.fftSize').beEqualTo(options.fftSize);
should(node.maxDecibels, 'node1.maxDecibels')
.beEqualTo(options.maxDecibels);
should(node.minDecibels, 'node1.minDecibels')
.beEqualTo(options.minDecibels);
should(node.smoothingTimeConstant, 'node1.smoothingTimeConstant')
.beEqualTo(options.smoothingTimeConstant);
task.done();
});
audit.define('construct invalid options', (task, should) => {
let node;
should(
() => {
node = new AnalyserNode(context, {fftSize: 33});
},
'node = new AnalyserNode(c, { fftSize: 33 })')
.throw(DOMException, 'IndexSizeError');
should(
() => {
node = new AnalyserNode(context, {maxDecibels: -500});
},
'node = new AnalyserNode(c, { maxDecibels: -500 })')
.throw(DOMException, 'IndexSizeError');
should(
() => {
node = new AnalyserNode(context, {minDecibels: -10});
},
'node = new AnalyserNode(c, { minDecibels: -10 })')
.throw(DOMException, 'IndexSizeError');
should(
() => {
node = new AnalyserNode(context, {smoothingTimeConstant: 2});
},
'node = new AnalyserNode(c, { smoothingTimeConstant: 2 })')
.throw(DOMException, 'IndexSizeError');
should(function() {
node = new AnalyserNode(context, {frequencyBinCount: 33});
}, 'node = new AnalyserNode(c, { frequencyBinCount: 33 })').notThrow();
should(node.frequencyBinCount, 'node.frequencyBinCount')
.beEqualTo(1024);
task.done();
});
audit.define('setting min/max', (task, should) => {
let node;
// Recall the default values of minDecibels and maxDecibels are -100,
// and -30, respectively. Setting both values in the constructor should
// not signal an error in any of the following cases.
let options = {minDecibels: -10, maxDecibels: 20};
should(
() => {
node = new AnalyserNode(context, options);
},
'node = new AnalyserNode(c, ' + JSON.stringify(options) + ')')
.notThrow();
options = {maxDecibels: 20, minDecibels: -10};
should(
() => {
node = new AnalyserNode(context, options);
},
'node = new AnalyserNode(c, ' + JSON.stringify(options) + ')')
.notThrow();
options = {minDecibels: -200, maxDecibels: -150};
should(
() => {
node = new AnalyserNode(context, options);
},
'node = new AnalyserNode(c, ' + JSON.stringify(options) + ')')
.notThrow();
options = {maxDecibels: -150, minDecibels: -200};
should(
() => {
node = new AnalyserNode(context, options);
},
'node = new AnalyserNode(c, ' + JSON.stringify(options) + ')')
.notThrow();
// But these should signal because minDecibel > maxDecibel
options = {maxDecibels: -150, minDecibels: -10};
should(
() => {
node = new AnalyserNode(context, options);
},
'node = new AnalyserNode(c, ' + JSON.stringify(options) + ')')
.throw(DOMException, 'IndexSizeError');
options = {minDecibels: -10, maxDecibels: -150};
should(
() => {
node = new AnalyserNode(context, options);
},
'node = new AnalyserNode(c, ' + JSON.stringify(options) + ')')
.throw(DOMException, 'IndexSizeError');
task.done();
});
audit.run();
</script>
</body>
</html>

View file

@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<title>
realtimeanalyser-basic.html
</title>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../../../webaudio/resources/audit-util.js"></script>
<script src="../../../webaudio/resources/audit.js"></script>
</head>
<body>
<script id="layout-test-code">
let context = 0;
let audit = Audit.createTaskRunner();
audit.define('Basic AnalyserNode test', function(task, should) {
context = new AudioContext();
let analyser = context.createAnalyser();
should(analyser.numberOfInputs, 'Number of inputs for AnalyserNode')
.beEqualTo(1);
should(analyser.numberOfOutputs, 'Number of outputs for AnalyserNode')
.beEqualTo(1);
should(analyser.minDecibels, 'Default minDecibels value')
.beEqualTo(-100);
should(analyser.maxDecibels, 'Default maxDecibels value')
.beEqualTo(-30);
should(
analyser.smoothingTimeConstant,
'Default smoothingTimeConstant value')
.beEqualTo(0.8);
let expectedValue = -50 - (1 / 3);
analyser.minDecibels = expectedValue;
should(analyser.minDecibels, 'node.minDecibels = ' + expectedValue)
.beEqualTo(expectedValue);
expectedValue = -40 - (1 / 3);
analyser.maxDecibels = expectedValue;
should(analyser.maxDecibels, 'node.maxDecibels = ' + expectedValue)
.beEqualTo(expectedValue);
task.done();
});
audit.run();
</script>
</body>
</html>

View file

@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head>
<title>
realtimeanalyser-fft-sizing.html
</title>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../../../webaudio/resources/audit-util.js"></script>
<script src="../../../webaudio/resources/audit.js"></script>
</head>
<body>
<script id="layout-test-code">
let audit = Audit.createTaskRunner();
function doTest(fftSize, illegal, should) {
let c = new OfflineAudioContext(1, 1000, 44100);
let a = c.createAnalyser();
let message = 'Setting fftSize to ' + fftSize;
let tester = function() {
a.fftSize = fftSize;
};
if (illegal) {
should(tester, message).throw(DOMException, 'IndexSizeError');
} else {
should(tester, message).notThrow();
}
}
audit.define(
{
label: 'FFT size test',
description: 'Test that re-sizing the FFT arrays does not fail.'
},
function(task, should) {
doTest(-1, true, should);
doTest(0, true, should);
doTest(1, true, should);
for (let i = 2; i <= 0x20000; i *= 2) {
if (i >= 32 && i <= 32768)
doTest(i, false, should);
else
doTest(i, true, should);
doTest(i + 1, true, should);
}
task.done();
});
audit.run();
</script>
</body>
</html>