Added Audio

This commit is contained in:
Tinglyyy
2026-03-01 02:55:12 +01:00
parent 2a1767ef74
commit 88ae3012f7
4 changed files with 103 additions and 4 deletions

View File

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

View File

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

View File

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