- [NEW] music.duration()

- [NEW] music.state()
- [NEW] contsants music.INVALID, music.PLAYING, music.PAUSED, music.STOPPED i music.DISABLED
- [NEW] El exemple en lua inclos te una secció per a mostrar les funcionalitats de la música
This commit is contained in:
2026-05-29 20:01:26 +02:00
parent eea4a67542
commit a45cb95030
9 changed files with 99 additions and 4 deletions
+29
View File
@@ -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
BIN
View File
Binary file not shown.
+24 -2
View File
@@ -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<int>(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<int>(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<static_cast<int>(musics.size()) && musics[current].state==state::playing) stop();
+2 -1
View File
@@ -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();
}
+8
View File
@@ -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)
{
+2
View File
@@ -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();
+19
View File
@@ -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);
+1 -1
View File
@@ -1,3 +1,3 @@
#pragma once
#define MINI_VERSION "1.5.10"
#define MINI_VERSION "1.5.11"
+14
View File
@@ -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 = {}