Files
cutdex/lib/features/media/audio_asset_tile.dart
T
lingjhf 27e77fff39 feat: initial Cutdex application
Add the Flutter video editing application with project management, media
import, timeline editing, preview, AI assistance, diagnostics and settings.

Include shared UI and native host packages, platform runners, application
tests and release tooling.

Provide usage and contribution guides, the MIT license and third-party
notices. Exclude local credentials, build outputs and validation data.
2026-10-05 23:36:23 +08:00

135 lines
4.1 KiB
Dart

import 'dart:math' as math;
import 'package:cutdex_sdk/project.dart';
import 'package:cutdex_sdk/timeline/model.dart';
import 'package:cutdex_sdk/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/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(
Icons.audio_file_outlined,
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;
}