-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFMMain.mm
More file actions
317 lines (301 loc) · 7.16 KB
/
FMMain.mm
File metadata and controls
317 lines (301 loc) · 7.16 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "FMMain.h"
#include "Midi.h"
#include "Channel8.h"
#include "ChannelManager.h"
#define CHECK_PEAK 0
#define CHECK_BUFFER 0
#define v2(p) (((u8 *)(p))[0] | ((u8 *)(p))[1] << 8)
FMMain *gFMMain;
#ifdef __APPLE__
#define ATT 3e-7f
float FMMain::buf[2][FMMain::BUFN], FMMain::bufAlt[2][FMMain::BUFN];
#else
#define ATT 7
short FMMain::buf[2 * FMMain::BUFN], FMMain::bufAlt[2 * FMMain::BUFN];
#endif
#ifdef WIN32
#define LOCK() (WaitForSingleObject(mutex, INFINITE))
#define UNLOCK() (ReleaseMutex(mutex))
HANDLE FMMain::mutex;
#else
#define LOCK() (pthread_mutex_lock(&mutex))
#define UNLOCK() (pthread_mutex_unlock(&mutex))
pthread_mutex_t FMMain::mutex;
#endif
int FMMain::ncore, FMMain::bufN, FMMain::bufRemain, FMMain::quit;
bool FMMain::positionRequest, FMMain::bufferActive;
float FMMain::position, FMMain::amp;
#if defined(MULTI_THREAD) && defined(__linux)
pthread_t *FMMain::thread;
#endif
FMMain::FMMain() : chType(0) {
Operator8::MakeTable();
#ifdef __APPLE__
size_t len = sizeof(ncore);
int selection[] = { CTL_HW, HW_NCPU };
if (sysctl(selection, 2, &ncore, &len, NULL, 0)) ncore = 1;
#elif defined(WIN32)
ncore = 1;
#else
ncore = sysconf(_SC_NPROCESSORS_CONF);
#endif
#ifdef WIN32
mutex = CreateMutex(0, FALSE, 0);
#else
pthread_mutex_init(&mutex, NULL);
#endif
#if defined(MULTI_THREAD) && defined(__linux)
thread = new pthread_t[ncore];
#endif
}
FMMain::~FMMain() {
#ifdef WIN32
CloseHandle(mutex);
#else
pthread_mutex_destroy(&mutex);
#endif
#if defined(MULTI_THREAD) && defined(__linux)
delete[] thread;
#endif
}
int FMMain::LoadTone(const char *tonefile) {
FILE *fi;
#if defined(WIN32) && _MSC_VER >= 1400
if (fopen_s(&fi, tonefile, "rb") != 0) return 1;
#else
fi = fopen(tonefile, "rb");
if (!fi) return 1;
#endif
fseek(fi, 0, SEEK_END);
size_t len = ftell(fi);
rewind(fi);
u8 *buf = new u8[len], *p = buf + 4;
fread(buf, len, 1, fi);
fclose(fi);
Channel8::AppendToneData(&p[v2(&p[0])]);
ChannelManager::AppendDrumData((Drum *)&p[v2(&p[2])]);
return 0;
}
void FMMain::NewChannelManager(int n) {
if (!gMan) {
gMan = new ChannelManager(n);
SetChType(chType);
}
}
void FMMain::SetChType(int type) {
chType = type;
}
bool FMMain::MainLoop() {
signal(SIGINT, Terminate);
#ifndef WIN32
signal(SIGTSTP, Next);
#endif
bufferActive = false;
bufRemain = -1;
bufN = quit = 0;
amp = 1.f;
gMan->Clear();
do {
int n = (BUFN - bufN) / MIDI_UNIT;
if (n > 0) {
n *= MIDI_UNIT;
#ifdef __APPLE__
int r = Generate(bufAlt[0], bufAlt[1], n);
#else
int r = Generate(bufAlt, n);
#endif
if (r >= 0) {
LOCK();
#ifdef __APPLE__
memmove(&buf[0][bufN], bufAlt[0], sizeof(float) * n);
memmove(&buf[1][bufN], bufAlt[1], sizeof(float) * n);
#else
memmove(&buf[2 * bufN], bufAlt, sizeof(short) * 2 * n);
#endif
bufN += n;
UNLOCK();
bufferActive = true;
}
if (r > 0) {
if (bufRemain < 0) bufRemain = BUFN;
else if (!bufRemain) return quit > 0;
}
}
else {
#ifdef WIN32
Sleep(10);
#else
usleep(10000);
#endif
}
} while (quit >= 0);
return false;
}
#if defined(MULTI_THREAD) && !defined(__APPLE__)
static void *update1(void *ptr) {
while (!gMan->Update1((s32 *)ptr, MIDI_UNIT))
;
return NULL;
}
#endif
#ifdef __APPLE__
int FMMain::Generate(float *buf0, float *buf1, int numSamples) { // OS X
#else
int FMMain::Generate(short *buf0, int numSamples) { // others
#endif
s32 **buf = new s32 *[ncore], **src = new s32 *[ncore];
int i;
for (i = 0; i < ncore; i++) {
buf[i] = new s32[2 * numSamples];
memset(buf[i], 0, sizeof(s32) * 2 * numSamples);
}
int r = 0;
for (int ofs = 0; ofs < numSamples; ofs += MIDI_UNIT) {
if (gMidi) {
if (positionRequest) {
positionRequest = false;
gMan->KeyOffAll();
gMidi->Position(position);
}
else r = quit ? 1 : gMidi->Process();
}
else r = -1;
#ifdef MULTI_THREAD
if (ncore > 1) {
gMan->StartEnum();
#ifdef __APPLE__
dispatch_group_t g = dispatch_group_create();
for (i = 0; i < ncore; i++) {
s32 *p = buf[i];
dispatch_group_async(g, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (!gMan->Update1(&p[ofs << 1], MIDI_UNIT))
;
});
}
dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
dispatch_release(g);
#else
for (i = 0; i < ncore; i++)
if (pthread_create(&thread[i], NULL, update1, &buf[i][ofs << 1])) {
return -1;
}
for (i = 0; i < ncore; i++)
pthread_join(thread[i], NULL);
#endif
}
else gMan->Update(&buf[0][ofs << 1], MIDI_UNIT);
#else
gMan->Update(&buf[0][ofs << 1], MIDI_UNIT);
#endif
gMan->Poll();
}
#if CHECK_PEAK
static int peak = -20;
float log2 = logf(2.f);
#endif
for (i = 0; i < ncore; i++) src[i] = buf[i];
for (i = numSamples; i; i--) {
#ifdef __APPLE__
#ifdef MULTI_THREAD
s32 d0 = 0, d1 = 0;
for (int j = ncore - 1; j >= 0; j--) {
d0 += *src[j]++;
d1 += *src[j]++;
}
*buf0++ = ATT * d0;
*buf1++ = ATT * d1;
#else
*buf0++ = ATT * *src[0]++;
*buf1++ = ATT * *src[0]++;
#endif
#else
#ifdef MULTI_THREAD
s32 d0 = 0, d1 = 0;
for (int j = ncore - 1; j >= 0; j--) {
d0 += *src[j]++;
d1 += *src[j]++;
}
d0 >>= ATT;
if (d0 < -32768) d0 = -32768;
else if (d0 > 32767) d0 = 32767;
d1 >>= ATT;
*buf0++ = d0;
if (d1 < -32768) d1 = -32768;
else if (d1 > 32767) d1 = 32767;
*buf0++ = d1;
#else
s32 d0 = *src[0]++ >> ATT;
if (d0 < -32768) d0 = -32768;
else if (d0 > 32767) d0 = 32767;
*buf0++ = d0;
d0 = *src[0]++ >> ATT;
if (d0 < -32768) d0 = -32768;
else if (d0 > 32767) d0 = 32767;
*buf0++ = d0;
#endif
#endif
#if CHECK_PEAK
#ifdef __APPLE__
int db = int(6.f * logf(fabsf(buf0[-1])) / log2);
#else
int db = int(6.f * logf(fabsf(d0 / 32768.f)) / log2);
#endif
if (peak < db) {
peak = db;
printf("peak=%+ddB%s\n", peak, peak >= 0 ? " (saturated)" : "");
}
#endif
}
delete[] src;
for (i = 0; i < ncore; i++) delete[] buf[i];
delete[] buf;
return r;
}
#ifdef __APPLE__
void FMMain::Callback(float *buf0, float *buf1, int numSamples) {
#else
void FMMain::Callback(short *buf0, int numSamples) {
#endif
if (bufferActive && numSamples <= bufN) {
LOCK();
#ifdef __APPLE__
if (quit) {
for (int i = 0; i < numSamples; i++) {
buf0[i] = amp * buf[0][i];
buf1[i] = amp * buf[1][i];
}
amp *= .8f;
}
else {
memmove(buf0, buf[0], sizeof(float) * numSamples);
memmove(buf1, buf[1], sizeof(float) * numSamples);
}
memmove(buf[0], &buf[0][numSamples], sizeof(float) * (BUFN - numSamples));
memmove(buf[1], &buf[1][numSamples], sizeof(float) * (BUFN - numSamples));
#else
if (quit) {
for (int i = 0; i < 2 * numSamples; i++)
buf0[i] = amp * buf[i];
amp *= .8f;
}
else memmove(buf0, buf, sizeof(short) * 2 * numSamples);
memmove(buf, &buf[2 * numSamples], sizeof(short) * 2 * (BUFN - numSamples));
#endif
bufN -= numSamples;
UNLOCK();
if (bufRemain > 0 && (bufRemain -= numSamples) < 0) bufRemain = 0;
#if CHECK_BUFFER
static int cnt, bufNMin = BUFN;
if (bufNMin > bufN) bufNMin = bufN, cnt = 100;
else if (!--cnt) printf("bufmin=%.f%%%s\n", 100. * bufNMin / BUFN, bufNMin ? "" : " (underflow)");
#endif
}
else {
#ifdef __APPLE__
memset(buf0, 0, sizeof(float) * numSamples);
memset(buf1, 0, sizeof(float) * numSamples);
#else
memset(buf0, 0, sizeof(short) * 2 * numSamples);
#endif
}
}