commit 4c745909a7e02470d669f8ffc4cd0f0184e4f5b0
Author: Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Sep 20 20:23:57 2025 -0600
Commit: Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Sep 20 20:23:57 2025 -0600
add return value for sfx
Not thoroughly tested to match yet, but its there and works in basic cases
---
source/Audio.cpp | 22 ++++++++++++----
source/Audio.h | 2 +-
source/picoluaapi.cpp | 10 +++++--
test/audiotests.cpp | 22 ++++++++--------
test/picoluaapitests.cpp | 68 ++++++++++++++++++++++++------------------------
5 files changed, 71 insertions(+), 53 deletions(-)
diff --git a/source/Audio.cpp b/source/Audio.cpp
index 4c25f3b..f1c5508 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -45,10 +45,20 @@ audioState_t* Audio::getAudioState() {
return &_audioState;
}
-void Audio::api_sfx(int sfx, int channel, int offset){
+int Audio::api_sfx(int sfx, int channel, int offset, int length){
+ //TODO: handle length
+ //match pico 8 behavior
+ if (sfx < -2) {
+ sfx = 0;
+ }
+ if (length < 0) {
+ length = 31;
+ }
- if (sfx < -2 || sfx > 63 || channel < -2 || channel > 3 || offset > 31) {
- return;
+ //pico 8 returns nil in some of these cases, but I think I need to handle that
+ // in the lua api
+ if (sfx > 63 || channel < -2 || channel > 3 || offset > 31 || length > 31) {
+ return 0;
}
//CHANNEL -2: to stop the given sound from playing on any channel
@@ -58,7 +68,7 @@ void Audio::api_sfx(int sfx, int channel, int offset){
_audioState._sfxChannels[i].sfxId = -1;
}
}
- return;
+ return 0;
}
if (sfx == -1)
@@ -122,7 +132,9 @@ void Audio::api_sfx(int sfx, int channel, int offset){
_audioState._sfxChannels[channel].prev_note.n.setKey(24);
// There is no default value for “previous volume”.
_audioState._sfxChannels[channel].prev_note.n.setVolume(0);
- }
+ }
+
+ return channel;
}
void Audio::api_music(int pattern, int16_t fade_len, int16_t mask){
diff --git a/source/Audio.h b/source/Audio.h
index 4d8f4db..cc36bc5 100644
--- a/source/Audio.h
+++ b/source/Audio.h
@@ -103,7 +103,7 @@ class Audio {
void resetAudioState();
audioState_t* getAudioState();
- void api_sfx(int sfx, int channel, int offset);
+ int api_sfx(int sfx, int channel, int offset, int length);
void api_music(int pattern, int16_t fade_len, int16_t mask);
int16_t getCurrentSfxId(int channel);
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 5cbdfde..3417964 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -1043,9 +1043,15 @@ int sfx(lua_State *L) {
if (lua_gettop(L) > 2) {
offset = (int)lua_tonumber(L, 3);
}
+ int length = -1;
+ if (lua_gettop(L) > 3) {
+ length = (int)lua_tonumber(L, 4);
+ }
- _audioForLuaApi->api_sfx((int)n, channel, offset);
- return 0;
+ int result = _audioForLuaApi->api_sfx((int)n, channel, offset, length);
+ lua_pushnumber(L, result);
+
+ return 1;
}
//Memory
diff --git a/test/audiotests.cpp b/test/audiotests.cpp
index 1decba9..4a373e1 100644
--- a/test/audiotests.cpp
+++ b/test/audiotests.cpp
@@ -176,24 +176,24 @@ TEST_CASE("audio class behaves as expected") {
SUBCASE("api_sfx() with valid values sets the sfx to be played") {
int channel = 0;
int sfxId = 3;
- audio->api_sfx(sfxId, channel, 0);
+ audio->api_sfx(sfxId, channel, 0, 31);
CHECK_EQ(audioState->_sfxChannels[0].sfxId, sfxId);
}
SUBCASE("api_sfx() with -1 channel finds first available") {
- audio->api_sfx(1, 0, 0);
- audio->api_sfx(2, 1, 0);
- audio->api_sfx(5, -1, 0);
+ audio->api_sfx(1, 0, 0, 31);
+ audio->api_sfx(2, 1, 0, 31);
+ audio->api_sfx(5, -1, 0, 31);
CHECK_EQ(audioState->_sfxChannels[2].sfxId, 5);
}
SUBCASE("api_sfx() with -2 channel stops the sfx on any channel") {
- audio->api_sfx(1, 0, 0);
- audio->api_sfx(10, 1, 0);
- audio->api_sfx(20, 2, 0);
- audio->api_sfx(1, 3, 0);
- audio->api_sfx(1, -2, 0);
+ audio->api_sfx(1, 0, 0, 31);
+ audio->api_sfx(10, 1, 0, 31);
+ audio->api_sfx(20, 2, 0, 31);
+ audio->api_sfx(1, 3, 0, 31);
+ audio->api_sfx(1, -2, 0, 31);
CHECK_EQ(audioState->_sfxChannels[0].sfxId, -1);
@@ -204,14 +204,14 @@ TEST_CASE("audio class behaves as expected") {
SUBCASE("api_sfx() -2 sfx id stops looping") {
audioState->_sfxChannels[3].can_loop = true;
- audio->api_sfx(-2, 3, 0);
+ audio->api_sfx(-2, 3, 0, 31);
CHECK_EQ(audioState->_sfxChannels[3].can_loop, false);
}
SUBCASE("api_sfx() -2 sfx id stops looping") {
audioState->_sfxChannels[3].can_loop = true;
- audio->api_sfx(-2, 3, 0);
+ audio->api_sfx(-2, 3, 0, 31);
CHECK_EQ(audioState->_sfxChannels[3].can_loop, false);
}
diff --git a/test/picoluaapitests.cpp b/test/picoluaapitests.cpp
index 9af2a03..32845cc 100644
--- a/test/picoluaapitests.cpp
+++ b/test/picoluaapitests.cpp
@@ -26,40 +26,40 @@ TEST_CASE("audio stats") {
lua_State *L = luaL_newstate();
SUBCASE("get sfx 0") {
- audio->api_sfx(5,0,0);
- audio->api_sfx(6,1,0);
- audio->api_sfx(7,2,0);
- audio->api_sfx(8,3,0);
+ audio->api_sfx(5,0,0,31);
+ audio->api_sfx(6,1,0,31);
+ audio->api_sfx(7,2,0,31);
+ audio->api_sfx(8,3,0,31);
lua_pushnumber(L, 16);
stat(L);
CHECK_EQ((int) lua_tonumber(L, -1), 5);
}
SUBCASE("get sfx 1") {
- audio->api_sfx(5,0,0);
- audio->api_sfx(6,1,0);
- audio->api_sfx(7,2,0);
- audio->api_sfx(8,3,0);
+ audio->api_sfx(5,0,0,31);
+ audio->api_sfx(6,1,0,31);
+ audio->api_sfx(7,2,0,31);
+ audio->api_sfx(8,3,0,31);
lua_pushnumber(L, 17);
stat(L);
CHECK_EQ((int) lua_tonumber(L, -1), 6);
}
SUBCASE("get sfx 2") {
- audio->api_sfx(5,0,0);
- audio->api_sfx(6,1,0);
- audio->api_sfx(7,2,0);
- audio->api_sfx(8,3,0);
+ audio->api_sfx(5,0,0,31);
+ audio->api_sfx(6,1,0,31);
+ audio->api_sfx(7,2,0,31);
+ audio->api_sfx(8,3,0,31);
lua_pushnumber(L, 18);
stat(L);
CHECK_EQ((int) lua_tonumber(L, -1), 7);
}
SUBCASE("get sfx 3") {
- audio->api_sfx(5,0,0);
- audio->api_sfx(6,1,0);
- audio->api_sfx(7,2,0);
- audio->api_sfx(8,3,0);
+ audio->api_sfx(5,0,0,31);
+ audio->api_sfx(6,1,0,31);
+ audio->api_sfx(7,2,0,31);
+ audio->api_sfx(8,3,0,31);
lua_pushnumber(L, 19);
stat(L);
@@ -67,40 +67,40 @@ TEST_CASE("audio stats") {
}
SUBCASE("get sfx 0 updated") {
- audio->api_sfx(5,0,0);
- audio->api_sfx(6,1,0);
- audio->api_sfx(7,2,0);
- audio->api_sfx(8,3,0);
+ audio->api_sfx(5,0,0,31);
+ audio->api_sfx(6,1,0,31);
+ audio->api_sfx(7,2,0,31);
+ audio->api_sfx(8,3,0,31);
lua_pushnumber(L, 46);
stat(L);
CHECK_EQ((int) lua_tonumber(L, -1), 5);
}
SUBCASE("get sfx 1 updated") {
- audio->api_sfx(5,0,0);
- audio->api_sfx(6,1,0);
- audio->api_sfx(7,2,0);
- audio->api_sfx(8,3,0);
+ audio->api_sfx(5,0,0,31);
+ audio->api_sfx(6,1,0,31);
+ audio->api_sfx(7,2,0,31);
+ audio->api_sfx(8,3,0,31);
lua_pushnumber(L, 47);
stat(L);
CHECK_EQ((int) lua_tonumber(L, -1), 6);
}
SUBCASE("get sfx 2 updated") {
- audio->api_sfx(5,0,0);
- audio->api_sfx(6,1,0);
- audio->api_sfx(7,2,0);
- audio->api_sfx(8,3,0);
+ audio->api_sfx(5,0,0,31);
+ audio->api_sfx(6,1,0,31);
+ audio->api_sfx(7,2,0,31);
+ audio->api_sfx(8,3,0,31);
lua_pushnumber(L, 48);
stat(L);
CHECK_EQ((int) lua_tonumber(L, -1), 7);
}
SUBCASE("get sfx 3 updated") {
- audio->api_sfx(5,0,0);
- audio->api_sfx(6,1,0);
- audio->api_sfx(7,2,0);
- audio->api_sfx(8,3,0);
+ audio->api_sfx(5,0,0,31);
+ audio->api_sfx(6,1,0,31);
+ audio->api_sfx(7,2,0,31);
+ audio->api_sfx(8,3,0,31);
lua_pushnumber(L, 49);
stat(L);
@@ -108,14 +108,14 @@ TEST_CASE("audio stats") {
}
SUBCASE("get note 0") {
- audio->api_sfx(5,0,0);
+ audio->api_sfx(5,0,0,31);
lua_pushnumber(L, 20);
stat(L);
CHECK_EQ((int) lua_tonumber(L, -1), 0);
}
SUBCASE("get note 0 updated") {
- audio->api_sfx(5,0,0);
+ audio->api_sfx(5,0,0,31);
lua_pushnumber(L, 20);
stat(L);
CHECK_EQ((int) lua_tonumber(L, -1), 0);