-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume_resource.cpp
More file actions
82 lines (69 loc) · 1.84 KB
/
volume_resource.cpp
File metadata and controls
82 lines (69 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Created by Bobini on 10/02/2026.
//
#include "volume_resource.h"
#include "safe_main_thread_call.h"
volume_resource::volume_resource()
: resource("volume://."),
play_callback_impl_base(flag_on_volume_change)
{
}
mcp::json volume_resource::get_metadata() const
{
return {
{"uri", get_uri()},
{"name", "Volume"},
{
"description",
"Tracks the playback volume and mute state. "
"Subscribe to this resource to get notified of volume changes. "
"To control the volume, use the set_volume or toggle_mute tools."
}
};
}
mcp::json volume_resource::read() const
{
auto [volume_db, is_muted, custom_mode] = safe_main_thread_call([]()
{
auto pc = playback_control::get();
float volume = pc->get_volume();
bool muted = pc->is_muted();
// Check if custom volume mode is active (v3 API)
bool custom_mode = false;
auto pc_v3 = playback_control_v3::get();
if (pc_v3.is_valid())
{
custom_mode = pc_v3->custom_volume_is_active();
}
return std::make_tuple(volume, muted, custom_mode);
});
mcp::json result = {
{"uri", get_uri()},
{"volume_db", volume_db},
{"is_muted", is_muted}
};
// Format a human-readable text description
std::string text;
if (is_muted)
{
text = "Volume: Muted";
}
else if (custom_mode)
{
text = std::format("Volume: {} dB (custom mode)", volume_db);
}
else
{
text = std::format("Volume: {} dB", volume_db);
}
result["text"] = text;
return result;
}
void volume_resource::on_volume_change(float p_new_val)
{
notify_change();
}
void volume_resource::notify_change() const
{
mcp::resource_manager::instance().notify_resource_changed(get_uri());
}