Here's a standalone HTML file that creates an interactive audiogram viewer with the features you requested:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Audiogram Viewer with SII Calculator</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 1200px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
color: #333;
}
.chart-container {
position: relative;
height: 500px;
margin: 20px 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin: 20px 0;
justify-content: center;
}
.control-group {
background-color: #f9f9f9;
padding: 15px;
border-radius: 8px;
border: 1px solid #ddd;
}
.slider-container {
margin: 10px 0;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.frequency-labels {
display: flex;
justify-content: space-between;
margin: 0 60px 10px 40px;
font-size: 12px;
color: #666;
}
.info-panel {
margin-top: 20px;
padding: 15px;
background-color: #f0f8ff;
border-radius: 8px;
border: 1px solid #b0d0ff;
}
.speech-sound-legend {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin-top: 10px;
gap: 10px;
}
.legend-item {
display: flex;
align-items: center;
font-size: 12px;
}
.legend-color {
width: 12px;
height: 12px;
margin-right: 5px;
border-radius: 2px;
}
</style>
</head>
<body>
<div class="container">
<h1>Audiogram Viewer with SII Calculator</h1>
<div class="frequency-labels" id="frequencyLabels">
<!-- Will be populated with frequency labels -->
</div>
<div class="chart-container">
<canvas id="audiogramChart"></canvas>
</div>
<div class="controls">
<div class="control-group">
<h3>Hearing Thresholds</h3>
<p>Drag points on the chart to adjust thresholds</p>
<div>
<label>Right Ear (Red)</label>
<div id="rightEarControls"></div>
</div>
<div>
<label>Left Ear (Blue)</label>
<div id="leftEarControls"></div>
</div>
</div>
<div class="control-group">
<h3>Noise & SII</h3>
<div class="slider-container">
<label for="bgNoiseSlider">Background Noise (dB HL): <span id="bgNoiseValue">40</span></label>
<input type="range" id="bgNoiseSlider" min="0" max="80" value="40" step="5">
</div>
<div>
<label>SII Score: <span id="siiScore">0.00</span></label>
<div style="width: 200px; height: 20px; background: linear-gradient(to right, red, yellow, green); margin: 5px 0;">
<div id="siiBar" style="height: 100%; width: 0%; background: black;"></div>
</div>
</div>
</div>
</div>
<div class="info-panel">
<h3>Speech Sounds</h3>
<p>This audiogram shows speech sounds with transparency indicating audibility.
Sounds become more transparent when they're harder to hear due to hearing loss or background noise.</p>
<div class="speech-sound-legend">
<div class="legend-item">
<div class="legend-color" style="background-color: rgba(255, 0, 0, 1.0);"></div>
<span>Clearly audible</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: rgba(255, 0, 0, 0.5);"></div>
<span>Partially audible</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: rgba(255, 0, 0, 0.2);"></div>
<span>Hard to hear</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: rgba(255, 0, 0, 0.05);"></div>
<span>Inaudible</span>
</div>
</div>
</div>
</div>
<script>
// Audiogram data and configuration
const frequencies = [250, 500, 1000, 2000, 4000, 8000];
const frequencyLabels = ['250', '500', '1K', '2K', '4K', '8K'];
const intensityLevels = Array.from({length: 12}, (_, i) => i * 10); // 0 to 110 dB
// Speech sounds data: frequency (Hz), intensity (dB), phoneme, vowel/consonant
const speechSounds = [
// Vowels (generally lower frequency, higher intensity)
{ freq: 270, int: 60, phoneme: 'i', type: 'vowel', f1: 270, f2: 2300 }, // /i/ as in "see"
{ freq: 530, int: 60, phoneme: 'ɛ', type: 'vowel', f1: 530, f2: 1850 }, // /ɛ/ as in "set"
{ freq: 500, int: 60, phoneme: 'æ', type: 'vowel', f1: 700, f2: 1200 }, // /æ/ as in "cat"
{ freq: 500, int: 60, phoneme: 'ɑ', type: 'vowel', f1: 700, f2: 950 }, // /ɑ/ as in "father"
{ freq: 350, int: 60, phoneme: 'ʌ', type: 'vowel', f1: 500, f2: 1200 }, // /ʌ/ as in "cut"
{ freq: 400, int: 60, phoneme: 'ʊ', type: 'vowel', f1: 400, f2: 1050 }, // /ʊ/ as in "put"
{ freq: 300, int: 60, phoneme: 'u', type: 'vowel', f1: 300, f2: 900 }, // /u/ as in "food"
{ freq: 500, int: 60, phoneme: 'ə', type: 'vowel', f1: 500, f2: 1600 }, // /ə/ as in "about"
// Consonants
{ freq: 3000, int: 30, phoneme: 's', type: 'consonant' }, // /s/ as in "see"
{ freq: 4000, int: 35, phoneme: 'ʃ', type: 'consonant' }, // /ʃ/ as in "she"
{ freq: 6000, int: 40, phoneme: 't', type: 'consonant' }, // /t/ as in "tea"
{ freq: 5000, int: 35, phoneme: 'k', type: 'consonant' }, // /k/ as in "key"
{ freq: 1200, int: 30, phoneme: 'm', type: 'consonant' }, // /m/ as in "me"
{ freq: 800, int: 35, phoneme: 'n', type: 'consonant' }, // /n/ as in "knee"
{ freq: 1500, int: 25, phoneme: 'l', type: 'consonant' }, // /l/ as in "lee"
{ freq: 2000, int: 35, phoneme: 'r', type: 'consonant' }, // /r/ as in "red"
{ freq: 700, int: 40, phoneme: 'b', type: 'consonant' }, // /b/ as in "be"
{ freq: 700, int: 40, phoneme: 'd', type: 'consonant' }, // /d/ as in "do"
{ freq: 300, int: 45, phoneme: 'g', type: 'consonant' }, // /g/ as in "go"
{ freq: 250, int: 50, phoneme: 'w', type: 'consonant' }, // /w/ as in "we"
{ freq: 350, int: 45, phoneme: 'j', type: 'consonant' }, // /j/ as in "yes"
{ freq: 1800, int: 25, phoneme: 'v', type: 'consonant' }, // /v/ as in "view"
{ freq: 1700, int: 25, phoneme: 'ð', type: 'consonant' }, // /ð/ as in "this"
{ freq: 1600, int: 25, phoneme: 'z', type: 'consonant' }, // /z/ as in "zoo"
{ freq: 4000, int: 45, phoneme: 'f', type: 'consonant' }, // /f/ as in "fun"
{ freq: 4200, int: 50, phoneme: 'θ', type: 'consonant' }, // /θ/ as in "thin"
];
// Initialize hearing thresholds (normal hearing)
let rightEar = [20, 20, 20, 20, 20, 20]; // dB HL at each frequency
let leftEar = [20, 20, 20, 20, 20, 20]; // dB HL at each frequency
// Chart configuration
let chart;
let isDragging = false;
let draggedPoint = null;
let bgNoiseLevel = 40; // dB HL
let siiScore = 0;
// Initialize the application
function init() {
createFrequencyLabels();
createControlInputs();
createChart();
updateSII();
// Set up event listeners
document.getElementById('bgNoiseSlider').addEventListener('input', function() {
bgNoiseLevel = parseInt(this.value);
document.getElementById('bgNoiseValue').textContent = bgNoiseLevel;
updateSII();
updateChart();
});
}
// Create frequency labels below the chart
function createFrequencyLabels() {
const container = document.getElementById('frequencyLabels');
frequencies.forEach(freq => {
const label = document.createElement('div');
label.textContent = `${freq} Hz`;
container.appendChild(label);
});
}
// Create control inputs for each ear and frequency
function createControlInputs() {
const rightContainer = document.getElementById('rightEarControls');
const leftContainer = document.getElementById('leftEarControls');
frequencies.forEach((freq, idx) => {
// Right ear controls
const rightLabel = document.createElement('label');
rightLabel.textContent = `${freq} Hz:`;
rightLabel.htmlFor = `right_${idx}`;
rightContainer.appendChild(rightLabel);
const rightInput = document.createElement('input');
rightInput.type = 'range';
rightInput.id = `right_${idx}`;
rightInput.min = '0';
rightInput.max = '110';
rightInput.value = rightEar[idx];
rightInput.style.verticalAlign = 'middle';
rightContainer.appendChild(rightInput);
const rightValue = document.createElement('span');
rightValue.id = `right_value_${idx}`;
rightValue.textContent = `${rightEar[idx]} dB`;
rightValue.style.marginLeft = '5px';
rightContainer.appendChild(rightValue);
rightContainer.appendChild(document.createElement('br'));
rightInput.addEventListener('input', function() {
rightEar[idx] = parseInt(this.value);
document.getElementById(`right_value_${idx}`).textContent = `${this.value} dB`;
updateChartData();
});
// Left ear controls
const leftLabel = document.createElement('label');
leftLabel.textContent = `${freq} Hz:`;
leftLabel.htmlFor = `left_${idx}`;
leftContainer.appendChild(leftLabel);
const leftInput = document.createElement('input');
leftInput.type = 'range';
leftInput.id = `left_${idx}`;
leftInput.min = '0';
leftInput.max = '110';
leftInput.value = leftEar[idx];
leftInput.style.verticalAlign = 'middle';
leftContainer.appendChild(leftInput);
const leftValue = document.createElement('span');
leftValue.id = `left_value_${idx}`;
leftValue.textContent = `${leftEar[idx]} dB`;
leftValue.style.marginLeft = '5px';
leftContainer.appendChild(leftValue);
leftContainer.appendChild(document.createElement('br'));
leftInput.addEventListener('input', function() {
leftEar[idx] = parseInt(this.value);
document.getElementById(`left_value_${idx}`).textContent = `${this.value} dB`;
updateChartData();
});
});
}
// Create the audiogram chart
function createChart() {
const ctx = document.getElementById('audiogramChart').getContext('2d');
chart = new Chart(ctx, {
type: 'line',
data: {
labels: frequencies,
datasets: [
// Right ear thresholds
{
label: 'Right Ear',
data: rightEar.map((value, index) => ({x: frequencies[index], y: value})),
borderColor: 'red',
backgroundColor: 'red',
borderWidth: 2,
pointRadius: 6,
pointHoverRadius: 10,
fill: false,
spanGaps: false
},
// Left ear thresholds
{
label: 'Left Ear',
data: leftEar.map((value, index) => ({x: frequencies[index], y: value})),
borderColor: 'blue',
backgroundColor: 'blue',
borderWidth: 2,
pointRadius: 6,
pointHoverRadius: 10,
fill: false,
spanGaps: false
},
// Speech sounds
{
label: 'Speech Sounds',
data: speechSounds.map(sound => ({x: sound.freq, y: sound.int})),
backgroundColor: sound => getSoundColor(sound),
borderColor: 'rgba(200, 200, 200, 0.2)',
borderWidth: 1,
pointRadius: 8,
pointHoverRadius: 12,
pointStyle: 'circle',
fill: false
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
type: 'logarithmic',
title: {
display: false,
text: 'Frequency (Hz)'
},
min: 250,
max: 8000,
ticks: {
callback: function(value) {
if (value >= 1000) {
return value/1000 + 'K';
}
return value;
}
},
grid: {
display: true
}
},
y: {
title: {
display: true,
text: 'Hearing Level (dB HL)'
},
min: 110,
max: 0,
reverse: true,
ticks: {
stepSize: 10,
callback: function(value) {
return value + ' dB';
}
},
grid: {
display: true
}
}
},
plugins: {
legend: {
display: true,
position: 'top'
},
tooltip: {
callbacks: {
label: function(context) {
const dataIndex = context.dataIndex;
const datasetIndex = context.datasetIndex;
if (datasetIndex === 2) { // Speech sounds
const sound = speechSounds[dataIndex];
const audibility = calculateAudibility(sound);
return `${sound.phoneme} (${sound.freq} Hz, ${sound.int} dB) - ${audibility.toFixed(1)}% audible`;
} else {
const freq = frequencies[dataIndex];
const value = context.parsed.y;
return `${value} dB at ${freq} Hz`;
}
}
}
}
},
onClick: handleChartClick,
onHover: handleChartHover,
animation: {
duration: 0
}
}
});
// Add event listeners for drag functionality
document.getElementById('audiogramChart').addEventListener('mousedown', handleMouseDown);
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
}
// Update chart data when thresholds change
function updateChartData() {
chart.data.datasets[0].data = rightEar.map((value, index) => ({x: frequencies[index], y: value}));
chart.data.datasets[1].data = leftEar.map((value, index) => ({x: frequencies[index], y: value}));
chart.data.datasets[2].backgroundColor = speechSounds.map(sound => getSoundColor(sound));
chart.update();
}
// Handle mouse down for dragging
function handleMouseDown(e) {
const canvas = document.getElementById('audiogramChart');
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// Get the points that were clicked
const elements = chart.getElementsAtEventForMode(e, 'nearest', { intersect: true }, true);
if (elements.length > 0) {
const element = elements[0];
if (element.datasetIndex < 2) { // Only allow dragging of threshold points
isDragging = true;
draggedPoint = element;
}
}
}
// Handle mouse move for dragging
function handleMouseMove(e) {
if (!isDragging || !draggedPoint) return;
const canvas = document.getElementById('audiogramChart');
const rect = canvas.getBoundingClientRect();
const chartY = e.clientY - rect.top;
// Convert pixel position to value in chart coordinates
const yScale = chart.scales.y;
let value = yScale.getValueForPixel(chartY);
// Round to nearest 5 dB
value = Math.round(value / 5) * 5;
// Clamp to valid range
value = Math.max(0, Math.min(110, value));
// Update the data
const datasetIndex = draggedPoint.datasetIndex;
const dataIndex = draggedPoint.index;
if (datasetIndex === 0) { // Right ear
rightEar[dataIndex] = value;
document.getElementById(`right_${dataIndex}`).value = value;
document.getElementById(`right_value_${dataIndex}`).textContent = `${value} dB`;
} else if (datasetIndex === 1) { // Left ear
leftEar[dataIndex] = value;
document.getElementById(`left_${dataIndex}`).value = value;
document.getElementById(`left_value_${dataIndex}`).textContent = `${value} dB`;
}
updateChartData();
updateSII();
}
// Handle mouse up to stop dragging
function handleMouseUp() {
isDragging = false;
draggedPoint = null;
}
// Handle chart click
function handleChartClick(e, elements) {
// This can be expanded for additional click functionality
}
// Handle chart hover
function handleChartHover(e, elements) {
// Change cursor if hovering over a point that can be dragged
const target = e.native.target;
if (elements.length > 0 && elements[0].datasetIndex < 2) {
target.style.cursor = 'grab';
} else {
target.style.cursor = 'default';
}
}
// Calculate audibility of a speech sound based on hearing thresholds and noise
function calculateAudibility(sound) {
// Find the hearing threshold at the sound's frequency
const freq = sound.freq;
let rightThreshold = interpolateThreshold(freq, rightEar);
let leftThreshold = interpolateThreshold(freq, leftEar);
// Use better ear (lower threshold)
const threshold = Math.min(rightThreshold, leftThreshold);
// Signal level at the ear
const signalLevel = sound.int - threshold;
// Available headroom before discomfort (assuming UCL of 100 dB HL)
const ucl = 100;
const uclMargin = ucl - threshold;
// Reduce signal level if it would exceed UCL
const effectiveSignalLevel = Math.min(signalLevel, uclMargin);
// Noise level at this frequency
const noiseLevel = bgNoiseLevel;
// SNR at the ear
const snr = effectiveSignalLevel - (noiseLevel - threshold);
// Calculate audibility based on SNR
// 0 dB SNR = 50% audible, 10+ dB SNR = 100% audible, -10 dB SNR = 0% audible
let audibility = 0;
if (snr >= 10) {
audibility = 100;
} else if (snr <= -10) {
audibility = 0;
} else {
audibility = ((snr + 10) / 20) * 100;
}
// Ensure within bounds
audibility = Math.max(0, Math.min(100, audibility));
return audibility;
}
// Interpolate threshold at a given frequency
function interpolateThreshold(freq, thresholds) {
// Find the two closest frequencies
let lowerIdx = 0;
let upperIdx = frequencies.length - 1;
for (let i = 0; i < frequencies.length - 1; i++) {
if (frequencies[i] <= freq && freq <= frequencies[i+1]) {
lowerIdx = i;
upperIdx = i+1;
break;
}
}
// Linear interpolation on log scale
const logFreq = Math.log10(freq);
const logLower = Math.log10(frequencies[lowerIdx]);
const logUpper = Math.log10(frequencies[upperIdx]);
const ratio = (logFreq - logLower) / (logUpper - logLower);
return thresholds[lowerIdx] + ratio * (thresholds[upperIdx] - thresholds[lowerIdx]);
}
// Get color for speech sound based on audibility
function getSoundColor(sound) {
const audibility = calculateAudibility(sound);
const alpha = audibility / 100;
return `rgba(255, 0, 0, ${alpha})`;
}
// Calculate SII (Speech Intelligibility Index)
function calculateSII() {
// SII calculation based on ANSI S3.5-1997
// Simplified version using critical bands
const bands = [
{ freq: 250, bandwidth: 125 },
{ freq: 500, bandwidth: 250 },
{ freq: 1000, bandwidth: 500 },
{ freq: 2000, bandwidth: 1000 },
{ freq: 4000, bandwidth: 2000 }
];
let totalSII = 0;
let totalWeight = 0;
// Band importance weights (approximate)
const weights = [0.12, 0.17, 0.20, 0.23, 0.18];
bands.forEach((band, idx) => {
// Find speech sounds in this band
const soundsInBand = speechSounds.filter(sound =>
sound.freq >= band.freq - band.bandwidth/2 &&
sound.freq <= band.freq + band.bandwidth/2
);
if (soundsInBand.length === 0) return;
// Calculate average audibility in this band
const avgAudibility = soundsInBand.reduce((sum, sound) => {
return sum + calculateAudibility(sound);
}, 0) / soundsInBand.length;
// Convert to SII contribution (0-1 scale)
const siiContribution = avgAudibility / 100 * weights[idx];
totalSII += siiContribution;
totalWeight += weights[idx];
});
// Normalize by total weights
return totalWeight > 0 ? totalSII / totalWeight : 0;
}
// Update SII calculation and display
function updateSII() {
siiScore = calculateSII();
document.getElementById('siiScore').textContent = siiScore.toFixed(2);
document.getElementById('siiBar').style.width = `${siiScore * 100}%`;
// Update speech sound colors
if (chart && chart.data.datasets[2]) {
chart.data.datasets[2].backgroundColor = speechSounds.map(sound => getSoundColor(sound));
chart.update();
}
}
// Update the chart display
function updateChart() {
updateChartData();
updateSII();
}
// Initialize when page loads
window.onload = init;
</script>
</body>
</html>
The app works entirely in the browser with no server requirements, using Chart.js for visualization.