From 0d312b11e3e928363afa94cc01debd092b76d9c1 Mon Sep 17 00:00:00 2001 From: James Ball Date: Thu, 12 May 2022 22:28:09 +0100 Subject: [PATCH] Add frequency spinner to set specific frequencies --- .../ball/gui/controller/ImageController.java | 32 ++++++--- .../ball/gui/controller/MainController.java | 7 +- src/main/resources/fxml/image.fxml | 72 ++++++++++--------- 3 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/main/java/sh/ball/gui/controller/ImageController.java b/src/main/java/sh/ball/gui/controller/ImageController.java index bbc91a2..d0b2554 100644 --- a/src/main/java/sh/ball/gui/controller/ImageController.java +++ b/src/main/java/sh/ball/gui/controller/ImageController.java @@ -4,10 +4,8 @@ import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.scene.control.Button; -import javafx.scene.control.CheckBox; -import javafx.scene.control.Slider; -import javafx.scene.control.TextField; +import javafx.scene.control.*; +import javafx.scene.input.KeyCode; import javafx.scene.shape.SVGPath; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -56,6 +54,8 @@ public class ImageController implements Initializable, SubController { @FXML private Slider frequencySlider; @FXML + private Spinner frequencySpinner; + @FXML private SVGPath frequencyMidi; @FXML private CheckBox frequencyMic; @@ -162,13 +162,27 @@ public class ImageController implements Initializable, SubController { translationScaleSlider.valueProperty().addListener((e, old, scale) -> translateEffect.setScale(scale.doubleValue())); - // converts the value of frequencySlider to the actual frequency that it represents so that it - // can increase at an exponential scale. - frequencySlider.valueProperty().addListener((o, old, f) -> frequency.set(Math.pow(MAX_FREQUENCY, f.doubleValue()))); - frequency.addListener((o, old, f) -> frequencySlider.setValue(Math.log(f.doubleValue()) / Math.log(MAX_FREQUENCY))); + frequencySpinner.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0, MAX_FREQUENCY, MidiNote.MIDDLE_C, 1)); + frequencySpinner.valueProperty().addListener((o, old, f) -> { + frequency.set(f); + }); + + frequency.addListener((o, old, f) -> { + frequencySlider.setValue(Math.log(f.doubleValue()) / Math.log(MAX_FREQUENCY)); + frequencySpinner.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0, MAX_FREQUENCY, f.doubleValue(), 1)); + }); audioPlayer.setFrequency(frequency); // default value is middle C frequency.set(MidiNote.MIDDLE_C); + frequencySlider.setOnKeyPressed(e -> { + if (e.getCode() == KeyCode.LEFT || e.getCode() == KeyCode.RIGHT) { + frequency.set(Math.pow(MAX_FREQUENCY, frequencySlider.getValue())); + } + }); + frequencySlider.setOnMouseDragged(e -> { + frequency.set(Math.pow(MAX_FREQUENCY, frequencySlider.getValue())); + }); + volumeSlider.valueProperty().addListener((e, old, value) -> { audioPlayer.setVolume(value.doubleValue() / 3.0); }); @@ -232,6 +246,8 @@ public class ImageController implements Initializable, SubController { translationXTextField.setText(x.getTextContent()); translationYTextField.setText(y.getTextContent()); + frequency.set(Math.pow(MAX_FREQUENCY, frequencySlider.getValue())); + // For backwards compatibility we assume a default value Element ellipse = (Element) element.getElementsByTagName("ellipse").item(0); translateEllipseCheckBox.setSelected(ellipse != null && Boolean.parseBoolean(ellipse.getTextContent())); diff --git a/src/main/java/sh/ball/gui/controller/MainController.java b/src/main/java/sh/ball/gui/controller/MainController.java index 21fcb17..a083078 100644 --- a/src/main/java/sh/ball/gui/controller/MainController.java +++ b/src/main/java/sh/ball/gui/controller/MainController.java @@ -409,7 +409,12 @@ public class MainController implements Initializable, FrequencyListener, MidiLis deadzoneSpinner.valueProperty().addListener((e, old, deadzone) -> this.midiDeadzone = deadzone); List printableSliders = new ArrayList<>(); - sliders.forEach(slider -> printableSliders.add(new PrintableSlider(slider))); + sliders.forEach(slider -> { + // don't allow frequency slider to be changed + if (!slider.getId().contains("frequency")) { + printableSliders.add(new PrintableSlider(slider)); + } + }); sliderComboBox.setItems(FXCollections.observableList(printableSliders)); sliderComboBox.valueProperty().addListener((e, old, slider) -> { sliderMinTextField.setText(FORMAT.format(slider.slider.getMin())); diff --git a/src/main/resources/fxml/image.fxml b/src/main/resources/fxml/image.fxml index 3489a58..dc30de5 100644 --- a/src/main/resources/fxml/image.fxml +++ b/src/main/resources/fxml/image.fxml @@ -4,6 +4,7 @@ + @@ -12,25 +13,25 @@ - -