feat(new tool): Sound Tester

Fix #589
This commit is contained in:
ShareVB committed 2026-10-30 22:12:06 +01:00
1 parent ec33d7df26
commit f122bafaa9
7 files changed
+313

No files matched your search

@@ -0,0 +1,41 @@
class PinkNoiseProcessor extends AudioWorkletProcessor {
b0 = 0;
b1 = 0;
b2 = 0;
b3 = 0;
b4 = 0;
b5 = 0;
b6 = 0;
process(inputs, outputs) {
const output = outputs[0];
for (const channel of output) {
for (let i = 0; i < channel.length; i++) {
const white = Math.random() * 2 - 1;
this.b0 = 0.99886 * this.b0 + white * 0.0555179;
this.b1 = 0.99332 * this.b1 + white * 0.0750759;
this.b2 = 0.969 * this.b2 + white * 0.153852;
this.b3 = 0.8665 * this.b3 + white * 0.3104856;
this.b4 = 0.55 * this.b4 + white * 0.5329522;
this.b5 = -0.7616 * this.b5 - white * 0.016898;
let pink = this.b0 + this.b1 + this.b2 + this.b3 + this.b4 + this.b5 + this.b6 + white * 0.5362;
this.b6 = white * 0.115926;
channel[i] = pink * 0.11;
}
}
return true;
}
}
registerProcessor('pink-noise-processor', PinkNoiseProcessor);
+36
View File
@@ -0,0 +1,36 @@
class SweepProcessor extends AudioWorkletProcessor {
phase = 0;
position = 0;
startFreq = 20;
endFreq = 20000;
duration = 10;
process(inputs, outputs) {
const output = outputs[0];
const totalSamples = sampleRate * this.duration;
for (const channel of output) {
for (let i = 0; i < channel.length; i++) {
const progress = this.position / totalSamples;
const freq = this.startFreq * Math.pow(this.endFreq / this.startFreq, progress);
channel[i] = Math.sin(this.phase) * 0.2;
this.phase += (2 * Math.PI * freq) / sampleRate;
this.position++;
}
}
if (this.position >= totalSamples) {
return false;
}
return true;
}
}
registerProcessor('sweep-processor', SweepProcessor);
+49
View File
@@ -0,0 +1,49 @@
class ToneProcessor extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{
name: 'frequency',
defaultValue: 440,
minValue: 20,
maxValue: 20000,
},
{
name: 'gain',
defaultValue: 0.2,
minValue: 0,
maxValue: 1,
},
];
}
phase = 0;
process(inputs, outputs, parameters) {
const output = outputs[0];
const frequencies = parameters.frequency;
const gains = parameters.gain;
for (let channel = 0; channel < output.length; channel++) {
const data = output[channel];
for (let i = 0; i < data.length; i++) {
const frequency = frequencies.length > 1 ? frequencies[i] : frequencies[0];
const gain = gains.length > 1 ? gains[i] : gains[0];
data[i] = Math.sin(this.phase) * gain;
this.phase += (2 * Math.PI * frequency) / sampleRate;
if (this.phase > Math.PI * 2) {
this.phase -= Math.PI * 2;
}
}
}
return true;
}
}
registerProcessor('tone-processor', ToneProcessor);
@@ -0,0 +1,15 @@
class WhiteNoiseProcessor extends AudioWorkletProcessor {
process(inputs, outputs) {
const output = outputs[0];
for (const channel of output) {
for (let i = 0; i < channel.length; i++) {
channel[i] = Math.random() * 2 - 1;
}
}
return true;
}
}
registerProcessor('white-noise-processor', WhiteNoiseProcessor);
+12
View File
@@ -0,0 +1,12 @@
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Sound Tester',
path: '/sound-tester',
description: 'Test your speakers and headphones by playing test sounds. Adjust volume and check for audio clarity.',
keywords: ['sound', 'speaker', 'tester'],
component: () => import('./sound-tester.vue'),
icon: defineAsyncComponent(() => import('@vicons/tabler/es/Speakerphone')),
createdAt: new Date('2026-10-30'),
category: 'Forensic',
});
+54
View File
@@ -0,0 +1,54 @@
<script setup lang="ts">
import { useAudioTester } from './useAudioTester';
const { playTone, playSweep, playWhiteNoise, playPinkNoise, stop } = useAudioTester();
const frequency = ref(440);
const bassTests = [20, 40, 60];
const trebleTests = [8000, 12000, 16000];
</script>
<template>
<div>
<n-card title="General" mb-2>
<n-button @click="playTone(440)" mr-1> 440Hz / La </n-button>
<n-button @click="playSweep" mr-1> Sweep </n-button>
<n-button @click="playWhiteNoise" mr-1> White Noise </n-button>
<n-button @click="playPinkNoise" mr-1> Pink Noise </n-button>
<n-button type="error" @click="stop" ml-2> Stop </n-button>
</n-card>
<n-card title="Channel Tests" mb-2>
<n-button @click="playTone(440, -1)" mr-1> Left </n-button>
<n-button @click="playTone(440, 1)" mr-1> Right </n-button>
<n-button type="error" @click="stop" ml-2> Stop </n-button>
</n-card>
<n-card title="Bass Tests" mb-2>
<n-button v-for="freq in bassTests" :key="freq" @click="playTone(freq)" mr-1> {{ freq }}Hz </n-button>
<n-button type="error" @click="stop" ml-2> Stop </n-button>
</n-card>
<n-card title="Treble Tests" mb-2>
<n-button v-for="freq in trebleTests" :key="freq" @click="playTone(freq)" mr-1> {{ freq / 1000 }}kHz </n-button>
<n-button type="error" @click="stop" ml-2> Stop </n-button>
</n-card>
<n-card title="Custom Frequency" mb-2>
<n-form-item label="Frequency (Hz):" label-placement="left">
<n-slider v-model:value="frequency" :min="20" :max="20000" :step="1" mr-1 />
<n-input-number v-model:value="frequency" :min="20" :max="20000" />
</n-form-item>
<n-space justify="center">
<n-button type="primary" @click="playTone(frequency)"> Play this frequency </n-button>
<n-button type="error" @click="stop" ml-2> Stop </n-button>
</n-space>
</n-card>
</div>
</template>
+106
View File
@@ -0,0 +1,106 @@
import { onBeforeUnmount } from 'vue';
import { appBaseUrl as base } from '@/utils/base-url';
export function useAudioTester() {
let context: AudioContext | null = null;
let node: AudioNode | null = null;
let oscillator: AudioWorkletNode | null = null;
async function getContext() {
if (context) {
return context;
}
context = new AudioContext();
await context.audioWorklet.addModule(`${base}audio-worklets/tone-processor.js`);
await context.audioWorklet.addModule(`${base}audio-worklets/sweep-processor.js`);
await context.audioWorklet.addModule(`${base}audio-worklets/white-noise-processor.js`);
await context.audioWorklet.addModule(`${base}audio-worklets/pink-noise-processor.js`);
return context;
}
function stop() {
node?.disconnect();
oscillator?.disconnect();
node = null;
oscillator = null;
}
async function playTone(frequency: number, pan = 0) {
stop();
const ctx = await getContext();
const tone = new AudioWorkletNode(ctx, 'tone-processor');
tone.parameters.get('frequency')?.setValueAtTime(frequency, ctx.currentTime);
const panner = new StereoPannerNode(ctx, {
pan,
});
tone.connect(panner);
panner.connect(ctx.destination);
oscillator = tone;
node = panner;
}
async function playSweep() {
stop();
const ctx = await getContext();
const worklet = new AudioWorkletNode(ctx, 'sweep-processor');
worklet.connect(ctx.destination);
oscillator = worklet;
}
async function playWhiteNoise() {
stop();
const ctx = await getContext();
const worklet = new AudioWorkletNode(ctx, 'white-noise-processor');
worklet.connect(ctx.destination);
oscillator = worklet;
}
async function playPinkNoise() {
stop();
const ctx = await getContext();
const worklet = new AudioWorkletNode(ctx, 'pink-noise-processor');
worklet.connect(ctx.destination);
oscillator = worklet;
}
async function destroy() {
stop();
if (context) {
await context.close();
context = null;
}
}
onBeforeUnmount(destroy);
return {
playTone,
playSweep,
playWhiteNoise,
playPinkNoise,
stop,
};
}