-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTeleportLib.cpp
274 lines (229 loc) · 7.24 KB
/
TeleportLib.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64)
#define UNITY_WIN 1
#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP)
# define UNITY_WINRT 1
#endif
#elif defined(__MACH__) || defined(__APPLE__)
#define UNITY_OSX 1
#elif defined(__ANDROID__)
#define UNITY_ANDROID 1
#elif defined(__linux__)
#define UNITY_LINUX 1
#endif
#ifndef UNITY_WIN
#define UNITY_WIN 0
#endif
#ifndef UNITY_WINRT
#define UNITY_WINRT 0
#endif
#ifndef UNITY_OSX
#define UNITY_OSX 0
#endif
#ifndef UNITY_ANDROID
#define UNITY_ANDROID 0
#endif
#ifndef UNITY_LINUX
#define UNITY_LINUX 0
#endif
#include <stdio.h>
#if UNITY_OSX | UNITY_LINUX
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#elif UNITY_WIN
#include <windows.h>
#endif
#include "TeleportLib.h"
#if defined(__GNUC__) || defined(__SNC__)
#define TELEPORT_ALIGN(val) __attribute__((aligned(val))) __attribute__((packed))
#elif defined(_MSC_VER)
#define TELEPORT_ALIGN(val) __declspec(align(val))
#else
#define TELEPORT_ALIGN(val)
#endif
namespace Teleport
{
const int NUMPARAMS = 4;
const int NUMSTREAMS = 8;
struct Parameter
{
float value;
int changed;
} TELEPORT_ALIGN(4);
struct Stream
{
enum { LENGTH = 2 * 44100 };
Parameter params[NUMPARAMS];
int readpos;
int writepos;
float buffer[LENGTH];
inline bool Read(float& val)
{
int r = readpos;
if (r == writepos)
return false;
readpos = (r == LENGTH - 1) ? 0 : (r + 1);
val = buffer[r];
return true;
}
inline bool Feed(float input)
{
int w = (writepos == LENGTH - 1) ? 0 : (writepos + 1);
buffer[w] = input;
writepos = w;
return true;
}
inline int GetNumBuffered() const
{
int b = writepos - readpos;
if (b < 0)
b += LENGTH;
return b;
}
} TELEPORT_ALIGN(4);
struct SharedMemory
{
Stream streams[NUMSTREAMS];
};
class SharedMemoryHandle
{
protected:
SharedMemory* data;
#if UNITY_WIN
HANDLE hMapFile;
#endif
public:
SharedMemoryHandle()
{
for (int attempt = 0; attempt < 10; attempt++)
{
bool clearmemory = true;
#if UNITY_WIN
#if !UNITY_WINRT
char filename[1024];
sprintf_s(filename, "UnityAudioTeleport%d", attempt);
hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(SharedMemory), filename);
#else
wchar_t filePath[MAX_PATH];
GetTempPathW(MAX_PATH, filePath);
wchar_t fileName[30];
swprintf_s(fileName, L"UnityAudioTeleport%d", attempt);
wcscat_s(filePath, fileName);
hMapFile = CreateFileMappingFromApp(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, sizeof(SharedMemory), filePath);
#endif
if (hMapFile == NULL)
{
clearmemory = false;
#if !UNITY_WINRT
hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, filename);
#else
hMapFile = OpenFileMappingFromApp(FILE_MAP_ALL_ACCESS, FALSE, filePath);
#endif
}
if (hMapFile == NULL)
{
printf("Could not create file mapping object (%d).\n", GetLastError());
continue;
}
data = (SharedMemory*)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(SharedMemory));
if (data == NULL)
{
printf("Could not map view of file (%d).\n", GetLastError());
CloseHandle(hMapFile);
continue;
}
#else
char filename[1024];
#if UNITY_LINUX
// a shared memory object should be identified by a name of the form /somename;
// that is, a null-terminated string of up to NAME_MAX (i.e., 255) characters consisting of an initial slash,
// followed by one or more characters, none of which are slashes.
sprintf(filename, "/UnityAudioTeleport%d", attempt);
#else
sprintf(filename, "/tmp/UnityAudioTeleport%d", attempt);
#endif
clearmemory = (access(filename, F_OK) == -1);
int handle = shm_open(filename, O_RDWR | O_CREAT, 0777);
if (handle == -1)
{
fprintf(stderr, "Open failed: %s\n", strerror(errno));
continue;
}
if (ftruncate(handle, sizeof(SharedMemory)) == -1)
{
fprintf(stderr, "ftruncate error (ignored)\n");
//continue;
}
data = (SharedMemory*)mmap(0, sizeof(SharedMemory), PROT_READ | PROT_WRITE, MAP_SHARED, handle, 0);
if (data == (void*)-1)
{
fprintf(stderr, "mmap failed\n");
continue;
}
//close(handle);
//shm_unlink(filename);
#endif
if (clearmemory)
memset(data, 0, sizeof(SharedMemory));
break; // intentional (see continue's above)
}
}
~SharedMemoryHandle()
{
#if UNITY_WIN
UnmapViewOfFile(data);
CloseHandle(hMapFile);
#else
if (data)
{
munmap(data, sizeof(SharedMemory));
}
#endif
}
inline SharedMemory* operator->() const { return data; }
};
inline SharedMemoryHandle& GetSharedMemory()
{
static SharedMemoryHandle shared;
return shared;
}
}
extern "C" UNITY_AUDIODSP_EXPORT_API int TeleportFeed(int stream, float* samples, int numsamples)
{
Teleport::Stream& s = Teleport::GetSharedMemory()->streams[stream];
for (int n = 0; n < numsamples; n++)
s.Feed(samples[n]);
return s.writepos;
}
extern "C" UNITY_AUDIODSP_EXPORT_API int TeleportRead(int stream, float* samples, int numsamples)
{
Teleport::Stream& s = Teleport::GetSharedMemory()->streams[stream];
for (int n = 0; n < numsamples; n++)
if (!s.Read(samples[n]))
samples[n] = 0.0f;
return s.readpos;
}
extern "C" UNITY_AUDIODSP_EXPORT_API int TeleportGetNumBuffered(int stream)
{
Teleport::Stream& s = Teleport::GetSharedMemory()->streams[stream];
return s.GetNumBuffered();
}
extern "C" UNITY_AUDIODSP_EXPORT_API int TeleportSetParameter(int stream, int index, float value)
{
Teleport::Stream& s = Teleport::GetSharedMemory()->streams[stream];
s.params[index].changed = 1;
s.params[index].value = value;
return 1;
}
extern "C" UNITY_AUDIODSP_EXPORT_API int TeleportGetParameter(int stream, int index, float* value)
{
Teleport::Stream& s = Teleport::GetSharedMemory()->streams[stream];
*value = s.params[index].value;
int changed = s.params[index].changed;
s.params[index].changed = 0;
return changed;
}