Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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