@waveform-playlist/ui-components 9.0.0 → 9.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -649,6 +649,40 @@ function useChunkedCanvasRefs() {
649
649
 
650
650
  // src/components/Channel.tsx
651
651
  import { MAX_CANVAS_WIDTH } from "@waveform-playlist/core";
652
+
653
+ // src/utils/peakRendering.ts
654
+ function aggregatePeaks(data, bits, startIndex, endIndex) {
655
+ if (startIndex * 2 + 1 >= data.length) {
656
+ return null;
657
+ }
658
+ const maxValue = 2 ** (bits - 1);
659
+ let minPeak = data[startIndex * 2] / maxValue;
660
+ let maxPeak = data[startIndex * 2 + 1] / maxValue;
661
+ for (let p = startIndex + 1; p < endIndex; p++) {
662
+ if (p * 2 + 1 >= data.length) break;
663
+ const pMin = data[p * 2] / maxValue;
664
+ const pMax = data[p * 2 + 1] / maxValue;
665
+ if (pMin < minPeak) minPeak = pMin;
666
+ if (pMax > maxPeak) maxPeak = pMax;
667
+ }
668
+ return { min: minPeak, max: maxPeak };
669
+ }
670
+ function calculateBarRects(x, barWidth, halfHeight, minPeak, maxPeak, drawMode) {
671
+ const min = Math.abs(minPeak * halfHeight);
672
+ const max = Math.abs(maxPeak * halfHeight);
673
+ if (drawMode === "normal") {
674
+ return [{ x, y: halfHeight - max, width: barWidth, height: max + min }];
675
+ }
676
+ return [
677
+ { x, y: 0, width: barWidth, height: halfHeight - max },
678
+ { x, y: halfHeight + min, width: barWidth, height: halfHeight - min }
679
+ ];
680
+ }
681
+ function calculateFirstBarPosition(canvasStartGlobal, barWidth, step) {
682
+ return Math.floor((canvasStartGlobal - barWidth + step) / step) * step;
683
+ }
684
+
685
+ // src/components/Channel.tsx
652
686
  import { jsx as jsx5 } from "react/jsx-runtime";
653
687
  function createCanvasFillStyle(ctx, color, width, height) {
654
688
  if (!isWaveformGradient(color)) {
@@ -718,7 +752,6 @@ var Channel = (props) => {
718
752
  const globalPixelOffset = canvasIdx * MAX_CANVAS_WIDTH;
719
753
  const ctx = canvas.getContext("2d");
720
754
  const h2 = Math.floor(waveHeight / 2);
721
- const maxValue = 2 ** (bits - 1);
722
755
  if (ctx) {
723
756
  ctx.resetTransform();
724
757
  ctx.clearRect(0, 0, canvas.width, canvas.height);
@@ -734,21 +767,16 @@ var Channel = (props) => {
734
767
  ctx.fillStyle = createCanvasFillStyle(ctx, fillColor, canvasWidth, waveHeight);
735
768
  const canvasStartGlobal = globalPixelOffset;
736
769
  const canvasEndGlobal = globalPixelOffset + canvasWidth;
737
- const firstBarGlobal = Math.floor((canvasStartGlobal - barWidth + step) / step) * step;
770
+ const firstBarGlobal = calculateFirstBarPosition(canvasStartGlobal, barWidth, step);
738
771
  for (let barGlobal = Math.max(0, firstBarGlobal); barGlobal < canvasEndGlobal; barGlobal += step) {
739
772
  const x = barGlobal - canvasStartGlobal;
740
773
  if (x + barWidth <= 0) continue;
741
- const peakIndex = barGlobal;
742
- if (peakIndex * 2 + 1 < data.length) {
743
- const minPeak = data[peakIndex * 2] / maxValue;
744
- const maxPeak = data[peakIndex * 2 + 1] / maxValue;
745
- const min = Math.abs(minPeak * h2);
746
- const max = Math.abs(maxPeak * h2);
747
- if (drawMode === "normal") {
748
- ctx.fillRect(x, h2 - max, barWidth, max + min);
749
- } else {
750
- ctx.fillRect(x, 0, barWidth, h2 - max);
751
- ctx.fillRect(x, h2 + min, barWidth, h2 - min);
774
+ const peakEnd = Math.min(barGlobal + step, length);
775
+ const peak = aggregatePeaks(data, bits, barGlobal, peakEnd);
776
+ if (peak) {
777
+ const rects = calculateBarRects(x, barWidth, h2, peak.min, peak.max, drawMode);
778
+ for (const rect of rects) {
779
+ ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
752
780
  }
753
781
  }
754
782
  }