Skip to content

Commit

Permalink
Android/IOS speech volume & pitch
Browse files Browse the repository at this point in the history
  • Loading branch information
dlutton committed Apr 17, 2018
1 parent b7d8a45 commit 0a32ab3
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ build/
ios/.generated/
packages
pubspec.lock
.vscode
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog

## 0.0.3

- Adding IOS/Android speech pitch and volume

## 0.0.2

- Flutter formatting and fixing pubspec sdk versioning
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ A flutter text to speech plugin (Swift,Java)
- [x] get languages
- [x] set language
- [x] speech rate
- [x] speech volume
- [x] speech pitch

## Usage

Expand Down Expand Up @@ -50,6 +52,10 @@ List<dynamic> languages = await flutterTts.getLanguages;
await flutterTts.setLanguage("en-US");
await flutterTts.setRate(1.0);
await flutterTts.setVolume(1.0);
await flutterTts.setPitch(1.0);
```

### Listening for platform calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class FlutterTtsPlugin implements MethodCallHandler {
private final Activity activity;
private TextToSpeech tts;
String uuid;
Bundle bundle;

/**
* Plugin registration.
Expand All @@ -33,8 +34,8 @@ private FlutterTtsPlugin(Activity activity, MethodChannel channel) {
this.channel = channel;
this.channel.setMethodCallHandler(this);

bundle = new Bundle();
tts = new TextToSpeech(activity.getApplicationContext(), onInitListener);
uuid = UUID.randomUUID().toString();
};

private UtteranceProgressListener utteranceProgressListener = new UtteranceProgressListener() {
Expand Down Expand Up @@ -96,6 +97,12 @@ public void onMethodCall(MethodCall call, Result result) {
String rate = call.arguments.toString();
setRate(Float.parseFloat(rate));
result.success(1);
} else if (call.method.equals("setVolume")) {
String volume = call.arguments.toString();
setVolume(Float.parseFloat(volume), result);
} else if (call.method.equals("setPitch")) {
String pitch = call.arguments.toString();
setPitch(Float.parseFloat(pitch), result);
} else if (call.method.equals("setLanguage")){
String language = call.arguments.toString();
setLanguage(language, result);
Expand Down Expand Up @@ -125,6 +132,28 @@ void setLanguage(String language, Result result) {
if (checkLanguage(locale)) {
tts.setLanguage(locale);
result.success(1);
} else {
result.success(0);
}
}

void setVolume(float volume, Result result) {
if (volume >= 0.0F && volume <= 1.0F) {
bundle.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, volume);
result.success(1);
} else {
Log.e("error", "Invalid volume " + volume + " value - Range is from 0.0 to 1.0");
result.success(0);
}
}

void setPitch(float pitch, Result result) {
if (pitch >= 0.5F && pitch <= 2.0F) {
tts.setPitch(pitch);
result.success(1);
} else {
Log.e("error", "Invalid pitch " + pitch + " value - Range is from 0.5 to 2.0");
result.success(0);
}
}

Expand All @@ -137,7 +166,8 @@ void getLanguages(Result result) {
}

void speak(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, uuid);
uuid = UUID.randomUUID().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, bundle, uuid);
}

void stop() {
Expand Down
6 changes: 6 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ A flutter text to speech plugin (Swift,Java)
- [x] get languages
- [x] set language
- [x] speech rate
- [x] speech volume
- [x] speech pitch

## Usage

Expand Down Expand Up @@ -50,6 +52,10 @@ List<dynamic> languages = await flutterTts.getLanguages;
await flutterTts.setLanguage("en-US");
await flutterTts.setRate(1.0);
await flutterTts.setVolume(1.0);
await flutterTts.setPitch(1.0);
```

### Listening for platform calls
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
9705A1C41CF9048500538489 /* Embed Frameworks */,
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
98A16AC41D9BED755B0C5559 /* [CP] Embed Pods Frameworks */,
AEA3A1C207CFA922A52CD034 /* [CP] Copy Pods Resources */,
B78DF43B102A908C2B3F0CFF /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -286,7 +286,7 @@
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
AEA3A1C207CFA922A52CD034 /* [CP] Copy Pods Resources */ = {
B78DF43B102A908C2B3F0CFF /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
Expand Down
31 changes: 30 additions & 1 deletion ios/Classes/SwiftFlutterTtsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class SwiftFlutterTtsPlugin: NSObject, FlutterPlugin, AVSpeechSynthesizer
let synthesizer = AVSpeechSynthesizer()
var language: String = AVSpeechSynthesisVoice.currentLanguageCode()
var rate: Float = AVSpeechUtteranceDefaultSpeechRate
var volume: Float = 1.0
var pitch: Float = 1.0

var channel = FlutterMethodChannel()

Expand Down Expand Up @@ -37,6 +39,14 @@ public class SwiftFlutterTtsPlugin: NSObject, FlutterPlugin, AVSpeechSynthesizer
self.setRate(rate: Float(rate))
result(1)
break
case "setVolume":
let volume: Double = call.arguments as! Double
self.setVolume(volume: Float(volume), result: result)
break
case "setPitch":
let pitch: Double = call.arguments as! Double
self.setPitch(pitch: Float(pitch), result: result)
break
case "stop":
self.stop()
result(1)
Expand All @@ -53,6 +63,8 @@ public class SwiftFlutterTtsPlugin: NSObject, FlutterPlugin, AVSpeechSynthesizer
let utterance = AVSpeechUtterance(string: text)
utterance.voice = AVSpeechSynthesisVoice(language: self.language)
utterance.rate = self.rate
utterance.volume = self.volume
utterance.pitchMultiplier = self.pitch

self.synthesizer.speak(utterance)
}
Expand All @@ -63,7 +75,6 @@ public class SwiftFlutterTtsPlugin: NSObject, FlutterPlugin, AVSpeechSynthesizer
voices.append(voice.language)
}
if !(voices.contains(language)){
self.channel.invokeMethod("speak.onError", arguments: "Invalid language code - \(language)")
result(0)
} else {
self.language = language
Expand All @@ -75,6 +86,24 @@ public class SwiftFlutterTtsPlugin: NSObject, FlutterPlugin, AVSpeechSynthesizer
self.rate = rate
}

private func setVolume(volume: Float, result: FlutterResult) {
if (volume >= 0.0 && volume <= 1.0) {
self.volume = volume
result(1)
} else {
result(0)
}
}

private func setPitch(pitch: Float, result: FlutterResult) {
if (volume >= 0.5 && volume <= 2.0) {
self.pitch = pitch
result(1)
} else {
result(0)
}
}

private func stop() {
self.synthesizer.stopSpeaking(at: AVSpeechBoundary.immediate)
}
Expand Down
6 changes: 6 additions & 0 deletions lib/flutter_tts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class FlutterTts {
Future<dynamic> setRate(double rate) =>
_channel.invokeMethod('setRate', rate);

Future<dynamic> setVolume(double volume) =>
_channel.invokeMethod('setVolume', volume);

Future<dynamic> setPitch(double pitch) =>
_channel.invokeMethod('setPitch', pitch);

Future<dynamic> stop() => _channel.invokeMethod('stop');

Future<dynamic> pause() => _channel.invokeMethod('pause');
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_tts
description: A flutter plugin for Text to Speech
version: 0.0.2
version: 0.0.3
author: Daniel Lutton <eyedea32@gmail.com>
homepage: https://github.com/dlutton/flutter_tts

Expand Down

0 comments on commit 0a32ab3

Please sign in to comment.