diff options
28 files changed, 1744 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5f737e --- /dev/null +++ b/.gitignore @@ -0,0 +1,119 @@ +# User-specific stuff +.idea/ + +*.iml +*.ipr +*.iws + +# IntelliJ +out/ +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +.gradle +build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Cache of project +.gradletasknamecache + +**/build/ + +# Common working directory +run/ +runs/ + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..c15d7ff --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2026 Spelis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..4298703 --- /dev/null +++ b/build.gradle @@ -0,0 +1,95 @@ +plugins { + id 'fabric-loom' version '1.15-SNAPSHOT' + id 'maven-publish' +} + +version = project.mod_version +group = project.maven_group + +base { + archivesName = project.archives_base_name +} + + +fabricApi { + configureDataGeneration { + client = true + } +} + +repositories { + maven { url "https://maven.terraformersmc.com/releases/" } + maven { url "https://maven.shedaniel.me/" } +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + modImplementation "me.shedaniel.cloth:cloth-config-fabric:21.11.153" + modImplementation "com.terraformersmc:modmenu:17.0.0-beta.1" +} + +processResources { + inputs.property "version", project.version + inputs.property "minecraft_version", project.minecraft_version + inputs.property "loader_version", project.loader_version + filteringCharset "UTF-8" + + filesMatching("fabric.mod.json") { + expand "version": project.version, + "minecraft_version": project.minecraft_version, + "loader_version": project.loader_version + } +} + +def targetJavaVersion = 21 +tasks.withType(JavaCompile).configureEach { + // ensure that the encoding is set to UTF-8, no matter what the system default is + // this fixes some edge cases with special characters not displaying correctly + // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html + // If Javadoc is generated, this must be specified in that task too. + it.options.encoding = "UTF-8" + if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) { + it.options.release.set(targetJavaVersion) + } +} + +java { + def javaVersion = JavaVersion.toVersion(targetJavaVersion) + if (JavaVersion.current() < javaVersion) { + toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion) + } + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() +} + +jar { + from("LICENSE") { + rename { "${it}_${project.archives_base_name}" } + } +} + +// configure the maven publication +publishing { + publications { + create("mavenJava", MavenPublication) { + artifactId = project.archives_base_name + from components.java + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..65c1f57 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,14 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G +# Fabric Properties +# check these on https://modmuss50.me/fabric.html +minecraft_version=1.21.11 +yarn_mappings=1.21.11+build.4 +loader_version=0.18.0 +# Mod Properties +mod_version=1.0 +maven_group=eu.projnull.spelis +archives_base_name=note2input +# Dependencies +# check this on https://modmuss50.me/fabric.html +fabric_version=0.141.3+1.21.11 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar Binary files differnew file mode 100644 index 0000000..f8e1ee3 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.jar diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..23449a2 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists @@ -0,0 +1,248 @@ +#!/bin/sh + +# +# Copyright © 2015 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..e509b2d --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,93 @@ +@rem
+@rem Copyright 2015 the original author or authors.
+@rem
+@rem Licensed under the Apache License, Version 2.0 (the "License");
+@rem you may not use this file except in compliance with the License.
+@rem You may obtain a copy of the License at
+@rem
+@rem https://www.apache.org/licenses/LICENSE-2.0
+@rem
+@rem Unless required by applicable law or agreed to in writing, software
+@rem distributed under the License is distributed on an "AS IS" BASIS,
+@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+@rem See the License for the specific language governing permissions and
+@rem limitations under the License.
+@rem
+@rem SPDX-License-Identifier: Apache-2.0
+@rem
+
+@if "%DEBUG%"=="" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%"=="" set DIRNAME=.
+@rem This is normally unused
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Resolve any "." and ".." in APP_HOME to make it shorter.
+for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if %ERRORLEVEL% equ 0 goto execute
+
+echo. 1>&2
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
+echo. 1>&2
+echo Please set the JAVA_HOME variable in your environment to match the 1>&2
+echo location of your Java installation. 1>&2
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto execute
+
+echo. 1>&2
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
+echo. 1>&2
+echo Please set the JAVA_HOME variable in your environment to match the 1>&2
+echo location of your Java installation. 1>&2
+
+goto fail
+
+:execute
+@rem Setup the command line
+
+
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
+
+:end
+@rem End local scope for the variables with windows NT shell
+if %ERRORLEVEL% equ 0 goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+set EXIT_CODE=%ERRORLEVEL%
+if %EXIT_CODE% equ 0 set EXIT_CODE=1
+if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
+exit /b %EXIT_CODE%
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..f91a4fe --- /dev/null +++ b/settings.gradle @@ -0,0 +1,9 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + gradlePluginPortal() + } +} 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); + } +} diff --git a/src/main/resources/assets/note2input/icon.png b/src/main/resources/assets/note2input/icon.png Binary files differnew file mode 100644 index 0000000..3b4b0ca --- /dev/null +++ b/src/main/resources/assets/note2input/icon.png diff --git a/src/main/resources/assets/note2input/lang/en_us.json b/src/main/resources/assets/note2input/lang/en_us.json new file mode 100644 index 0000000..54ca3c5 --- /dev/null +++ b/src/main/resources/assets/note2input/lang/en_us.json @@ -0,0 +1,21 @@ +{ + "key.look_left": "Look Left", + "key.look_right": "Look Right", + "key.look_up": "Look Up", + "key.look_down": "Look Down", + + "config.note2input.title": "Note2Input Settings", + + "config.note2input.category.general": "General", + "config.note2input.category.keys": "Keybindings", + + "config.note2input.enable": "Enable Mod", + "config.note2input.input_threshold": "Input Threshold", + "config.note2input.feedback_threshold": "Feedback Threshold", + "config.note2input.gain": "Gain", + + "config.note2input.input_device": "Input Device", + "config.note2input.output_device": "Feedback Output Device", + + "config.note2input.note_to_key": "Bind for %s" +}
\ No newline at end of file diff --git a/src/main/resources/banner.png b/src/main/resources/banner.png Binary files differnew file mode 100644 index 0000000..e4a3881 --- /dev/null +++ b/src/main/resources/banner.png diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..a52933d --- /dev/null +++ b/src/main/resources/fabric.mod.json @@ -0,0 +1,34 @@ +{ + "schemaVersion": 1, + "id": "note2input", + "version": "${version}", + "name": "Note2Input", + "description": "Turns notes from your microphone into input", + "authors": [], + "contact": {}, + "license": "MIT", + "icon": "assets/note2input/icon.png", + "environment": "client", + "entrypoints": { + "fabric-datagen": [ + "eu.projnull.spelis.note2input.client.DataGenerator" + ], + "client": [ + "eu.projnull.spelis.note2input.client.Client" + ], + "main": [ + "eu.projnull.spelis.note2input.Main" + ], + "modmenu": [ + "eu.projnull.spelis.note2input.config.ConfigScreen" + ] + }, + "mixins": [ + "note2input.mixins.json" + ], + "depends": { + "fabricloader": ">=${loader_version}", + "fabric-api": "*", + "minecraft": "${minecraft_version}" + } +} diff --git a/src/main/resources/note2input.mixins.json b/src/main/resources/note2input.mixins.json new file mode 100644 index 0000000..38e4c5b --- /dev/null +++ b/src/main/resources/note2input.mixins.json @@ -0,0 +1,16 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "eu.projnull.spelis.note2input.mixin", + "compatibilityLevel": "JAVA_21", + "mixins": [ + ], + "client": [ + ], + "injectors": { + "defaultRequire": 1 + }, + "overwrites": { + "requireAnnotations": true + } +} |
