diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..5fa9493 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/main/java/org/openautonomousconnection/luascript/hosts/AudioHost.java b/src/main/java/org/openautonomousconnection/luascript/hosts/AudioHost.java index d40f52a..e05eb62 100644 --- a/src/main/java/org/openautonomousconnection/luascript/hosts/AudioHost.java +++ b/src/main/java/org/openautonomousconnection/luascript/hosts/AudioHost.java @@ -1,9 +1,28 @@ +/* + * This code, and therefore this entire project, is licensed under + * the Open Autonomous Public License (OAPL) v1.0, or one that + * derives from that license. + * For more details, please refer to the LICENSE file. + * + * Copyright (c) 2026 Maple. + */ + package org.openautonomousconnection.luascript.hosts; +import org.openautonomousconnection.luascript.type.AudioValue; + import java.io.File; +/** + * Abstract audio service host + */ public interface AudioHost { - void play(File audioFile); + /** + * Load an audio file as a LuaValue (AudioValue type) + * @param audioFile audio media file + * @return reference object to media + */ + AudioValue load(File audioFile); } diff --git a/src/main/java/org/openautonomousconnection/luascript/tables/AudioTable.java b/src/main/java/org/openautonomousconnection/luascript/tables/AudioTable.java index c0de21b..c1a67f1 100644 --- a/src/main/java/org/openautonomousconnection/luascript/tables/AudioTable.java +++ b/src/main/java/org/openautonomousconnection/luascript/tables/AudioTable.java @@ -17,12 +17,12 @@ public class AudioTable extends ScriptTable { protected void define(HostServices services) { AudioHost audioHost = services.audio().orElseThrow(() -> new IllegalStateException("AudioHost not provided")); - table().set("play", new OneArgFunction() { + table().set("load", new OneArgFunction() { @Override public LuaValue call(LuaValue arg) { String fileName = arg.checkjstring(); - audioHost.play(new File(fileName)); - return LuaValue.NIL; + + return audioHost.load(new File(fileName)); } }); } diff --git a/src/main/java/org/openautonomousconnection/luascript/type/AudioValue.java b/src/main/java/org/openautonomousconnection/luascript/type/AudioValue.java new file mode 100644 index 0000000..4583f1d --- /dev/null +++ b/src/main/java/org/openautonomousconnection/luascript/type/AudioValue.java @@ -0,0 +1,77 @@ +/* + * This code, and therefore this entire project, is licensed under + * the Open Autonomous Public License (OAPL) v1.0, or one that + * derives from that license. + * For more details, please refer to the LICENSE file. + * + * Copyright (c) 2026 Maple. + */ + +package org.openautonomousconnection.luascript.type; + +import javafx.scene.media.MediaPlayer; +import lombok.Getter; +import lombok.Setter; +import org.luaj.vm2.LuaFunction; +import org.luaj.vm2.LuaNumber; +import org.luaj.vm2.LuaTable; +import org.luaj.vm2.LuaValue; +import org.openautonomousconnection.luascript.tables.AudioTable; + +/** + * Different from {@link AudioTable} in that this is an instance gathered from {@code AudioTable::load} + */ +public class AudioValue extends LuaTable { + @Getter @Setter + private MediaPlayer mediaPlayer; + + public AudioValue(MediaPlayer mediaPlayer) { + this.mediaPlayer = mediaPlayer; + + this.set("play", new LuaFunction() { + @Override + public LuaValue call() { + mediaPlayer.play(); + + return LuaValue.NIL; + } + }); + + this.set("stop", new LuaFunction() { + @Override + public LuaValue call() { + mediaPlayer.stop(); + + return LuaValue.NIL; + } + }); + + this.set("pause", new LuaFunction() { + @Override + public LuaValue call() { + mediaPlayer.pause(); + + return LuaValue.NIL; + } + }); + + this.set("setVolume", new LuaFunction() { + @Override + public LuaValue call(LuaValue volume) { + double v = volume.checkdouble(); + + mediaPlayer.setVolume(Math.clamp(v, 0, 1)); + + return LuaValue.NIL; + } + }); + + this.set("getVolume", new LuaFunction() { + @Override + public LuaValue call() { + return LuaNumber.valueOf(mediaPlayer.getVolume()); + } + }); + } + +}