From d9b930cfe8a420afaab9f9e9550b425ad5397e4d Mon Sep 17 00:00:00 2001 From: Pedro Cardona <1724279+atoko@users.noreply.github.com> Date: Mon, 12 Sep 2022 11:05:56 -0400 Subject: [PATCH] atoko | Metronome MediaMarker duration contributed by digits0801 --- .../com/utsusynth/utsu/engine/Metronome.java | 14 ++- .../utsu/files/PreferencesManager.java | 2 + .../utsusynth/utsu/model/song/Metronome.java | 85 ------------------- 3 files changed, 7 insertions(+), 94 deletions(-) delete mode 100644 src/main/java/com/utsusynth/utsu/model/song/Metronome.java diff --git a/src/main/java/com/utsusynth/utsu/engine/Metronome.java b/src/main/java/com/utsusynth/utsu/engine/Metronome.java index e184b4b..26ce233 100644 --- a/src/main/java/com/utsusynth/utsu/engine/Metronome.java +++ b/src/main/java/com/utsusynth/utsu/engine/Metronome.java @@ -70,19 +70,15 @@ void attach( List generateMarkers( double seconds, - double tempo + double bpm ) { ArrayList events = new ArrayList<>(); - int ratio = Math.floorDiv((int)seconds * 1000, (int) 60000); - double beats = tempo * ratio; - double steps = seconds * ratio; + double bps = bpm * (1.0 / 60.0); + double ms_beat = (1.0 / bps) * (1000.0); - for (int i = 0; i < 200; i++) { - if (i * 1000 > seconds * 1000) { - break; - } - events.add(new Duration(i * 1000)); + for (int i = 0; i < seconds * 1000; i+=ms_beat) { + events.add(new Duration(i)); } return events; diff --git a/src/main/java/com/utsusynth/utsu/files/PreferencesManager.java b/src/main/java/com/utsusynth/utsu/files/PreferencesManager.java index e1601be..0129da9 100644 --- a/src/main/java/com/utsusynth/utsu/files/PreferencesManager.java +++ b/src/main/java/com/utsusynth/utsu/files/PreferencesManager.java @@ -36,6 +36,8 @@ public class PreferencesManager { private BooleanProperty showVoicebankFaceTemp; private BooleanProperty showVoicebankBodyTemp; + private BooleanProperty isMetronomeEnabled; + public PreferencesManager( @SettingsPath File settingsPath, DocumentBuilderFactory documentBuilderFactory, diff --git a/src/main/java/com/utsusynth/utsu/model/song/Metronome.java b/src/main/java/com/utsusynth/utsu/model/song/Metronome.java deleted file mode 100644 index 699c991..0000000 --- a/src/main/java/com/utsusynth/utsu/model/song/Metronome.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.utsusynth.utsu.model.song; - -import javafx.collections.ModifiableObservableListBase; -import javafx.collections.ObservableList; -import javafx.collections.ObservableMap; -import javafx.scene.media.Media; -import javafx.scene.media.MediaMarkerEvent; -import javafx.scene.media.MediaPlayer; -import javafx.util.Duration; - -import java.util.ArrayList; -import java.util.function.Consumer; - -public class Metronome { - - public Metronome( - String clickUri, - MediaPlayer songPlayer, - Integer t - ) { - attach( - songPlayer, - new MediaPlayer(new Media( - clickUri - )), - generateMarkers(songPlayer.getTotalDuration().toSeconds(), t) - ); - } - - public void attach( - MediaPlayer songPlayer, - MediaPlayer metronomePlayer, - ObservableList markers - ) { - ObservableMap mediaMarkers = songPlayer.getMedia().getMarkers(); - markers.forEach(mme -> - mediaMarkers.put( - mme.getMarker().getKey(), - mme.getMarker().getValue() - ) - ); - - songPlayer.setOnMarker((MediaMarkerEvent mme) -> { - metronomePlayer.stop(); - metronomePlayer.play(); - }); - } - - private ObservableList generateMarkers( - double duration, - int tempo - ) { - - ArrayList events = new ArrayList<>(); - int steps = Math.floorDiv((int)duration, tempo); - - // Not thread safe - return new ModifiableObservableListBase<>() { - @Override - public MediaMarkerEvent get(int i) { - return events.get(i); - } - - @Override - public int size() { - return events.size(); - } - - @Override - protected void doAdd(int i, MediaMarkerEvent mediaMarkerEvent) { - events.add(i, mediaMarkerEvent); - } - - @Override - protected MediaMarkerEvent doSet(int i, MediaMarkerEvent mediaMarkerEvent) { - return events.set(i, mediaMarkerEvent); - } - - @Override - protected MediaMarkerEvent doRemove(int i) { - return events.remove(i); - } - }; - } -} \ No newline at end of file