Prefer natural voices for read aloud
Rank platform voice quality and select the most natural automatic voice while keeping explicit voice choices intact. Surface natural labels, hide novelty voices in the picker, and guide Apple users to download enhanced voices when needed.
This commit is contained in:
@@ -23,11 +23,13 @@ class SettingsReadAloudSection extends StatefulWidget {
|
||||
|
||||
class _SettingsReadAloudSectionState extends State<SettingsReadAloudSection> {
|
||||
SpeechController get _speech => widget.controller ?? speechController;
|
||||
late final Future<List<SpeechVoice>> _voices;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
unawaited(_speech.restore());
|
||||
_voices = _speech.voices();
|
||||
}
|
||||
|
||||
Future<void> _pickVoice() async {
|
||||
@@ -65,12 +67,13 @@ class _SettingsReadAloudSectionState extends State<SettingsReadAloudSection> {
|
||||
title: const Text('Voice'),
|
||||
subtitle: Text(
|
||||
voice == null
|
||||
? 'System default'
|
||||
? 'Automatic (most natural)'
|
||||
: '${voice.name} · ${voice.locale}',
|
||||
),
|
||||
chevron: true,
|
||||
onTap: () => unawaited(_pickVoice()),
|
||||
),
|
||||
_voiceDownloadHint(context),
|
||||
SettingsGroupItem(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
@@ -131,4 +134,38 @@ class _SettingsReadAloudSectionState extends State<SettingsReadAloudSection> {
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Widget _voiceDownloadHint(BuildContext context) {
|
||||
return FutureBuilder<List<SpeechVoice>>(
|
||||
future: _voices,
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData ||
|
||||
!needsSpeechVoiceHint(
|
||||
snapshot.data!,
|
||||
View.of(context).platformDispatcher.locale.languageCode,
|
||||
supportsVoiceDownloadHint:
|
||||
_speech.engine.supportsVoiceDownloadHint,
|
||||
)) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
const hint =
|
||||
'For a more natural voice, download one in System Settings > '
|
||||
'Accessibility > Spoken Content > System voice > Manage voices.';
|
||||
return SettingsGroupItem(
|
||||
key: const Key('settings-read-aloud-voice-hint'),
|
||||
child: Tooltip(
|
||||
message: hint,
|
||||
child: Text(
|
||||
hint,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,17 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import '../chat/speech/speech.dart';
|
||||
|
||||
bool needsSpeechVoiceHint(
|
||||
List<SpeechVoice> voices,
|
||||
String language, {
|
||||
bool supportsVoiceDownloadHint = false,
|
||||
}) {
|
||||
if (!supportsVoiceDownloadHint) return false;
|
||||
return voices
|
||||
.where((voice) => voice.language == language.toLowerCase())
|
||||
.every((voice) => !voice.isNatural);
|
||||
}
|
||||
|
||||
/// The picker's answer: a voice, or null for the system default.
|
||||
typedef SpeechVoiceChoice = ({SpeechVoice? voice});
|
||||
|
||||
@@ -22,7 +33,7 @@ Future<SpeechVoiceChoice?> showSpeechVoicePicker(
|
||||
}
|
||||
|
||||
/// Voices grouped by language subtag, the [device] language first and the
|
||||
/// rest by name; voices within a group by name.
|
||||
/// rest by name; natural voices and offline voices come first within a group.
|
||||
List<({String language, List<SpeechVoice> voices})> groupSpeechVoices(
|
||||
List<SpeechVoice> voices,
|
||||
String device,
|
||||
@@ -41,11 +52,7 @@ List<({String language, List<SpeechVoice> voices})> groupSpeechVoices(
|
||||
for (final language in languages)
|
||||
(
|
||||
language: language,
|
||||
voices: groups[language]!
|
||||
..sort((a, b) {
|
||||
final name = a.name.compareTo(b.name);
|
||||
return name != 0 ? name : a.locale.compareTo(b.locale);
|
||||
}),
|
||||
voices: groups[language]!..sort(compareSpeechVoices),
|
||||
),
|
||||
];
|
||||
}
|
||||
@@ -113,7 +120,10 @@ class _VoicePickerDialogState extends State<_VoicePickerDialog> {
|
||||
contentPadding: const EdgeInsets.symmetric(vertical: 16),
|
||||
// A list needs a definite width; keep it to a readable dialog measure.
|
||||
content: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 560),
|
||||
constraints: BoxConstraints(
|
||||
maxWidth: 560,
|
||||
maxHeight: MediaQuery.sizeOf(context).height * .6,
|
||||
),
|
||||
child: SizedBox(
|
||||
width: double.maxFinite,
|
||||
child: FutureBuilder<List<SpeechVoice>>(
|
||||
@@ -122,6 +132,17 @@ class _VoicePickerDialogState extends State<_VoicePickerDialog> {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Text(
|
||||
'Voices could not be loaded. You can cancel and try again.',
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
final voices = snapshot.data ?? const <SpeechVoice>[];
|
||||
return _list(context, groupSpeechVoices(voices, device));
|
||||
},
|
||||
@@ -154,9 +175,12 @@ class _VoicePickerDialogState extends State<_VoicePickerDialog> {
|
||||
Navigator.of(context).pop((voice: voice));
|
||||
},
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
primary: false,
|
||||
children: [
|
||||
const RadioListTile<String>(value: '', title: Text('System default')),
|
||||
const RadioListTile<String>(
|
||||
value: '',
|
||||
title: Text('Automatic (most natural)'),
|
||||
),
|
||||
if (groups.isEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
|
||||
@@ -181,7 +205,23 @@ class _VoicePickerDialogState extends State<_VoicePickerDialog> {
|
||||
Expanded(
|
||||
child: RadioListTile<String>(
|
||||
value: _keyOf(voice),
|
||||
title: Text(voice.name),
|
||||
title: Row(
|
||||
children: [
|
||||
Flexible(child: Text(voice.name)),
|
||||
if (voice.isNatural)
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.only(
|
||||
start: 8,
|
||||
),
|
||||
child: Text(
|
||||
'Natural',
|
||||
style: theme.textTheme.labelSmall?.copyWith(
|
||||
color: theme.colorScheme.primary,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
subtitle: Text(voice.locale),
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user