Skip to content

Commit

Permalink
expose sound channels params
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Auto committed Oct 14, 2023
1 parent ac41ad0 commit 242c30c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 8 deletions.
95 changes: 95 additions & 0 deletions src/game_api/script/sol_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#pragma once

#include <array>
#include <cstddef>
#include <cstdint>
#include <span>
#include <stdexcept>

template <class T>
struct ZeroIndexArray
{
using value_type = T;
using iterator_category = std::contiguous_iterator_tag;
using difference_type = size_t;
using pointer = T*;
using reference = T&;
using iterator = T*;

ZeroIndexArray(T* data, size_t size)
: data(data), data_size(size){};

ZeroIndexArray(std::span<T> arr)
{
data = arr.data();
data_size = arr.size();
};
~ZeroIndexArray()
{
data_size = 0;
};

T& operator[](int index)
{
return data[index];
}
T& operator=(T&& other) noexcept
{
// Guard self assignment
if (this == &other)
return *this;

data = other.data;
data_size = other.data_size;
return *this;
}
iterator begin() const
{
return iterator(data);
}
iterator end() const
{
return iterator(data + data_size);
}
bool empty() const
{
return data_size == 0;
}
size_t size() const
{
return data_size;
}
T* data;
size_t data_size{0};
};

namespace sol
{
template <class T>
struct is_container<ZeroIndexArray<T>> : std::true_type
{
};

template <class T>
struct usertype_container<ZeroIndexArray<T>>
{
static int size(lua_State* L)
{
ZeroIndexArray<T>& v = sol::stack::get<ZeroIndexArray<T>&>(L, 1);
return stack::push(L, v.size());
}
// Used by default implementation
static auto begin(lua_State*, ZeroIndexArray<T>& self)
{
return self.begin();
}
static auto end(lua_State*, ZeroIndexArray<T>& self)
{
return self.end();
}
static std::ptrdiff_t index_adjustment(lua_State*, ZeroIndexArray<T>&)
{
return 0;
}
};
} // namespace sol
11 changes: 7 additions & 4 deletions src/game_api/script/usertypes/sound_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "logger.h" // for DEBUG
#include "script/lua_backend.hpp" // for LuaBackend
#include "script/safe_cb.hpp" // for make_safe_cb
#include "script/sol_helper.hpp" //
#include "sound_manager.hpp" // for CustomSound, PlayingSound, SoundMa...
#include "string_aliases.hpp" // for VANILLA_SOUND

Expand Down Expand Up @@ -167,10 +168,12 @@ void register_usertypes(sol::state& lua, SoundManager* sound_manager)
&SoundMeta::x,
"y",
&SoundMeta::y,
//"left_channel",
//&SoundMeta::left_channel, // TODO: index 0-37 instead of 1-38
//"right_channel",
//&SoundMeta::right_channel,
"left_channel",
sol::property([&lua](SoundMeta* sm) // -> std::array<float, 38>
{ return ZeroIndexArray<float>(sm->left_channel) /**/; }),
"right_channel",
sol::property([](SoundMeta* sm) // -> std::array<float, 38>
{ return ZeroIndexArray<float>(sm->right_channel) /**/; }),
"start_over",
&SoundMeta::start_over,
"playing",
Expand Down
11 changes: 7 additions & 4 deletions src/game_api/sound_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,13 @@ struct SoundMeta
{
float x;
float y;
SoundInfo* sound_info; // param to FMOD::Studio::EventInstance::SetParameterByID (this ptr + 0x30)
uint64_t fmod_param_id; // param to FMOD::Studio::EventInstance::SetParameterByID
std::array<float, 38> left_channel; // VANILLA_SOUND_PARAM
std::array<float, 38> right_channel; // VANILLA_SOUND_PARAM
SoundInfo* sound_info; // param to FMOD::Studio::EventInstance::SetParameterByID (this ptr + 0x30)
uint64_t fmod_param_id; // param to FMOD::Studio::EventInstance::SetParameterByID

/// Use VANILLA_SOUND_PARAM as index, warning: special case with first index at 0, loop using pairs will get you all results but the key/index will be wrong, ipairs will have correct key/index but will skip the first element
std::array<float, 38> left_channel;
/// Use VANILLA_SOUND_PARAM as index warning: special case with first index at 0, loop using pairs will get you all results but the key/index will be wrong, ipairs will have correct key/index but will skip the first element
std::array<float, 38> right_channel;

/// when false, current track starts from the beginning, is immediately set back to true
bool start_over;
Expand Down

0 comments on commit 242c30c

Please sign in to comment.