-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSC.cpp
More file actions
147 lines (127 loc) · 3.26 KB
/
Copy pathOSC.cpp
File metadata and controls
147 lines (127 loc) · 3.26 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
#include "OSC.h"
#include <iostream>
auto OSC::GetOSCData(const std::string& osc_addr, float v) -> std::vector<unsigned char>
{
std::vector<unsigned char> data;
for (int i = 0; i < osc_addr.size(); ++i)
data.push_back(osc_addr[i]);
for(int i = 0; i < 4 - osc_addr.size() % 4; ++i)
data.push_back(0);
data.push_back(',');
data.push_back('f');
data.push_back(0);
data.push_back(0);
unsigned char* bytes = reinterpret_cast<unsigned char*>(&v);
for (int i = sizeof(float) - 1; i >= 0; --i)
data.push_back(bytes[i]);
return data;
}
auto OSC::GetOSCData(const std::string& osc_addr, bool v) -> std::vector<unsigned char>
{
std::vector<unsigned char> data;
for (int i = 0; i < osc_addr.size(); ++i)
data.push_back(osc_addr[i]);
for (int i = 0; i < 4 - osc_addr.size() % 4; ++i)
data.push_back(0);
data.push_back(',');
if(v)
data.push_back('T');
else
data.push_back('F');
data.push_back(0);
data.push_back(0);
return data;
}
auto OSC::GetOSCData(const std::string& osc_addr, int v) -> std::vector<unsigned char>
{
std::vector<unsigned char> data;
for (int i = 0; i < osc_addr.size(); ++i)
data.push_back(osc_addr[i]);
for (int i = 0; i < 4 - osc_addr.size() % 4; ++i)
data.push_back(0);
data.push_back(',');
data.push_back('i');
data.push_back(0);
data.push_back(0);
unsigned char* bytes = reinterpret_cast<unsigned char*>(&v);
for (int i = sizeof(float) - 1; i >= 0; --i)
data.push_back(bytes[i]);
return data;
}
OSC::OSC() :
port(0)
{
}
void OSC::Init(const std::string& address, unsigned int port)
{
this->addr = address;
this->port = port;
if (socket.bind(sf::Socket::AnyPort) != sf::Socket::Done)
{
AddLog("Can't bind port in " + std::to_string(port));
}
AddLog("Init socket port:" + std::to_string(socket.getLocalPort()));
}
void OSC::Init(std::string&& address, unsigned int port)
{
this->addr = std::move(address);
this->port = port;
if (socket.bind(sf::Socket::AnyPort) != sf::Socket::Done)
{
AddLog("Can't bind port in " + std::to_string(port));
}
AddLog("Init socket port:" + std::to_string(socket.getLocalPort()));
}
void OSC::SetOSCPort(int port)
{
this->port = port;
}
bool OSC::SendOSC(std::string osc_addr, float v)
{
auto data = GetOSCData(osc_addr, v);
if (socket.send(&data[0], data.size(), addr, port) != sf::Socket::Done)
{
AddLog("Socket send failed.");
return false;
}
AddLog("Socket send : " + osc_addr + ", value:" + std::to_string(v));
return true;
}
bool OSC::SendOSC(std::string osc_addr, bool v)
{
auto data = GetOSCData(osc_addr, v);
if (socket.send(&data[0], data.size(), addr, port) != sf::Socket::Done)
{
AddLog("Socket send failed.");
return false;
}
AddLog("Socket send : " + osc_addr + ", value:" + std::to_string(v));
return true;
}
bool OSC::SendOSC(std::string osc_addr, int v)
{
auto data = GetOSCData(osc_addr, v);
if (socket.send(&data[0], data.size(), addr, port) != sf::Socket::Done)
{
AddLog("Socket send failed.");
return false;
}
AddLog("Socket send : " + osc_addr + ", value:" + std::to_string(v));
return true;
}
auto OSC::GetLogs() -> const std::deque<std::string>&
{
return logs;
}
void OSC::AddLog(const std::string& str)
{
if (log_lock == true)
return;
while (logs.size() > max_log)
logs.pop_front();
logs.push_back(str);
}
void OSC::ClearLogs()
{
logs.clear();
}