-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoscout.cc
109 lines (86 loc) · 2.83 KB
/
oscout.cc
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
/*
* oscout.cc
*
* Copyright (C) 2016 Mikael Djurfeldt <[email protected]>
* and Gerhard Eckel <[email protected]>
*
* based on udpout.c by Mikael Djurfeldt
*
* music-osc is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* libneurosim is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <mpi.h>
#include <music.hh>
#include <cstring>
#include <stdexcept>
#include "lo/lo.h"
#define TIMESTEP 0.001
#define DELAY 0.0
#define SCPORT "57121"
#if 0
typedef float real;
#define MPI_MYREAL MPI::FLOAT
#else
typedef double real;
#define MPI_MYREAL MPI::DOUBLE
#endif
int main (int args, char* argv[])
{
MUSIC::Setup* setup = new MUSIC::Setup (args, argv);
char *host = argv[1]; // usually wand.pdc.kth.se
char *port = argv[2]; // usually SuperCollider OSC input port 57120
MUSIC::ContInputPort* keydata = setup->publishContInput ("in");
MPI::Intracomm comm = setup->communicator ();
int nProcesses = comm.Get_size ();
if (nProcesses > 1) {
std::cout << "udpout: needs to run in a single MPI process\n";
exit (1);
}
int width = 0;
if (keydata->hasWidth ())
width = keydata->width ();
else
comm.Abort (1);
lo_address address = lo_address_new(host, port);
lo_blob blob = lo_blob_new(width, NULL);
char *blobdata = (char*)lo_blob_dataptr(blob);
lo_timetag timetag = {0, 0}; // so far unused
int msgCount = 0;
real buf[width];
MUSIC::ArrayData dmap (buf, MPI_MYREAL, 0, width);
keydata->map (&dmap, DELAY, 1, false);
double stoptime;
setup->config ("stoptime", &stoptime);
MUSIC::Runtime* runtime = new MUSIC::Runtime (setup, TIMESTEP);
for (; runtime->time () < stoptime; runtime->tick ()) {
lo_message message = lo_message_new();
lo_bundle bundle = lo_bundle_new(timetag);
for (int i = 0; i < width; i++) {
blobdata[i] = buf[i];
}
lo_message_add_int32(message, msgCount++);
lo_message_add_double(message, runtime->time());
lo_message_add_blob(message, blob);
lo_bundle_add_message(bundle, "/keystate", message);
if (lo_send_bundle(address, bundle) == -1)
throw std::runtime_error ("oscout: send_bundle");
lo_message_free(message);
lo_bundle_free(bundle);
}
lo_address_free(address);
lo_blob_free(blob);
runtime->finalize ();
delete runtime;
return 0;
}