XGravity/Public/ecma/Sounds/HitExplosionSound.ecma.js

52 lines
1.7 KiB
JavaScript

"use strict";
const HitExplosionSound = (function(){
return function(audio_ctx, output_node, volume_multiplier = 1.0){
const now = audio_ctx.currentTime;
const buffer_size = audio_ctx.sampleRate * 0.48;
const buffer = audio_ctx.createBuffer(1, buffer_size, audio_ctx.sampleRate);
const data = buffer.getChannelData(0);
for(let i = 0; i < buffer_size; i++){
data[i] = (Math.random() * 2) - 1;
}
const noise = audio_ctx.createBufferSource();
noise.buffer = buffer;
const filter = audio_ctx.createBiquadFilter();
const gain = audio_ctx.createGain();
// Ruido sordo filtrado en graves
filter.type = "lowpass";
filter.frequency.setValueAtTime(420, now);
filter.frequency.exponentialRampToValueAtTime(32, now + 0.48);
gain.gain.setValueAtTime(0.9 * volume_multiplier, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.48);
noise.connect(filter);
filter.connect(gain);
gain.connect(output_node);
// Subgrave sintetizado para dar golpe de casco en el vacío
const osc = audio_ctx.createOscillator();
const osc_gain = audio_ctx.createGain();
osc.type = "triangle";
osc.frequency.setValueAtTime(130, now);
osc.frequency.exponentialRampToValueAtTime(25, now + 0.35);
osc_gain.gain.setValueAtTime(0.8 * volume_multiplier, now);
osc_gain.gain.exponentialRampToValueAtTime(0.001, now + 0.35);
osc.connect(osc_gain);
osc_gain.connect(output_node);
osc.start(now);
osc.stop(now + 0.35);
noise.start(now);
noise.stop(now + 0.48);
};
})();