-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioPlayer.stories.tsx
More file actions
656 lines (606 loc) · 17.8 KB
/
AudioPlayer.stories.tsx
File metadata and controls
656 lines (606 loc) · 17.8 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
import * as React from 'react';
import type { Meta, StoryObj } from '@storybook/react-vite';
import { AudioPlayer } from './AudioPlayer';
import { Card, CardContent, CardHeader, CardTitle } from '../Card';
import { Text } from '../Text';
import { Avatar } from '../Avatar';
import { Badge } from '../Badge';
// ============================================================================
// Audio Sample Generation
// ============================================================================
/**
* Creates a synthetic audio blob URL using Web Audio API.
* This generates a short tone that works locally without CORS issues.
*/
function createSampleAudioUrl(durationSec = 5, frequency = 440): string {
// Create audio context
const audioContext = new (
window.AudioContext ||
(window as typeof window & { webkitAudioContext?: typeof AudioContext })
.webkitAudioContext
)();
const sampleRate = audioContext.sampleRate;
const numSamples = Math.floor(sampleRate * durationSec);
// Create stereo buffer
const buffer = audioContext.createBuffer(2, numSamples, sampleRate);
// Generate a pleasant tone with envelope
for (let channel = 0; channel < 2; channel++) {
const data = buffer.getChannelData(channel);
for (let i = 0; i < numSamples; i++) {
const t = i / sampleRate;
// Apply envelope (fade in/out)
const envelope =
Math.min(t * 4, 1) * Math.min((durationSec - t) * 4, 1) * 0.3;
// Generate tone with harmonics
const fundamental = Math.sin(2 * Math.PI * frequency * t);
const harmonic1 = Math.sin(2 * Math.PI * frequency * 2 * t) * 0.5;
const harmonic2 = Math.sin(2 * Math.PI * frequency * 3 * t) * 0.25;
data[i] = envelope * (fundamental + harmonic1 + harmonic2);
}
}
// Convert to WAV format
const wavBuffer = audioBufferToWav(buffer);
const blob = new Blob([wavBuffer], { type: 'audio/wav' });
return URL.createObjectURL(blob);
}
/**
* Converts an AudioBuffer to WAV format
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function audioBufferToWav(buffer: any): ArrayBuffer {
const numChannels = buffer.numberOfChannels;
const sampleRate = buffer.sampleRate;
const format = 1; // PCM
const bitDepth = 16;
const bytesPerSample = bitDepth / 8;
const blockAlign = numChannels * bytesPerSample;
const samples = buffer.length;
const dataSize = samples * blockAlign;
const bufferSize = 44 + dataSize;
const arrayBuffer = new ArrayBuffer(bufferSize);
const view = new DataView(arrayBuffer);
// WAV header
const writeString = (offset: number, str: string) => {
for (let i = 0; i < str.length; i++) {
view.setUint8(offset + i, str.charCodeAt(i));
}
};
writeString(0, 'RIFF');
view.setUint32(4, bufferSize - 8, true);
writeString(8, 'WAVE');
writeString(12, 'fmt ');
view.setUint32(16, 16, true); // Subchunk1Size
view.setUint16(20, format, true);
view.setUint16(22, numChannels, true);
view.setUint32(24, sampleRate, true);
view.setUint32(28, sampleRate * blockAlign, true);
view.setUint16(32, blockAlign, true);
view.setUint16(34, bitDepth, true);
writeString(36, 'data');
view.setUint32(40, dataSize, true);
// Write interleaved audio data
let offset = 44;
for (let i = 0; i < samples; i++) {
for (let ch = 0; ch < numChannels; ch++) {
const sample = Math.max(-1, Math.min(1, buffer.getChannelData(ch)[i]));
view.setInt16(
offset,
sample < 0 ? sample * 0x8000 : sample * 0x7fff,
true
);
offset += 2;
}
}
return arrayBuffer;
}
// Create sample audio URLs (memoized to avoid regenerating)
let _sampleAudioUrl: string | null = null;
let _shortAudioUrl: string | null = null;
let _longAudioUrl: string | null = null;
function getSampleAudio(): string {
if (!_sampleAudioUrl) {
_sampleAudioUrl = createSampleAudioUrl(10, 440); // 10 seconds, A4 note
}
return _sampleAudioUrl;
}
function getShortAudio(): string {
if (!_shortAudioUrl) {
_shortAudioUrl = createSampleAudioUrl(3, 523.25); // 3 seconds, C5 note
}
return _shortAudioUrl;
}
function getLongAudio(): string {
if (!_longAudioUrl) {
_longAudioUrl = createSampleAudioUrl(30, 329.63); // 30 seconds, E4 note
}
return _longAudioUrl;
}
// ============================================================================
// Meta
// ============================================================================
const meta: Meta<typeof AudioPlayer> = {
title: 'Components/AudioPlayer',
component: AudioPlayer,
parameters: {
layout: 'centered',
},
tags: ['autodocs'],
argTypes: {
variant: {
control: 'select',
options: ['inline', 'compact', 'waveform', 'full'],
description: 'Visual variant of the player',
},
size: {
control: 'select',
options: ['sm', 'md', 'lg'],
description: 'Size of the player',
},
showTime: {
control: 'boolean',
description: 'Show time display (compact and waveform variants)',
},
showDuration: {
control: 'boolean',
description: 'Show duration (inline variant)',
},
showPlaybackRate: {
control: 'boolean',
description: 'Show playback speed control',
},
disabled: {
control: 'boolean',
description: 'Disable the player',
},
},
};
export default meta;
type Story = StoryObj<typeof AudioPlayer>;
// ============================================================================
// Basic Stories
// ============================================================================
export const Default: Story = {
render: () => (
<AudioPlayer src={getSampleAudio()} variant="compact" showTime />
),
decorators: [
(Story) => (
<div className="w-80">
<Story />
</div>
),
],
};
export const Inline: Story = {
render: () => (
<AudioPlayer src={getShortAudio()} variant="inline" showDuration />
),
};
export const InlineWithTitle: Story = {
render: () => (
<AudioPlayer
src={getSampleAudio()}
variant="inline"
title="Voice Message"
showDuration
/>
),
};
export const Compact: Story = {
render: () => (
<div className="w-80">
<AudioPlayer src={getSampleAudio()} variant="compact" showTime />
</div>
),
};
export const CompactWithPlaybackRate: Story = {
render: () => (
<div className="w-96">
<AudioPlayer
src={getSampleAudio()}
variant="compact"
showTime
showPlaybackRate
/>
</div>
),
};
export const Waveform: Story = {
render: () => (
<div className="w-96">
<AudioPlayer src={getSampleAudio()} variant="waveform" showTime />
</div>
),
};
export const WaveformWithTitle: Story = {
render: () => (
<div className="w-[500px]">
<AudioPlayer
src={getLongAudio()}
variant="waveform"
title="Meeting Recording - January 2026"
showTime
showPlaybackRate
/>
</div>
),
};
// ============================================================================
// Full Variant (Waveform + Progress Bar)
// ============================================================================
/**
* The Full variant combines a waveform visualization with a progress bar scrubber.
* Both are kept in sync - clicking on either one seeks to that position.
*/
export const Full: Story = {
render: () => (
<div className="w-96">
<AudioPlayer src={getSampleAudio()} variant="full" showTime />
</div>
),
};
/**
* Full variant with title and playback rate control.
*/
export const FullWithTitle: Story = {
render: () => (
<div className="w-[500px]">
<AudioPlayer
src={getLongAudio()}
variant="full"
title="Podcast Episode - Tech Talk"
showTime
showPlaybackRate
/>
</div>
),
};
/**
* Full variant with custom waveform colors.
*/
export const FullCustomColors: Story = {
render: () => (
<div className="w-[500px]">
<AudioPlayer
src={getSampleAudio()}
variant="full"
title="Custom Styled Audio"
showTime
showPlaybackRate
waveColor="#cbd5e1"
progressColor="#8b5cf6"
/>
</div>
),
};
// ============================================================================
// Size Variants
// ============================================================================
export const Sizes: Story = {
render: () => (
<div className="space-y-6">
<div className="space-y-2">
<Text weight="medium">Small</Text>
<div className="w-64">
<AudioPlayer
src={getSampleAudio()}
variant="compact"
size="sm"
showTime
/>
</div>
</div>
<div className="space-y-2">
<Text weight="medium">Medium (default)</Text>
<div className="w-72">
<AudioPlayer
src={getSampleAudio()}
variant="compact"
size="md"
showTime
/>
</div>
</div>
<div className="space-y-2">
<Text weight="medium">Large</Text>
<div className="w-80">
<AudioPlayer
src={getSampleAudio()}
variant="compact"
size="lg"
showTime
/>
</div>
</div>
</div>
),
};
export const InlineSizes: Story = {
render: () => (
<div className="space-y-4">
<div className="flex items-center gap-4">
<Text size="sm" className="w-16">
Small
</Text>
<AudioPlayer
src={getShortAudio()}
variant="inline"
size="sm"
title="Audio"
showDuration
/>
</div>
<div className="flex items-center gap-4">
<Text size="sm" className="w-16">
Medium
</Text>
<AudioPlayer
src={getShortAudio()}
variant="inline"
size="md"
title="Audio"
showDuration
/>
</div>
<div className="flex items-center gap-4">
<Text size="sm" className="w-16">
Large
</Text>
<AudioPlayer
src={getShortAudio()}
variant="inline"
size="lg"
title="Audio"
showDuration
/>
</div>
</div>
),
};
// ============================================================================
// Custom Colors (Waveform)
// ============================================================================
export const CustomWaveformColors: Story = {
render: () => (
<div className="w-96">
<AudioPlayer
src={getSampleAudio()}
variant="waveform"
showTime
waveColor="#e2e8f0"
progressColor="#22c55e"
/>
</div>
),
};
// ============================================================================
// Real-World Use Cases
// ============================================================================
export const VoiceNotesList: Story = {
render: () => {
const voiceNotes = [
{
id: '1',
title: 'Meeting Notes',
timestamp: '2 hours ago',
duration: '1:45',
},
{
id: '2',
title: 'Quick Reminder',
timestamp: 'Yesterday',
duration: '0:32',
},
{
id: '3',
title: 'Project Ideas',
timestamp: '2 days ago',
duration: '3:15',
},
];
return (
<Card className="w-96">
<CardHeader>
<CardTitle>Voice Notes</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
{voiceNotes.map((note) => (
<div
key={note.id}
className="flex items-center gap-3 rounded-lg border border-neutral-200 p-3 dark:border-neutral-700"
>
<AudioPlayer src={getSampleAudio()} variant="inline" size="sm" />
<div className="min-w-0 flex-1">
<Text size="sm" weight="medium" truncate>
{note.title}
</Text>
<Text size="xs" variant="muted">
{note.timestamp} • {note.duration}
</Text>
</div>
</div>
))}
</CardContent>
</Card>
);
},
};
export const ChatMessage: Story = {
render: () => (
<div className="w-80 space-y-3">
{/* Received message */}
<div className="flex gap-2">
<Avatar name="Jane Smith" size="sm" />
<div className="space-y-1">
<div className="rounded-2xl rounded-tl-sm bg-neutral-100 px-3 py-2 dark:bg-neutral-800">
<AudioPlayer
src={getSampleAudio()}
variant="compact"
size="sm"
showTime
/>
</div>
<Text size="xs" variant="muted">
2:34 PM
</Text>
</div>
</div>
{/* Sent message */}
<div className="flex flex-row-reverse gap-2">
<Avatar name="You" size="sm" />
<div className="space-y-1">
<div className="bg-primary-600 rounded-2xl rounded-tr-sm px-3 py-2">
<AudioPlayer
src={getShortAudio()}
variant="compact"
size="sm"
showTime
className="[&_button]:bg-white/20 [&_button]:text-white [&_div]:bg-white/30 [&_div>div]:bg-white [&_span]:text-white/80"
/>
</div>
<Text size="xs" variant="muted" className="text-right">
2:35 PM
</Text>
</div>
</div>
</div>
),
};
export const PodcastPlayer: Story = {
render: () => (
<Card className="w-[500px]">
<CardContent className="pt-6">
<div className="flex gap-4">
<div className="bg-primary-500 h-24 w-24 shrink-0 overflow-hidden rounded-lg">
<div className="flex h-full items-center justify-center text-2xl text-white">
🎙️
</div>
</div>
<div className="min-w-0 flex-1">
<Badge variant="secondary" size="sm" className="mb-2">
Episode 42
</Badge>
<Text weight="semibold" className="line-clamp-2">
The Future of Voice Interfaces in Healthcare
</Text>
<Text size="sm" variant="muted" className="mt-1">
Tech Health Podcast • 45 min
</Text>
</div>
</div>
<div className="mt-4">
<AudioPlayer
src={getLongAudio()}
variant="waveform"
showTime
showPlaybackRate
waveformHeight={48}
/>
</div>
</CardContent>
</Card>
),
};
export const AudioAttachment: Story = {
render: () => (
<div className="w-72">
<div className="rounded-lg border border-neutral-200 p-3 dark:border-neutral-700">
<div className="mb-2 flex items-center gap-2">
<div className="bg-primary-100 dark:bg-primary-900/30 flex h-8 w-8 items-center justify-center rounded">
<svg
className="text-primary-600 dark:text-primary-400 h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M9 19V6l12-3v13M9 19c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zm12-3c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zM9 10l12-3"
/>
</svg>
</div>
<div className="min-w-0 flex-1">
<Text size="sm" weight="medium" truncate>
voice-memo-2026-01-21.mp3
</Text>
<Text size="xs" variant="muted">
1.2 MB
</Text>
</div>
</div>
<AudioPlayer
src={getSampleAudio()}
variant="compact"
size="sm"
showTime
/>
</div>
</div>
),
};
// ============================================================================
// States
// ============================================================================
export const Disabled: Story = {
render: () => (
<div className="w-80">
<AudioPlayer src={getSampleAudio()} variant="compact" showTime disabled />
</div>
),
};
// ============================================================================
// All Variants Overview
// ============================================================================
export const AllVariants: Story = {
render: () => (
<div className="space-y-8">
<div className="space-y-2">
<Text as="h3" weight="semibold">
Inline Variant
</Text>
<Text size="sm" variant="muted" className="mb-4">
Minimal footprint - perfect for lists and inline contexts
</Text>
<div className="flex flex-wrap gap-4">
<AudioPlayer src={getShortAudio()} variant="inline" showDuration />
<AudioPlayer
src={getShortAudio()}
variant="inline"
title="Voice Note"
showDuration
/>
</div>
</div>
<div className="space-y-2">
<Text as="h3" weight="semibold">
Compact Variant
</Text>
<Text size="sm" variant="muted" className="mb-4">
Progress bar with time - great for messages and attachments
</Text>
<div className="w-80">
<AudioPlayer src={getSampleAudio()} variant="compact" showTime />
</div>
</div>
<div className="space-y-2">
<Text as="h3" weight="semibold">
Waveform Variant
</Text>
<Text size="sm" variant="muted" className="mb-4">
Full waveform visualization - ideal for podcasts and detailed playback
</Text>
<div className="w-96">
<AudioPlayer
src={getSampleAudio()}
variant="waveform"
title="Audio Recording"
showTime
showPlaybackRate
/>
</div>
</div>
</div>
),
};