From f97c5076e598033693fffd8f8a90e52afad255c9 Mon Sep 17 00:00:00 2001 From: James Ball Date: Wed, 19 May 2021 20:23:49 +0100 Subject: [PATCH] Add more file extension filters and provide a default file namme --- src/main/java/sh/ball/gui/Controller.java | 56 +++++++++++++++-------- src/main/resources/fxml/osci-render.fxml | 2 +- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/main/java/sh/ball/gui/Controller.java b/src/main/java/sh/ball/gui/Controller.java index a4ebab5..b02c6dc 100644 --- a/src/main/java/sh/ball/gui/Controller.java +++ b/src/main/java/sh/ball/gui/Controller.java @@ -13,6 +13,8 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.ResourceBundle; @@ -202,6 +204,15 @@ public class Controller implements Initializable { bitCrushSlider.valueProperty().addListener(bitCrushListener); bitCrushCheckBox.selectedProperty().addListener(bitCrushListener); + fileChooser.setInitialFileName("out.wav"); + fileChooser.getExtensionFilters().addAll( + new FileChooser.ExtensionFilter("All Files", "*.*"), + new FileChooser.ExtensionFilter("WAV Files", "*.wav"), + new FileChooser.ExtensionFilter("Wavefront OBJ Files", "*.obj"), + new FileChooser.ExtensionFilter("SVG Files", "*.svg"), + new FileChooser.ExtensionFilter("Text Files", "*.txt") + ); + chooseFileButton.setOnAction(e -> { File file = fileChooser.showOpenDialog(stage); if (file != null) { @@ -209,24 +220,7 @@ public class Controller implements Initializable { } }); - recordButton.setOnAction(event -> { - recording = !recording; - if (recording) { - recordLabel.setText("Recording..."); - recordButton.setText("Stop Recording"); - renderer.startRecord(); - } else { - recordLabel.setText(""); - recordButton.setText("Record"); - AudioInputStream input = renderer.stopRecord(); - try { - AudioSystem.write(input, AudioFileFormat.Type.WAVE, new File("out.wav")); - input.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - }); + recordButton.setOnAction(event -> toggleRecord()); setObjectRotateSpeed(DEFAULT_ROTATE_SPEED); @@ -238,6 +232,32 @@ public class Controller implements Initializable { new Thread(renderer).start(); } + private void toggleRecord() { + recording = !recording; + if (recording) { + recordLabel.setText("Recording..."); + recordButton.setText("Stop Recording"); + renderer.startRecord(); + } else { + recordButton.setText("Record"); + AudioInputStream input = renderer.stopRecord(); + try { + File file = fileChooser.showSaveDialog(stage); + SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); + Date date = new Date(System.currentTimeMillis()); + if (file == null) { + file = new File("out-" + formatter.format(date) + ".wav"); + } + AudioSystem.write(input, AudioFileFormat.Type.WAVE, file); + input.close(); + recordLabel.setText("Saved to " + file.getAbsolutePath()); + } catch (IOException e) { + recordLabel.setText(""); + e.printStackTrace(); + } + } + } + private void setFocalLength(double focalLength) { producer.setFrameSettings(ObjSettingsFactory.focalLength(focalLength)); } diff --git a/src/main/resources/fxml/osci-render.fxml b/src/main/resources/fxml/osci-render.fxml index 718d30d..b71e2a6 100644 --- a/src/main/resources/fxml/osci-render.fxml +++ b/src/main/resources/fxml/osci-render.fxml @@ -75,6 +75,6 @@