Skip to content

Commit

Permalink
feat: add option to allow non default speakers
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Apr 11, 2024
1 parent f44947d commit 968c9d9
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 26 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.21)
project(venmic LANGUAGES CXX VERSION 3.3.5)
project(venmic LANGUAGES CXX VERSION 3.4.0)

# --------------------------------------------------------------------------------------------------------
# Library options
Expand Down Expand Up @@ -87,7 +87,7 @@ include("cmake/cpm.cmake")

CPMFindPackage(
NAME rohrkabel
VERSION 5.0
VERSION 5.1
GIT_REPOSITORY "https://github.com/Curve/rohrkabel"
)

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ The Rest-Server exposes three simple endpoints
The setting `ignore_devices` is optional and will default to `true`.
When enabled it will prevent hardware-devices like speakers and microphones from being linked to the virtual microphone.

The setting `only_default_speakers` is optional and will default to `true`.
When enabled it will prevent linking against nodes that don't play to the default speaker.

The setting `workaround` is also optional and will default to an empty array.
When set, venmic will redirect the first node that matches the all of the specified properties to itself.
When set, venmic will redirect the first node that matches all of the specified properties to itself.
</blockquote>

* (GET) `/unlink`
Expand Down
18 changes: 10 additions & 8 deletions addon/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ struct patchbay : public Napi::ObjectWrap<patchbay>
return Napi::Boolean::New(env, false);
}

auto include = to_array<vencord::prop>(data.Get("include"));
auto exclude = to_array<vencord::prop>(data.Get("exclude"));
auto ignore_devices = convert<bool>(data.Get("ignore_devices"));
auto workaround = to_array<vencord::prop>(data.Get("workaround"));
auto include = to_array<vencord::prop>(data.Get("include"));
auto exclude = to_array<vencord::prop>(data.Get("exclude"));
auto ignore_devices = convert<bool>(data.Get("ignore_devices"));
auto only_default_speakers = convert<bool>(data.Get("only_default_speakers"));
auto workaround = to_array<vencord::prop>(data.Get("workaround"));

if (!include && !exclude)
{
Expand All @@ -174,10 +175,11 @@ struct patchbay : public Napi::ObjectWrap<patchbay>
}

vencord::patchbay::get().link({
.include = include.value_or(std::vector<vencord::prop>{}),
.exclude = exclude.value_or(std::vector<vencord::prop>{}),
.ignore_devices = ignore_devices.value_or(true),
.workaround = workaround.value_or(std::vector<vencord::prop>{}),
.include = include.value_or(std::vector<vencord::prop>{}),
.exclude = exclude.value_or(std::vector<vencord::prop>{}),
.ignore_devices = ignore_devices.value_or(true),
.only_default_speakers = only_default_speakers.value_or(true),
.workaround = workaround.value_or(std::vector<vencord::prop>{}),
});

return Napi::Boolean::New(env, true);
Expand Down
4 changes: 3 additions & 1 deletion include/vencord/patchbay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ namespace vencord
std::vector<prop> include;
std::vector<prop> exclude;

public:
bool ignore_devices{true};
bool only_default_speakers{true};

public:
std::vector<prop> workaround;
};

Expand Down
2 changes: 2 additions & 0 deletions lib/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface LinkData
exclude: Prop[];

ignore_devices?: boolean;
only_default_speakers?: boolean;

workaround?: Prop[];
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"private": false,
"license": "MPL-2.0",
"author": "Curve (https://github.com/Curve)",
"version": "3.3.5",
"version": "3.4.0",
"main": "./lib/index.js",
"types": "./lib/module.d.ts",
"scripts": {
Expand Down
7 changes: 4 additions & 3 deletions server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ template <>
struct glz::meta<vencord::link_options>
{
using T = vencord::link_options;
static constexpr auto value = object("exclude", &T::exclude, //
"include", &T::include, //
"ignore_devices", &T::ignore_devices, //
static constexpr auto value = object("exclude", &T::exclude, //
"include", &T::include, //
"ignore_devices", &T::ignore_devices, //
"only_default_speakers", &T::only_default_speakers, //
"workaround", &T::workaround);
};

Expand Down
30 changes: 21 additions & 9 deletions src/patchbay.impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,30 @@ namespace vencord

void patchbay::impl::on_link(std::uint32_t id)
{
if (!speaker || !options.include.empty())
if (!options.include.empty() || (options.only_default_speakers && !speaker))
{
return;
}

auto &info = links[id];

if (info.input.node != speaker->id)
const auto output_id = info.output.node;
const auto input_id = info.input.node;

if (options.only_default_speakers && input_id != speaker->id)
{
logger::get()->trace("[patchbay] (on_link) {} is not connected to speaker but with {}", id,
info.input.node);
logger::get()->trace("[patchbay] (on_link) {} is not connected to speaker but with {}", id, input_id);
return;
}

const auto node = info.output.node;
auto &output = nodes[node]; // The node emitting sound
auto &output = nodes[output_id]; // The node emitting sound
auto &input = nodes[input_id]; // The node receiving sound

if (!options.only_default_speakers && input.info.props["device.id"].empty())
{
logger::get()->trace("[patchbay] (on_link) {} is not playing to a device: {}", id, input_id);
return;
}

auto match = [&output](const auto &prop)
{
Expand All @@ -288,7 +296,7 @@ namespace vencord

core->update();

link(node);
link(output_id);
}

void patchbay::impl::on_node(std::uint32_t id)
Expand Down Expand Up @@ -439,9 +447,13 @@ namespace vencord
logger::get()->info("[patchbay] (handle) found default metadata: {}", global.id);

meta_listener->on<pw::metadata_event::property>(
[this]<typename... T>(T &&...args)
[this](const char *key, auto property)
{
meta_update(std::forward<T>(args)...);
if (key)
{
meta_update(key, std::move(property));
}

return 0;
});

Expand Down
5 changes: 4 additions & 1 deletion tests/node/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ assert.throws(() => patchbay.link({ include: {} }), /key-value/ig);

assert.doesNotThrow(() => patchbay.link({ include: [{ key: "node.name", value: "Firefox" }] }));
assert.doesNotThrow(() => patchbay.link({ exclude: [{ key: "node.name", value: "Firefox" }] }));
assert.doesNotThrow(() => patchbay.link({ exclude: [{ key: "node.name", value: "Firefox" }], ignore_devices: true }));

assert.doesNotThrow(() => patchbay.link({ include: [{ key: "node.name", value: "Firefox" }], exclude: [{ key: "object.id", value: "100" }] }));

assert.doesNotThrow(() => patchbay.link({ exclude: [{ key: "node.name", value: "Firefox" }], ignore_devices: true }));
assert.doesNotThrow(() => patchbay.link({ exclude: [{ key: "node.name", value: "Firefox" }], ignore_devices: true, only_default_speakers: true }));

assert.doesNotThrow(() => patchbay.unlink());

0 comments on commit 968c9d9

Please sign in to comment.