This repository was archived by the owner on Feb 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidiServer.cpp
More file actions
251 lines (226 loc) · 6.54 KB
/
MidiServer.cpp
File metadata and controls
251 lines (226 loc) · 6.54 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "MidiServer.h"
//CONSTRUCTORS
MidiServer::MidiServer(){
midi = new RtMidiIn();
this->server_fd = this->client_fd = this->valread = 0;
this->configurePort();
this->clearBuffer();
}
MidiServer::MidiServer(int portNum){
midi = new RtMidiIn();
this->server_fd = this->client_fd = this->valread = 0;
if(portNum < 1024){
cout << "port must be standard user access (# > 1024)"<<endl;
this->configurePort();
}
else{
this->portno = portNum;
}
this->clearBuffer();
}
MidiServer::~MidiServer(){
if(this->midi != nullptr){
if(midi->isPortOpen()){
midi->closePort();
}
delete midi;
}
}
//success message agreed upon by server/client
void MidiServer::sendSuccess(){
if(!client_fd){
cout << "server not yet bound attempting to send success"<<endl;
return;
}
send(client_fd, "success\n", 8, 0);
}
//greeting upon new connection
int MidiServer::sendGreeting() {
if(!server_fd){
cout << "server_fd not yet defined?" <<endl;
return -1;
}
this->clearBuffer();
send(client_fd, "Welcome to the Network Midi Server by Shawn Pacarar", 51, 0);
valread = read(client_fd, buffer, buffer_size);
if(!valread){
cout << "nothing here, client likely got disconnected" <<endl;
return -1;
}
cout << buffer <<endl;
return 0;
}
//START SERVER
//integer results will return 0 if success, -1 if failure, -2 if retry is allowed
void MidiServer::startServer(){
int result = 0;
cout << "starting midi server" <<endl;
result = this->bindServer();
if(result == -1) return;
cout << "server bound and ready"<<endl;
bool running = true;
while(running){
result = this->awaitConnection();
if(result == -1) continue;
cout << "connection established" <<endl;
result = this->sendGreeting();
if(result == -1)continue;
cout << "greeting sent succesfully"<<endl;
result = this->sendMidiPortInformation();
//-2 = failure to bind port (message not understood?) retry
//-1 = unacceptable failure (restart)
while(result == -2){
//just keep trying for now
this->sendMidiPortInformation();
}
if(result == -1)continue;
//at this step server configuration should be complete
//the midi input port should be bound
//the server is ready to listen to music.
this->sendSuccess();
cout << "midi port succesfully bound and success message sent"<<endl;
//bool listening = true;
readMidiInput();//continous function
}
}
//SERVER CONFIGURATION
//this just sets port to some arbitrary default I picked
int MidiServer::configurePort(){
cout << "defaulting to port 4445"<<endl;
this->portno = 4445;
return 0;
}
//create socket file descriptor and bind socket to port
int MidiServer::bindServer(){
int opt = 1;
//set server file descriptor
this->server_fd = socket(AF_INET, SOCK_STREAM, 0);
if(this->server_fd == 0){
cout << "error setting file descriptor"<<endl;
return -1;
}
//attack socket to port
if(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))){
cout << "error attaching socket to port"<<endl;
return -1;
}
this->address.sin_family = AF_INET;
this->address.sin_addr.s_addr = INADDR_ANY;
this->address.sin_port = htons(this->portno);
if(bind(this->server_fd, (struct sockaddr *)&(this->address), sizeof(this->address))<0){
cout << "error binding port to address"<<endl;
return -1;
}
cout << "port: "<<this->portno<<" bound to server"<<endl;
return 0;
}
void MidiServer::clearBuffer(){
memset(this->buffer, 0, this->buffer_size);
}
//not sure if both listen and accept should be here.
//unclear how listen exactly works
int MidiServer::awaitConnection(){
if (listen(server_fd, 5) < 0){
return -1;
}
else {
int addrlen = sizeof(address);
client_fd = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen);
}
return client_fd;
}
int MidiServer::sendMidiPortInformation(){
//probe midi ports
//construct a message to send where size is known
//send message to client
//attempt listening to port
//if success send success
//otherwise send over errors
int inPorts = midi->getPortCount();
std::string currentPortName = "";
if(inPorts == -1){
return -1;
}
string message = "";
message += "(input ports: " + to_string(inPorts) +")\r\n";
std::cout << "(input ports: " << inPorts <<")\n\0";
for(int i = 0; i < inPorts; i++){
try {
currentPortName = midi->getPortName(i);
}
catch(RtMidiError &error) {
error.printMessage();
return -1;
}
message += "Input Port #" + to_string(i) + " : " + currentPortName;
std::cout << " Input Port #" <<i<<" : "<<currentPortName << std::endl;
}
send(client_fd, message.c_str(), message.size(), 0);
this->clearBuffer();
valread = read(client_fd, buffer, buffer_size);
if(!valread){
cout << "nothing here, client likely got disconnected" <<endl;
return -1;
}
try{
int mPort = -1;
//1 digit response
//offset unsigned char by 48 for integer... lame way but idk what else would work
mPort = int(buffer[0]) - 48;
cout << mPort<<endl;
if(midi->isPortOpen()){
midi->closePort();
}
midi->openPort(mPort);
}
catch(RtMidiError &e){
e.printMessage();
return -1;
}
return 0;
}
//listens on an open channel
int MidiServer::readMidiInput(){
if(!midi->isPortOpen()){
cout << "no midi port is open when attempting to read inputs!"<<endl;
return -1;
}
if(!server_fd || !client_fd){
cout << "server and/or client is not yet bound when attempting to read inputs!"<<endl;
return -1;
}
//set callback to send server information
midi->setCallback(&convertMidiInput, (void*)this);
midi->ignoreTypes(false,false,false);
cout << "reading inputs from piano and sending through callback"<<endl;
cout << "press enter to stop reading data"<<endl;
//listen to a midi message and fill up buffer
char msg;
//rtMidi way of continous read
cin.get(msg);
return 0;
}
//rtMidi input callback
void MidiServer::convertMidiInput(double deltatime, std::vector< unsigned char > *message, void *userData){
MidiServer* m = (MidiServer*)userData;
static string types[3] = {"type ", "key ", "velocity "};
string sendData = "";
unsigned int nBytes = message->size();
for ( unsigned int i=0; i<nBytes; i++ ){
cout << types[i%3] << " = " << (int)message->at(i) << ", ";
sendData += (int)message->at(i);
//cout << "sendData: "<<sendData<<endl;
}
if ( nBytes > 0 ){
cout << "stamp = " << deltatime << std::endl;
//cout << "sendData: "<<sendData<<endl;
m->sendMidiInput(sendData);
}
}
void MidiServer::sendMidiInput(string &message){
if(!client_fd || !server_fd){
cout << "server and/or client is not yet bound when attempting to send input to client"<<endl;
return;
}
send(client_fd, message.c_str(), message.size(), 0);
}