diff --git a/public/audio-worklets/pink-noise-processor.js b/public/audio-worklets/pink-noise-processor.js new file mode 100644 index 00000000..4aad95ee --- /dev/null +++ b/public/audio-worklets/pink-noise-processor.js @@ -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); diff --git a/public/audio-worklets/sweep-processor.js b/public/audio-worklets/sweep-processor.js new file mode 100644 index 00000000..66f3c525 --- /dev/null +++ b/public/audio-worklets/sweep-processor.js @@ -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); diff --git a/public/audio-worklets/tone-processor.js b/public/audio-worklets/tone-processor.js new file mode 100644 index 00000000..3038bc4d --- /dev/null +++ b/public/audio-worklets/tone-processor.js @@ -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); diff --git a/public/audio-worklets/white-noise-processor.js b/public/audio-worklets/white-noise-processor.js new file mode 100644 index 00000000..291f7c6e --- /dev/null +++ b/public/audio-worklets/white-noise-processor.js @@ -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); diff --git a/src/tools/sound-tester/index.ts b/src/tools/sound-tester/index.ts new file mode 100644 index 00000000..1706be84 --- /dev/null +++ b/src/tools/sound-tester/index.ts @@ -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', +}); diff --git a/src/tools/sound-tester/sound-tester.vue b/src/tools/sound-tester/sound-tester.vue new file mode 100644 index 00000000..27575ebe --- /dev/null +++ b/src/tools/sound-tester/sound-tester.vue @@ -0,0 +1,54 @@ + + + diff --git a/src/tools/sound-tester/useAudioTester.ts b/src/tools/sound-tester/useAudioTester.ts new file mode 100644 index 00000000..43ea4b45 --- /dev/null +++ b/src/tools/sound-tester/useAudioTester.ts @@ -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, + }; +}