All files / player-browser/src/view/beatoraja/select chart-images.ts

50.79% Statements 32/63
47.5% Branches 19/40
60% Functions 6/10
61.7% Lines 29/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                                    29x                                           17x 17x     17x                   53x 19x 19x 19x   7x 7x   7x 1x 1x     6x                       3x 3x 3x 1x   3x                 1x 1x 1x 1x 1x 1x 1x                                 17x                                                                                         1x 3x      
/**
 * The beatoraja select view's chart images and graphs (`BeatorajaSelectView`): the focused chart's `#STAGEFILE`,
 * `#BACKBMP` and `#BANNER` bitmaps for the skin's synthetic image ids (-100 / -101 / -102), decoded through the view and
 * kept per chart until the part is disposed, and the data of its BPM graph and notes graph, worked out once per chart.
 *
 * The part reads the view's state through {@link BeatorajaSelectChartImagesContext}; the select view passes it.
 */
 
import type { Texture } from 'pixi.js';
import {
  computeBeatorajaChartNoteDistribution,
  type BeatorajaChartNoteDistribution,
} from '../../../chart/beatoraja/note-distribution.ts';
import type { LibraryChart, LibraryEntry } from '@be-music/player-scene-model';
import { logger } from '../../../logger.ts';
import { destroyTextureAndRevokeBlobURL } from '../../../media/textures.ts';
import type { BeatorajaChartImages } from '../../../skin/beatoraja/chart-images.ts';
 
const log = logger('beatoraja-select');
 
/** What the chart images read on the select view: its fields of these names. */
export interface BeatorajaSelectChartImagesContext {
  /** Decodes a chart's own images; without it the synthetic image ids stay hidden. */
  readonly decodeChartImages: ((chartEntry: LibraryChart) => Promise<BeatorajaChartImages>) | undefined;
  readonly disposed: boolean;
  readonly entries: ReadonlyArray<LibraryEntry>;
  /** The cursor: `entries[currentIndex]` is the focused bar. */
  readonly currentIndex: number;
}
 
/** The focused chart's images and graphs. See the module comment. */
export class BeatorajaSelectChartImages {
  private readonly ctx: BeatorajaSelectChartImagesContext;
 
  /**
   * Per-song chart-image cache. Keyed by `LibraryChart.id`; populated lazily by
   * {@link refreshChartImagesForFocus} as the user moves the cursor. Re-focusing a
   * previously-loaded song hits the cache and skips the IO. Sentinel `null` records a
   * decode in flight, or one that failed, so we don't retry every cursor move.
   */
  private readonly chartImageCache = new Map<string, BeatorajaChartImages | null>();
  private disposed = false;
 
  constructor(ctx: BeatorajaSelectChartImagesContext) {
    this.ctx = ctx;
  }
 
  /**
   * Lazily decode the focused song's `#STAGEFILE` / `#BACKBMP` / `#BANNER` bitmaps via the context's
   * `decodeChartImages`. Caches by `song.id` so re-focusing a previously-loaded song hits the cache. Images that land
   * after the cursor moved on are kept for when it comes back; those that land after the part is disposed are
   * destroyed.
   */
  refreshChartImagesForFocus(chartEntry: LibraryChart | undefined): void {
    if (chartEntry === undefined) return;
    const decoder = this.ctx.decodeChartImages;
    Iif (decoder === undefined) return;
    if (this.chartImageCache.has(chartEntry.id)) return;
    // Mark as in-flight so concurrent focus changes don't fire duplicate decodes.
    this.chartImageCache.set(chartEntry.id, null);
    void decoder(chartEntry)
      .then((images) => {
        if (this.ctx.disposed || this.disposed) {
          destroyChartImages(images);
          return;
        }
        // The next frame picks up the new texture via `chartImageProvider`, which reads the cache every frame.
        this.chartImageCache.set(chartEntry.id, images);
      })
      .catch((error: unknown) => {
        // Decode failed; leave the cache entry as `null` so we don't retry on every cursor settle. User-visible: the
        // banner / stagefile / backbmp slot stays hidden.
        log.warn('chart-image decode failed', { chartId: chartEntry.id, error });
        this.chartImageCache.set(chartEntry.id, null);
      });
  }
 
