diff --git a/data/main.lua b/data/main.lua index d98863d..9d3ea8f 100644 --- a/data/main.lua +++ b/data/main.lua @@ -2,6 +2,8 @@ x=0 rot=0 +music_states = { "INVALID", "PLAYING", "PAUSED", "STOPPED", "DISABLED" } +music_pos = 0 function mini.init() s = surf.load("gfx/logo.gif") @@ -18,6 +20,7 @@ function mini.init() print("========================") f = font.load("font.fnt") + --music.play("mus_menu.ogg") end function mini.update() @@ -50,4 +53,30 @@ function mini.update() draw.text("0146",100,50,28) font.current(font.DEFAULT) rot=rot+1 + + -- MUSICA + draw.text("music:", 1, 20, 22) + draw.text("duration:" .. music.duration(), 1, 28, 28) + draw.text("position:" .. music.pos(), 1, 36, 28) + draw.text("state: " .. music_states[music.state()+1], 1, 44, 28) + + if key.press(key.P) then + if music.state() == music.PLAYING then + music.pause() + elseif music.state() == music.PAUSED then + music.resume() + else + music.play("mus_menu.ogg") + end + elseif key.press(key.S) then + music.stop() + elseif key.press(key.C) then + if music.state() == music.PLAYING then + music_pos = music.pos() + music.stop() + else + music.play("mus_menu.ogg") + music.pos(music_pos) + end + end end diff --git a/data/mus_menu.ogg b/data/mus_menu.ogg new file mode 100644 index 0000000..d9782ab Binary files /dev/null and b/data/mus_menu.ogg differ diff --git a/source/backends/SDL3/audio.cpp b/source/backends/SDL3/audio.cpp index 3be73a0..f4e1c5a 100644 --- a/source/backends/SDL3/audio.cpp +++ b/source/backends/SDL3/audio.cpp @@ -320,6 +320,7 @@ namespace backend if (m.stream) SDL_DestroyAudioStream(m.stream); m.stream = nullptr; + current = -1; } void fadeOut(int milliseconds) @@ -390,18 +391,39 @@ namespace backend { if (!music::enabled) return 0; if (current<0 || current>static_cast(musics.size())) { - log_msg(LOG_FAIL, "music::getPosition: Illegal music handle: %i\n", current); + //log_msg(LOG_FAIL, "music::getPosition: Illegal music handle: %i\n", current); return 0; } auto &m = musics[current]; if (m.state == state::invalid) { - log_msg(LOG_FAIL, "music::getPosition: Invalidated music handle: %i\n", current); + //log_msg(LOG_FAIL, "music::getPosition: Invalidated music handle: %i\n", current); return 0; } int sample = stb_vorbis_get_sample_offset(m.vorbis); return float(sample) / float(m.info.sample_rate); } + float getDuration() + { + if (!music::enabled) return 0; + if (current < 0 || current > static_cast(musics.size())) { + //log_msg(LOG_FAIL, "music::getDuration: Illegal music handle: %i\n", current); + return 0; + } + + auto &m = musics[current]; + if (m.state == state::invalid) { + //log_msg(LOG_FAIL, "music::getDuration: Invalidated music handle: %i\n", current); + return 0; + } + + // Total de muestras del stream + int total_samples = stb_vorbis_stream_length_in_samples(m.vorbis); + + // Convertir a segundos + return float(total_samples) / float(m.info.sample_rate); + } + void enable(bool value) { if (!value && music::enabled && current>=0 && current(musics.size()) && musics[current].state==state::playing) stop(); diff --git a/source/backends/backend.h b/source/backends/backend.h index 10c323b..6f01427 100644 --- a/source/backends/backend.h +++ b/source/backends/backend.h @@ -83,7 +83,7 @@ namespace backend namespace music { - enum state { invalid, playing, paused, stopped, disabled }; + enum state { invalid=0, playing=1, paused=2, stopped=3, disabled=4 }; int load(const char* filename); int load(const uint8_t* buffer, uint32_t length); @@ -97,6 +97,7 @@ namespace backend float setVolume(float vol); void setPosition(float value); float getPosition(); + float getDuration(); void enable(bool value); bool isEnabled(); } diff --git a/source/mini/audio/audio.cpp b/source/mini/audio/audio.cpp index 5a23d89..bec4ca5 100644 --- a/source/mini/audio/audio.cpp +++ b/source/mini/audio/audio.cpp @@ -38,6 +38,14 @@ namespace mini backend::audio::music::stop(); } + float duration() { + return backend::audio::music::getDuration(); + } + + int state() { + return backend::audio::music::getState(); + } + namespace pos { void set(float value) { diff --git a/source/mini/audio/audio.h b/source/mini/audio/audio.h index 9bbb7fe..6ed7c29 100644 --- a/source/mini/audio/audio.h +++ b/source/mini/audio/audio.h @@ -17,6 +17,8 @@ namespace mini void set(float value); float get(); } + float duration(); + int state(); namespace enable { void set(const bool value); const bool get(); diff --git a/source/mini/lua/lua.wrappers.cpp b/source/mini/lua/lua.wrappers.cpp index 7fa085e..c0737de 100644 --- a/source/mini/lua/lua.wrappers.cpp +++ b/source/mini/lua/lua.wrappers.cpp @@ -629,6 +629,16 @@ namespace mini } } + static int duration(lua_State *L) { + lua_pushnumber(L, mini::audio::music::duration()); + return 1; + } + + static int state(lua_State *L) { + lua_pushinteger(L, mini::audio::music::state()); + return 1; + } + static int enable(lua_State *L) { if (lua_gettop(L) == 0) { lua_pushboolean(L, mini::audio::music::enable::get()); @@ -1104,7 +1114,16 @@ namespace mini lua_pushcfunction(L,wrappers::music::resume); lua_setfield(L, -2, "resume"); lua_pushcfunction(L,wrappers::music::stop); lua_setfield(L, -2, "stop"); lua_pushcfunction(L,wrappers::music::pos); lua_setfield(L, -2, "pos"); + lua_pushcfunction(L,wrappers::music::duration); lua_setfield(L, -2, "duration"); + lua_pushcfunction(L,wrappers::music::state); lua_setfield(L, -2, "state"); lua_pushcfunction(L,wrappers::music::enable); lua_setfield(L, -2, "enabled"); + + lua_pushinteger(L, 0); lua_setfield(L, -2, "INVALID"); + lua_pushinteger(L, 1); lua_setfield(L, -2, "PLAYING"); + lua_pushinteger(L, 2); lua_setfield(L, -2, "PAUSED"); + lua_pushinteger(L, 3); lua_setfield(L, -2, "STOPPED"); + lua_pushinteger(L, 4); lua_setfield(L, -2, "DISABLED"); + lua_setglobal(L, "music"); lua_newtable(L); diff --git a/source/mini/version.h b/source/mini/version.h index 9cdd1c5..7996100 100644 --- a/source/mini/version.h +++ b/source/mini/version.h @@ -1,3 +1,3 @@ #pragma once -#define MINI_VERSION "1.5.10" +#define MINI_VERSION "1.5.11" diff --git a/vscode/library.lua b/vscode/library.lua index b2ae7c3..76e7406 100644 --- a/vscode/library.lua +++ b/vscode/library.lua @@ -399,6 +399,14 @@ function music.pos() end ---Set the playing position of the currently loaded song function music.pos(pos) end +---@return number time +---Get the duration of the currently loaded song +function music.duration() end + +---@return integer value +---Get the state of the music +function music.state() end + ---@return boolean value ---Get if music is enabled function music.enabled() end @@ -407,6 +415,12 @@ function music.enabled() end ---Set if music is enabled or not function music.enabled(value) end +music.INVALID = 0 +music.PLAYING = 1 +music.PAUSED = 2 +music.STOPPED = 3 +music.DISABLED = 4 + ---@class sound sound = {}