forked from mihow/bpmdj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpmplay.cpp
629 lines (586 loc) · 18.3 KB
/
bpmplay.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/****
BpmDj v4.2-pl4: Free Dj Tools
Copyright (C) 2001-2012 Werner Van Belle
http://bpmdj.yellowcouch.org/
This program 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 2 of the License, or
(at your option) any later version.
This program 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.
See the authors.txt for a full list of people involved.
****/
#ifndef __loaded__bpmplay_cpp__
#define __loaded__bpmplay_cpp__
using namespace std;
#include <Qt/qapplication.h>
#include <Qt/qmessagebox.h>
#include <Qt/qlistview.h>
#include <Qt/qlcdnumber.h>
#include <Qt/qgroupbox.h>
#include <stdlib.h>
#include <cstdio>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <Qt/qradiobutton.h>
#include <libgen.h>
#include <time.h>
#include <sys/times.h>
#include <assert.h>
#include <Qt/qtabwidget.h>
#include <math.h>
#include <pthread.h>
#include "player.h"
#include "energy-analyzer.h"
#include "bpm-analyzer-dialog.h"
#include "spectrum-analyzer.h"
#include "rhythm-analyzer.h"
#include "md5-analyzer.h"
#include "dsp-drivers.h"
#include "dsp-oss.h"
#include "dsp-alsa.h"
#include "dsp-none.h"
#include "memory.h"
#include "player-core.h"
#include "scripts.h"
#include "signals.h"
#include "histogram-property.h"
#include "smallhistogram-type.h"
#include "bpmplay.h"
#include "capacity-checker.h"
#include "bpmplay-event.h"
#include "clock-drivers.h"
#include "hues.h"
#include "info.h"
#include "bpm-analyzer-impl.h"
#include "bpm-analyzer-env.h"
#include "bpm-analyzer-autocor.h"
#include "bpm-analyzer-2010.h"
#include "bpm-analyzer-2001.h"
#include "bpm-analyzer-wec.h"
/*-------------------------------------------
* Templates we need
*-------------------------------------------*/
template class smallhistogram_type<32>;
template class smallhistogram_type<96>;
template class histogram_property<32>;
template class histogram_property<96>;
template float8 normalize_abs_max<float8>(float8*, long);
template float8 find_abs_max<float8>(float8*, long);
template class Fft<2>;
template class Signal<float8, 2>;
template class Signal<short, 2>;
template class BasicSignal<float8, 2>;
template class BasicSignal<short, 2>;
template class HalfComplex<float8, 2>;
template class Sample<float8, 2>;
template class Sample<short, 2>;
template Sample<short, 2>::Sample(Sample<float8, 2>);
template Sample<float8, 2>::Sample(Sample<short, 2>);
template class Shift<short, 2>;
template class Shift<float8,2>;
template void BasicSignal<float8, 2>::
add<short>(BasicSignal<short, 2> const&);
template void BasicSignal<float8, 2>::
add<float8>(BasicSignal<float8, 2> const&);
template BasicSignal<float8, 2>& BasicSignal<float8, 2>::
operator-=<float8>(BasicSignal<float8, 2> const&);
template BasicSignal<short, 2>& BasicSignal<short, 2>::
operator-=<short>(BasicSignal<short, 2> const&);
template Signal<float8, 2>::
Signal(BasicSignal<short, 2> const&);
template Signal<float8, 2>::
Signal(BasicSignal<float8, 2> const&);
template class Haar<float8, float8, 2>;
template class Haar<short, float8, 2>;
template class SignalIO<signed2,2>;
template void SignalIO<short, 2>::
writeSamples(BasicSignal<float8, 2> const&, unsigned4);
template void SignalIO<short, 2>::
readSamples(BasicSignal<float8, 2>&, unsigned4);
template void SignalIO<short, 2>::
writeSamples(BasicSignal<signed2, 2> const&, unsigned4);
template void SignalIO<short, 2>::
readSamples(BasicSignal<signed2, 2>&, unsigned4);
/*-------------------------------------------
* Constants & Variables
*-------------------------------------------*/
static int opt_batch = 0;
static int opt_bpm = 0;
static int arg_bpm = 0;
static int opt_color = 0;
static int opt_create = 0;
static int opt_energy = 0;
static int opt_rhythm = 0;
static int arg_low = 80;
static int arg_high = 160;
static bool opt_remote = false;
static bool opt_copymusic = false;
static const char * arg_remote = "";
bool opt_setup = false;
const char * arg_config = "";
bool opt_check = false;
PlayerConfig* config;
/*-------------------------------------------
* Parsing arguments
*-------------------------------------------*/
QApplication *app;
Player *player_window;
void options_failure(const char* err)
{
QMessageBox::critical(NULL,
"Command line arguments",
QString(err),
QMessageBox::Ok,QMessageBox::NoButton);
printf("Usage: bpmplay <options> argument\n\n"
" --config name name of the configfile\n"
" -s --setup setup a configuration\n"
" -c --create create an index file if none exists\n"
" -q --quiet be quiet\n"
" -m arg --match arg song to match tempo with\n"
"--remote------------------------------------------\n"
" --remote user@host executes bpmplay remotely\n"
" --copymusic copy music first\n"
"--analysis----------------------------------------\n"
" -b --batch no ui output, md5sum is automatically checked\n"
" --bpm [1,3,4,5,6] measure bpm with specified technique (default = 1)\n"
" 1: autodifference\n"
" 3: envelope spectrum\n"
" 4: autocorrelation\n"
" 5: autocorrelation * envelope spectrum\n"
" 6: bpm2010\n"
" -l nbr --low nbr lowest bpm to look for (default = 120)\n"
" -h nbr --high nbr highest bpm to look for (default = 160)\n"
" --spectrum obtain color & echo information\n"
" --rhythm obtain rhythm information\n"
" -e --energy measure energy levels\n"
" argument the index file of the song to handle\n\n%s\n\n",err);
_exit(1);
}
#define next_arg if (++i>=argc) \
options_failure("missing argument to last option");
#define arg_int(name) next_arg name=atoi(argv[i]);
#define arg_str(name) next_arg name=argv[i];
#define arg_float(name) next_arg name=atof(argv[i]);
void process_options(int argc, char* argv[])
{
int i = 1 ;
bool opt_config = false;
for(i = 1 ; i < argc ; i ++)
{
if (argv[i][0]=='-')
{
char* arg = NULL;
// check long opt ?
if (argv[i][1]=='-')
arg=argv[i]+2;
else if (argv[i][1]==0 || argv[i][2]!=0)
options_failure("option neither short or long");
else arg=argv[i]+1;
// check option
if (option(arg,"quiet","q")) opt_quiet = true;
else if (option(arg,"batch","b")) opt_batch=true;
else if (option(arg,"setup","s")) opt_setup=true;
else if (option(arg,"energy","e")) opt_energy=true;
else if (option(arg,"bpm"))
{
opt_bpm=true;
arg_int(arg_bpm);
}
else if (option(arg,"remote"))
{
opt_remote=true;
arg_str(arg_remote);
argv[i]=argv[i-1]=(char*)"";
}
else if (option(arg,"copymusic")) opt_copymusic=true;
else if (option(arg,"config"))
{
opt_config=true;
arg_str(arg_config);
}
else if (option(arg,"create","c")) opt_create = true;
else if (option(arg,"spectrum")) opt_color = true;
else if (option(arg,"rhythm")) opt_rhythm = true;
else if (option(arg,"low","l"))
{
arg_int(arg_low);
}
else if (option(arg,"high","h"))
{
arg_int(arg_high);
}
else if (option(arg,"check-version"))
{
next_arg;
if (strcmp(argv[i],VERSION)!=0)
{
char err[5000];
sprintf(err,"version mismatch\nbpmplay (%s) is version "
VERSION
"\nbpmdj is version %s",arg_config,argv[i]);
options_failure(err);
_exit(1);
}
opt_check=true;
}
else if (option(arg,"match","m"))
{
opt_match=true;
arg_str(arg_match);
}
}
else
argument = argv[i];
}
if (opt_remote)
{
// when working remote we don't initialize the
// local configuration file
dsp = new dsp_none(*config);
}
else
{
// initialize the local configuration file if mentioned
if (opt_config)
config = new PlayerConfig(arg_config);
else
config = new PlayerConfig();
// are we setting up the show ?
if (opt_setup)
{
dsp = new dsp_none(*config);
return;
}
// are we planning to analyze ?
if (opt_batch)
dsp = new dsp_none(*config);
else
{
dsp = dsp_driver::get_driver(config);
if (is_none_driver() && !opt_check)
QMessageBox::information(NULL, "DSP Driver Selection",
"No DSP driver has been selected. Go to the\n"
"options tab and select an appropriate one.",
QMessageBox::Ok,QMessageBox::NoButton);
}
// we always need a clock driver also for the analyzers.
// During analysis we might set the tempo with set_normal_period.
metronome = new clock_driver();
// initialize the DSP now
if (argument==NULL)
options_failure("the program requires that at least one\n"
".idx file is passed as a command line argument");
if (opt_bpm)
{
if (arg_bpm<1)
options_failure("selected BPM analyzing technique too low (starts"
" with 1)\n");
if (arg_bpm==2)
options_failure("The FFT guided autodifferencer has been removed.\n");
if (arg_bpm>6)
options_failure("selected BPM analyzing technique too hi (ends"
" with 6)\n");
}
if ((opt_color || opt_bpm || opt_energy) && !opt_batch)
options_failure("to start an analyzer, you need to supply the "
"--batch option");
}
}
bool show_error(int err, int err2, QString text)
{
if (err==err2)
{
Error(true,text.toAscii().data());
return true;
}
return false;
}
/**
* The main application thread must be the window thread. This is very annoying
* but that's the way it is. So, we create the application, create the window
* and then initialize the core. This cannot be done in this function
* this should be done when the application is initialized and the event
* loop running (otherwise we cannot give error boxes). Therefore we
* post an event to the player_window which will be received as soon
* as the event loop starts running
*/
void setup_start()
{
playing = NULL;
check_capacities();
player_window = new Player();
player_window->tab->setCurrentIndex(TAB_OPTIONS);
player_window->show();
config->load_ui_position(player_window);
app->postEvent(player_window,new InitAndStart());
app->exec();
config->save_ui_position(player_window);
delete(player_window);
}
void normal_start()
{
int err = core_meta_init();
if (show_error(err, err_needidx,
"Please enter the index file, not the "+extensionList()+
" file\nAn index file can be made with 'bpmplay -c'\n"
"The argument you passed was "+QString(argument)))
_exit(err);
player_window = new Player();
player_window->show();
config->load_ui_position(player_window);
app->postEvent(player_window,new InitAndStart());
app->exec();
config->save_ui_position(player_window);
delete(player_window);
}
/**
* When checking the player we automatically check
* which players are available and check this with the player
* configuration...
*/
void check_start()
{
int err = core_meta_init();
assert(err!=err_needidx);
check_capacities();
// do we have unavailable capacities that are enabled (!disabled)
if (unavailable_capacities & ~config->get_disabled_capacities())
{
const QString a=QString("Mismatched capacities");
const QString b=QString("The enabled capacities include at least one "
"capacity\nthat cannot be found on this machine (")
+QString(arg_config)+QString(")");
QMessageBox::critical(NULL,a,b,QMessageBox::Ok,0,0);
_exit(10);
}
player_window = new Player();
player_window->showMinimized();
app->postEvent(player_window,new InitAndStart());
app->exec();
err = player_window->result() == QDialog::Rejected;
delete(player_window);
if (err)
_exit(10);
}
void batch_start()
{
int nr=0;
// 0. core init: synchronous without opening DSP
core_meta_init();
core_object_init(true);
if (!playing)
{
Debug("No valid index file given to analyze");
return;
}
Info("%d. Wave written: %s",nr++,
playing->readable_description().toAscii().data());
// 1. md5sum
if (playing->get_md5sum().isEmpty())
{
Md5Analyzer * md5_analyzer = new Md5Analyzer();
md5_analyzer->start();
Info("%d. Md5 sum: %s",nr++,
playing->get_md5sum().toAscii().data());
}
// 2. energy levels
if (!playing->fully_defined_energy() || opt_energy)
{
EnergyAnalyzer * energy_analyzer = new EnergyAnalyzer();
energy_analyzer->start();
Info("%d. Min, max : (L: %d, R: %d), (L: %d, R: %d)",nr++,
(int)playing->get_min().left,
(int)playing->get_min().right,
(int)playing->get_max().left,
(int)playing->get_max().right);
Info("%d. Mean, RMS : (L: %d, R: %d), (L: %g, R: %g)",nr++,
(int)playing->get_mean().left,
(int)playing->get_mean().right,
playing->get_power().left,
playing->get_power().right);
}
// 3. BPM
if (opt_bpm)
{
if (playing->get_time_in_seconds()>=760)
Info("%d. Bpm count skipped because song is longer than 760s",nr++);
else if (playing->get_min()==playing->get_max())
Info("%d. Bpm count skipped because song is empty",nr++);
else
{
BpmAnalyzerImpl* bpmanalimp=NULL;
FILE * file = openCoreRawFile();
assert(file);
int fd = fileno(file);
unsigned4 map_length = fsize(file);
unsigned4 startbpm=arg_low;
unsigned4 stopbpm=arg_high;
stereo_sample2* audio = (stereo_sample2*)mmap(NULL,map_length,PROT_READ,MAP_SHARED,fd,0);
assert(audio!=MAP_FAILED);
assert(audio);
if (arg_bpm==3)
{
assert(diskrate==44100);
bpmanalimp=new BpmAnalyzerEnv(audio,map_length/4,startbpm,stopbpm);
}
else if (arg_bpm==4)
{
assert(diskrate==44100);
bpmanalimp=new BpmAnalyzerAutocor(audio,map_length/4,startbpm,stopbpm);
}
else if (arg_bpm==5)
bpmanalimp=new BpmAnalyzerWec(audio,map_length/4,diskrate,startbpm,stopbpm);
else if (arg_bpm==6)
bpmanalimp=new BpmAnalyzer2010(audio,map_length/4,diskrate,startbpm,stopbpm);
else if (arg_bpm==1)
{
assert(diskrate==44100);
bpmanalimp=new BpmAnalyzer2001(audio,map_length/4,startbpm,stopbpm);
}
assert(bpmanalimp);
while(bpmanalimp->step()) ;
tempo_type result(bpmanalimp->tempo);
set_normalperiod_metarate(diskrate_to_metarate(tempo_to_period(result).period*4));
Info("%d. Bpm count: %s",nr++,
playing->get_tempo().qstring().toAscii().data());
}
}
// 4. spectrum
if (opt_color)
{
Info("%d. Spectrum / Echo",nr++);
SpectrumAnalyzer *counter = new SpectrumAnalyzer();
counter->start();
}
// 5. rhythm
if (opt_rhythm)
{
if (playing->get_time_in_seconds()>=760)
Info("%d. Rhythm analysis skipped because song is longer than 760s",
nr++);
else if (!playing->get_spectrum())
Info("%d. Need spectrum before analyzing rhythm",nr++);
else
{
RhythmAnalyzer *r = new RhythmAnalyzer();
r->start();
Info("%d. Rhythm",nr++);
}
}
// 98. Setting tags as necessary (Bug #195)
if (playing->get_min()==playing->get_max())
{
Info("%d. Tagging song as Empty because of no sensible energy levels",
nr++);
playing->set_tags(playing->get_tags()+" Empty");
}
if (playing->get_time_in_seconds()>=760)
{
Info("%d. Tagging song as TooLongToAnalyze because of its length",nr++);
playing->set_tags(playing->get_tags()+" TooLongToAnalyze");
}
if (playing->get_time_in_seconds()>=30*60)
{
Info("%d. Tagging song as Long since it is longer than 30'",nr++);
playing->set_tags(playing->get_tags()+" Long");
}
if (playing->get_time_in_seconds()<30)
{
Info("%d. Tagging song as Short since it is shorter than 30\"",nr++);
playing->set_tags(playing->get_tags()+" Short");
}
// 99. finish the core -> remove the raw file
core_done();
}
bool to_remote(char* filename)
{
return vexecute(SSH" %s mkdir -p %s",arg_remote,
escape(escape(dirname(strdup(filename)))))
&& vexecute(" scp -q %s %s:%s",escape(filename),arg_remote,
escape(escape(dirname(strdup(filename)))));
}
bool from_remote(char* filename)
{
return vexecute(" scp -q %s:%s %s ",arg_remote,
escape(escape(filename)),escape(filename));
}
bool delete_remote(char* filename)
{
return vexecute(SSH" %s rm %s",arg_remote,escape(escape(filename)));
}
int remote(int argc, char* argv[])
{
char mp3[2000];
if (!opt_setup)
{
Index * toplay = new Index(argument);
if (opt_copymusic)
Remote("Uploading song & index file");
else
Remote("Uploading index file");
sprintf(mp3,"./music/%s",
toplay->get_filename().toAscii().data());
if (opt_copymusic)
if (!to_remote(mp3)) return 1;
if (!to_remote(argument)) return 2;
if (opt_match && strcmp(arg_match,argument))
if (!to_remote(arg_match)) return 3;
}
char newcmd[2000];
sprintf(newcmd,SSH" %s ",arg_remote);
for(int i = 0 ; i < argc ; i ++)
{
strcat(newcmd,escape(escape(argv[i])));
strcat(newcmd," ");
}
Remote("Executing remote player");
if (!execute("Executing remote player",newcmd)) return 4;
if (!opt_setup)
{
Remote("Downloading index file");
if (!from_remote(argument)) return 5;
if (!delete_remote(argument)) return 6;
if (opt_copymusic)
if (!delete_remote(mp3)) return 7;
if (opt_match && strcmp(arg_match,argument))
if (!delete_remote(arg_match)) return 8;
}
return 0;
}
int main(int argc, char *argv[])
{
init_embedded_files();
dsp_driver::init();
app = new QApplication(argc,argv);
init_hues();
process_options(argc,argv);
if (opt_remote)
_exit(remote(argc,argv));
if (opt_create)
{
Index *index = createNewIndexFor(argument,"./");
argument = strdup(index->get_storedin().toAscii().data());
delete(index);
}
if (opt_check)
check_start();
else if (opt_batch)
batch_start();
else if (opt_setup)
setup_start();
else
normal_start();
delete(app);
_exit(0);
}
#endif // __loaded__bpmplay_cpp__