Skip to content

Instantly share code, notes, and snippets.

@rustyconover
Created January 12, 2021 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rustyconover/a6d8cc4ccba2c551c799535401da2b57 to your computer and use it in GitHub Desktop.
Save rustyconover/a6d8cc4ccba2c551c799535401da2b57 to your computer and use it in GitHub Desktop.
Load and Analyze PCM samples
// Keep an array of indexes of loud samples.
const loud_indexes = [0];
{
const pcm_data = fs.readFileSync(pcm_output, { encoding: null });
const pcm_values = new Int16Array(pcm_data.buffer);
// The first segment may be longer than normal due to Icecast
// buffering.
if (pcm_values.length > (window_length / 1000) * sample_rate * 1.2) {
return;
}
pcm_values.forEach((v, idx) => {
if (Math.abs(v) > loud_threshold) {
loud_indexes.push(idx);
}
});
loud_indexes.push(pcm_values.length - 1);
};
let total_silence_seconds = 0;
if (loud_indexes.length < 2) {
total_silence_seconds = window_length / 1000;
}
// Loop over all of the indexes where a sample was louder
// then the threshold and determine the amount of time
// that the channel was silent, if its > .25 of a second
// count that duration of time as silence.
for (let i = 1; i < loud_indexes.length; i++) {
const sample_dist = loud_indexes[i] - loud_indexes[i - 1];
if (sample_dist > (sample_rate * .25)) {
diff_indexes.push(sample_dist);
total_silence_seconds += sample_dist / sample_rate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment