Skip to content

Commit

Permalink
some audio work
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed Nov 14, 2024
1 parent 75929f2 commit 447095e
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 26 deletions.
10 changes: 10 additions & 0 deletions include/modules/audio/Audio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace love

bool play(Source* source);

void stop();

void stop(Source* source);

void pause(Source* source);
Expand All @@ -34,6 +36,14 @@ namespace love

float getVolume() const;

static bool play(const std::vector<Source*>& sources);

static void stop(const std::vector<Source*>& sources);

static void pause(const std::vector<Source*>& sources);

std::vector<Source*> pause();

private:
Pool* pool;

Expand Down
4 changes: 4 additions & 0 deletions include/modules/audio/wrap_Audio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ namespace Wrap_Audio

int stop(lua_State* L);

int setVolume(lua_State* L);

int getVolume(lua_State* L);

int open(lua_State* L);
} // namespace Wrap_Audio
1 change: 0 additions & 1 deletion platform/hac/include/driver/display/Framebuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ namespace love

dk::Image image;
CMemPool::Handle memory;
dk::ImageLayout layout;
};
} // namespace love
4 changes: 2 additions & 2 deletions platform/hac/source/common/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace love
// clang-format off
inline constinit ScreenInfo HANDHELD_MODE[0x01] =
{
{ 0, 0, "default", 1280, 720 },
{ 0, 0, "default", 1280, 720 }
};

inline constinit ScreenInfo DOCKED_MODE[0x01] =
{
{ 0, 0, "default", 1920, 1080 },
{ 0, 0, "default", 1920, 1080 }
};
// clang-format on

Expand Down
17 changes: 5 additions & 12 deletions platform/hac/source/driver/display/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,19 @@ namespace love
const auto flags = depth ? BASE_FLAGS : MAIN_FLAGS;
const auto format = depth ? DkImageFormat_Z24S8 : DkImageFormat_RGBA8_Unorm;

dk::ImageLayout layout {};
dk::ImageLayoutMaker { device }
.setFlags(flags)
.setFormat(format)
.setDimensions(info.width, info.height)
.initialize(this->layout);
.initialize(layout);

const auto layoutSize = this->layout.getSize();
const auto alignment = this->layout.getAlignment();

this->memory = images.allocate(layoutSize, alignment);

const auto& memoryBlock = this->memory.getMemBlock();
auto memoryOffset = this->memory.getOffset();

this->image.initialize(this->layout, memoryBlock, memoryOffset);
this->memory = images.allocate(layout.getSize(), layout.getAlignment());
this->image.initialize(layout, this->memory.getMemBlock(), this->memory.getOffset());
}

void Framebuffer::destroy()
{
if (this->memory)
this->memory.destroy();
this->memory.destroy();
}
} // namespace love
25 changes: 25 additions & 0 deletions source/modules/audio/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,41 @@ namespace love
return source->play();
}

bool Audio::play(const std::vector<Source*>& sources)
{
return Source::play(sources);
}

void Audio::stop()
{
Source::stop(this->pool);
}

void Audio::stop(Source* source)
{
source->stop();
}

void Audio::stop(const std::vector<Source*>& sources)
{
Source::stop(sources);
}

void Audio::pause(Source* source)
{
source->pause();
}

void Audio::pause(const std::vector<Source*>& sources)
{
Source::pause(sources);
}

std::vector<Source*> Audio::pause()
{
return Source::pause(this->pool);
}

void Audio::setVolume(float volume)
{
DigitalSound::getInstance().setMasterVolume(volume);
Expand Down
86 changes: 75 additions & 11 deletions source/modules/audio/wrap_Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,63 @@ int Wrap_Audio::newSource(lua_State* L)
return luax_typeerror(L, 1, "Decoder or SoundData");
}

static std::vector<Source*> readSourceList(lua_State* L, int index)
{
if (index < 0)
index += lua_gettop(L) + 1;

int count = luax_objlen(L, index);
std::vector<Source*> sources(count);

for (int i = 0; i < count; i++)
{
lua_rawgeti(L, index, i + 1);
sources[i] = luax_checksource(L, -1);
lua_pop(L, 1);
}

return sources;
}

static std::vector<Source*> readSourceVararg(lua_State* L, int index)
{
const auto top = lua_gettop(L);

if (index < 0)
index += top + 1;

int count = top - index + 1;
std::vector<Source*> sources(count);

for (int i = 0; i <= top; index++, i++)
sources[i] = luax_checksource(L, -1);

return sources;
}

int Wrap_Audio::play(lua_State* L)
{
if (lua_istable(L, 1))
return 0;
luax_pushboolean(L, instance()->play(readSourceList(L, 1)));
else if (lua_gettop(L) > 1)
return 0;
luax_pushboolean(L, instance()->play(readSourceVararg(L, 1)));
else
{
auto* source = luax_checksource(L, 1);
luax_pushboolean(L, source->play());

return 1;
}

return 1;
}

int Wrap_Audio::stop(lua_State* L)
{
if (lua_isnone(L, 1))
return 0;
instance()->stop();
if (lua_istable(L, 1))
return 0;
instance()->stop(readSourceList(L, 1));
else if (lua_gettop(L) > 1)
return 0;
instance()->stop(readSourceVararg(L, 1));
else
{
auto* source = luax_checksource(L, 1);
Expand All @@ -96,10 +130,23 @@ int Wrap_Audio::stop(lua_State* L)

int Wrap_Audio::pause(lua_State* L)
{
if (lua_istable(L, 1))
return 0;
if (lua_isnone(L, 1))
{
auto sources = instance()->pause();
lua_createtable(L, (int)sources.size(), 0);

for (int index = 0; index < (int)sources.size(); index++)
{
luax_pushtype(L, sources[index]);
lua_rawseti(L, -2, index + 1);
}

return 1;
}
else if (lua_istable(L, 1))
instance()->pause(readSourceList(L, 1));
else if (lua_gettop(L) > 1)
return 0;
instance()->pause(readSourceVararg(L, 1));
else
{
auto* source = luax_checksource(L, 1);
Expand All @@ -109,13 +156,30 @@ int Wrap_Audio::pause(lua_State* L)
return 0;
}

int Wrap_Audio::setVolume(lua_State* L)
{
float volume = luaL_checknumber(L, 1);
instance()->setVolume(volume);

return 0;
}

int Wrap_Audio::getVolume(lua_State* L)
{
lua_pushnumber(L, instance()->getVolume());

return 1;
}

// clang-format off
static constexpr luaL_Reg functions[]=
{
{ "newSource", Wrap_Audio::newSource },
{ "play", Wrap_Audio::play },
{ "stop", Wrap_Audio::stop },
{ "pause", Wrap_Audio::pause }
{ "pause", Wrap_Audio::pause },
{ "setVolume", Wrap_Audio::setVolume },
{ "getVolume", Wrap_Audio::getVolume }
};

static constexpr lua_CFunction types[]=
Expand Down

0 comments on commit 447095e

Please sign in to comment.