-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscsynth-interop.cpp
208 lines (175 loc) · 6.45 KB
/
scsynth-interop.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
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
#include "scsynth-interop.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <memory.h>
#ifdef SC_WIN32
#include <pthread.h>
#include <winsock2.h>
#else
#include <sys/wait.h>
#endif
#include <SC_WorldOptions.h>
#include <SC_SndBuf.h>
/************************************************************************/
// init / cleanup
/************************************************************************/
int scsynth_interop_init()
{
#ifdef SC_WIN32
#ifdef SC_WIN32_STATIC_PTHREADS
// initialize statically linked pthreads library
pthread_win32_process_attach_np();
#endif
// initialize winsock
WSAData wsaData;
int nCode;
if ((nCode = WSAStartup(MAKEWORD(1, 1), &wsaData)) != 0) {
scprintf( "WSAStartup() failed with error code %d.\n", nCode );
return -1;
}
#else // SC_WIN32
//catch_crashes();
#endif
return 0;
}
void scsynth_interop_cleanup()
{
#ifdef SC_WIN32
// clean up winsock
WSACleanup();
#ifdef SC_WIN32_STATIC_PTHREADS
// clean up statically linked pthreads
pthread_win32_process_detach_np();
#endif
#endif
}
/************************************************************************/
// Server start / quit
/************************************************************************/
#ifdef SC_WIN32
// according to this page: http://www.mkssoftware.com/docs/man3/setlinebuf.3.asp
// setlinebuf is equivalent to the setvbuf call below.
inline int setlinebuf(FILE *stream)
{
return setvbuf( stream, (char*)0, _IONBF, 0 );
}
#endif
struct ScsynthInteropStartOptions* scsynth_interop_get_default_start_options()
{
struct ScsynthInteropStartOptions *options = (struct ScsynthInteropStartOptions*)malloc(sizeof(struct ScsynthInteropStartOptions));
memset(options, 0, sizeof(struct ScsynthInteropStartOptions));
options->numControlBusChannels = kDefaultWorldOptions.mNumControlBusChannels;
options->numAudioBusChannels = kDefaultWorldOptions.mNumAudioBusChannels;
options->numInputBusChannels = kDefaultWorldOptions.mNumInputBusChannels;
options->numOutputBusChannels = kDefaultWorldOptions.mNumOutputBusChannels;
options->bufLength = kDefaultWorldOptions.mBufLength;
options->preferredHardwareBufferFrameSize = kDefaultWorldOptions.mPreferredHardwareBufferFrameSize;
options->preferredSampleRate = kDefaultWorldOptions.mPreferredSampleRate;
options->numBuffers = kDefaultWorldOptions.mNumBuffers;
options->maxNodes = kDefaultWorldOptions.mMaxNodes;
options->maxGraphDefs = kDefaultWorldOptions.mMaxGraphDefs;
options->realTimeMemorySize = kDefaultWorldOptions.mRealTimeMemorySize;
options->maxWireBufs = kDefaultWorldOptions.mMaxWireBufs;
options->numRGens = kDefaultWorldOptions.mNumRGens;
options->verbosity = 1;
options->UGensPluginPath = "../server/plugins/";
options->inDeviceName = "";
options->outDeviceName = "";
return options;
}
void* scsynth_interop_start(struct ScsynthInteropStartOptions *inOptions)
{
setlinebuf(stdout);
WorldOptions options = kDefaultWorldOptions;
if( -1 != inOptions->numControlBusChannels) {
options.mNumControlBusChannels = inOptions->numControlBusChannels;
}
if( -1 != inOptions->numAudioBusChannels) {
options.mNumAudioBusChannels = inOptions->numAudioBusChannels;
}
if( -1 != inOptions->numInputBusChannels) {
options.mNumInputBusChannels= inOptions->numInputBusChannels;
}
if( -1 != inOptions->numOutputBusChannels) {
options.mNumOutputBusChannels = inOptions->numOutputBusChannels;
}
if( -1 != inOptions->bufLength) {
options.mBufLength = inOptions->bufLength;
}
if( -1 != inOptions->preferredHardwareBufferFrameSize) {
options.mPreferredHardwareBufferFrameSize = inOptions->preferredHardwareBufferFrameSize;
}
if( -1 != inOptions->preferredSampleRate) {
options.mPreferredSampleRate = inOptions->preferredSampleRate;
}
if( -1 != inOptions->numBuffers) {
options.mNumBuffers = inOptions->numBuffers;
}
if( -1 != inOptions->maxNodes) {
options.mMaxNodes = inOptions->maxNodes;
}
if( -1 != inOptions->maxGraphDefs) {
options.mMaxGraphDefs = inOptions->maxGraphDefs;
}
if( -1 != inOptions->realTimeMemorySize) {
options.mRealTimeMemorySize = inOptions->realTimeMemorySize;
}
if( -1 != inOptions->maxWireBufs) {
options.mMaxWireBufs = inOptions->maxWireBufs;
}
if( -1 != inOptions->numRGens) {
options.mNumRGens = inOptions->numRGens;
}
if( 0 != strcmp("", inOptions->inDeviceName )) { // not empty
options.mInDeviceName = inOptions->inDeviceName;
}
if( 0 != strcmp("", inOptions->outDeviceName )) { // not empty
options.mOutDeviceName = inOptions->outDeviceName;
}
options.mVerbosity = inOptions->verbosity;
options.mUGensPluginPath = inOptions->UGensPluginPath;
struct World *world = World_New(&options);
if (!world) return 0;
if(options.mVerbosity >=0) {
scprintf("SuperCollider 3 server ready..\n");
}
fflush(stdout);
return (void*)world;
}
void scsynth_interop_wait_for_quit(void* w)
{
struct World *inWorld = (struct World*)w;
World_WaitForQuit(inWorld);
}
/************************************************************************/
// Soundbuffer
/************************************************************************/
void* scsynth_interop_copy_sndbuf(void* w, unsigned index)
{
struct World *world = (struct World*)w;
bool didChange;
struct SndBuf* buf = (struct SndBuf*) malloc(sizeof(struct SndBuf));
memset(buf, 0, sizeof(struct SndBuf));
int serverErr = World_CopySndBuf(world, index, buf, false, &didChange);
return (void*)buf;
}
/************************************************************************/
// Server communication
/************************************************************************/
int scsynth_interop_open_udp(void* world, int inPort)
{
struct World *inWorld = (struct World*)world;
return World_OpenUDP(inWorld, inPort);
}
int scsynth_interop_open_tcp(void* world, int inPort, int inMaxConnections, int inBacklog)
{
struct World *inWorld = (struct World*)world;
return World_OpenTCP(inWorld, inPort, inMaxConnections, inBacklog);
}
bool scsynth_interop_send_packet(void*world, int inSize, char *inData, void (*func)(void*, void*,int))
{
struct World *inWorld = (struct World*)world;
ReplyFunc inFunc = (ReplyFunc)func;
return World_SendPacket(inWorld, inSize, inData, inFunc);
}