-
Notifications
You must be signed in to change notification settings - Fork 7
/
VoiceToTextController.java
110 lines (80 loc) · 2.91 KB
/
VoiceToTextController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.github.kilianB.example.voiceToTextPlayback;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.github.kilianB.apis.googleTextToSpeech.GLanguage;
import com.github.kilianB.apis.googleTextToSpeech.GoogleTextToSpeech;
import com.github.kilianB.apis.googleTextToSpeech.GoogleTextToSpeechAdapter;
import com.github.kilianB.exception.SonosControllerException;
import com.github.kilianB.sonos.SonosDevice;
import com.github.kilianB.sonos.SonosDiscovery;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXTextField;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.util.StringConverter;
/**
* @author Kilian
*
*/
public class VoiceToTextController {
@FXML
private JFXTextField textToConvertField;
@FXML
private JFXButton announceBtn;
@FXML
private JFXComboBox<SonosDevice> speakerCbox;
private ObservableList<SonosDevice> devices = FXCollections.observableList(new ArrayList<>());
private NetworkFileProvider fileProvider;
/**
* @param fileProvider
*/
public VoiceToTextController(NetworkFileProvider fileProvider) {
this.fileProvider = Objects.requireNonNull(fileProvider);
}
@FXML
public void initialize() {
//Initialize combo box
speakerCbox.itemsProperty().set(devices);
speakerCbox.setConverter(new SonosDeviceStringConverter());
SonosDiscovery.discoverAsynch(3,device->{
devices.add(device);
});
//Convert and play back
announceBtn.setOnAction( e ->{
String text = textToConvertField.getText();
SonosDevice currentDevice = speakerCbox.getSelectionModel().getSelectedItem();
if(currentDevice != null && text != null && !text.isEmpty()) {
System.out.println("Start request");
GoogleTextToSpeech gtts = new GoogleTextToSpeech("mp3TempFiles/");
gtts.convertTextAsynch(text,GLanguage.English_GB,"Anounce",true,new GoogleTextToSpeechAdapter() {
@Override
public void mergeCompleted(File f,int id) {
System.out.println("Merge completed");
//We got the file we want to play back.
String uri = "http://"+fileProvider.toMappedPath(f.getAbsolutePath());
try {
currentDevice.clip(uri,null);
} catch (IOException | SonosControllerException | InterruptedException e) {
e.printStackTrace();
}
}
});
}
});
}
class SonosDeviceStringConverter extends StringConverter<SonosDevice>{
@Override
public String toString(SonosDevice object) {
return object.getRoomNameCached();
}
@Override
public SonosDevice fromString(String string) {
throw new UnsupportedOperationException("Can't construct device from string");
}
}
}