-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOsc.cpp
120 lines (109 loc) · 3.13 KB
/
Osc.cpp
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
#include "framework.h"
#include "Osc.h"
#include <iostream>
using namespace std;
Osc::Osc(int recv_from_port, int send_to_port, function<void(OSCPP::Server::Message const&)> handler)
: m_handler(handler)
{
m_udp = make_unique<OscUdp>(recv_from_port, send_to_port);
}
void Osc::handlePacket(OSCPP::Server::Packet const& packet)
{
if (packet.isBundle()) {
// Convert to bundle
OSCPP::Server::Bundle bundle(packet);
// Get packet stream
OSCPP::Server::PacketStream packets(bundle.packets());
// Iterate over all the packets and call handlePacket recursively.
// Cuidado: Might lead to stack overflow!
while (!packets.atEnd()) {
handlePacket(packets.next());
}
}
else {
// Convert to message
OSCPP::Server::Message msg(packet);
m_handler(msg);
}
}
void Osc::recvPacket()
{
size_t size = m_udp->recv(m_recvBuffer.data(), m_recvBuffer.size());
if (size == 0) return;
handlePacket(OSCPP::Server::Packet(m_recvBuffer.data(), size));
}
void Osc::sendPacket(OSCPP::Client::Packet const& packet)
{
m_udp->send(packet.data(), packet.size());
}
void Osc::sendBool(const char* address, bool value)
{
array<char, kMaxPacketSize> buffer;
OSCPP::Client::Packet packet(buffer.data(), buffer.size());
packet
//.openBundle(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count())
.openMessage(address, 1)
.boolean(value)
.closeMessage();
//.closeBundle();
sendPacket(packet);
}
void Osc::sendInt(const char* address, int value)
{
array<char, kMaxPacketSize> buffer;
OSCPP::Client::Packet packet(buffer.data(), buffer.size());
packet
//.openBundle(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count())
.openMessage(address, 1)
.int32(value)
.closeMessage();
//.closeBundle();
sendPacket(packet);
}
void Osc::sendFloat(const char* address, float value)
{
array<char, kMaxPacketSize> buffer;
OSCPP::Client::Packet packet(buffer.data(), buffer.size());
packet
//.openBundle(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count())
.openMessage(address, 1)
.float32(value)
.closeMessage();
//.closeBundle();
sendPacket(packet);
}
template <typename T>
void Osc::SetParameter(const char* parameter, T value)
{
throw runtime_error("Unsupported type");
}
template <>
void Osc::SetParameter<bool>(const char* parameter, bool value)
{
sendBool(format("/avatar/parameters/{}", parameter).c_str(), value);
}
template <>
void Osc::SetParameter<int>(const char* parameter, int value)
{
sendInt(format("/avatar/parameters/{}", parameter).c_str(), value);
}
template <>
void Osc::SetParameter<float>(const char* parameter, float value)
{
sendFloat(format("/avatar/parameters/{}", parameter).c_str(), value);
}
void Osc::Start()
{
m_thread = thread(&Osc::ListernThread, this);
}
void Osc::ListernThread()
{
while (!m_terminated) {
recvPacket();
}
}
void Osc::Terminate()
{
m_terminated = true;
m_thread.join();
}