@drop-ai/ui-utils 0.2.0 → 0.2.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.
Files changed (2) hide show
  1. package/README.md +274 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,274 @@
1
+ # @drop-ai/ui-utils
2
+
3
+ Framework-agnostic UI utilities for building DAW interfaces on top of [`@drop-ai/core`](https://www.npmjs.com/package/@drop-ai/core). Provides the missing layer between headless domain models and visual rendering — viewport math, waveform computation, ruler ticks, and playhead tracking.
4
+
5
+ ```bash
6
+ npm install @drop-ai/ui-utils @drop-ai/core
7
+ ```
8
+
9
+ ## Features
10
+
11
+ - **TimelineViewport** — Zoom, scroll, frame ↔ pixel conversion with anchor-aware zooming
12
+ - **TrackLayout** — Track height management and vertical position computation
13
+ - **Waveform** — Peak computation from `AudioBuffer` + Canvas 2D rendering
14
+ - **Ruler Ticks** — Tick position/label calculation for BBT, timecode, min:sec, and samples
15
+ - **Playhead Tracker** — `requestAnimationFrame`-based transport position tracking
16
+ - **Zero framework dependency** — Works with React, Vue, Svelte, vanilla JS, or any Canvas-based renderer
17
+
18
+ ---
19
+
20
+ ## Quick Start
21
+
22
+ ```typescript
23
+ import { Session, AudioEngine, ClockMode } from '@drop-ai/core';
24
+ import {
25
+ TimelineViewport,
26
+ TrackLayout,
27
+ PlayheadTracker,
28
+ computePeaks,
29
+ renderWaveform,
30
+ computeRulerTicks,
31
+ } from '@drop-ai/ui-utils';
32
+
33
+ // 1. Create a viewport
34
+ const viewport = new TimelineViewport(44100);
35
+ viewport.setPixelsPerSecond(100);
36
+ viewport.setViewportWidth(canvas.width);
37
+ viewport.setDuration(180); // 3 minutes
38
+
39
+ // 2. Convert frames to pixels
40
+ const regionX = viewport.frameToPixel(region.start);
41
+ const regionW = viewport.framesToWidth(region.length);
42
+
43
+ // 3. Render a waveform
44
+ const peaks = computePeaks(audioBuffer, 512);
45
+ const ctx = canvas.getContext('2d')!;
46
+ renderWaveform({ ctx, peaks, width: 800, height: 120, color: '#4a9eff' });
47
+
48
+ // 4. Compute ruler ticks
49
+ const ticks = computeRulerTicks({ viewport, mode: ClockMode.BBT, bpm: 120 });
50
+ for (const tick of ticks) {
51
+ ctx.fillText(tick.label, tick.x - viewport.scrollX, 18);
52
+ }
53
+
54
+ // 5. Track the playhead
55
+ const tracker = new PlayheadTracker(viewport, () => engine.session.transportFrame);
56
+ tracker.moved.connect(({ x }) => {
57
+ playheadEl.style.transform = `translateX(${x}px)`;
58
+ });
59
+ tracker.start();
60
+ ```
61
+
62
+ ---
63
+
64
+ ## API Reference
65
+
66
+ ### TimelineViewport
67
+
68
+ Manages zoom level and horizontal scroll, providing frame ↔ pixel conversion.
69
+
70
+ ```typescript
71
+ const viewport = new TimelineViewport(sampleRate);
72
+ ```
73
+
74
+ | Method | Description |
75
+ |--------|-------------|
76
+ | `setPixelsPerSecond(pps)` | Set zoom level (clamped 1–1000) |
77
+ | `setScrollX(px)` | Set horizontal scroll offset in pixels |
78
+ | `setViewportWidth(px)` | Set the visible width of the viewport |
79
+ | `setDuration(seconds)` | Set total session duration |
80
+ | `frameToPixel(frame)` | Convert frame → absolute pixel X |
81
+ | `pixelToFrame(px)` | Convert absolute pixel X → frame |
82
+ | `frameToViewportPixel(frame)` | Convert frame → viewport-relative pixel |
83
+ | `viewportPixelToFrame(px)` | Convert viewport-relative pixel → frame |
84
+ | `framesToWidth(frames)` | Convert duration in frames → pixel width |
85
+ | `widthToFrames(px)` | Convert pixel width → duration in frames |
86
+ | `zoom(direction, focus?, anchorPx?, playheadFrame?)` | Zoom in/out with anchor |
87
+ | `zoomToFit()` | Zoom to show the entire session |
88
+ | `zoomToRange(startFrame, endFrame)` | Zoom to show a specific range |
89
+ | `scrollToFrame(frame, center?)` | Scroll to make a frame visible |
90
+ | `isFrameVisible(frame)` | Check if a frame is in the visible viewport |
91
+
92
+ **Properties:** `pixelsPerSecond`, `scrollX`, `framesPerPixel`, `contentWidth`, `visibleStartFrame`, `visibleEndFrame`
93
+
94
+ **Signal:** `changed` — emitted on any viewport state change.
95
+
96
+ ---
97
+
98
+ ### TrackLayout
99
+
100
+ Computes vertical positions for an ordered list of tracks.
101
+
102
+ ```typescript
103
+ const layout = new TrackLayout();
104
+ layout.setTracks(['track-1', 'track-2', 'track-3']);
105
+ layout.setTrackHeight('track-1', 120);
106
+
107
+ const entry = layout.getEntry('track-1');
108
+ // { trackId: 'track-1', y: 0, height: 120 }
109
+
110
+ const hit = layout.getTrackAtY(150);
111
+ // { trackId: 'track-2', y: 120, height: 80 }
112
+ ```
113
+
114
+ | Method | Description |
115
+ |--------|-------------|
116
+ | `setTracks(trackIds)` | Set ordered track list (top → bottom) |
117
+ | `setTrackHeight(trackId, height)` | Set height for a track (24–500px) |
118
+ | `setCollapsed(trackId, collapsed)` | Collapse/expand a track |
119
+ | `getEntry(trackId)` | Get `{ trackId, y, height }` for a track |
120
+ | `getEntries()` | Get all layout entries |
121
+ | `getTrackAtY(y)` | Hit-test: find track at a Y coordinate |
122
+ | `totalHeight` | Total height of all tracks |
123
+
124
+ **Signal:** `changed` — emitted on layout change.
125
+
126
+ ---
127
+
128
+ ### Waveform: computePeaks
129
+
130
+ Compute peak data from audio samples for efficient waveform rendering.
131
+
132
+ ```typescript
133
+ // From AudioBuffer
134
+ const peaks = computePeaks(audioBuffer, 512);
135
+ const peaksR = computePeaks(audioBuffer, 512, 1); // channel 1
136
+
137
+ // From raw Float32Array
138
+ const peaks = computePeaksFromSamples(channelData, 512);
139
+
140
+ // Choose resolution for current zoom level
141
+ const resolution = recommendResolution(viewport.framesPerPixel);
142
+ ```
143
+
144
+ Returns a `PeakData` object (compatible with `@drop-ai/core`'s `Source.setPeakData()`):
145
+
146
+ ```typescript
147
+ interface PeakData {
148
+ min: Float32Array; // min sample per entry
149
+ max: Float32Array; // max sample per entry
150
+ rms: Float32Array; // RMS per entry
151
+ length: number; // number of entries
152
+ resolution: number; // frames per entry
153
+ }
154
+ ```
155
+
156
+ ---
157
+
158
+ ### Waveform: renderWaveform
159
+
160
+ Draw waveforms onto a Canvas 2D context.
161
+
162
+ ```typescript
163
+ // From pre-computed peaks (recommended for large buffers)
164
+ renderWaveform({
165
+ ctx,
166
+ peaks,
167
+ width: 800,
168
+ height: 120,
169
+ color: '#4a9eff',
170
+ logScale: true, // optional: dB scaling
171
+ sourceStart: 44100, // optional: offset into source
172
+ sourceLength: 88200, // optional: range to render
173
+ });
174
+
175
+ // Stereo (L top half, R bottom half with separator)
176
+ renderStereoWaveform(ctx, peaksL, peaksR, 800, 120);
177
+
178
+ // Direct from samples (convenience for small buffers)
179
+ renderWaveformFromSamples(ctx, channelData, 800, 120, { color: '#ff6b6b' });
180
+ ```
181
+
182
+ ---
183
+
184
+ ### computeRulerTicks
185
+
186
+ Compute ruler tick positions and labels for the visible viewport. Automatically adjusts tick density based on zoom level.
187
+
188
+ ```typescript
189
+ import { ClockMode } from '@drop-ai/core';
190
+
191
+ const ticks = computeRulerTicks({
192
+ viewport,
193
+ mode: ClockMode.MINSEC,
194
+ });
195
+
196
+ for (const tick of ticks) {
197
+ const x = tick.x - viewport.scrollX;
198
+ ctx.beginPath();
199
+ ctx.moveTo(x, 0);
200
+ ctx.lineTo(x, tick.major ? 20 : 10);
201
+ ctx.stroke();
202
+ if (tick.major) ctx.fillText(tick.label, x + 3, 18);
203
+ }
204
+ ```
205
+
206
+ **Supported modes:** `ClockMode.BBT` (bars/beats), `ClockMode.MINSEC`, `ClockMode.TIMECODE`, `ClockMode.SAMPLES`
207
+
208
+ Each tick has: `{ x, frame, label, major }`.
209
+
210
+ ---
211
+
212
+ ### PlayheadTracker
213
+
214
+ Tracks transport position via `requestAnimationFrame` and emits pixel coordinates.
215
+
216
+ ```typescript
217
+ const tracker = new PlayheadTracker(
218
+ viewport,
219
+ () => engine.session.transportFrame,
220
+ );
221
+
222
+ tracker.followPlayhead = true; // auto-scroll viewport
223
+
224
+ tracker.moved.connect(({ frame, x }) => {
225
+ playheadEl.style.transform = `translateX(${x}px)`;
226
+ });
227
+
228
+ tracker.start();
229
+ // ... later
230
+ tracker.stop();
231
+ tracker.dispose();
232
+ ```
233
+
234
+ ---
235
+
236
+ ## Architecture
237
+
238
+ ```
239
+ ┌─────────────────────────────────────────────────────┐
240
+ │ Your App (React, Vue, Svelte, vanilla JS) │
241
+ │ │
242
+ │ ┌───────────────────────────────────────────────┐ │
243
+ │ │ UI Components (your code) │ │
244
+ │ │ Canvas rendering, DOM manipulation, events │ │
245
+ │ └──────────────────┬────────────────────────────┘ │
246
+ │ │ uses │
247
+ │ ┌──────────────────▼────────────────────────────┐ │
248
+ │ │ @drop-ai/ui-utils │ │
249
+ │ │ TimelineViewport, TrackLayout, computePeaks, │ │
250
+ │ │ renderWaveform, computeRulerTicks, │ │
251
+ │ │ PlayheadTracker │ │
252
+ │ └──────────────────┬────────────────────────────┘ │
253
+ │ │ depends on │
254
+ │ ┌──────────────────▼────────────────────────────┐ │
255
+ │ │ @drop-ai/core │ │
256
+ │ │ Session, Track, Region, Source, AudioEngine, │ │
257
+ │ │ CommandExecutor, Signal, PeakData, ClockMode │ │
258
+ │ └───────────────────────────────────────────────┘ │
259
+ └─────────────────────────────────────────────────────┘
260
+ ```
261
+
262
+ ---
263
+
264
+ ## Dependencies
265
+
266
+ | Package | Type | Description |
267
+ |---------|------|-------------|
268
+ | `@drop-ai/core` | runtime | Domain models, Signal, PeakData, ClockMode |
269
+
270
+ ---
271
+
272
+ ## License
273
+
274
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drop-ai/ui-utils",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Framework-agnostic UI utilities for building DAW interfaces on top of @drop-ai/core",
5
5
  "license": "MIT",
6
6
  "type": "module",