forked from hselasky/midipp
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmidipp.cpp
539 lines (445 loc) · 10.9 KB
/
midipp.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*-
* Copyright (c) 2009-2019 Hans Petter Selasky. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "midipp.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <err.h>
#include <getopt.h>
#include "midipp_mainwindow.h"
#include "midipp_scores.h"
#ifdef __ANDROID__
#include <qpa/qplatformnativeinterface.h>
#endif
Mpp :: Mpp() :
ColorBlack(0x00,0x00,0x00,0xff),
ColorWhite(0xff,0xff,0xff,0xff),
ColorGrey(0xc0,0xc0,0xc0,0xff),
ColorLogo(0xc4,0x40,0x20,0xff),
ColorGreen(0x40,0xc4,0x20,0xff),
ColorLight(0x80,0x80,0x80,0xff),
ColorYellow(0xc0,0xc0,0x00,0xff)
{
}
Mpp :: ~Mpp()
{
}
Q_DECL_EXPORT class Mpp Mpp;
Q_DECL_EXPORT const QString
MppChanName(int channel, int other)
{
if (channel == 9)
return (QString("DRUMS"));
if (channel >= 0 && channel <= 15)
return (QString("CH::%1").arg(channel + 1));
switch (other) {
case MPP_CHAN_ANY:
return (QString("CH::ANY"));
case MPP_CHAN_NONE:
return (QString("CH::NONE"));
default:
return (QString("CH::UNDEF"));
}
}
Q_DECL_EXPORT const QString
MppDevName(int device, int other)
{
if (device >= 0 && device < MPP_MAX_DEVS)
return (QString("DEV::%1").arg(device + 1));
switch (other) {
case MPP_DEV_ALL:
return (QString("DEV::ALL"));
case MPP_DEV_NONE:
return (QString("DEV::NONE"));
default:
return (QString("DEV::UNDEF"));
}
}
Q_DECL_EXPORT QString
MppBaseName(const QString & fname)
{
QFileInfo fi(fname);
return (fi.fileName());
}
Q_DECL_EXPORT char *
MppQStringToAscii(const QString &s)
{
QByteArray temp = s.toUtf8();
int len = temp.size();
char *data = temp.data();
char *ptr;
ptr = (char *)malloc(len + 1);
if (ptr == NULL)
return (NULL);
memcpy(ptr, data, len);
ptr[len] = 0; /* NUL terminate */
return (ptr);
}
Q_DECL_EXPORT uint8_t
MppIsLabel(const QString &str)
{
return ((str.size() > 1) && (str[0] == 'L') && str[1].isDigit());
}
Q_DECL_EXPORT void
MppSplitBaseTreble(const int *score, uint8_t num, int *base, uint8_t *nbase,
int *treble, uint8_t *ntreble)
{
uint8_t stats[MPP_MAX_CHORD_BANDS] = {};
uint8_t count[MPP_MAX_CHORD_BANDS] = {};
uint8_t x;
int key;
uint8_t nb = 0;
uint8_t nt = 0;
for (x = 0; x != num; x++)
stats[MPP_BAND_REM(score[x], MPP_MAX_CHORD_BANDS)]++;
/*
* Treble only: C0
* Bass only: C0 C1 or C0 C0
* Bass and Treble: C0 C1 C2 or C0 C0 C1
*/
for (x = 0; x != num; x++) {
uint32_t rem;
key = score[x];
rem = MPP_BAND_REM(key, MPP_MAX_CHORD_BANDS);
if (stats[rem] == 1) {
treble[nt++] = key;
} else if (count[rem]++ < 2) {
base[nb++] = key;
} else {
treble[nt++] = key;
}
}
*nbase = nb;
*ntreble = nt;
}
Q_DECL_EXPORT QString
MppReadFile(const QString &fname)
{
QFile file(fname);
QString retval;
QMessageBox box;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
goto error;
retval = QString::fromUtf8(file.readAll());
if (file.error()) {
file.close();
goto error;
}
file.close();
return (retval);
error:
box.setText(QObject::tr("Could not read from file!"));
box.setStandardButtons(QMessageBox::Ok);
box.setIcon(QMessageBox::Critical);
box.setWindowIcon(QIcon(MppIconFile));
box.setWindowTitle(MppVersion);
box.exec();
return (QString());
}
Q_DECL_EXPORT void
MppWriteFile(const QString &fname, QString text)
{
QFile file(fname);
QMessageBox box;
if (!file.open(QIODevice::WriteOnly | QIODevice::Text |
QIODevice::Truncate)) {
goto error;
}
file.write(text.toUtf8());
if (file.error()) {
file.close();
goto error;
}
file.close();
return;
error:
box.setText(QObject::tr("Could not write to file!"));
box.setStandardButtons(QMessageBox::Ok);
box.setIcon(QMessageBox::Critical);
box.setWindowIcon(QIcon(MppIconFile));
box.setWindowTitle(MppVersion);
box.exec();
}
Q_DECL_EXPORT uint8_t
MppReadRawFile(const QString &fname, QByteArray *pdata)
{
QFile file(fname);
if (!file.open(QIODevice::ReadOnly))
goto failure;
*pdata = file.readAll();
if (file.error()) {
file.close();
goto failure;
}
file.close();
return (0);
failure:
return (1);
}
Q_DECL_EXPORT uint8_t
MppWriteRawFile(const QString &fname, QByteArray *pdata)
{
QFile file(fname);
if (!file.open(QIODevice::WriteOnly |
QIODevice::Truncate))
goto failure;
file.write(*pdata);
if (file.error()) {
file.close();
goto failure;
}
file.close();
return (0);
failure:
return (1);
}
static size_t
MppSortIndex(size_t t)
{
t ^= t >> 1;
t ^= t >> 2;
t ^= t >> 4;
t ^= t >> 8;
t ^= t >> 16;
if (sizeof(t) > 4)
t ^= t >> ((sizeof(t) > 4) ? 32 : 0);
return (t);
}
static int
MppSortXform(void *ptr, size_t n, size_t lim, size_t size, MppCmp_t *fn, void *arg)
{
#define MPP_XSORT_TABLE_MAX (1 << 4)
size_t x, y, z;
size_t t, u, v;
size_t p[MPP_XSORT_TABLE_MAX];
int retval = 0;
x = n;
while (1) {
/* optimise */
if (x >= MPP_XSORT_TABLE_MAX)
v = MPP_XSORT_TABLE_MAX;
else if (x >= 2)
v = x;
else
break;
/* divide down */
x /= v;
/* generate ramp table */
for (t = 0; t != v; t++)
p[t] = MppSortIndex(x * (t ^ (t / 2)));
/* bitonic sort */
for (y = 0; y != n; y += (v * x)) {
for (z = 0; z != x; z++) {
size_t w = y + z;
/* insertion sort */
for (t = 1; t != v; t++) {
/* check for arrays which are not power of two */
if ((w ^ p[t]) >= lim)
break;
for (u = t; u != 0; u--) {
void *pa = ((uint8_t *)ptr) + (w ^ p[u - 1]) * size;
void *pb = ((uint8_t *)ptr) + (w ^ p[u]) * size;
if (fn(arg, pa, pb) > 0) {
uint8_t temp[size];
memcpy(temp, pa, sizeof(temp));
memcpy(pa, pb, sizeof(temp));
memcpy(pb, temp, sizeof(temp));
retval = 1;
} else {
break;
}
}
}
}
}
}
return (retval);
}
Q_DECL_EXPORT void
MppSort(void *ptr, size_t n, size_t size, MppCmp_t *fn, void *arg)
{
size_t max;
if (n <= 1)
return;
for (max = 1; max < n; max <<= 1)
;
while (MppSortXform(ptr, max, n, size, fn, arg))
;
}
Q_DECL_EXPORT void
MppSort(int *ptr, size_t num)
{
size_t i, j;
for (i = 0; i != num; i++) {
for (j = i + 1; j != num; j++) {
if (ptr[i] > ptr[j]) {
int temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
}
}
Q_DECL_EXPORT void
MppTrans(int *ptr, size_t num, int ntrans)
{
if (num == 0)
return;
MppSort(ptr, num);
if (ntrans < 0) {
while (ntrans++) {
ptr[num - 1] -= MPP_MAX_BANDS;
MppSort(ptr, num);
}
} else if (ntrans > 0) {
while (ntrans--) {
ptr[0] += MPP_MAX_BANDS;
MppSort(ptr, num);
}
}
}
#ifdef HAVE_SCREENSHOT
Q_DECL_EXPORT void
MppScreenShot(QWidget *widget, QApplication &app)
{
static uint32_t counter;
/* make sure drawing and resizing is complete */
app.processEvents();
QString fname;
fname.sprintf("screenshot%03d.png", counter++);
QPixmap pixmap(widget->size());
widget->render(&pixmap);
pixmap.save(fname, "PNG");
}
#endif
static const char *mpp_input_file;
static int mpp_pdf_print;
static void
usage(void)
{
fprintf(stderr, "midipp [-f <score_file.txt>] [-p show_print]\n");
exit(1);
}
static const struct option midipp_opts[] = {
{ "NSDocumentRevisionsDebugMode", required_argument, NULL, ' ' },
{ NULL, 0, NULL, 0 }
};
const QString MppVersion("MIDI Player Pro v2.0.4");
const QString MppIconFile(":/midipp.png");
Q_DECL_EXPORT int
main(int argc, char **argv)
{
int c;
/* must be first, before any threads are created */
signal(SIGPIPE, SIG_IGN);
QApplication app(argc, argv);
/* set consistent double click interval */
app.setDoubleClickInterval(250);
Mpp.HomeDirMid = QDir::homePath();
Mpp.HomeDirTxt = QDir::homePath();
Mpp.HomeDirGp3 = QDir::homePath();
Mpp.HomeDirMXML = QDir::homePath();
Mpp.HomeDirBackground = QDir::homePath();
while ((c = getopt_long_only(argc, argv, "f:ph", midipp_opts, NULL)) != -1) {
switch (c) {
case 'f':
mpp_input_file = optarg;
break;
case 'p':
mpp_pdf_print = 1;
break;
case ' ':
/* ignore */
break;
default:
usage();
break;
}
}
umidi20_init();
c = umidi20_jack_init("midipp");
if (c != 0 && c != -2 && mpp_pdf_print == 0) {
QMessageBox box;
box.setText(QObject::tr("Could not connect to "
"the JACK subsystem!"));
box.setStandardButtons(QMessageBox::Ok);
box.setIcon(QMessageBox::Critical);
box.setWindowIcon(QIcon(MppIconFile));
box.setWindowTitle(MppVersion);
box.exec();
}
c = umidi20_coremidi_init("midipp");
if (c != 0 && c != -2 && mpp_pdf_print == 0) {
QMessageBox box;
box.setText(QObject::tr("Could not connect to "
"the COREMIDI subsystem!"));
box.setStandardButtons(QMessageBox::Ok);
box.setIcon(QMessageBox::Critical);
box.setWindowIcon(QIcon(MppIconFile));
box.setWindowTitle(MppVersion);
box.exec();
}
#ifdef __ANDROID__
QPlatformNativeInterface *interface = app.platformNativeInterface();
c = umidi20_android_init("midipp",
interface->nativeResourceForIntegration("QtActivity"));
if (c != 0 && c != -2 && mpp_pdf_print == 0) {
QMessageBox box;
box.setText(QObject::tr("Could not connect to "
"the Android MIDI subsystem!"));
box.setStandardButtons(QMessageBox::Ok);
box.setIcon(QMessageBox::Critical);
box.setWindowIcon(QIcon(MppIconFile));
box.setWindowTitle(MppVersion);
box.exec();
}
#endif
MppScoreVariantInit();
MppMainWindow *pmain = new MppMainWindow();
if (mpp_input_file != NULL) {
if (pmain->scores_main[0]->handleScoreFileOpenSub(
QString(mpp_input_file)) != 0) {
errx(1, "Could not open file '%s'", mpp_input_file);
}
}
if (mpp_pdf_print) {
pmain->scores_main[0]->handleScorePrint();
exit(0);
} else {
/* show configuration window by default */
if (strcmp(pmain->deviceName[0], "X:") == 0) {
if (pmain->handle_config_dev(0, 1) == QDialog::Accepted) {
pmain->handle_config_apply();
}
}
pmain->show();
#ifdef HAVE_SCREENSHOT
pmain->ScreenShot(app);
#endif
}
return (app.exec());
}