From 92a982b2f0cec876adda3dd23e243ddc9c51f727 Mon Sep 17 00:00:00 2001 From: hhhzzzsss Date: Sun, 2 Jun 2024 12:28:24 -0500 Subject: [PATCH] Added velocity threshold --- .../songplayer/CommandProcessor.java | 35 +++++++++++++++++++ .../github/hhhzzzsss/songplayer/Config.java | 1 + .../songplayer/conversion/MidiConverter.java | 15 ++++---- .../songplayer/conversion/NBSConverter.java | 2 +- .../songplayer/conversion/SPConverter.java | 3 +- .../songplayer/playing/SongHandler.java | 10 +++--- .../hhhzzzsss/songplayer/song/Note.java | 8 +++++ 7 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java index 7eb7c25..d744738 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java @@ -54,6 +54,7 @@ public class CommandProcessor { commands.add(new toggleFakePlayerCommand()); commands.add(new setStageTypeCommand()); commands.add(new toggleMovementCommand()); + commands.add(new setVelocityThresholdCommand()); commands.add(new toggleAutoCleanup()); commands.add(new cleanupLastStageCommand()); commands.add(new announcementCommand()); @@ -950,6 +951,40 @@ public class CommandProcessor { } } + private static class setVelocityThresholdCommand extends Command { + public String getName() { + return "setVelocityThreshold"; + } + public String[] getAliases() { + return new String[]{"velocityThreshold", "threshold"}; + } + public String[] getSyntax() { + return new String[] {""}; + } + public String getDescription() { + return "Sets the minimum velocity below which notes won't be played (applies to midi and nbs). This must be a number from 0 to 100. For song items, the threshold is baked in upon item creation."; + } + public boolean processCommand(String args) { + if (args.length() > 0) { + try { + int threshold = Integer.parseInt(args); + if (threshold < 0 || threshold > 100) { + SongPlayer.addChatMessage("§cVelocity threshold must be a value between 0 and 100"); + return true; + } + Config.getConfig().velocityThreshold = threshold; + SongPlayer.addChatMessage("§6Set velocity threshold to " + threshold); + Config.saveConfigWithErrorHandling(); + return true; + } catch (NumberFormatException e) { + return false; + } + } else { + return false; + } + } + } + private static class toggleAutoCleanup extends Command { public String getName() { return "toggleAutoCleanup"; diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/Config.java b/src/main/java/com/github/hhhzzzsss/songplayer/Config.java index 9b15391..96b9373 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/Config.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/Config.java @@ -24,6 +24,7 @@ public class Config { public Stage.StageType stageType = Stage.StageType.DEFAULT; public boolean swing = false; public boolean rotate = false; + public int velocityThreshold = 0; public boolean doAnnouncement = false; public String announcementMessage = "&6Now playing: &3[name]"; public boolean autoCleanup = false; diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java index 89fb227..5e996fd 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java @@ -90,18 +90,19 @@ public class MidiConverter { instrumentIds[sm.getChannel()] = sm.getData1(); } else if (sm.getCommand() == NOTE_ON) { - if (sm.getData2() == 0) continue; int pitch = sm.getData1(); + int velocity = sm.getData2(); + if (velocity == 0) continue; // Just ignore notes with velocity 0 long deltaTick = event.getTick() - prevTick; prevTick = event.getTick(); microTime += (mpq/tpq) * deltaTick; Note note; if (sm.getChannel() == 9) { - note = getMidiPercussionNote(pitch, microTime); + note = getMidiPercussionNote(pitch, velocity, microTime); } else { - note = getMidiInstrumentNote(instrumentIds[sm.getChannel()], pitch, microTime); + note = getMidiInstrumentNote(instrumentIds[sm.getChannel()], pitch, velocity, microTime); } if (note != null) { song.add(note); @@ -141,7 +142,7 @@ public class MidiConverter { return song; } - public static Note getMidiInstrumentNote(int midiInstrument, int midiPitch, long microTime) { + public static Note getMidiInstrumentNote(int midiInstrument, int midiPitch, int velocity, long microTime) { com.github.hhhzzzsss.songplayer.song.Instrument instrument = null; com.github.hhhzzzsss.songplayer.song.Instrument[] instrumentList = instrumentMap.get(midiInstrument); if (instrumentList != null) { @@ -161,15 +162,15 @@ public class MidiConverter { int noteId = pitch + instrument.instrumentId*25; long time = microTime / 1000L; - return new Note(noteId, time); + return new Note(noteId, time, velocity); } - private static Note getMidiPercussionNote(int midiPitch, long microTime) { + private static Note getMidiPercussionNote(int midiPitch, int velocity, long microTime) { if (percussionMap.containsKey(midiPitch)) { int noteId = percussionMap.get(midiPitch); long time = microTime / 1000L; - return new Note(noteId, time); + return new Note(noteId, time, velocity); } return null; } diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java index f81f2da..7572947 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java @@ -161,7 +161,7 @@ public class NBSConverter { int pitch = note.key-33; int noteId = pitch + instrument.instrumentId*25; - song.add(new Note(noteId, getMilliTime(note.tick, tempo))); + song.add(new Note(noteId, getMilliTime(note.tick, tempo), layerVolume)); } song.length = song.get(song.size()-1).time + 50; diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/SPConverter.java b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/SPConverter.java index 41da087..3a2ff56 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/SPConverter.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/SPConverter.java @@ -1,5 +1,6 @@ package com.github.hhhzzzsss.songplayer.conversion; +import com.github.hhhzzzsss.songplayer.Config; import com.github.hhhzzzsss.songplayer.Util; import com.github.hhhzzzsss.songplayer.song.Note; import com.github.hhhzzzsss.songplayer.song.Song; @@ -81,7 +82,7 @@ public class SPConverter { song.sort(); long prevTime = 0; - for (Note note : song.notes) { + for (Note note : song.notes) if (note.velocity >= Config.getConfig().velocityThreshold) { writeShort(os, note.noteId); writeVarLong(os, note.time - prevTime); prevTime = note.time; diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java b/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java index 29fd86e..2a807dc 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java @@ -400,10 +400,12 @@ public class SongHandler { currentSong.advanceTime(); while (currentSong.reachedNextNote()) { Note note = currentSong.getNextNote(); - BlockPos bp = stage.noteblockPositions.get(note.noteId); - if (bp != null) { - attackBlock(bp); - somethingPlayed = true; + if (note.velocity >= Config.getConfig().velocityThreshold) { + BlockPos bp = stage.noteblockPositions.get(note.noteId); + if (bp != null) { + attackBlock(bp); + somethingPlayed = true; + } } } if (somethingPlayed) { diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/song/Note.java b/src/main/java/com/github/hhhzzzsss/songplayer/song/Note.java index e596958..aa67d08 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/song/Note.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/song/Note.java @@ -3,9 +3,17 @@ package com.github.hhhzzzsss.songplayer.song; public class Note implements Comparable { public int noteId; public long time; + public int velocity; public Note(int note, long time) { this.noteId = note; this.time = time; + this.velocity = 100; + } + + public Note(int note, long time, int velocity) { + this.noteId = note; + this.time = time; + this.velocity = velocity; } @Override