Skip to content

Commit

Permalink
[flutter_tts] Update flutter_tts to 3.6.3 (flutter-tizen#582)
Browse files Browse the repository at this point in the history
Co-authored-by: Swift Kim <[email protected]>
  • Loading branch information
JSUYA and swift-kim authored Jun 1, 2023
1 parent 4895559 commit 3f5dd7e
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 20 deletions.
5 changes: 4 additions & 1 deletion packages/flutter_tts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## NEXT
## 1.4.0

* Update flutter_tts to 3.6.3.
* Update the example app.
* Add support for `getDefaultVoice` and `getMaxSpeechInputLength`.
* Increase the minimum Flutter version to 3.3.

## 1.3.0
Expand Down
6 changes: 4 additions & 2 deletions packages/flutter_tts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ This package is not an _endorsed_ implementation of `flutter_tts`. Therefore, yo

```yaml
dependencies:
flutter_tts: ^3.5.0
flutter_tts_tizen: ^1.3.0
flutter_tts: ^3.6.3
flutter_tts_tizen: ^1.4.0
```
Then you can import `flutter_tts` in your Dart code:
Expand All @@ -33,3 +33,5 @@ The features supported by Tizen are as follows. Other features are not supported
- [x] set language
- [x] set speech rate
- [x] set speech volume (requires privilege `http://tizen.org/privilege/volume.set` in `tizen_manifest.xml`)
- [x] get default voice
- [x] get max speech input length
62 changes: 49 additions & 13 deletions packages/flutter_tts/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';

void main() {
runApp(MyApp());
}
void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
Expand All @@ -20,7 +18,10 @@ class _MyAppState extends State<MyApp> {
double volume = 0.5;
double pitch = 1.0;
double rate = 0.5;

String? _newVoiceText;
int? _inputLength;

TtsState ttsState = TtsState.stopped;

get isPlaying => ttsState == TtsState.playing;
Expand All @@ -29,7 +30,7 @@ class _MyAppState extends State<MyApp> {
get isContinued => ttsState == TtsState.continued;

@override
void initState() {
initState() {
super.initState();
initTts();
}
Expand All @@ -39,6 +40,8 @@ class _MyAppState extends State<MyApp> {

_setAwaitOptions();

_getDefaultVoice();

flutterTts.setStartHandler(() {
setState(() {
print("Playing");
Expand Down Expand Up @@ -84,6 +87,13 @@ class _MyAppState extends State<MyApp> {

Future<dynamic> _getLanguages() => flutterTts.getLanguages;

Future _getDefaultVoice() async {
var voice = await flutterTts.getDefaultVoice;
if (voice != null) {
print(voice);
}
}

Future _speak() async {
await flutterTts.setVolume(volume);
await flutterTts.setSpeechRate(rate);
Expand Down Expand Up @@ -146,13 +156,17 @@ class _MyAppState extends State<MyApp> {
title: const Text('Flutter TTS Tizen example app'),
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(children: [
scrollDirection: Axis.vertical,
child: Column(
children: [
_inputSection(),
_btnSection(),
_futureBuilder(),
_buildSliders()
])),
_buildSliders(),
_getMaxSpeechInputLengthSection(),
],
),
),
),
);
}
Expand All @@ -172,32 +186,38 @@ class _MyAppState extends State<MyApp> {
alignment: Alignment.topCenter,
padding: EdgeInsets.only(top: 25.0, left: 25.0, right: 25.0),
child: TextField(
maxLines: 11,
minLines: 6,
onChanged: (String value) {
_onChange(value);
},
));

Widget _btnSection() {
return Container(
padding: EdgeInsets.only(top: 50.0),
child: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
padding: EdgeInsets.only(top: 50.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildButtonColumn(Colors.green, Colors.greenAccent, Icons.play_arrow,
'PLAY', _speak),
_buildButtonColumn(
Colors.red, Colors.redAccent, Icons.stop, 'STOP', _stop),
_buildButtonColumn(
Colors.blue, Colors.blueAccent, Icons.pause, 'PAUSE', _pause),
]));
],
),
);
}

Widget _languageDropDownSection(dynamic languages) => Container(
padding: EdgeInsets.only(top: 50.0),
padding: EdgeInsets.only(top: 10.0),
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
DropdownButton(
value: language,
items: getLanguageDropDownMenuItems(languages),
onChanged: changedLanguageDropDownItem,
)
),
]));

Column _buildButtonColumn(Color color, Color splashColor, IconData icon,
Expand All @@ -221,6 +241,22 @@ class _MyAppState extends State<MyApp> {
]);
}

Widget _getMaxSpeechInputLengthSection() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
child: Text('Get max speech input length'),
onPressed: () async {
_inputLength = await flutterTts.getMaxSpeechInputLength;
setState(() {});
},
),
Text("$_inputLength characters"),
],
);
}

