Repository navigation
Expand file tree
/
Copy pathExport.cpp
More file actions
232 lines (200 loc) · 7.31 KB
/
Copy pathExport.cpp
File metadata and controls
232 lines (200 loc) · 7.31 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <cstdint>
#include <cstddef>
#include <map>
#include <mutex>
#include <set>
#include <stdexcept>
#include <string>
#include <utility>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
namespace {
constexpr std::int32_t PortInDevice = 0;
constexpr std::int32_t PortInPcm = 1;
constexpr std::int32_t PortInVoice = 2;
constexpr std::int32_t PortOutPcm = 3;
constexpr std::int32_t PortOutVoice = 4;
constexpr std::int32_t PortOutDevice = 5;
constexpr std::int32_t PortStateReady = 1;
constexpr std::uint32_t VoiceFrameMs = 20;
struct Port {
std::int32_t type;
float volume;
std::uint32_t bitrate;
};
struct Voice {
std::mutex mutex;
bool initialized = false;
bool started = false;
std::uint32_t nextId = 0;
std::map<std::uint32_t, Port> ports;
std::set<std::pair<std::uint32_t, std::uint32_t>> connections;
};
Voice& State() {
static Voice voice;
return voice;
}
[[noreturn]] void Fail(const char* function, const std::string& why) {
throw std::logic_error(std::string(function) + ": " + why);
}
Port& RequirePort(Voice& voice, std::uint32_t id, const char* function) {
if (!voice.initialized) Fail(function, "library not initialized");
const auto found = voice.ports.find(id);
if (found == voice.ports.end()) Fail(function, "unknown port " + std::to_string(id));
return found->second;
}
bool IsInput(std::int32_t type) { return type == PortInDevice || type == PortInPcm || type == PortInVoice; }
}
extern "C" {
int APS5_VABI sceVoiceInit(VoiceInitParam* param, int32_t version) {
(void)version;
if (param == nullptr) APS5_INVALID_ARG_EX;
auto& voice = State();
std::lock_guard lock(voice.mutex);
if (voice.initialized) Fail(__func__, "already initialized");
voice.initialized = true;
return 0;
}
int APS5_VABI sceVoiceSetThreadsParams(void* params) {
if (params == nullptr) APS5_INVALID_ARG_EX;
return 0;
}
int APS5_VABI sceVoiceStart(const VoiceStartParam* param) {
if (param == nullptr) APS5_INVALID_ARG_EX;
auto& voice = State();
std::lock_guard lock(voice.mutex);
if (!voice.initialized) Fail(__func__, "library not initialized");
voice.started = true;
return 0;
}
int APS5_VABI sceVoiceStop(void) {
auto& voice = State();
std::lock_guard lock(voice.mutex);
if (!voice.initialized) Fail(__func__, "library not initialized");
voice.started = false;
return 0;
}
int APS5_VABI sceVoiceEnd_nid_postfix(void) {
auto& voice = State();
std::lock_guard lock(voice.mutex);
if (!voice.initialized) Fail(__func__, "library not initialized");
voice.ports.clear();
voice.connections.clear();
voice.started = false;
voice.initialized = false;
return 0;
}
int APS5_VABI sceVoiceCreatePort(uint32_t* port_id, const VoicePortParam* param) {
if (port_id == nullptr || param == nullptr) APS5_INVALID_ARG_EX;
if (param->port_type < PortInDevice || param->port_type > PortOutDevice) Fail(__func__, "unknown port type " + std::to_string(param->port_type));
auto& voice = State();
std::lock_guard lock(voice.mutex);
if (!voice.initialized) Fail(__func__, "library not initialized");
const bool voicePort = param->port_type == PortInVoice || param->port_type == PortOutVoice;
const auto id = voice.nextId++;
voice.ports.emplace(id, Port{param->port_type, param->volume, voicePort ? static_cast<std::uint32_t>(param->voice.bitrate) : 0u});
*port_id = id;
return 0;
}
int APS5_VABI sceVoiceDeletePort(uint32_t port_id) {
auto& voice = State();
std::lock_guard lock(voice.mutex);
RequirePort(voice, port_id, __func__);
std::erase_if(voice.connections, [port_id](const auto& link) { return link.first == port_id || link.second == port_id; });
voice.ports.erase(port_id);
return 0;
}
int APS5_VABI sceVoiceConnectIPortToOPort(uint32_t input_port_id, uint32_t output_port_id) {
auto& voice = State();
std::lock_guard lock(voice.mutex);
if (!IsInput(RequirePort(voice, input_port_id, __func__).type)) Fail(__func__, "first port is not an input port");
if (IsInput(RequirePort(voice, output_port_id, __func__).type)) Fail(__func__, "second port is not an output port");
voice.connections.emplace(input_port_id, output_port_id);
return 0;
}
int APS5_VABI sceVoiceDisconnectIPortFromOPort(uint32_t input_port_id, uint32_t output_port_id) {
auto& voice = State();
std::lock_guard lock(voice.mutex);
RequirePort(voice, input_port_id, __func__);
RequirePort(voice, output_port_id, __func__);
if (voice.connections.erase({input_port_id, output_port_id}) == 0) Fail(__func__, "ports are not connected");
return 0;
}
int APS5_VABI sceVoiceGetBitRate(uint32_t port_id, uint32_t* bitrate) {
if (bitrate == nullptr) APS5_INVALID_ARG_EX;
auto& voice = State();
std::lock_guard lock(voice.mutex);
const auto& port = RequirePort(voice, port_id, __func__);
if (port.type != PortInVoice && port.type != PortOutVoice) Fail(__func__, "port does not carry encoded voice");
*bitrate = port.bitrate;
return 0;
}
int APS5_VABI sceVoiceSetVolume(uint32_t port_id, float volume) {
auto& voice = State();
std::lock_guard lock(voice.mutex);
RequirePort(voice, port_id, __func__).volume = volume;
return 0;
}
int APS5_VABI sceVoiceGetVolume(uint32_t port_id, float* volume) {
if (volume == nullptr) APS5_INVALID_ARG_EX;
auto& voice = State();
std::lock_guard lock(voice.mutex);
*volume = RequirePort(voice, port_id, __func__).volume;
return 0;
}
int APS5_VABI sceVoiceGetPortInfo(uint32_t port_id, VoicePortInfo* info) {
if (info == nullptr) APS5_INVALID_ARG_EX;
auto& voice = State();
std::lock_guard lock(voice.mutex);
const auto& port = RequirePort(voice, port_id, __func__);
*info = VoicePortInfo{};
info->port_type = port.type;
info->state = PortStateReady;
if (port.bitrate != 0) info->frame_size = (port.bitrate * VoiceFrameMs + 7999u) / 8000u;
return 0;
}
int APS5_VABI sceVoiceGetPortAttr(uint32_t port_id, int32_t attr, void* value, int32_t size) {
(void)value;
(void)size;
auto& voice = State();
std::lock_guard lock(voice.mutex);
RequirePort(voice, port_id, __func__);
Fail(__func__, "attribute " + std::to_string(attr) + " not implemented");
}
int APS5_VABI sceVoiceReadFromOPort(uint32_t output_port_id, void* data, uint32_t* size) {
if (data == nullptr || size == nullptr) APS5_INVALID_ARG_EX;
auto& voice = State();
std::lock_guard lock(voice.mutex);
if (IsInput(RequirePort(voice, output_port_id, __func__).type)) Fail(__func__, "port is not an output port");
*size = 0;
return 0;
}
int APS5_VABI sceVoiceWriteToIPort(uint32_t input_port_id, const void* data, uint32_t* size, int16_t frame_gaps) {
(void)input_port_id;
(void)data;
(void)size;
(void)frame_gaps;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceVoiceSetMuteFlag() {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceVoiceGetResourceInfo(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceVoiceEnableChat(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceVoiceResetPort(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceVoiceDisableChat(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
}