- [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:
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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,3 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define MINI_VERSION "1.5.10"
|
||||
#define MINI_VERSION "1.5.11"
|
||||
|
||||
Reference in New Issue
Block a user