Add API methods for controlling audio

Hook up Update() callback
Add Audio QOL plugin with mute/unmute hotkey and better settings persistence
This commit is contained in:
Ceikry 2022-09-26 21:05:02 -05:00
parent 7a7be2a54b
commit ec255eea52
5 changed files with 130 additions and 4 deletions

View file

@ -44,7 +44,9 @@ public abstract class Plugin {
public void OnXPUpdate(int skill, int xp) {} public void OnXPUpdate(int skill, int xp) {}
/** /**
* Update() is called once per tick, aka once every 600ms. * Update() is called once every 1000 client loops.
* This should be used for things that do need to update occasionally during runtime,
* but don't need to update super often.
*/ */
public void Update() {} public void Update() {}

View file

@ -257,4 +257,56 @@ public class API {
public static void DispatchCommand(String command) { public static void DispatchCommand(String command) {
Cheat.sendCheatPacket(JagString.of(command)); Cheat.sendCheatPacket(JagString.of(command));
} }
public static void PlaySound(int volume, int trackId, int delay) {
SoundPlayer.play(volume, trackId, delay);
}
public static void PlayMusic(int volume, int trackId) {
MidiPlayer.playFadeOut(trackId, client.js5Archive6, volume);
}
public static void PlayJingle(int jingleId) {
MusicPlayer.playJingle(-1, jingleId);
}
public static void SetMusicVolume(int volume) {
Preferences.musicVolume = volume;
}
public static int GetMusicVolume() {
return Preferences.musicVolume;
}
public static void SetSoundVolume(int volume) {
Preferences.soundEffectVolume = volume;
}
public static int GetSoundVolume() {
return Preferences.soundEffectVolume;
}
public static void SetAmbientVolume(int volume) {
Preferences.ambientSoundsVolume = volume;
}
public static int GetAmbientVolume() {
return Preferences.ambientSoundsVolume;
}
public static boolean IsMusicPlaying() {
return MidiPlayer.isPlaying();
}
public static boolean IsSoundPlaying() {
return SoundPlayer.size != 0;
}
public static void SendMessage(String message) {
Chat.add(JagString.EMPTY, 0, JagString.of(message));
}
public static void RequestNewSong() {
SoundPlayer.sendTrackEndPacket();
}
} }

View file

@ -128,9 +128,13 @@ public class SoundPlayer {
} }
MidiPlayer.jingle = false; MidiPlayer.jingle = false;
} else if (Preferences.musicVolume != 0 && MusicPlayer.groupId != -1 && !MidiPlayer.isPlaying()) { } else if (Preferences.musicVolume != 0 && MusicPlayer.groupId != -1 && !MidiPlayer.isPlaying()) {
sendTrackEndPacket();
}
}
public static void sendTrackEndPacket() {
Protocol.outboundBuffer.p1isaac(137); Protocol.outboundBuffer.p1isaac(137);
Protocol.outboundBuffer.p4(MusicPlayer.groupId); Protocol.outboundBuffer.p4(MusicPlayer.groupId);
MusicPlayer.groupId = -1; MusicPlayer.groupId = -1;
} }
} }
}

View file

@ -1589,6 +1589,7 @@ public final class client extends GameShell {
@Pc(24) GregorianCalendar gregorianCalendar = new GregorianCalendar(); @Pc(24) GregorianCalendar gregorianCalendar = new GregorianCalendar();
MiniMenu.gregorianDateSeed = gregorianCalendar.get(Calendar.HOUR_OF_DAY) * 600 + gregorianCalendar.get(Calendar.MINUTE) * 10 + gregorianCalendar.get(Calendar.SECOND) / 6; MiniMenu.gregorianDateSeed = gregorianCalendar.get(Calendar.HOUR_OF_DAY) * 600 + gregorianCalendar.get(Calendar.MINUTE) * 10 + gregorianCalendar.get(Calendar.SECOND) / 6;
aRandom1.setSeed(MiniMenu.gregorianDateSeed); aRandom1.setSeed(MiniMenu.gregorianDateSeed);
PluginRepository.Update();
} }
this.js5NetworkLoop(); this.js5NetworkLoop();
if (js5MasterIndex != null) { if (js5MasterIndex != null) {

View file

@ -0,0 +1,67 @@
package AudioQOL
import plugin.Plugin
import plugin.annotations.PluginMeta
import plugin.api.API
import plugin.api.FontColor
import plugin.api.FontType
import plugin.api.TextModifier
import rt4.Keyboard
import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent
@PluginMeta(
author = "Ceikry",
description = "Provides some QOL for audio, including better settings persistence",
version = 1.0
)
class plugin : Plugin() {
var isMute = false
var lastVolumes: Triple<Int,Int,Int>? = null
override fun Update() {
if (isMute) return
API.StoreData("sound-vol", API.GetSoundVolume())
API.StoreData("music-vol", API.GetMusicVolume())
API.StoreData("amb-vol", API.GetAmbientVolume())
}
override fun Init() {
val soundVol = API.GetData("sound-vol") as? Int ?: 255
val musicVol = API.GetData("music-vol") as? Int ?: 127
val ambVol = API.GetData("amb-vol") as? Int ?: 127
API.SetMusicVolume(soundVol)
API.SetAmbientVolume(musicVol)
API.SetSoundVolume(ambVol)
API.AddKeyboardListener(object : KeyAdapter() {
override fun keyPressed(e: KeyEvent) {
if (API.IsKeyPressed(Keyboard.KEY_CTRL) && e.keyCode == KeyEvent.VK_M) {
toggleMute()
}
}
})
}
private fun toggleMute() {
isMute = !isMute
if (isMute) {
lastVolumes = Triple(API.GetMusicVolume(), API.GetSoundVolume(), API.GetAmbientVolume())
API.SetMusicVolume(0)
API.SetSoundVolume(0)
API.SetAmbientVolume(0)
API.SendMessage("Audio Muted.")
if (API.IsMusicPlaying()) {
API.PlayMusic(0, -1)
}
} else {
API.SetMusicVolume(lastVolumes?.first ?: 255)
API.SetSoundVolume(lastVolumes?.second ?: 127)
API.SetAmbientVolume(lastVolumes?.third ?: 127)
API.SendMessage("Audio Unmuted.")
if ((lastVolumes?.first ?: 255) > 0) {
API.RequestNewSong()
}
}
}
}