remotion
Version:
Make videos programmatically
67 lines (66 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAmplification = exports.getShouldAmplify = void 0;
const react_1 = require("react");
const log_1 = require("./log");
let warned = false;
const warnOnce = (logLevel) => {
if (warned) {
return;
}
warned = true;
log_1.Log.warn(logLevel, 'AudioContext is not supported in this browser');
};
const getShouldAmplify = (volume) => {
return volume > 1;
};
exports.getShouldAmplify = getShouldAmplify;
const useAmplification = ({ mediaRef, volume, logLevel, }) => {
var _a;
const shouldAmplify = (0, exports.getShouldAmplify)(volume);
const audioStuffRef = (0, react_1.useRef)(null);
const currentVolumeRef = (0, react_1.useRef)(volume);
currentVolumeRef.current = volume;
(0, react_1.useLayoutEffect)(() => {
var _a;
if (!shouldAmplify) {
return;
}
if (!AudioContext) {
warnOnce(logLevel);
return;
}
if (!mediaRef.current) {
return;
}
if (audioStuffRef.current) {
return;
}
const audioContext = new AudioContext({
latencyHint: 'interactive',
});
const source = new MediaElementAudioSourceNode(audioContext, {
mediaElement: mediaRef.current,
});
const gainNode = new GainNode(audioContext, {
gain: Math.max(currentVolumeRef.current, 1),
});
audioStuffRef.current = {
gainNode,
source,
audioContext,
};
source.connect(gainNode);
gainNode.connect(audioContext.destination);
log_1.Log.trace(logLevel, `Starting to amplify ${(_a = mediaRef.current) === null || _a === void 0 ? void 0 : _a.src}. Gain = ${currentVolumeRef.current}`);
}, [logLevel, mediaRef, shouldAmplify]);
if (audioStuffRef.current) {
const valueToSet = Math.max(volume, 1);
if (audioStuffRef.current.gainNode.gain.value !== valueToSet) {
audioStuffRef.current.gainNode.gain.value = valueToSet;
log_1.Log.trace(logLevel, `Setting gain to ${valueToSet} for ${(_a = mediaRef.current) === null || _a === void 0 ? void 0 : _a.src}`);
}
}
return audioStuffRef;
};
exports.useAmplification = useAmplification;