From 08f48c055b24e6ce4b147810793e7bcf20d3bb7b Mon Sep 17 00:00:00 2001 From: lensferno Date: Sat, 12 Mar 2022 13:47:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=AF=E7=B4=AF=E6=80=A7=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=88=E4=B8=80=E8=82=A1ms=E5=91=B3?= =?UTF-8?q?=EF=BC=89=200.=20=E6=96=B0=E5=A2=9E=E8=AF=AD=E8=A8=80=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=B8=85=E9=99=A4=E5=8A=9F=E8=83=BD=201.=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=AF=AD=E9=9F=B3=E9=9F=B3=E9=A2=91=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E9=80=89=E6=8B=A9=202.=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E8=AE=BE=E7=BD=AE=E4=B8=8D=E7=94=9F=E6=95=88?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=203.=20=E5=9B=A0=E7=99=BE=E5=BA=A6?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E5=90=88=E6=88=90=E7=9A=84=E9=99=90=E5=88=B6?= =?UTF-8?q?=EF=BC=8C=E5=B1=8F=E8=94=BD=E9=83=A8=E5=88=86=E8=AF=AD=E9=9F=B3?= =?UTF-8?q?=E5=BA=93=204.=20=E5=85=B6=E4=BB=96=E7=9A=84=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=B0=8F=E4=BF=AE=E5=B0=8F=E6=94=B9=E4=B8=8E=E4=BC=98=20?= =?UTF-8?q?=E5=8C=96=205.=20=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=B7=B2=E5=9C=A8=E8=AE=A1=E5=88=92=E4=B8=AD?= =?UTF-8?q?=EF=BC=88=E5=88=92=E6=8E=89=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dogename/configs/ConfigLoader.java | 53 +++---- .../dogename/configs/ConfigValuesBean.java | 4 - .../dogename/configs/VoiceConfig.java | 64 +++++--- .../controllers/MainInterfaceController.java | 9 +- .../controllers/SettingsPaneController.java | 14 +- .../VoiceSettingsPaneController.java | 86 +++++++---- .../me/lensferno/dogename/data/History.java | 2 +- .../dogename/select/core/Worker.java | 1 + .../lensferno/dogename/utils/DialogMaker.java | 13 +- .../lensferno/dogename/voice/VoicePlayer.java | 53 ++++--- .../FXMLs/AdvancedVoiceSettingsPane.fxml | 53 ------- .../dogename/FXMLs/MainInterface.fxml | 2 +- .../dogename/FXMLs/NameManagerPane.fxml | 2 +- .../dogename/FXMLs/VoiceSettingsPane.fxml | 139 ++++++++++-------- 14 files changed, 256 insertions(+), 239 deletions(-) delete mode 100644 Dogename/src/main/java/me/lensferno/dogename/configs/ConfigValuesBean.java delete mode 100644 Dogename/src/main/resources/me/lensferno/dogename/FXMLs/AdvancedVoiceSettingsPane.fxml diff --git a/Dogename/src/main/java/me/lensferno/dogename/configs/ConfigLoader.java b/Dogename/src/main/java/me/lensferno/dogename/configs/ConfigLoader.java index 1a0655f..6794aec 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/configs/ConfigLoader.java +++ b/Dogename/src/main/java/me/lensferno/dogename/configs/ConfigLoader.java @@ -11,19 +11,17 @@ import me.lensferno.dogename.configs.adapters.DoublePropertyAdapter; import me.lensferno.dogename.configs.adapters.IntegerPropertyAdapter; import me.lensferno.dogename.configs.adapters.StringPropertyAdapter; import me.lensferno.dogename.utils.FilePath; +import me.lensferno.dogename.voice.VoicePlayer; import org.apache.commons.io.IOUtils; import java.io.*; import java.nio.charset.StandardCharsets; public class ConfigLoader { - - private final String mainConfigLocation = FilePath.toSpecificPathForm("files/Config.json"); private final String voiceConfigLocation = FilePath.toSpecificPathForm("files/VoiceConfig.json"); //ConfigValuesBean config; private MainConfig mainConfig; - private VoiceConfig voiceConfig; public String getMainConfigLocation() { return mainConfigLocation; @@ -38,7 +36,6 @@ public class ConfigLoader { } public MainConfig readConfigFromFile(String fileLocation) { - //property属性应该要自定义一个json适配器才能解析出来 Gson gson = new GsonBuilder() .registerTypeAdapter(SimpleBooleanProperty.class, new BooleanPropertyAdapter()) @@ -48,7 +45,7 @@ public class ConfigLoader { .setPrettyPrinting() .create(); - String ConfigJSON; + String configJson; try { File configFile = new File(fileLocation); @@ -60,9 +57,10 @@ public class ConfigLoader { return mainConfig; } InputStream inputStream = new FileInputStream(configFile); - ConfigJSON = IOUtils.toString(inputStream, StandardCharsets.UTF_8); + configJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8); + inputStream.close(); - mainConfig = gson.fromJson(ConfigJSON, MainConfig.class); + mainConfig = gson.fromJson(configJson, MainConfig.class); if (mainConfig == null) { mainConfig = new MainConfig(); @@ -82,7 +80,6 @@ public class ConfigLoader { } public VoiceConfig readVoiceConfigFromFile(String fileLocation) { - //property属性应该要自定义一个json适配器才能解析出来 Gson gson = new GsonBuilder() .registerTypeAdapter(SimpleBooleanProperty.class, new BooleanPropertyAdapter()) @@ -92,7 +89,7 @@ public class ConfigLoader { .setPrettyPrinting() .create(); - String ConfigJSON; + String configJson; try { File configFile = new File(fileLocation); @@ -100,38 +97,31 @@ public class ConfigLoader { configFile.getParentFile().mkdirs(); configFile.createNewFile(); - voiceConfig = new VoiceConfig(); + VoicePlayer.voiceConfig = new VoiceConfig(); writeVoiceConfigToFile(voiceConfigLocation); - return voiceConfig; + return VoicePlayer.voiceConfig; } InputStream inputStream = new FileInputStream(configFile); - ConfigJSON = IOUtils.toString(inputStream, StandardCharsets.UTF_8); + configJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8); + inputStream.close(); - writeVoiceConfigToFile(voiceConfigLocation); - voiceConfig = gson.fromJson(ConfigJSON, VoiceConfig.class); - if (voiceConfig == null) { - voiceConfig = new VoiceConfig(); + VoicePlayer.voiceConfig = gson.fromJson(configJson, VoiceConfig.class); + + if (VoicePlayer.voiceConfig == null) { + VoicePlayer.voiceConfig = new VoiceConfig(); writeVoiceConfigToFile(voiceConfigLocation); - return voiceConfig; + return VoicePlayer.voiceConfig; } } catch (Exception e) { System.out.println("Error to load voice config file:" + e + "\nUse Default voice config."); - voiceConfig = new VoiceConfig(); + VoicePlayer.voiceConfig = new VoiceConfig(); writeVoiceConfigToFile(voiceConfigLocation); - return voiceConfig; + return VoicePlayer.voiceConfig; } - return this.voiceConfig; - } - - // - public MainConfig setValuesToProperty() { - //mainconfig.set..(config.get..) - //...so on - // - return this.mainConfig; + return VoicePlayer.voiceConfig; } private String toJSON(MainConfig config) { @@ -146,8 +136,7 @@ public class ConfigLoader { return gson.toJson(config); } - private String VoiceConfigtoJSON(VoiceConfig config) { - + private String getConfigJson(VoiceConfig config) { Gson gson = new GsonBuilder() .registerTypeAdapter(SimpleDoubleProperty.class, new DoublePropertyAdapter()) .setPrettyPrinting() @@ -168,7 +157,7 @@ public class ConfigLoader { IOUtils.write(toJSON(this.mainConfig).getBytes(StandardCharsets.UTF_8), stream); OutputStream voiceConfigFileStream = new FileOutputStream(voiceConfigFile); - IOUtils.write(VoiceConfigtoJSON(this.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream); + IOUtils.write(getConfigJson(VoicePlayer.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream); } catch (Exception e) { System.out.println("Error in writing all config:" + e); @@ -202,7 +191,7 @@ public class ConfigLoader { outputFile.createNewFile(); } OutputStream voiceConfigFileStream = new FileOutputStream(voiceConfigFile); - IOUtils.write(VoiceConfigtoJSON(this.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream); + IOUtils.write(getConfigJson(VoicePlayer.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream); } catch (Exception e) { System.out.println("Error in writing voice config:" + e); diff --git a/Dogename/src/main/java/me/lensferno/dogename/configs/ConfigValuesBean.java b/Dogename/src/main/java/me/lensferno/dogename/configs/ConfigValuesBean.java deleted file mode 100644 index b5aa7aa..0000000 --- a/Dogename/src/main/java/me/lensferno/dogename/configs/ConfigValuesBean.java +++ /dev/null @@ -1,4 +0,0 @@ -package me.lensferno.dogename.configs; - -public class ConfigValuesBean { -} diff --git a/Dogename/src/main/java/me/lensferno/dogename/configs/VoiceConfig.java b/Dogename/src/main/java/me/lensferno/dogename/configs/VoiceConfig.java index e39c408..e9d9b6f 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/configs/VoiceConfig.java +++ b/Dogename/src/main/java/me/lensferno/dogename/configs/VoiceConfig.java @@ -1,43 +1,51 @@ package me.lensferno.dogename.configs; +import com.google.gson.annotations.Expose; import javafx.beans.property.SimpleDoubleProperty; -import javafx.event.ActionEvent; -import javafx.fxml.FXML; +import javafx.beans.property.SimpleIntegerProperty; +import javafx.beans.property.SimpleStringProperty; public class VoiceConfig { + @Expose + public static final int DEFAULT_SPEED = 5; + @Expose + public static final int DEFAULT_INTONATION = 5; - public final int DEFAULT_SPEED = 5; - public final int DEFAULT_INTONATION = 5; - + @Expose + public static final int AUDIO_FORMAT_WAV = 6; + @Expose + public static final int AUDIO_FORMAT_MP3 = 3; //度小宇=1,度小美=0,度逍遥=3,度丫丫=4 //度博文=106,度小童=110,度小萌=111,度米朵=103,度小娇=5 - private String speaker; - private int selectedSpeaker; + private String speakerIdString; + private int audioFormat; + private int speaker; private final SimpleDoubleProperty speed; private final SimpleDoubleProperty intonation; public VoiceConfig() { - selectedSpeaker = 0; - speaker = "1"; - speed = new SimpleDoubleProperty(DEFAULT_SPEED); - intonation = new SimpleDoubleProperty(DEFAULT_INTONATION); + this.speaker = 0; + this.speakerIdString = "1"; + this.speed = new SimpleDoubleProperty(DEFAULT_SPEED); + this.intonation = new SimpleDoubleProperty(DEFAULT_INTONATION); + this.audioFormat = AUDIO_FORMAT_WAV; } - public String getSpeaker() { - return speaker; + public String getSpeakerIdString() { + return speakerIdString; } - public void setSpeaker(String speaker) { - this.speaker = speaker; + public void setSpeakerIdString(String speakerIdString) { + this.speakerIdString = speakerIdString; } - public int getSelectedSpeaker() { - return selectedSpeaker; + public int getSpeaker() { + return speaker; } - public void setSelectedSpeaker(int selectedSpeaker) { - this.selectedSpeaker = selectedSpeaker; + public void setSpeaker(int speaker) { + this.speaker = speaker; } public double getSpeed() { @@ -64,10 +72,22 @@ public class VoiceConfig { return intonation; } - @FXML - void showAdvancedVoiceSettings(ActionEvent event) { - + public void setAudioFormat(int audioFormat) { + this.audioFormat = audioFormat; } + public int getAudioFormat() { + return audioFormat; + } + public static String getAudioFileSuffix(int format) { + switch (format) { + case VoiceConfig.AUDIO_FORMAT_WAV: + return "wav"; + case VoiceConfig.AUDIO_FORMAT_MP3: + return "mp3"; + default: + return "cache"; + } + } } diff --git a/Dogename/src/main/java/me/lensferno/dogename/controllers/MainInterfaceController.java b/Dogename/src/main/java/me/lensferno/dogename/controllers/MainInterfaceController.java index 94532f2..f291b5b 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/controllers/MainInterfaceController.java +++ b/Dogename/src/main/java/me/lensferno/dogename/controllers/MainInterfaceController.java @@ -24,6 +24,7 @@ import me.lensferno.dogename.select.Selector; import me.lensferno.dogename.utils.DialogMaker; import me.lensferno.dogename.utils.FilePath; import me.lensferno.dogename.utils.ocr.OcrTool; +import me.lensferno.dogename.voice.VoicePlayer; import java.io.File; import java.io.IOException; @@ -38,10 +39,10 @@ public final class MainInterfaceController { History history = new History(); MainConfig mainConfig; - VoiceConfig voiceConfig; Random random = new Random(); Data data = new Data(); Selector selector = new Selector(); + @FXML private Pane rootPane; @FXML @@ -88,7 +89,7 @@ public final class MainInterfaceController { public void setUpConfig(ConfigLoader configLoader) { mainConfig = configLoader.readConfigFromFile(FilePath.toSpecificPathForm("files/Config.json")); - voiceConfig = configLoader.readVoiceConfigFromFile(FilePath.toSpecificPathForm("files/VoiceConfig.json")); + VoicePlayer.voiceConfig = configLoader.readVoiceConfigFromFile(FilePath.toSpecificPathForm("files/VoiceConfig.json")); } @FXML @@ -108,7 +109,7 @@ public final class MainInterfaceController { } @FXML - void showNunberSetting(ActionEvent event) { + void showNumberSetting(ActionEvent event) { if (selector.isWorkerRunning()) { new DialogMaker(rootPane).createMessageDialog("(・。・)", "安排中......\n为保证运行的稳定,此时还不能进行该操作哦。"); @@ -159,8 +160,6 @@ public final class MainInterfaceController { settingsPaneController.setToggleGroup(); settingsPaneController.bindProperties(mainConfig); - settingsPaneController.setVoiceConfig(voiceConfig); - settingsPaneController.setRootPane(rootPane); settingsPaneController.setData(data); diff --git a/Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java b/Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java index 9253fde..caf9d5e 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java +++ b/Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java @@ -13,14 +13,14 @@ import me.lensferno.dogename.configs.MainConfig; import me.lensferno.dogename.configs.VoiceConfig; import me.lensferno.dogename.data.Data; import me.lensferno.dogename.utils.DialogMaker; +import me.lensferno.dogename.voice.VoicePlayer; public class SettingsPaneController extends VBox { - MainConfig mainConfig; - VoiceConfig voiceConfig; Pane rootPane; Data data; + @FXML private JFXCheckBox showSayingBtn; @FXML @@ -42,7 +42,6 @@ public class SettingsPaneController extends VBox { @FXML private JFXRadioButton fixedTimes; - public SettingsPaneController() { FXMLLoader loader = new FXMLLoader(getClass().getResource("/me/lensferno/dogename/FXMLs/SettingsPane.fxml")); loader.setRoot(this); @@ -58,10 +57,6 @@ public class SettingsPaneController extends VBox { this.mainConfig = mainConfig; } - public void setVoiceConfig(VoiceConfig voiceConfig) { - this.voiceConfig = voiceConfig; - } - public void setRootPane(Pane rootPane) { this.rootPane = rootPane; } @@ -107,8 +102,8 @@ public class SettingsPaneController extends VBox { @FXML void showVoiceSettingsPane(ActionEvent event) { - VoiceSettingsPaneController voiceSettingsPaneController = new VoiceSettingsPaneController(); - voiceSettingsPaneController.bindPropertied(voiceConfig); + VoiceSettingsPaneController voiceSettingsPaneController = new VoiceSettingsPaneController(rootPane); + voiceSettingsPaneController.bindPropertied(); new DialogMaker(rootPane).createDialogWithOneBtn("语音设置", voiceSettingsPaneController); } @@ -116,7 +111,6 @@ public class SettingsPaneController extends VBox { void showEqualMode(ActionEvent event) { new DialogMaker(rootPane).createMessageDialog("什么?", "//有待补充。;-)"); - } @FXML diff --git a/Dogename/src/main/java/me/lensferno/dogename/controllers/VoiceSettingsPaneController.java b/Dogename/src/main/java/me/lensferno/dogename/controllers/VoiceSettingsPaneController.java index 2688530..94c9224 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/controllers/VoiceSettingsPaneController.java +++ b/Dogename/src/main/java/me/lensferno/dogename/controllers/VoiceSettingsPaneController.java @@ -1,34 +1,51 @@ package me.lensferno.dogename.controllers; import com.jfoenix.controls.JFXComboBox; +import com.jfoenix.controls.JFXRadioButton; import com.jfoenix.controls.JFXSlider; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; +import javafx.scene.control.Toggle; +import javafx.scene.control.ToggleGroup; +import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import me.lensferno.dogename.configs.VoiceConfig; +import me.lensferno.dogename.utils.DialogMaker; +import me.lensferno.dogename.voice.VoicePlayer; +import java.io.File; +import java.util.HashMap; import java.util.logging.Logger; public class VoiceSettingsPaneController extends VBox { - public static final ObservableList shownSpeakerList = FXCollections.observableArrayList(); + private static final ObservableList speakerList = FXCollections.observableArrayList(); + + //度小宇=1,度小美=0,度逍遥=3,度丫丫=4 + //度博文=106,度小童=110,度小萌=111,度米朵=103,度小娇=5 private final String[] speakers = { "1", "0", "3", "4", "106", "110", "111", "103", "5"}; + Logger log = Logger.getLogger("VoiceSettingsPaneControllerLogger"); - VoiceConfig voiceConfig = new VoiceConfig(); + @FXML private JFXSlider intonationBar; @FXML private JFXComboBox speakerSelectBar; @FXML private JFXSlider voiceSpeedBar; - //度小宇=1,度小美=0,度逍遥=3,度丫丫=4 - //度博文=106,度小童=110,度小萌=111,度米朵=103,度小娇=5 + @FXML + private JFXRadioButton wavFormatButton; + @FXML + private JFXRadioButton mp3FormatButton; + + private final Pane rootPane; - public VoiceSettingsPaneController() { + public VoiceSettingsPaneController(Pane rootPane) { + this.rootPane = rootPane; FXMLLoader loader = new FXMLLoader(getClass().getResource("/me/lensferno/dogename/FXMLs/VoiceSettingsPane.fxml")); loader.setRoot(this); loader.setController(this); @@ -37,41 +54,50 @@ public class VoiceSettingsPaneController extends VBox { } catch (Exception e) { log.warning("Error to load settings pane FXML: " + e); } - if (shownSpeakerList.isEmpty()) { - shownSpeakerList.addAll( - "度小宇=1", "度小美=0", "度逍遥=3", "度丫丫=4", - "度博文=106", "度小童=110", "度小萌=111", "度米朵=103", "度小娇=5"); + if (speakerList.isEmpty()) { + speakerList.addAll( + "度小宇=1", "度小美=0", "度逍遥=3", "度丫丫=4" + // 非高级API用户,这些精品音频库用不了(等把“高级设置”弄出来再说吧...) + /*, "度博文=106", "度小童=110", "度小萌=111", "度米朵=103", "度小娇=5"*/); } - speakerSelectBar.setItems(shownSpeakerList); + speakerSelectBar.setItems(speakerList); } - public void bindPropertied(VoiceConfig voiceConfig) { - - this.voiceConfig = voiceConfig; + @FXML + ToggleGroup audioFormatToggleGroup = new ToggleGroup(); + HashMap audioFormatMap = new HashMap<>(); - //speakerSelectBar. + public void bindPropertied() { + wavFormatButton.setToggleGroup(audioFormatToggleGroup); + mp3FormatButton.setToggleGroup(audioFormatToggleGroup); + audioFormatMap.put(VoiceConfig.AUDIO_FORMAT_WAV, wavFormatButton); + audioFormatMap.put(VoiceConfig.AUDIO_FORMAT_MP3, mp3FormatButton); - speakerSelectBar.selectionModelProperty().addListener((observable, oldValue, newValue) -> { - System.out.println("what?" + newValue.getSelectedIndex()); - this.voiceConfig.setSpeaker(speakers[newValue.getSelectedIndex()]); - this.voiceConfig.setSelectedSpeaker(newValue.getSelectedIndex()); - }); + audioFormatToggleGroup.selectToggle(audioFormatMap.get(VoicePlayer.voiceConfig.getAudioFormat())); + audioFormatToggleGroup.selectedToggleProperty().addListener((observable, oldValue, selectBtn) -> + VoicePlayer.voiceConfig.setAudioFormat(selectBtn.equals(wavFormatButton) ? VoiceConfig.AUDIO_FORMAT_WAV : VoiceConfig.AUDIO_FORMAT_MP3)); - //speakerSelectBar.setValue(this.voiceConfig.getSpeaker()); + speakerSelectBar.getSelectionModel().select(VoicePlayer.voiceConfig.getSpeaker()); + speakerSelectBar.valueProperty().addListener((observable, oldValue, newValue) -> { + int selectedSpeakerIndex = speakerSelectBar.getSelectionModel().getSelectedIndex(); - voiceSpeedBar.valueProperty().bindBidirectional(voiceConfig.speedProperty()); - intonationBar.valueProperty().bindBidirectional(voiceConfig.intonationProperty()); + VoicePlayer.voiceConfig.setSpeakerIdString(speakers[selectedSpeakerIndex]); + VoicePlayer.voiceConfig.setSpeaker(selectedSpeakerIndex); + }); - speakerSelectBar.getSelectionModel().select(this.voiceConfig.getSelectedSpeaker()); + voiceSpeedBar.valueProperty().bindBidirectional(VoicePlayer.voiceConfig.speedProperty()); + intonationBar.valueProperty().bindBidirectional(VoicePlayer.voiceConfig.intonationProperty()); } @FXML - void showAdvancedVoiceSettings(ActionEvent event) { - + void clearCache(ActionEvent event) { + File[] cacheFiles = new File(VoicePlayer.cachePath).listFiles(); + if (cacheFiles != null) { + for (File cacheFile : cacheFiles) { + cacheFile.delete(); + } + } + new DialogMaker(rootPane).createMessageDialog("嘿咻~","缓存已清除"); } - - -} - - +} \ No newline at end of file diff --git a/Dogename/src/main/java/me/lensferno/dogename/data/History.java b/Dogename/src/main/java/me/lensferno/dogename/data/History.java index ee70a2b..822f451 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/data/History.java +++ b/Dogename/src/main/java/me/lensferno/dogename/data/History.java @@ -27,7 +27,7 @@ public class History { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(historyFile)); history = (ArrayList) ois.readObject(); - + ois.close(); } catch (EOFException e) { history = new ArrayList<>(); System.out.println("History file is empty."); diff --git a/Dogename/src/main/java/me/lensferno/dogename/select/core/Worker.java b/Dogename/src/main/java/me/lensferno/dogename/select/core/Worker.java index 8eea89b..d81e8e3 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/select/core/Worker.java +++ b/Dogename/src/main/java/me/lensferno/dogename/select/core/Worker.java @@ -3,6 +3,7 @@ package me.lensferno.dogename.select.core; import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.StringProperty; import me.lensferno.dogename.configs.MainConfig; +import me.lensferno.dogename.configs.VoiceConfig; import me.lensferno.dogename.data.Data; import me.lensferno.dogename.data.History; import me.lensferno.dogename.utils.Random; diff --git a/Dogename/src/main/java/me/lensferno/dogename/utils/DialogMaker.java b/Dogename/src/main/java/me/lensferno/dogename/utils/DialogMaker.java index 0682a41..290b6f6 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/utils/DialogMaker.java +++ b/Dogename/src/main/java/me/lensferno/dogename/utils/DialogMaker.java @@ -3,6 +3,7 @@ package me.lensferno.dogename.utils; import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXDialog; import com.jfoenix.controls.JFXDialogLayout; +import com.jfoenix.controls.events.JFXDialogEvent; import javafx.beans.NamedArg; import javafx.event.ActionEvent; import javafx.event.EventHandler; @@ -21,7 +22,6 @@ public class DialogMaker { Pane rootPane; JFXDialog dialog; - public DialogMaker(@NamedArg("rootPane") Pane rootPane) { this.rootPane = rootPane; } @@ -39,7 +39,7 @@ public class DialogMaker { } //创建只有一个按钮的dialog - public void createDialogWithOneBtn(@NamedArg("title") String title, @NamedArg("theBody") Node body) { + public void createDialogWithOneBtn(@NamedArg("title") String title, @NamedArg("theBody") Node body, @NamedArg("OKEvent") EventHandler event) { //dialog.setPrefHeight(rootPane.getPrefHeight()); //dialog.setPrefWidth(rootPane.getPrefWidth()); @@ -47,12 +47,21 @@ public class DialogMaker { OKButton.setFont(Font.font("Microsoft YaHei", FontWeight.BOLD, 12)); OKButton.setPrefWidth(60); OKButton.setPrefHeight(30); + OKButton.addEventHandler(ActionEvent.ACTION, e -> dialog.close()); createDialog(title, body, OKButton); + if (event != null) { + dialog.setOnDialogClosed(event); + } + dialog.show(); } + public void createDialogWithOneBtn(@NamedArg("title") String title, @NamedArg("theBody") Node body) { + createDialogWithOneBtn(title, body, null); + } + //创建有OK和cancel按钮的dialog public void createDialogWithOKAndCancel(@NamedArg("title") String title, @NamedArg("message") String message, @NamedArg("OKEvent") EventHandler OKEvent) { //dialog.setPrefHeight(rootPane.getPrefHeight()); diff --git a/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java b/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java index c7ea28b..4ddadef 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java +++ b/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java @@ -1,39 +1,38 @@ package me.lensferno.dogename.voice; -import com.goxr3plus.streamplayer.stream.StreamPlayer; -import com.goxr3plus.streamplayer.stream.StreamPlayerException; import me.lensferno.dogename.configs.VoiceConfig; import me.lensferno.dogename.utils.FilePath; import me.lensferno.dogename.utils.IOUtil; import me.lensferno.dogename.utils.NetworkUtil; +import javax.sound.sampled.*; import java.io.File; import java.net.URLEncoder; public class VoicePlayer { - private final String VOICE_API = "https://tsn.baidu.com/text2audio"; + private static final String VOICE_API = "https://tsn.baidu.com/text2audio"; - String cachePath = FilePath.toSpecificPathForm("caches/voice/"); + public static final String cachePath = FilePath.toSpecificPathForm("caches/voice/"); - Token token; - StreamPlayer streamPlayer = new StreamPlayer(); - private final VoiceConfig voiceConfig; + private final Token token; + + public static VoiceConfig voiceConfig; public VoicePlayer(Token token, VoiceConfig voiceConfig) { this.token = token; - this.voiceConfig = voiceConfig; + VoicePlayer.voiceConfig = voiceConfig; } public void playVoice(String name) { - String speaker = voiceConfig.getSpeaker(); + String speaker = voiceConfig.getSpeakerIdString(); String intonation = String.valueOf(voiceConfig.getIntonation()); String speed = String.valueOf(voiceConfig.getSpeed()); String cacheName = String.format("%s_%s_%s_%s", name, speaker, speed, intonation); - File cache = new File(cachePath + cacheName + ".mp3"); + File cache = new File(String.format("%s%s.%s", cachePath, cacheName, VoiceConfig.getAudioFileSuffix(voiceConfig.getAudioFormat()))); new Thread(() -> { if (!cache.exists()) { @@ -52,13 +51,14 @@ public class VoicePlayer { boolean success; try { String requestUrl = String.format( - "%s?ctp=1&lan=zh&tok=%s&cuid=%s&spd=%s&pit=%s&per=%s&aue=3&tex=%s", + "%s?ctp=1&lan=zh&tok=%s&cuid=%s&spd=%s&pit=%s&per=%s&aue=%s&tex=%s", VOICE_API, token.getAccessToken(), NetworkUtil.getMacMD5(), speed, intonation, speaker, + voiceConfig.getAudioFormat(), URLEncoder.encode(name, "UTF-8") ); @@ -92,12 +92,31 @@ public class VoicePlayer { private void playSound(File file) { try { - streamPlayer.open(file); - streamPlayer.play(); - //streamPlayer.setGain(voiceConfig.getVolume() * 0.1); - } catch (StreamPlayerException e) { - e.printStackTrace(); - file.delete(); + AudioInputStream sourceAudioInputStream = AudioSystem.getAudioInputStream(file); + + AudioFormat sourceFormat = sourceAudioInputStream.getFormat(); + AudioFormat targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, + sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), + sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false); + + AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(targetFormat, sourceAudioInputStream); + + DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, targetFormat, AudioSystem.NOT_SPECIFIED); + + SourceDataLine audioLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); + audioLine.open(targetFormat); + audioLine.start(); + + byte[] buffer = new byte[1024]; + int length = audioInputStream.read(buffer); + while (length > 0) { + audioLine.write(buffer, 0, length); + length = audioInputStream.read(buffer); + } + + audioLine.drain(); + audioLine.stop(); + audioLine.close(); } catch (Exception e) { e.printStackTrace(); } diff --git a/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/AdvancedVoiceSettingsPane.fxml b/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/AdvancedVoiceSettingsPane.fxml deleted file mode 100644 index a4be1ce..0000000 --- a/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/AdvancedVoiceSettingsPane.fxml +++ /dev/null @@ -1,53 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/MainInterface.fxml b/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/MainInterface.fxml index a60c4f3..94b6fa8 100644 --- a/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/MainInterface.fxml +++ b/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/MainInterface.fxml @@ -52,7 +52,7 @@ - + diff --git a/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/NameManagerPane.fxml b/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/NameManagerPane.fxml index 6c2a01f..17668aa 100644 --- a/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/NameManagerPane.fxml +++ b/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/NameManagerPane.fxml @@ -50,7 +50,7 @@ - + diff --git a/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/VoiceSettingsPane.fxml b/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/VoiceSettingsPane.fxml index 2f80b09..1ad5c4b 100644 --- a/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/VoiceSettingsPane.fxml +++ b/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/VoiceSettingsPane.fxml @@ -1,61 +1,78 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +