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:
2026-11-13 15:59:35 +00:00
parent eeb07e5645
commit 3164b7c8df
10 changed files with 424 additions and 35 deletions
@@ -4,16 +4,26 @@ import 'package:pipelines_app/features/chat/speech/speech.dart';
/// A scripted [SpeechEngine]: it records calls and a test fires the platform's
/// events. [stop] reports a cancel like every real platform does.
class FakeSpeechEngine extends SpeechEngine {
FakeSpeechEngine({this.supported = true, this.available = const []});
FakeSpeechEngine({
this.supported = true,
this.available = const [],
this.voiceDownloadHint = false,
this.voiceFailure = false,
});
@override
final bool supported;
final List<SpeechVoice> available;
final bool voiceDownloadHint;
final bool voiceFailure;
final calls = <String>[];
final spoken = <String>[];
double? rate;
SpeechVoice? voice;
@override
bool get supportsVoiceDownloadHint => voiceDownloadHint;
@override
Future<void> speak(String text) async {
calls.add('speak');
@@ -36,7 +46,10 @@ class FakeSpeechEngine extends SpeechEngine {
Future<void> setVoice(SpeechVoice? voice) async => this.voice = voice;
@override
Future<List<SpeechVoice>> voices() async => available;
Future<List<SpeechVoice>> voices() async {
if (voiceFailure) throw StateError('voice inventory failed');
return available;
}
/// Reports [word] (its first occurrence at or after [from]) being spoken.
void speakWord(String word, {int from = 0}) {
@@ -165,4 +165,37 @@ void main() {
expect(speech.rate, defaultSpeechRate);
expect(store.saved, (rate: defaultSpeechRate, voice: null));
});
testWidgets('an explicitly selected voice does not require voice inventory', (
tester,
) async {
engine = FakeSpeechEngine(voiceFailure: true);
reader();
const voice = SpeechVoice(name: 'Daniel', locale: 'en-GB');
// The saved selection is passed directly to the engine, even if querying
// the platform voice inventory is unavailable.
await speech.setVoice(voice);
await speech.read('a', ['Hello']);
expect(engine.voice, voice);
expect(engine.spoken, ['Hello']);
});
testWidgets(
'automatic voice selects the most natural device-language voice',
(tester) async {
engine = FakeSpeechEngine(
available: const [
SpeechVoice(name: 'Compact', locale: 'en-US', qualityRank: 1),
SpeechVoice(name: 'Enhanced', locale: 'en-US', qualityRank: 2),
SpeechVoice(name: 'French Natural', locale: 'fr-FR', qualityRank: 3),
],
);
reader();
await speech.read('a', ['Hello']);
expect(engine.voice?.name, 'Enhanced');
expect(speech.voice, isNull);
},
);
}
@@ -0,0 +1,59 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/speech/speech.dart';
void main() {
test('parses Apple quality and identifier without changing identity', () {
final voice = SpeechVoice.fromPlatform({
'name': 'Samantha',
'locale': 'en-US',
'quality': 'premium',
'identifier': 'com.apple.voice.premium.en-US.Samantha',
});
expect(voice.qualityRank, 3);
expect(voice.isNatural, isTrue);
expect(voice, const SpeechVoice(name: 'Samantha', locale: 'en-US'));
});
test('parses Android quality and prefers offline on a quality tie', () {
final online = SpeechVoice.fromPlatform({
'name': 'English online',
'locale': 'en-US',
'quality': 'very high',
'network_required': '1',
});
final offline = SpeechVoice.fromPlatform({
'name': 'English local',
'locale': 'en-US',
'quality': 'very high',
'network_required': '0',
});
expect(bestSpeechVoice([online, offline], 'en'), offline);
});
test('recognises natural web voice names', () {
final voice = SpeechVoice.fromPlatform({
'name': 'Microsoft Aria Online (Natural)',
'locale': 'en-US',
});
expect(voice.qualityRank, 3);
expect(voice.isNatural, isTrue);
});
test('picker ranking buries compact novelty voices', () {
final natural = const SpeechVoice(
name: 'Samantha Enhanced',
locale: 'en-US',
qualityRank: 2,
);
final novelty = const SpeechVoice(
name: 'Zarvox',
locale: 'en-US',
qualityRank: 1,
);
expect(compareSpeechVoices(natural, novelty), lessThan(0));
});
}