Files
cutdex/lib/features/media/audio_asset_tile.dart
T
lingjhf 38ca180cf7
Gitea CI / Check code and tests (pull_request) Failing after 8s
Gitea CI / Build Android APK (pull_request) Skipped
Gitea CI / Build Windows x64 app (pull_request) Skipped
Gitea CI / Audit and package Windows x64 (pull_request) Skipped
Gitea CI / Assemble and validate CI artifacts (pull_request) Skipped
Gitea CI / Gitea CI passed (pull_request) Failing after 1s
refactor(core): own editor UI and pin verified native releases
2026-10-12 03:20:29 +08:00

136 lines
4.1 KiB
Dart

import 'dart:math' as math;
import 'package:cutdex_core/project.dart';
import 'package:cutdex_core/timeline/model.dart';
import 'package:cutdex_timeline_ui/cutdex_timeline_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../integrations/runtime/media_presentations.dart';
import 'package:cutdex_ui/icons.dart';
import 'package:cutdex_ui/theme.dart';
class AudioAssetTile extends StatelessWidget {
const AudioAssetTile({
super.key,
required this.asset,
this.showName = true,
this.aspectRatio = 16 / 9,
});
final AssetDocument asset;
final bool showName;
final double aspectRatio;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
AspectRatio(
key: ValueKey('audio-cover-${asset.id}'),
aspectRatio: aspectRatio,
child: ColoredBox(
color: AppSurfaces.of(context).previewBackground,
child: _AudioWaveformCover(asset),
),
),
if (showName) ...[
const SizedBox(height: AppSpacing.small),
Tooltip(
message: asset.name,
child: Text(
asset.name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: theme.textTheme.bodySmall,
),
),
],
],
);
}
}
class _AudioWaveformCover extends ConsumerWidget {
const _AudioWaveformCover(this.asset);
final AssetDocument asset;
@override
Widget build(BuildContext context, WidgetRef ref) {
final waveform = ref.watch(mediaWaveformProvider(asset)).value;
final theme = Theme.of(context);
if (waveform == null) {
return Center(
child: Icon(
AppIcons.audioFile,
size: AppIconSize.emptyState,
color: theme.colorScheme.onSurfaceVariant,
),
);
}
return CustomPaint(
key: ValueKey('audio-waveform-${asset.id}'),
painter: _AudioCoverPainter(
waveform,
theme.extension<TimelineThemeData>()?.audioAccentColor ??
theme.colorScheme.primary,
MediaQuery.devicePixelRatioOf(context),
),
);
}
}
class _AudioCoverPainter extends CustomPainter {
const _AudioCoverPainter(this.waveform, this.color, this.devicePixelRatio);
final TimelineWaveform waveform;
final Color color;
final double devicePixelRatio;
@override
void paint(Canvas canvas, Size size) {
if (size.isEmpty) return;
final columns = math.max(1, (size.width / 2).floor());
final start = waveform.sourceStartSeconds * TimelineClock.ticksPerSecond;
final duration =
waveform.frameCount /
waveform.sampleRate *
TimelineClock.ticksPerSecond;
final paint = Paint()
..color = color
..style = PaintingStyle.fill
..isAntiAlias = false;
final strokePixels = math.max(1, devicePixelRatio.round());
final bottomPixels = math.max(1, (size.height * devicePixelRatio).floor());
for (var column = 0; column < columns; column++) {
final envelope = waveform.envelope(
(start + duration * column / columns).round(),
(start + duration * (column + 1) / columns).round(),
);
final peak = math.max(envelope.minimum.abs(), envelope.maximum.abs());
final height = math.min(size.height, math.max(1.0, peak * size.height));
final x = (column + 0.5) * size.width / columns;
final leftPixels = (x * devicePixelRatio - strokePixels / 2).round();
final topPixels = ((size.height - height) * devicePixelRatio)
.round()
.clamp(0, bottomPixels - 1);
canvas.drawRect(
Rect.fromLTWH(
leftPixels / devicePixelRatio,
topPixels / devicePixelRatio,
strokePixels / devicePixelRatio,
(bottomPixels - topPixels) / devicePixelRatio,
),
paint,
);
}
}
@override
bool shouldRepaint(_AudioCoverPainter oldDelegate) =>
oldDelegate.waveform != waveform ||
oldDelegate.color != color ||
oldDelegate.devicePixelRatio != devicePixelRatio;
}