Added velocity threshold

This commit is contained in:
hhhzzzsss 2024-06-02 12:28:24 -05:00
parent 3d2f89e10f
commit 92a982b2f0
7 changed files with 61 additions and 13 deletions

View file

@ -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[] {"<number>"};
}
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";

View file

@ -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;

View file

@ -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;
}

View file

@ -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;

View file

@ -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;

View file

@ -400,12 +400,14 @@ public class SongHandler {
currentSong.advanceTime();
while (currentSong.reachedNextNote()) {
Note note = currentSong.getNextNote();
if (note.velocity >= Config.getConfig().velocityThreshold) {
BlockPos bp = stage.noteblockPositions.get(note.noteId);
if (bp != null) {
attackBlock(bp);
somethingPlayed = true;
}
}
}
if (somethingPlayed) {
stopAttack();
}

View file

@ -3,9 +3,17 @@ package com.github.hhhzzzsss.songplayer.song;
public class Note implements Comparable<Note> {
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