Bookmark this: tool.io/noise-level-comparison/source-code
Noise Level Comparison Source Code
Browse the implementation files for Noise Level Comparison.tool.ts typescript
import NoiseLevelComparison from './NoiseLevelComparison.svelte';
import {
calculateNoiseComparison,
noiseReferences
} from './calculation';
const parseLevel = (value: unknown): number | null => {
if (
(typeof value !== 'number' && typeof value !== 'string') ||
(typeof value === 'string' && value.trim() === '')
) {
return null;
}
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : null;
};
export const compareNoiseLevel = (
levelInput: unknown = 45,
sourceInput: unknown = 'Laptop fan',
comparisonInput: unknown = 'jet-takeoff'
): string => {
const level = parseLevel(levelInput);
if (
level === null ||
typeof sourceInput !== 'string' ||
typeof comparisonInput !== 'string'
) {
return '';
}
const result = calculateNoiseComparison(
level,
sourceInput,
comparisonInput
);
return result ? JSON.stringify(result) : '';
};
export const noiseLevelPreview = (
levelInput: unknown = 45,
comparisonInput: unknown = 'jet-takeoff'
): string => {
const serialized = compareNoiseLevel(
levelInput,
'Selected sound',
comparisonInput
);
if (!serialized) return '';
const result = JSON.parse(serialized) as ReturnType<
typeof calculateNoiseComparison
>;
if (!result) return '';
const direction =
result.differenceDb === 0
? 'about as intense as'
: result.differenceDb > 0
? 'more intense than'
: 'less intense than';
return `${
result.levelDb
} dBA\n\n${result.intensityRatio.toLocaleString('en-US', {
maximumFractionDigits: 1
})}× ${direction}\n${result.comparison.label} (${
result.comparison.db
} dBA)`;
};
export const Tool: TypeTool = {
name: 'Noise Level Comparison',
slug: 'noise-level-comparison',
intro:
'Compare a sound level with laptops, conversations, traffic, concerts, aircraft, and other familiar sources.',
tool: compareNoiseLevel,
i: [
{
example: '45',
label: 'Sound level',
placeholder: 'A-weighted decibels (dBA)',
type: 'number',
value: '45'
},
{
example: 'Laptop fan',
label: 'Sound source',
placeholder: 'Laptop fan',
type: 'text',
value: 'Laptop fan'
},
{
label: 'Compare with',
options: noiseReferences.map((reference) => ({
selected: reference.id === 'jet-takeoff',
text: `${reference.label} (${reference.db} dBA)`,
value: reference.id
})),
placeholder: 'Select a reference sound',
type: 'select',
value: 'jet-takeoff'
}
],
o: [
{
label: 'Noise comparison',
placeholder: 'Noise comparison',
render: 'custom',
renderas: NoiseLevelComparison,
type: 'text'
}
],
preview: {
i: [
{
label: 'Sound level',
placeholder: 'dBA',
type: 'number',
value: '45'
},
{
label: 'Compare with',
options: [
{
selected: true,
text: 'Jet at takeoff',
value: 'jet-takeoff'
},
{
text: 'Conversation',
value: 'conversation'
}
],
placeholder: 'Reference sound',
type: 'select',
value: 'jet-takeoff'
}
],
o: [
{
label: 'Comparison',
placeholder: 'Noise comparison',
render: 'textarea',
type: 'text'
}
],
tool: noiseLevelPreview
},
alternative: [
{ title: 'Weather Tool', url: '/weather' },
{ title: 'Compare Tool', url: '/compare' }
],
cite: [
{
title: 'NIDCD: How Loud is Too Loud?',
url: 'https://www.nidcd.nih.gov/health/how-loud-too-loud'
},
{
title: 'CDC/NIOSH: Understand Noise Exposure',
url: 'https://www.cdc.gov/niosh/noise/prevent/understand.html'
},
{
title:
'NIDCD: Hearing Protectors and Familiar Sound Levels',
url: 'https://www.nidcd.nih.gov/health/hearing-protectors'
}
]
};
export default Tool;
Description
Browse the implementation files for Noise Level Comparison.