Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
Create fireworks effect and lay groundwork for more effects. (#6)
Browse files Browse the repository at this point in the history
* Add MusicEffect interface, and created FireworkEffect class.
* Changed PulseBeat.java to PulseBeatEffect.java
* Updated Resources Root to match Maven
* Updated version in pom.xml
* Added way to change effect from the UI
* Updated version number in About dialog
  • Loading branch information
jaxcksn authored Dec 15, 2020
1 parent d26e97a commit 9eb56c5
Show file tree
Hide file tree
Showing 31 changed files with 612 additions and 293 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2020, Jaxcksn
~ All rights reserved.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Expand All @@ -9,7 +14,7 @@
</properties>
<groupId>dev.jaxcksn</groupId>
<artifactId>nanoleafMusicFX</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/dev/jaxcksn/nanoleafMusic/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package dev.jaxcksn.nanoleafMusic;

import com.github.kevinsawicki.http.HttpRequest;
import dev.jaxcksn.nanoleafMusic.effects.EffectType;
import dev.jaxcksn.nanoleafMusic.utility.DataManagerException;
import dev.jaxcksn.nanoleafMusic.utility.Settings;
import dev.jaxcksn.nanoleafMusic.utility.dMEC;
Expand Down Expand Up @@ -77,16 +78,18 @@ public static Settings loadSettings() {
int albumPaletteLength = preferences.getInt("numberOfAlbumColors",6);
if(albumPaletteLength > 12) {
albumPaletteLength = 12;
} else if (albumPaletteLength < 3){
} else if (albumPaletteLength < 3) {
albumPaletteLength = 3;
}
String colorPalette = preferences.get("colorPalette","#FF0000,#00FF00,#0000FF");
if(colorPalette.length() > 95) {
colorPalette = colorPalette.substring(0,95);
} else if ( colorPalette.length() < 23) {
colorPalette="#FF0000,#00FF00,#0000FF";
String colorPalette = preferences.get("colorPalette", "#FF0000,#00FF00,#0000FF");
if (colorPalette.length() > 95) {
colorPalette = colorPalette.substring(0, 95);
} else if (colorPalette.length() < 23) {
colorPalette = "#FF0000,#00FF00,#0000FF";
}
return new Settings(albumColors,albumPaletteLength,colorPalette);
String effectString = preferences.get("selectedEffect", "PULSEBEAT");
EffectType activeEffectType = EffectType.valueOf(effectString);
return new Settings(albumColors, albumPaletteLength, colorPalette, activeEffectType);
}

public static void updateSettings(Settings settings) {
Expand All @@ -98,6 +101,10 @@ public static void changeAlbumMode(boolean b) {
preferences.putBoolean("useAlbumColors", b);
}

public static void changeEffectType(EffectType effectType) {
preferences.put("selectedEffect", effectType.toString());
}

public static void clearSavedData() {
try {
preferences.clear();
Expand Down
63 changes: 47 additions & 16 deletions src/main/java/dev/jaxcksn/nanoleafMusic/EffectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import com.wrapper.spotify.requests.data.tracks.GetAudioAnalysisForTrackRequest;
import de.androidpit.colorthief.ColorThief;
import dev.jaxcksn.nanoleafMusic.controllers.PlaybackView;
import dev.jaxcksn.nanoleafMusic.effects.EffectType;
import dev.jaxcksn.nanoleafMusic.effects.FireworkEffect;
import dev.jaxcksn.nanoleafMusic.effects.MusicEffect;
import dev.jaxcksn.nanoleafMusic.effects.PulseBeatEffect;
import dev.jaxcksn.nanoleafMusic.utility.PaletteColor;
import dev.jaxcksn.nanoleafMusic.utility.PulseBeat;
import dev.jaxcksn.nanoleafMusic.utility.Settings;
import dev.jaxcksn.nanoleafMusic.utility.SpecificAudioAnalysis;
import io.github.rowak.nanoleafapi.Aurora;
Expand Down Expand Up @@ -49,11 +52,11 @@ public class EffectManager {
private int expiresIn;
public Aurora device;
private final PlaybackView viewController;
public PulseBeat pulseBeat;
private final PaletteColor defaultAccent = new PaletteColor("#0FD95F");
public MusicEffect activeEffect;

//--- Effect Variables
private boolean isRunning, isPlaying = false;
private boolean isRunning;
public boolean isPlaying = false;
private Track currentTrack;
private AudioAnalysis currentTrackAnalysis;
private int progress;
Expand All @@ -67,12 +70,36 @@ public EffectManager(SpotifyApi spotifyApi, int expiresIn, Aurora device, Playba
this.expiresIn = expiresIn;
this.device = device;
this.viewController = viewController;
this.pulseBeat = new PulseBeat(palette, device);

settings = DataManager.loadSettings();
switch (settings.activeEffectType) {
case FIREWORKS:
this.activeEffect = new FireworkEffect(palette, device);
break;
case PULSEBEAT:
this.activeEffect = new PulseBeatEffect(palette, device);
break;
}

System.out.println("\u001b[92;1m✔\u001b[0m Effect Manager Loaded");
startRefreshTimer();
}

public void switchEffect(EffectType effectType) {
System.out.println("\u001b[96;1mℹ\u001b[0m Changing Effect");
settings.activeEffectType = effectType;
Color[] currentPalette = activeEffect.getPalette();
switch (effectType) {
case FIREWORKS:
this.activeEffect = new FireworkEffect(currentPalette, device);
break;
case PULSEBEAT:
this.activeEffect = new PulseBeatEffect(currentPalette, device);
break;
}
}


public void reloadEffect() {
System.out.println("\n" + "\u001b[96;1mℹ\u001b[0m Attempting to Restart Effect");
sES.shutdownNow();
Expand All @@ -85,7 +112,7 @@ public void reloadEffect() {
if (settings.albumColors) {
displayTrackInformation(false, false);
} else {
pulseBeat.setPalette(palette);
activeEffect.setPalette(palette);
}
this.startEffect();
System.out.println("\u001b[92;1m✔\u001b[0m Finished Restarting Effect\n");
Expand Down Expand Up @@ -131,7 +158,7 @@ public void startEffect() {
if (isPlaying) {
try {
pulseTask();
} catch (StatusCodeException e) {
} catch (StatusCodeException | IOException e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -223,10 +250,12 @@ private void showSWAE(SpotifyWebApiException spotifyWebApiException) {
}
// ---

private void pulseTask() throws StatusCodeException {
SpecificAudioAnalysis analysis = SpecificAudioAnalysis.getAnalysis(currentTrackAnalysis,progress,100);
pulseBeat.run(analysis);
progress += 100;
private void pulseTask() throws StatusCodeException, IOException {
if (isPlaying) {
SpecificAudioAnalysis analysis = SpecificAudioAnalysis.getAnalysis(currentTrackAnalysis, progress, 100);
activeEffect.run(analysis);
progress += 100;
}
}

private void spotifyTask() throws ParseException, SpotifyWebApiException, IOException, InterruptedException {
Expand Down Expand Up @@ -277,22 +306,24 @@ private void spotifyTask() throws ParseException, SpotifyWebApiException, IOExce

}

private void displayTrackInformation(boolean updateArt, boolean isPaused) {
public void displayTrackInformation(boolean updateArt, boolean isPaused) {
if (!settings.albumColors) {
if (updateArt) {
activeEffect.setSongChanged();
Image[] artwork = currentTrack.getAlbum().getImages();
artworkURL = artwork[1].getUrl();
}

ArtistSimplified[] songArtists = currentTrack.getArtists();

new Thread(() -> {
pulseBeat.setPalette(palette);
viewController.setPlayback(currentTrack.getName(), songArtists, artworkURL, defaultAccent);
activeEffect.setPalette(palette);
viewController.setPlayback(currentTrack.getName(), songArtists, artworkURL);
}).start();
} else {
if (isPlaying) {
if (updateArt) {
activeEffect.setSongChanged();
Image[] artwork = currentTrack.getAlbum().getImages();
artworkURL = artwork[1].getUrl();
}
Expand All @@ -302,11 +333,11 @@ private void displayTrackInformation(boolean updateArt, boolean isPaused) {
try {
BufferedImage image = ImageIO.read(new URL(artworkURL));
int[][] colorArray = ColorThief.getPalette(image, 6);
pulseBeat.setPalette(colorArray);
activeEffect.setPalette(colorArray);
} catch (IOException e) {
e.printStackTrace();
}
viewController.setPlayback(currentTrack.getName(), songArtists, artworkURL, pulseBeat.accentColor);
viewController.setPlayback(currentTrack.getName(), songArtists, artworkURL);
}).start();
} else {
viewController.setPlayback(isPaused);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private List<AuroraMetadata> findNanoleaf() throws IOException {
try {
auroras.add(fromMDNSInstance(instance));
} catch (ipv6Exception ignored) {

System.out.println("IPV6Exception");
}
}));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void savePalette(ActionEvent aE) {
effectManager.settings.colorPalette = colorString.toString();
new Thread(() -> DataManager.updateSettings(effectManager.settings)).start();
effectManager.palette = PaletteColor.toEffectColorArray(colorString.toString());
effectManager.pulseBeat.setPalette(effectManager.palette);
effectManager.activeEffect.setPalette(effectManager.palette);
((Stage) saveBtn.getScene().getWindow()).close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import dev.jaxcksn.nanoleafMusic.DataManager;
import dev.jaxcksn.nanoleafMusic.EffectManager;
import dev.jaxcksn.nanoleafMusic.Main;
import dev.jaxcksn.nanoleafMusic.utility.PaletteColor;
import dev.jaxcksn.nanoleafMusic.effects.EffectType;
import dev.jaxcksn.nanoleafMusic.utility.Settings;
import io.github.rowak.nanoleafapi.Aurora;
import javafx.beans.value.ObservableValue;
Expand All @@ -20,6 +20,7 @@
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
Expand All @@ -41,21 +42,50 @@ public class PlaybackView {
public Rectangle trackArtFrame;
public MenuButton menuButton;
public MenuItem aboutMenuItem;
public ToggleGroup effectRadio;
public AnchorPane loadingPane;
public RadioMenuItem FireworksToggle;
public RadioMenuItem PulseBeatToggle;
public Label EffectLabel;
private EffectManager effectManager;

private Scene palettePickerScene;

public void initData(SpotifyApi spotifyApi, int expiresIn, Aurora device) {
PulseBeatToggle.setUserData(EffectType.PULSEBEAT);
FireworksToggle.setUserData(EffectType.FIREWORKS);

effectManager = new EffectManager(spotifyApi, expiresIn, device, this);
Settings loadedSettings = effectManager.settings;
if (loadedSettings.albumColors) {
colorPaletteSelector.setDisable(true);
albumColorsCheckbox.setSelected(true);

} else {
colorPaletteSelector.setDisable(false);
albumColorsCheckbox.setSelected(false);
}

switch (effectManager.settings.activeEffectType) {
case PULSEBEAT:
PulseBeatToggle.setSelected(true);
FireworksToggle.setSelected(false);
EffectLabel.setText("PULSEBEAT");
break;
case FIREWORKS:
PulseBeatToggle.setSelected(false);
FireworksToggle.setSelected(true);
EffectLabel.setText("FIREWORKS");
break;
}

effectRadio.selectedToggleProperty().addListener((ObservableValue<? extends Toggle> ov,
Toggle old_toggle, Toggle new_toggle) -> {
old_toggle.setSelected(false);
new_toggle.setSelected(true);
changeEffect((EffectType) new_toggle.getUserData());
});

albumColorsCheckbox.selectedProperty().addListener((ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) -> {
colorPaletteSelector.setDisable(new_val);
effectManager.settings.albumColors = new_val;
Expand All @@ -79,7 +109,7 @@ public void initData(SpotifyApi spotifyApi, int expiresIn, Aurora device) {
}
}

public void setPlayback(String songName, ArtistSimplified[] artists, String albumArtwork, PaletteColor accentColor) {
public void setPlayback(String songName, ArtistSimplified[] artists, String albumArtwork) {
trackArtFrame.setFill(new ImagePattern(new Image(albumArtwork)));
StringBuilder artistString = new StringBuilder("by ");
for (int i = 1; i <= artists.length; i++) {
Expand All @@ -90,8 +120,8 @@ public void setPlayback(String songName, ArtistSimplified[] artists, String albu
artistString.append(" & ");
}
artistString.append(artists[artistIndex].getName());
Parent root = mainPane.getScene().getRoot();
root.setStyle("-fx-playback-accent: " + accentColor.hexCode + ";");
//Parent root = mainPane.getScene().getRoot();
//root.setStyle("-fx-playback-accent: #0FD95F;");
}

trackName.setText(songName);
Expand Down Expand Up @@ -119,13 +149,37 @@ public void reloadEffectManager(ActionEvent event) {
effectManager.reloadEffect();
}

private void setLoading(boolean status) {
mainPane.setDisable(status);
loadingPane.setVisible(status);
}

public void changeEffect(EffectType effectType) {
setLoading(true);
new Thread(() -> {
effectManager.switchEffect(effectType);
if (effectManager.isPlaying) {
effectManager.displayTrackInformation(true, false);
}
setLoading(false);
}).start();

new Thread(() -> {
DataManager.changeEffectType(effectType);
}).start();

EffectLabel.setText(effectType.toString());
}

public void showAbout(ActionEvent event) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText("About this Program");
alert.setContentText("NanoleafMusic v1.0.0 \nCopyright (c) 2020, Jaxcksn. All rights reserved.");
alert.setContentText("NanoleafMusic v1.1-b \nCopyright (c) 2020, Jaxcksn.\nAll rights reserved.");
DialogPane dialogPane = alert.getDialogPane();
dialogPane.setMinHeight(Region.USE_PREF_SIZE);
dialogPane.getStylesheets().add("/gui.css");
alert.showAndWait();
}


}
10 changes: 10 additions & 0 deletions src/main/java/dev/jaxcksn/nanoleafMusic/effects/EffectType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2020, Jaxcksn
* All rights reserved.
*/

package dev.jaxcksn.nanoleafMusic.effects;

public enum EffectType {
PULSEBEAT, FIREWORKS
}
Loading

0 comments on commit 9eb56c5

Please sign in to comment.