UNPKG

sound-visualizer

Version:
109 lines (102 loc) 3.56 kB
"use strict"; 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/continuous/draw/impure.ts var impure_exports = {}; __export(impure_exports, { drawContinuousWave: () => drawContinuousWave }); module.exports = __toCommonJS(impure_exports); // 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(); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { drawContinuousWave });