diff options
| author | Elis Eriksson <Spelis@duck.com> | 2026-03-04 13:43:21 +0100 |
|---|---|---|
| committer | Elis Eriksson <Spelis@duck.com> | 2026-03-04 13:43:21 +0100 |
| commit | b8f18d9da8f2023af37d5302b9bf0cbc6f83e95f (patch) | |
| tree | 30cf5444509f97fadfd343b2f379cc3df06bc424 /src/main/java | |
| download | note2input-b8f18d9da8f2023af37d5302b9bf0cbc6f83e95f.tar note2input-b8f18d9da8f2023af37d5302b9bf0cbc6f83e95f.tar.gz note2input-b8f18d9da8f2023af37d5302b9bf0cbc6f83e95f.tar.bz2 note2input-b8f18d9da8f2023af37d5302b9bf0cbc6f83e95f.tar.lz note2input-b8f18d9da8f2023af37d5302b9bf0cbc6f83e95f.tar.xz note2input-b8f18d9da8f2023af37d5302b9bf0cbc6f83e95f.tar.zst note2input-b8f18d9da8f2023af37d5302b9bf0cbc6f83e95f.zip | |
Initial Commit
Diffstat (limited to 'src/main/java')
14 files changed, 1067 insertions, 0 deletions
diff --git a/src/main/java/eu/projnull/spelis/note2input/HudRenderer.java b/src/main/java/eu/projnull/spelis/note2input/HudRenderer.java new file mode 100644 index 0000000..a4829ef --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/HudRenderer.java @@ -0,0 +1,113 @@ +package eu.projnull.spelis.note2input; + +import eu.projnull.spelis.note2input.config.ConfigManager; +import eu.projnull.spelis.note2input.io.AudioManager; +import eu.projnull.spelis.note2input.util.MiscUtil; +import eu.projnull.spelis.note2input.util.Note; +import eu.projnull.spelis.note2input.util.UtilityKeybinds; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.render.RenderTickCounter; +import net.minecraft.text.Text; +import net.minecraft.util.math.MathHelper; + +public class HudRenderer { + + private float smoothedAlpha = 0f; + private float attackIndicator = 0f; + private float lastTime = System.nanoTime(); + + void render(DrawContext drawContext, RenderTickCounter tickCounter) { + var config = ConfigManager.get(); + if (!config.enable) return; + + MinecraftClient client = MinecraftClient.getInstance(); + double midi = AudioManager.audioInput.getCurrentMidi(); + double loudness = AudioManager.audioInput.getCurrentLoudness(); + double db = 20.0 * Math.log10(loudness); // convert rms to db + + // Smooth alpha for input handling + float targetAlpha = MiscUtil.normalizeDb(db); + smoothedAlpha = smoothedAlpha * 0.85f + targetAlpha * 0.15f; + + attackIndicator = Math.max((float) loudness, attackIndicator * 0.9f); + + // Apply key input to player rotation (render-thread smoothing) + long now = System.nanoTime(); + float deltaSeconds = (now-lastTime) / 10000000f; + lastTime = now; + UtilityKeybinds.applyLookInput(client, deltaSeconds); + + // HUD dimensions + int x = 10, y = 10; + int width = 160, height = 39; + drawContext.fill(x - 4, y - 4, x + width, y + height, 0x88000000); + + // Draw note info + String noteName = AudioManager.audioInput.getCurrentMidiValidity() ? Note.midiToNoteName(midi) : "Unknown"; + int noteColor = getNoteColor(noteName, AudioManager.audioInput.getCurrentMidiValidity()); + drawContext.drawText(client.textRenderer, "Note: " + noteName, x, y, noteColor, true); + KeyBinding currentKey = Main.getKeyMapper().getActiveBinding(); + if (currentKey != null) { + drawContext.drawText(client.textRenderer, Text.translatable(Main.getKeyMapper().getActiveBinding().getId()), x, y + 11, 0xFFAAAAAA, false); + } + + // Draw loudness bar + drawLoudnessBar(drawContext, x, y + 25, loudness); + + int attackWidth = (int) (140 * attackIndicator); + drawContext.fill(x, y + 33, x + attackWidth, y + 35, 0xAAFFFF00); + } + + private int getNoteColor(String noteName, boolean valid) { + if (!valid) return 0xFFFFFFFF; + + return switch (noteName.charAt(0)) { + case 'C' -> 0xFFFF5555; + case 'D' -> 0xFFFFAA00; + case 'E' -> 0xFFFFFF55; + case 'F' -> 0xFF55FF55; + case 'G' -> 0xFF55FFFF; + case 'A' -> 0xFF5555FF; + case 'B' -> 0xFFFF55FF; + default -> 0xFFFFFFFF; + }; + } + + private void drawLoudnessBar(DrawContext ctx, int x, int y, double loudness) { + var config = ConfigManager.get(); + + double normalized = MathHelper.clamp(loudness, 0f, 1f); + int filled = (int) (140 * normalized); + + // Background + ctx.fill(x, y, x + 140, y + 6, 0xFF333333); + + int barColor; + if (normalized <= 0.5) { + // green -> yellow + int green = 255; + int red = (int) (normalized * 2 * 255); + barColor = 0xFF000000 | (red << 16) | (green << 8); + } else { + // yellow -> red + int red = 255; + int green = (int) ((1 - normalized) * 2 * 255); + barColor = 0xFF000000 | (red << 16) | (green << 8); + } + + ctx.fill(x, y, x + filled, y + 6, barColor); + + double threshold = MathHelper.clamp(config.loudnessThreshold, 0.0, 1.0); + int thresholdX = x + (int) (140 * threshold); + int thresholdColor = 0xFFFFFFFF; // white marker + ctx.fill(thresholdX, y, thresholdX + 1, y + 6, thresholdColor); + + if (normalized >= threshold) { + int highlightAlpha = (int) (Math.min(1.0, (normalized - threshold) / (1 - threshold)) * 120); + ctx.fill(x, y, x + filled, y + 6, (highlightAlpha << 24) | 0xFFFFFF00); + } + } + +} diff --git a/src/main/java/eu/projnull/spelis/note2input/KeyMapper.java b/src/main/java/eu/projnull/spelis/note2input/KeyMapper.java new file mode 100644 index 0000000..3f5680d --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/KeyMapper.java @@ -0,0 +1,73 @@ +package eu.projnull.spelis.note2input; + +import eu.projnull.spelis.note2input.config.ConfigManager; +import eu.projnull.spelis.note2input.io.AudioManager; +import eu.projnull.spelis.note2input.util.Note; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBinding; + +public class KeyMapper { + + private final MinecraftClient client = MinecraftClient.getInstance(); + private KeyBinding activeBinding = null; + + public void register() { + ClientTickEvents.END_CLIENT_TICK.register(client -> update()); + } + + private void update() { + if (!ConfigManager.get().enable) { + releaseActive(); + return; + } + + if (client.player == null || AudioManager.audioInput == null) { + releaseActive(); + return; + } + + double midi = AudioManager.audioInput.getCurrentMidi(); + boolean valid = AudioManager.audioInput.getCurrentMidiValidity(); + double loudness = AudioManager.audioInput.getCurrentLoudness(); + + KeyBinding newBinding = null; + + if (midi > 0 + && loudness > ConfigManager.get().loudnessThreshold + && valid) { + + String note = Note.midiToNoteName(midi); + newBinding = ConfigManager.getKeyBinding(note); + } + + handleBindingChange(newBinding); + } + + private void handleBindingChange(KeyBinding newBinding) { + if (newBinding == activeBinding) return; + + // Release old key if we owned one + if (activeBinding != null) { + activeBinding.setPressed(false); + } + + activeBinding = newBinding; + + // Press new key + if (activeBinding != null) { + activeBinding.setPressed(true); + } + } + + private void releaseActive() { + if (activeBinding != null) { + activeBinding.setPressed(false); + activeBinding = null; + } + } + + public KeyBinding getActiveBinding() { + return activeBinding; + } +} diff --git a/src/main/java/eu/projnull/spelis/note2input/Main.java b/src/main/java/eu/projnull/spelis/note2input/Main.java new file mode 100644 index 0000000..a7f25be --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/Main.java @@ -0,0 +1,45 @@ +package eu.projnull.spelis.note2input; + +import eu.projnull.spelis.note2input.config.ConfigManager; +import eu.projnull.spelis.note2input.io.AudioManager; +import eu.projnull.spelis.note2input.util.UtilityKeybinds; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry; +import net.fabricmc.fabric.api.client.rendering.v1.hud.VanillaHudElements; +import net.minecraft.client.MinecraftClient; +import net.minecraft.util.Identifier; + +public class Main implements ModInitializer { + public static final String MOD_ID = "note2input"; + + private final HudRenderer hudRenderer = new HudRenderer(); + private static final KeyMapper keyMapper = new KeyMapper(); + + @Override + public void onInitialize() { + ClientTickEvents.END_CLIENT_TICK.register(this::tick); + AudioManager.startAll(); + + UtilityKeybinds.register(); + keyMapper.register(); + + HudElementRegistry.attachElementBefore(VanillaHudElements.HOTBAR, Identifier.of(MOD_ID, "hud"), hudRenderer::render); + + ConfigManager.initializeDefaults(); + } + + private void tick(MinecraftClient client) { + if (!AudioManager.audioInput.getActiveDeviceName().equals(ConfigManager.get().selectedMic)) { + AudioManager.audioInput.restart(); + } + if (!AudioManager.audioOutput.getActiveDeviceName().equals(ConfigManager.get().selectedOutput)) { + AudioManager.audioOutput.restart(); + } + } + + public static KeyMapper getKeyMapper() { + return keyMapper; + } + +} diff --git a/src/main/java/eu/projnull/spelis/note2input/client/Client.java b/src/main/java/eu/projnull/spelis/note2input/client/Client.java new file mode 100644 index 0000000..0c99433 --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/client/Client.java @@ -0,0 +1,10 @@ +package eu.projnull.spelis.note2input.client; + +import net.fabricmc.api.ClientModInitializer; + +public class Client implements ClientModInitializer { + + @Override + public void onInitializeClient() { + } +} diff --git a/src/main/java/eu/projnull/spelis/note2input/client/DataGenerator.java b/src/main/java/eu/projnull/spelis/note2input/client/DataGenerator.java new file mode 100644 index 0000000..d1e5541 --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/client/DataGenerator.java @@ -0,0 +1,12 @@ +package eu.projnull.spelis.note2input.client; + +import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint; +import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator; + +public class DataGenerator implements DataGeneratorEntrypoint { + + @Override + public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { + FabricDataGenerator.Pack pack = fabricDataGenerator.createPack(); + } +} diff --git a/src/main/java/eu/projnull/spelis/note2input/config/Config.java b/src/main/java/eu/projnull/spelis/note2input/config/Config.java new file mode 100644 index 0000000..72e8480 --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/config/Config.java @@ -0,0 +1,17 @@ +package eu.projnull.spelis.note2input.config; + +import java.util.HashMap; +import java.util.Map; + +public class Config { + + public double loudnessThreshold = 0.02; + public double feedbackThreshold = 0.02; + public String selectedMic = ""; + public String selectedOutput = ""; + public boolean enable = true; + + public float gain = 1.0f; + + public Map<String, String> noteToKeyName = new HashMap<>(); +} diff --git a/src/main/java/eu/projnull/spelis/note2input/config/ConfigManager.java b/src/main/java/eu/projnull/spelis/note2input/config/ConfigManager.java new file mode 100644 index 0000000..136a0a8 --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/config/ConfigManager.java @@ -0,0 +1,88 @@ +package eu.projnull.spelis.note2input.config; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import eu.projnull.spelis.note2input.util.Note; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBinding; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; + +public class ConfigManager { + + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + + private static final Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("note2input.json"); + + private static Config config; + + public static Config get() { + if (config == null) load(); + return config; + } + + public static void load() { + + if (!Files.exists(CONFIG_PATH)) { + config = new Config(); + initializeDefaults(); + save(); + return; + } + + try (Reader reader = Files.newBufferedReader(CONFIG_PATH)) { + config = GSON.fromJson(reader, Config.class); + if (config == null) { + config = new Config(); + } + } catch (IOException e) { + e.printStackTrace(); + config = new Config(); + } + + initializeDefaults(); + } + + public static void save() { + if (config == null) return; + + try (Writer writer = Files.newBufferedWriter(CONFIG_PATH)) { + GSON.toJson(config, writer); + } catch (IOException e) { + e.printStackTrace(); + } + } + + // Utility functions + + public static void initializeDefaults() { + for (String note : Note.getSupportedNotes()) { + ConfigManager.get().noteToKeyName.putIfAbsent(note, "NONE"); + } + } + + public static KeyBinding getKeyBinding(String note) { + String keyName = ConfigManager.get().noteToKeyName.get(note); + if (keyName == null || keyName.equals("NONE")) return null; + return KeyBinding.byId(keyName); + } + + public static List<String> getAllKeyBindingNames() { + MinecraftClient client = MinecraftClient.getInstance(); + if (client == null || client.options == null) return List.of(); + + return Arrays.stream(client.options.allKeys) + .map(KeyBinding::getId) + .distinct() + .sorted() + .toList(); + } + +} diff --git a/src/main/java/eu/projnull/spelis/note2input/config/ConfigScreen.java b/src/main/java/eu/projnull/spelis/note2input/config/ConfigScreen.java new file mode 100644 index 0000000..16747b8 --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/config/ConfigScreen.java @@ -0,0 +1,137 @@ +package eu.projnull.spelis.note2input.config; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; +import eu.projnull.spelis.note2input.io.AudioInput; +import eu.projnull.spelis.note2input.io.AudioOutput; +import eu.projnull.spelis.note2input.util.Note; +import me.shedaniel.clothconfig2.api.ConfigBuilder; +import me.shedaniel.clothconfig2.api.ConfigCategory; +import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; + +import java.util.List; +import java.util.Objects; + +public class ConfigScreen implements ModMenuApi { + public static Screen create(Screen parent) { + Config configManager = ConfigManager.get(); + + ConfigBuilder builder = ConfigBuilder.create() + .setParentScreen(parent) + .setTitle(Text.translatable("config.note2input.title")); + + ConfigEntryBuilder entryBuilder = builder.entryBuilder(); + + ConfigCategory general = builder.getOrCreateCategory(Text.translatable("config.note2input.category.general")); + ConfigCategory keys = builder.getOrCreateCategory(Text.translatable("config.note2input.category.keys")); + + addGeneralSettings(general, entryBuilder, configManager); + addDeviceSettings(general, entryBuilder, configManager); + addKeyMappings(keys, entryBuilder, configManager); + + builder.setSavingRunnable(ConfigManager::save); + + return builder.build(); + } + + private static void addGeneralSettings( + ConfigCategory category, + ConfigEntryBuilder entryBuilder, + Config config + ) { + category.addEntry(entryBuilder + .startBooleanToggle(Text.translatable("config.note2input.enable"), config.enable) + .setSaveConsumer(value -> config.enable = value) + .build()); + + category.addEntry(entryBuilder + .startDoubleField(Text.translatable("config.note2input.input_threshold"), config.loudnessThreshold) + .setMin(0.0) + .setMax(1.0) + .setSaveConsumer(value -> config.loudnessThreshold = value) + .build()); + + category.addEntry(entryBuilder + .startDoubleField(Text.translatable("config.note2input.feedback_threshold"), config.feedbackThreshold) + .setMin(0.0) + .setMax(1.0) + .setSaveConsumer(value -> config.feedbackThreshold = value) + .build()); + + category.addEntry(entryBuilder + .startFloatField(Text.translatable("config.note2input.gain"), config.gain) + .setMin(0.1f) + .setMax(10.0f) + .setSaveConsumer(value -> config.gain = value) + .build()); + + } + + private static void addDeviceSettings( + ConfigCategory category, + ConfigEntryBuilder entryBuilder, + Config config + ) { + List<String> inputDevices = AudioInput.getInputDeviceNames(); + List<String> outputDevices = AudioOutput.getOutputDeviceNames(); + + addDropdown( + category, + entryBuilder, + Text.translatable("config.note2input.input_device"), + inputDevices, + config.selectedMic, + value -> config.selectedMic = value + ); + + addDropdown( + category, + entryBuilder, + Text.translatable("config.note2input.output_device"), + outputDevices, + config.selectedOutput, + value -> config.selectedOutput = value + ); + } + + private static void addDropdown( + ConfigCategory category, + ConfigEntryBuilder entryBuilder, + Text label, + List<String> selections, + String currentValue, + java.util.function.Consumer<String> saveConsumer + ) { + category.addEntry(entryBuilder + .startStringDropdownMenu(label, Objects.requireNonNullElse(currentValue, "")) + .setSelections(selections) + .setSaveConsumer(saveConsumer) + .build()); + } + + private static void addKeyMappings( + ConfigCategory category, + ConfigEntryBuilder entryBuilder, + Config config + ) { + List<String> keyNames = ConfigManager.getAllKeyBindingNames(); + + for (String note : Note.getSupportedNotes()) { + String currentMapping = config.noteToKeyName.getOrDefault(note, ""); + + category.addEntry(entryBuilder + .startStringDropdownMenu(Text.translatable("config.note2input.note_to_key", note), currentMapping) + .setSelections(keyNames) + .setSaveConsumer(value -> config.noteToKeyName.put(note, value)) + .build()); + } + } + + // ModMenu hook + @Override + public ConfigScreenFactory<?> getModConfigScreenFactory() { + return ConfigScreen::create; + } +} diff --git a/src/main/java/eu/projnull/spelis/note2input/io/AudioInput.java b/src/main/java/eu/projnull/spelis/note2input/io/AudioInput.java new file mode 100644 index 0000000..13685cd --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/io/AudioInput.java @@ -0,0 +1,369 @@ +package eu.projnull.spelis.note2input.io; + +import eu.projnull.spelis.note2input.config.ConfigManager; + +import javax.sound.sampled.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +// All of this is vibe coded, I know nothing about detecting pitch or actually getting audio input in the first place. +// The AudioOutput was also vibe coded, but I could probably figure that out +// Also this entire project is a side quest, so why should i even care +public final class AudioInput { + + private static final float SAMPLE_RATE = 48000f; + private static final int BUFFER_SIZE = 2048; + + private static AudioInput INSTANCE; + + public static AudioInput getInstance() { + if (INSTANCE == null) INSTANCE = new AudioInput(); + return INSTANCE; + } + + private static final AudioFormat FORMAT = + new AudioFormat(SAMPLE_RATE, 16, 1, true, false); + + private final float[] hannWindow = createHannWindow(BUFFER_SIZE / 2); + private final AudioOutput audioOutput = AudioOutput.getInstance(); + + private TargetDataLine line; + private volatile boolean running; + + private volatile double currentMidi = -1; + private volatile double currentLoudness = 0; + private volatile boolean validMidi = false; + + private String activeDeviceName = ""; + + private AudioInput() {} + + public void start() { + try { + line = createLineForSelectedDevice(); + line.open(FORMAT, BUFFER_SIZE); + line.start(); + + activeDeviceName = ConfigManager.get().selectedMic; + running = true; + + Thread thread = new Thread(this::captureLoop, "Mic-Capture"); + thread.setDaemon(true); + thread.start(); + + } catch (LineUnavailableException e) { + throw new RuntimeException("Mic unavailable", e); + } + } + + public synchronized void restart() { + stop(); + start(); + } + + private void captureLoop() { + byte[] buffer = new byte[BUFFER_SIZE]; + + while (running) { + int read = line.read(buffer, 0, buffer.length); + if (read > 0) processAudio(buffer, read); + } + } + + private void processAudio(byte[] data, int length) { + float[] samples = toFloatSamples(data, length); + for (int i = 0; i < samples.length; i++) { + samples[i] *= ConfigManager.get().gain; + } + + // Compute loudness for RMS / dB calculations + currentLoudness = calculateRMS(samples); + + if (currentLoudness > ConfigManager.get().loudnessThreshold && audioOutput != null) { + audioOutput.write(data, length); + } + + // Remove DC offset and smooth edges + removeDC(samples); + applyWindow(hannWindow); + + // Only attempt detection if loud enough + if (currentLoudness < ConfigManager.get().loudnessThreshold) { + validMidi = false; + return; + } + + // Ensemble pitch detection + double freq1 = detectPitchAutocorrelation(samples); + double freq2 = detectPitchAMDF(samples); + double freq3 = detectPitchFFT(samples); + + // Convert to MIDI + double midi1 = freq1 > 0 ? frequencyToMidi(freq1) : -1; + double midi2 = freq2 > 0 ? frequencyToMidi(freq2) : -1; + double midi3 = freq3 > 0 ? frequencyToMidi(freq3) : -1; + + // fuck it we ball (get the average note or something) + double detectedMidi = medianOfPositive(midi1, midi2, midi3); + + // Debounce / smoothing: only accept new note if consistent with previous frame + if (detectedMidi > 0) { + if (Math.abs(detectedMidi - currentMidi) < 1.0 || !validMidi) { + currentMidi = detectedMidi; + validMidi = true; + } else { + // Small flicker protection: keep previous note + validMidi = false; + } + } else { + validMidi = false; + } + } + + /** Helper: median ignoring -1 (invalid) values */ + private double medianOfPositive(double... vals) { + double[] positives = Arrays.stream(vals).filter(v -> v > 0).sorted().toArray(); + if (positives.length == 0) return -1; + int mid = positives.length / 2; + if (positives.length % 2 == 0) { + return (positives[mid - 1] + positives[mid]) / 2.0; + } else { + return positives[mid]; + } + } + + private static float[] toFloatSamples(byte[] data, int length) { + int sampleCount = length / 2; + float[] samples = new float[sampleCount]; + + for (int i = 0, s = 0; i < length; i += 2, s++) { + int low = data[i] & 0xff; + int high = data[i + 1]; + int value = (high << 8) | low; + samples[s] = value / 32768f; + } + + return samples; + } + + private static double calculateRMS(float[] samples) { + double sum = 0; + for (float s : samples) sum += s * s; + return Math.sqrt(sum / samples.length); + } + + private static void removeDC(float[] samples) { + double mean = 0; + for (float s : samples) mean += s; + mean /= samples.length; + for (int i = 0; i < samples.length; i++) + samples[i] -= mean; + } + + private void applyWindow(float[] samples) { + for (int i = 0; i < samples.length && i < hannWindow.length; i++) + samples[i] *= hannWindow[i]; + } + + private static float[] createHannWindow(int size) { + float[] window = new float[size]; + for (int i = 0; i < size; i++) { + window[i] = (float)(0.5 * (1 - Math.cos(2 * Math.PI * i / (size - 1)))); + } + return window; + } + + private double detectPitchAutocorrelation(float[] samples) { + int size = samples.length; + + int minLag = (int)(SAMPLE_RATE / 1000.0); // max 1kHz + int maxLag = (int)(SAMPLE_RATE / 50.0); // min 50Hz + + double bestCorr = 0; + int bestLag = -1; + + for (int lag = minLag; lag < maxLag; lag++) { + double corr = 0; + double norm1 = 0, norm2 = 0; + + for (int i = 0; i < size - lag; i++) { + corr += samples[i] * samples[i + lag]; + norm1 += samples[i]*samples[i]; + norm2 += samples[i + lag]*samples[i + lag]; + } + + if (norm1 > 0 && norm2 > 0) { + corr /= Math.sqrt(norm1 * norm2); + } + + if (corr > bestCorr) { + bestCorr = corr; + bestLag = lag; + } + } + + // confidence check + if (bestCorr < 0.2) return -1; + + // harmonic check: reduce octave doubling + int halfLag = bestLag / 2; + if (halfLag >= minLag) { + double halfCorr = 0, norm = 0; + for (int i = 0; i < size - halfLag; i++) { + halfCorr += samples[i] * samples[i + halfLag]; + norm += samples[i]*samples[i]; + } + if (norm > 0) halfCorr /= norm; + if (halfCorr > bestCorr * 0.9) bestLag = halfLag; + } + + return SAMPLE_RATE / bestLag; + } + + private void fft(double[] re, double[] im) { + int n = re.length; + if (Integer.bitCount(n) != 1) throw new IllegalArgumentException("FFT size must be a power of 2"); + + // Bit-reversal permutation + for (int i = 0, j = 0; i < n; i++) { + if (i < j) { + double tempRe = re[i], tempIm = im[i]; + re[i] = re[j]; im[i] = im[j]; + re[j] = tempRe; im[j] = tempIm; + } + int bit = n >> 1; + while ((j & bit) != 0) { + j ^= bit; + bit >>= 1; + } + j ^= bit; + } + + // Cooley-Tukey + for (int len = 2; len <= n; len <<= 1) { + double angle = -2 * Math.PI / len; + double wLenRe = Math.cos(angle); + double wLenIm = Math.sin(angle); + for (int i = 0; i < n; i += len) { + double wRe = 1.0, wIm = 0.0; + for (int j = 0; j < len / 2; j++) { + int u = i + j; + int v = i + j + len / 2; + double tRe = re[v] * wRe - im[v] * wIm; + double tIm = re[v] * wIm + im[v] * wRe; + + re[v] = re[u] - tRe; + im[v] = im[u] - tIm; + re[u] += tRe; + im[u] += tIm; + + double tempRe = wRe * wLenRe - wIm * wLenIm; + wIm = wRe * wLenIm + wIm * wLenRe; + wRe = tempRe; + } + } + } + } + + private double detectPitchFFT(float[] samples) { + int n = samples.length; + double[] re = new double[n]; + double[] im = new double[n]; + for (int i = 0; i < n; i++) re[i] = samples[i]; + Arrays.fill(im, 0); + + fft(re, im); + + double maxMag = 0; + int maxBin = -1; + for (int i = 1; i < n/2; i++) { + double freq = i * SAMPLE_RATE / n; + if (freq < 20 || freq > 500) continue; // bass range + double mag = Math.sqrt(re[i]*re[i] + im[i]*im[i]); + if (mag > maxMag) { + maxMag = mag; + maxBin = i; + } + } + + if (maxBin < 0) return -1; + return maxBin * SAMPLE_RATE / n; + } + + private double detectPitchAMDF(float[] samples) { + int size = samples.length; + int minLag = (int)(SAMPLE_RATE / 1000.0); // 1000 Hz + int maxLag = (int)(SAMPLE_RATE / 50.0); // 50 Hz + + double bestDiff = Double.MAX_VALUE; + int bestLag = -1; + + for (int lag = minLag; lag < maxLag; lag++) { + double diff = 0; + for (int i = 0; i < size - lag; i++) { + diff += Math.abs(samples[i] - samples[i + lag]); + } + diff /= size - lag; + + if (diff < bestDiff) { + bestDiff = diff; + bestLag = lag; + } + } + + if (bestLag <= 0) return -1; + return SAMPLE_RATE / bestLag; + } + + private static double frequencyToMidi(double f) { + return 69 + 12 * (Math.log(f / 440.0) / Math.log(2)); + } + + // ---------- Device ---------- + + private TargetDataLine createLineForSelectedDevice() throws LineUnavailableException { + + String selected = ConfigManager.get().selectedMic; + + for (Mixer.Info info : AudioSystem.getMixerInfo()) { + + if (!info.getName().equals(selected)) continue; + + Mixer mixer = AudioSystem.getMixer(info); + Line.Info lineInfo = new DataLine.Info(TargetDataLine.class, FORMAT); + + if (mixer.isLineSupported(lineInfo)) + return (TargetDataLine) mixer.getLine(lineInfo); + } + + return AudioSystem.getTargetDataLine(FORMAT); + } + + // ---------- Public State ---------- + + public double getCurrentMidi() { return currentMidi; } + public double getCurrentLoudness() { return currentLoudness; } + public boolean getCurrentMidiValidity() { return validMidi; } + public String getActiveDeviceName() { return activeDeviceName; } + + public synchronized void stop() { + running = false; + if (line != null) { + line.stop(); + line.flush(); + line.close(); + line = null; + } + } + + public static List<String> getInputDeviceNames() { + List<String> names = new ArrayList<>(); + for (Mixer.Info info : AudioSystem.getMixerInfo()) { + Mixer mixer = AudioSystem.getMixer(info); + if (mixer.getTargetLineInfo().length > 0) + names.add(info.getName()); + } + return names; + } +} diff --git a/src/main/java/eu/projnull/spelis/note2input/io/AudioManager.java b/src/main/java/eu/projnull/spelis/note2input/io/AudioManager.java new file mode 100644 index 0000000..18a97ae --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/io/AudioManager.java @@ -0,0 +1,13 @@ +package eu.projnull.spelis.note2input.io; + +// I had bigger plans for this class but ended up becoming a lazy duct tape situation +// at least there aren't 100 AudioInput.getInstance() calls everywhere. +public class AudioManager { + public static final AudioInput audioInput = AudioInput.getInstance(); + public static final AudioOutput audioOutput = AudioOutput.getInstance(); + + public static void startAll() { + audioOutput.start(); + audioInput.start(); + } +} diff --git a/src/main/java/eu/projnull/spelis/note2input/io/AudioOutput.java b/src/main/java/eu/projnull/spelis/note2input/io/AudioOutput.java new file mode 100644 index 0000000..54da28d --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/io/AudioOutput.java @@ -0,0 +1,117 @@ +package eu.projnull.spelis.note2input.io; + +import eu.projnull.spelis.note2input.config.ConfigManager; + +import javax.sound.sampled.*; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.LinkedBlockingQueue; + +// this is vibe coded, except for getOutputDeviceNames, which is vibe coded slightly differently +public final class AudioOutput { + + private static final float SAMPLE_RATE = 48000f; + private static final int BUFFER_SIZE = 2048; + private static final AudioFormat FORMAT = new AudioFormat(SAMPLE_RATE, 16, 1, true, false); + + private static AudioOutput INSTANCE; + private final LinkedBlockingQueue<byte[]> audioQueue = new LinkedBlockingQueue<>(); + private SourceDataLine line; + private volatile boolean running; + private String activeDeviceName = ""; + + private AudioOutput() { + } + + public static AudioOutput getInstance() { + if (INSTANCE == null) INSTANCE = new AudioOutput(); + return INSTANCE; + } + + public static List<String> getOutputDeviceNames() { + List<String> names = new ArrayList<>(); + for (Mixer.Info info : AudioSystem.getMixerInfo()) { + Mixer mixer = AudioSystem.getMixer(info); + if (mixer.getSourceLineInfo().length > 0) names.add(info.getName()); + } + return names; + } + + private static SourceDataLine createLineForDevice(String deviceName) throws LineUnavailableException { + + for (Mixer.Info info : AudioSystem.getMixerInfo()) { + if (!info.getName().equals(deviceName)) continue; + + Mixer mixer = AudioSystem.getMixer(info); + Line.Info lineInfo = new DataLine.Info(SourceDataLine.class, FORMAT); + + if (mixer.isLineSupported(lineInfo)) return (SourceDataLine) mixer.getLine(lineInfo); + } + + // fallback to default + return (SourceDataLine) AudioSystem.getLine(new DataLine.Info(SourceDataLine.class, FORMAT)); + } + + public synchronized void start(String deviceName) { + try { + line = createLineForDevice(deviceName); + line.open(FORMAT, BUFFER_SIZE); + line.start(); + + activeDeviceName = deviceName; + running = true; + + Thread playbackThread = new Thread(this::playbackLoop, "Speaker-Playback"); + playbackThread.setDaemon(true); + playbackThread.start(); + + } catch (LineUnavailableException e) { + throw new RuntimeException("Speaker unavailable: " + deviceName, e); + } + } + + public synchronized void start() { + start(ConfigManager.get().selectedOutput); + } + + public synchronized void stop() { + running = false; + if (line != null) { + line.drain(); + line.stop(); + line.flush(); + line.close(); + line = null; + } + audioQueue.clear(); + } + + public synchronized void restart() { + stop(); + start(); + } + + public void write(byte[] data, int length) { + if (!running) return; + + byte[] copy = new byte[length]; + System.arraycopy(data, 0, copy, 0, length); + audioQueue.offer(copy); + } + + // ---------- Internal ---------- + + public String getActiveDeviceName() { + return activeDeviceName; + } + + private void playbackLoop() { + while (running) { + try { + byte[] data = audioQueue.take(); + line.write(data, 0, data.length); + } catch (InterruptedException ignored) { + } + } + } +} diff --git a/src/main/java/eu/projnull/spelis/note2input/util/MiscUtil.java b/src/main/java/eu/projnull/spelis/note2input/util/MiscUtil.java new file mode 100644 index 0000000..756474d --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/util/MiscUtil.java @@ -0,0 +1,11 @@ +package eu.projnull.spelis.note2input.util; + +import net.minecraft.util.math.MathHelper; + +public class MiscUtil { + public static float normalizeDb(double db) { + double minDb = -60, maxDb = -30; + double clamped = MathHelper.clamp(db, minDb, maxDb); + return (float) ((clamped - minDb) / (maxDb - minDb)); + } +} diff --git a/src/main/java/eu/projnull/spelis/note2input/util/Note.java b/src/main/java/eu/projnull/spelis/note2input/util/Note.java new file mode 100644 index 0000000..b6d076f --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/util/Note.java @@ -0,0 +1,28 @@ +package eu.projnull.spelis.note2input.util; + +import java.util.ArrayList; +import java.util.List; + +public class Note { + private static final String[] NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"}; + + public static String midiToNoteName(double midi) { + if (midi <= 0) return "Unknown"; + + int rounded = (int) Math.round(midi); // round to nearest semitone + int noteIndex = rounded % 12; // 0=C, 1=C#, etc. + int octave = (rounded / 12) - 1; // MIDI 60 → C4 + + return NOTE_NAMES[noteIndex] + octave; + } + + public static List<String> getSupportedNotes() { + List<String> notes = new ArrayList<>(); + + for (int midi = 31; midi <= 83; midi++) { + notes.add(Note.midiToNoteName(midi)); + } + + return notes; + } +} diff --git a/src/main/java/eu/projnull/spelis/note2input/util/UtilityKeybinds.java b/src/main/java/eu/projnull/spelis/note2input/util/UtilityKeybinds.java new file mode 100644 index 0000000..249fdb0 --- /dev/null +++ b/src/main/java/eu/projnull/spelis/note2input/util/UtilityKeybinds.java @@ -0,0 +1,34 @@ +package eu.projnull.spelis.note2input.util; + +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; +import org.lwjgl.glfw.GLFW; + +public class UtilityKeybinds { + public static KeyBinding lookLeftKey; + public static KeyBinding lookRightKey; + public static KeyBinding lookUpKey; + public static KeyBinding lookDownKey; + + public static void register() { + // Horizontal + lookLeftKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.look_left", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_LEFT, KeyBinding.Category.MOVEMENT)); + + lookRightKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.look_right", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_RIGHT, KeyBinding.Category.MOVEMENT)); + + // Vertical + lookUpKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.look_up", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UP, KeyBinding.Category.MOVEMENT)); + + lookDownKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.look_down", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_DOWN, KeyBinding.Category.MOVEMENT)); + } + + public static void applyLookInput(MinecraftClient client, float alpha) { + if (client.player == null) return; + if (UtilityKeybinds.lookLeftKey.isPressed()) client.player.setYaw(client.player.getYaw() - alpha); + if (UtilityKeybinds.lookRightKey.isPressed()) client.player.setYaw(client.player.getYaw() + alpha); + if (UtilityKeybinds.lookUpKey.isPressed()) client.player.setPitch(client.player.getPitch() - alpha); + if (UtilityKeybinds.lookDownKey.isPressed()) client.player.setPitch(client.player.getPitch() + alpha); + } +} |
