File size: 3,148 Bytes
56268ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
const startBtn = document.getElementById('startBtn');
const hint = document.getElementById('hint');
const numClustersInput = document.getElementById('numClustersInputID');
const thresholdInput = document.getElementById('thresholdInputID');
const textArea = document.getElementById('text');
const fileSelectCtrl = document.getElementById('file');
let sd = null;
let float32Samples = null;
Module = {};
Module.onRuntimeInitialized = function() {
console.log('Model files downloaded!');
console.log('Initializing speaker diarization ......');
sd = createOfflineSpeakerDiarization(Module)
console.log('sampleRate', sd.sampleRate);
hint.innerText =
'Initialized! Please select a wave file and click the Start button.';
fileSelectCtrl.disabled = false;
};
function onFileChange() {
var files = document.getElementById('file').files;
if (files.length == 0) {
console.log('No file selected');
float32Samples = null;
startBtn.disabled = true;
return;
}
textArea.value = '';
console.log('files: ' + files);
const file = files[0];
console.log(file);
console.log('file.name ' + file.name);
console.log('file.type ' + file.type);
console.log('file.size ' + file.size);
let audioCtx = new AudioContext({sampleRate: sd.sampleRate});
let reader = new FileReader();
reader.onload = function() {
console.log('reading file!');
audioCtx.decodeAudioData(reader.result, decodedDone);
};
function decodedDone(decoded) {
let typedArray = new Float32Array(decoded.length);
float32Samples = decoded.getChannelData(0);
startBtn.disabled = false;
}
reader.readAsArrayBuffer(file);
}
startBtn.onclick = function() {
textArea.value = '';
if (float32Samples == null) {
alert('Empty audio samples!');
startBtn.disabled = true;
return;
}
let numClusters = numClustersInput.value;
if (numClusters.trim().length == 0) {
alert(
'Please provide numClusters. Use -1 if you are not sure how many speakers are there');
return;
}
if (!numClusters.match(/^\d+$/)) {
alert(`number of clusters ${
numClusters} is not an integer .\nPlease enter an integer`);
return;
}
numClusters = parseInt(numClusters, 10);
if (numClusters < -1) {
alert(`Number of clusters should be >= -1`);
return;
}
let threshold = 0.5;
if (numClusters <= 0) {
threshold = thresholdInput.value;
if (threshold.trim().length == 0) {
alert('Please provide a threshold.');
return;
}
threshold = parseFloat(threshold);
if (threshold < 0) {
alert(`Pleaser enter a positive threshold`);
return;
}
}
let config = sd.config
config.clustering = {numClusters: numClusters, threshold: threshold};
sd.setConfig(config);
let segments = sd.process(float32Samples);
if (segments == null) {
textArea.value = 'No speakers detected';
return
}
let s = '';
let sep = '';
for (seg of segments) {
// clang-format off
s += sep + `${seg.start.toFixed(2)} -- ${seg.end.toFixed(2)} speaker_${seg.speaker}`
// clang-format on
sep = '\n';
}
textArea.value = s;
}
|