Widget _buildSliders() {
return Column(
children: [_volume(), _rate()],
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tts/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_tts: ^3.5.0
flutter_tts: ^3.6.3
flutter_tts_tizen:
path: ../

Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tts/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_tts_tizen
description: The Tizen implementation of flutter_tts plugin.
homepage: https://github.com/flutter-tizen/plugins
repository: https://github.com/flutter-tizen/plugins/tree/master/packages/flutter_tts
version: 1.3.0
version: 1.4.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
26 changes: 26 additions & 0 deletions packages/flutter_tts/tizen/src/flutter_tts_tizen_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
OnSetLanguage(arguments);
} else if (method_name == "getLanguages") {
OnGetLanguage();
} else if (method_name == "getDefaultVoice") {
OnGetDefaultVoice();
} else if (method_name == "getMaxSpeechInputLength") {
OnGetMaxSpeechInputLength();
} else if (method_name == "setVolume") {
OnSetVolume(arguments);
} else {
Expand Down Expand Up @@ -199,6 +203,28 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
SendResult(flutter::EncodableValue(list));
}

void OnGetDefaultVoice() {
std::optional<std::pair<std::string, std::string>> default_voice =
tts_->GetDefaultVoice();
if (!default_voice.has_value()) {
SendResult(flutter::EncodableValue(flutter::EncodableMap()));
return;
}

flutter::EncodableMap default_voice_map = {
{flutter::EncodableValue("locale"),
flutter::EncodableValue(default_voice->first)},
{flutter::EncodableValue("name"),
flutter::EncodableValue(default_voice->second)}};
SendResult(flutter::EncodableValue(default_voice_map));
}

void OnGetMaxSpeechInputLength() {
std::optional<int32_t> length = tts_->GetMaxSpeechInputLength();
SendResult(length.has_value() ? flutter::EncodableValue(*length)
: flutter::EncodableValue());
}

void OnSetVolume(const flutter::EncodableValue &arguments) {
if (std::holds_alternative<double>(arguments)) {
double volume = std::get<double>(arguments);
Expand Down
51 changes: 49 additions & 2 deletions packages/flutter_tts/tizen/src/text_to_speech.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool TextToSpeech::Initialize() {

void TextToSpeech::Prepare() {
char *language = nullptr;
int32_t voice_type = 0;
int voice_type = 0;
int ret = tts_get_default_voice(tts_, &language, &voice_type);
if (ret == TTS_ERROR_NONE) {
default_language_ = language;
Expand Down Expand Up @@ -131,6 +131,53 @@ std::vector<std::string> &TextToSpeech::GetSupportedLanaguages() {
return supported_lanaguages_;
}

std::optional<std::pair<std::string, std::string>>
TextToSpeech::GetDefaultVoice() {
char *language = nullptr;
int voice_type = 0;
int ret = tts_get_default_voice(tts_, &language, &voice_type);

if (ret != TTS_ERROR_NONE) {
LOG_ERROR("tts_get_default_voice failed: %s", get_error_message(ret));
return std::nullopt;
}

std::pair<std::string, std::string> default_voice;
if (language) {
default_voice.first = language;
free(language);
}

switch (voice_type) {
case TTS_VOICE_TYPE_AUTO:
default_voice.second = "auto";
break;
case TTS_VOICE_TYPE_MALE:
default_voice.second = "male";
break;
case TTS_VOICE_TYPE_FEMALE:
default_voice.second = "female";
break;
case TTS_VOICE_TYPE_CHILD:
default_voice.second = "child";
break;
default:
default_voice.second = "unknown";
break;
}
return default_voice;
}

std::optional<int32_t> TextToSpeech::GetMaxSpeechInputLength() {
unsigned int size = 0;
int ret = tts_get_max_text_size(tts_, &size);
if (ret != TTS_ERROR_NONE) {
LOG_ERROR("tts_get_max_text_size failed: %s", get_error_message(ret));
return std::nullopt;
}
return static_cast<int32_t>(size);
}

std::optional<TtsState> TextToSpeech::GetState() {
tts_state_e state;
int ret = tts_get_state(tts_, &state);
Expand Down Expand Up @@ -214,7 +261,7 @@ void TextToSpeech::SwitchVolumeOnStateChange(tts_state_e previous,
}

int32_t TextToSpeech::GetSpeechVolumeInternal() {
int32_t volume;
int volume;
int ret = sound_manager_get_volume(SOUND_TYPE_VOICE, &volume);
if (ret != SOUND_MANAGER_ERROR_NONE) {
LOG_ERROR("sound_manager_get_volume failed: %s", get_error_message(ret));
Expand Down
4 changes: 4 additions & 0 deletions packages/flutter_tts/tizen/src/text_to_speech.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class TextToSpeech {

std::vector<std::string> &GetSupportedLanaguages();

std::optional<std::pair<std::string, std::string>> GetDefaultVoice();

std::optional<int32_t> GetMaxSpeechInputLength();

std::optional<TtsState> GetState();

bool AddText(const std::string &text);
Expand Down

0 comments on commit 3f5dd7e

Please sign in to comment.