  /** Destroys the decoded images; nothing decodes after. */
  dispose(): void {
    Iif (this.disposed) return;
    this.disposed = true;
    for (const images of this.chartImageCache.values()) {
      Iif (images !== null) destroyChartImages(images);
    }
    this.chartImageCache.clear();
  }
 
  /**
   * Per-frame resolver for the synthetic chart-bitmap ids. Returns the FOCUSED song's
   * matching texture from the cache, or `undefined` (= destination hides) when the chart
   * didn't ship that bitmap or the decode is still in flight / failed.
   */
  resolveChartImageForFocus(syntheticId: number): Texture | undefined {
    const entry = this.ctx.entries[this.ctx.currentIndex];
    Iif (entry?.kind !== 'chart') return undefined;
    const cached = this.chartImageCache.get(entry.chart.id);
    Iif (cached === null || cached === undefined) return undefined;
    Iif (syntheticId === -100) return cached.stageFile;
    Iif (syntheticId === -101) return cached.backBMP;
    Eif (syntheticId === -102) return cached.banner;
    return undefined;
  }
 
  /**
   * Lazy analysis cache for the focused chart. `computeBeatorajaChartNoteDistribution`
   * walks the chart's events once and produces both the per-bucket histogram AND the BPM
   * segment list — sharing the walk between the bpmgraph and notes-graph renderers. Cached
   * per song-id so cursor moves through a folder don't redundantly re-analyse.
   */
  private analysisFor(chartEntry: LibraryChart): BeatorajaChartNoteDistribution {
    const cached = this.chartAnalysisCache.get(chartEntry.id);
    if (cached !== undefined) return cached;
    const result = computeBeatorajaChartNoteDistribution(chartEntry.chart);
    this.chartAnalysisCache.set(chartEntry.id, result);
    return result;
  }
  private readonly chartAnalysisCache = new Map<string, BeatorajaChartNoteDistribution>();
 
  /**
   * Per-frame resolver for the focused chart's BPM segment data (consumed by `bpmgraph`
   * destinations under the spec-faithful renderer path).
   */
  resolveBPMGraphDataForFocus():
    | {
        segments: ReadonlyArray<{ timeMs: number; bpm: number }>;
        mainBPM: number;
        minBPM: number;
        maxBPM: number;
        totalMs: number;
      }
    | undefined {
    const entry = this.ctx.entries[this.ctx.currentIndex];
    if (entry?.kind !== 'chart') return undefined;
    const analysis = this.analysisFor(entry.chart);
    if (analysis.bpmSegments.length === 0 || analysis.totalMs <= 0) return undefined;
    return {
      segments: analysis.bpmSegments,
      mainBPM: analysis.mainBPM,
      minBPM: analysis.minBPM,
      maxBPM: analysis.maxBPM,
      totalMs: analysis.totalMs,
    };
  }
 
  /**
   * Per-frame resolver for the focused chart's note-distribution histogram (judgegraph
   * type=0 under the spec-faithful renderer path). Returns the per-second × per-category
   * bucket data the renderer needs for the upstream `SkinNoteDistributionGraph` look.
   */
  resolveNoteDistributionForFocus():
    | { buckets: ReadonlyArray<ReadonlyArray<number>>; maxCount: number; totalMs: number }
    | undefined {
    const entry = this.ctx.entries[this.ctx.currentIndex];
    if (entry?.kind !== 'chart') return undefined;
    const analysis = this.analysisFor(entry.chart);
    if (analysis.buckets.length === 0) return undefined;
    return { buckets: analysis.buckets, maxCount: analysis.maxCount, totalMs: analysis.totalMs };
  }
}
 
function destroyChartImages(images: BeatorajaChartImages): void {
  for (const texture of [images.stageFile, images.backBMP, images.banner]) {
    Iif (texture !== undefined) destroyTextureAndRevokeBlobURL(texture, true);
  }
}