sound-visualizer
Version:
183 lines (172 loc) • 5.85 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/helpers/impure.ts
var impure_exports = {};
__export(impure_exports, {
clearCanvas: () => clearCanvas,
drawContinuousWave: () => drawContinuousWave,
drawCurrentWave: () => drawCurrentWave,
startAnalysis: () => startAnalysis
});
module.exports = __toCommonJS(impure_exports);
// src/common/draw/impure.ts
function clearCanvas(canvas) {
const context = canvas.getContext("2d");
if (!context)
return;
const { width, height } = canvas;
context.clearRect(0, 0, width, height);
}
// src/common/draw/options.ts
var defaultDrawOptions = {
strokeColor: "#000",
rectWidth: 2
};
// src/continuous/draw/options.ts
var defaultOptions = __spreadProps(__spreadValues({}, defaultDrawOptions), {
slices: 100,
barRadius: 0
});
// src/continuous/draw/pure.ts
function calculateLine(canvasHeight, value) {
let fraction = value / 255;
if (fraction < 0.05)
fraction += 0.05;
const start = canvasHeight / 2 * fraction;
const end = canvasHeight - start;
return { start, end };
}
// src/common/draw/pure.ts
var THICKNESS_MAP = {
thin: 1,
default: 2,
thick: 2.75
};
function widthFromOption(lineWidth, canvasWidth) {
if (typeof lineWidth === "number")
return lineWidth;
return THICKNESS_MAP[lineWidth] / 300 * canvasWidth;
}
// src/continuous/draw/impure.ts
function drawContinuousWave(canvas, audioHistory, options = defaultOptions) {
const context = canvas.getContext("2d");
if (!context)
return;
const { strokeColor = "#000000", rectWidth = "default", slices = 100, barRadius = 0 } = options;
const { height, width } = canvas;
const w = widthFromOption(rectWidth, width);
context.fillStyle = strokeColor;
context.clearRect(0, 0, width, height);
context.beginPath();
const sliceWidth = width / slices;
const historyToCheck = audioHistory.slice(-slices);
for (let i = 0; i < slices; i++) {
if (i >= audioHistory.length)
break;
const value = historyToCheck[i];
const { start, end } = calculateLine(height, value);
const x = i * sliceWidth;
if (barRadius <= 0) {
context.rect(x, start, w, end - start);
} else {
context.roundRect(x, start, w, end - start, barRadius);
}
}
context.fill();
}
// src/current/draw/options.ts
var defaultOptions2 = __spreadProps(__spreadValues({}, defaultDrawOptions), {
heightNorm: 1
});
// src/current/draw/impure.ts
function drawCurrentWave(canvas, audioData, options = defaultOptions2) {
const context = canvas.getContext("2d");
if (!context)
return;
const { strokeColor = "#000", rectWidth = "default", heightNorm = 1 } = options;
const { height, width } = canvas;
const sliceWidth = width / audioData.length;
context.lineWidth = widthFromOption(rectWidth, width);
context.strokeStyle = strokeColor;
context.clearRect(0, 0, width, height);
context.beginPath();
context.moveTo(0, height / 2);
for (let i = 0; i < audioData.length; i++) {
const x = i * sliceWidth;
const fraction = audioData[i] / 255;
const sectionSize = height * heightNorm;
const y = fraction * sectionSize + (height - sectionSize) * 0.5;
context.lineTo(x, y);
}
context.lineTo(sliceWidth * audioData.length, height / 2);
context.stroke();
}
// src/helpers/impure.ts
var activeAnalysers = /* @__PURE__ */ new Map();
function startAnalysis(audio) {
let activeAnalyser = activeAnalysers.get(audio.id);
if (!activeAnalyser) {
const audioContext2 = new window.AudioContext();
activeAnalyser = {
audioContext: audioContext2,
analyser: audioContext2.createAnalyser()
};
activeAnalysers.set(audio.id, activeAnalyser);
}
const { audioContext, analyser } = activeAnalyser;
const dataArray = new Uint8Array(analyser.frequencyBinCount);
const source = audioContext.createMediaStreamSource(audio);
source.connect(analyser);
function analyse() {
analyser.getByteTimeDomainData(dataArray);
return dataArray;
}
function disconnect() {
source.disconnect();
analyser.disconnect();
audioContext.close().then(() => {
activeAnalysers.delete(audio.id);
});
}
return { analyse, disconnect };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
clearCanvas,
drawContinuousWave,
drawCurrentWave,
startAnalysis
});
;