forked from kmatheussen/jack_capture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jack_capture.c
2728 lines (2166 loc) · 76.7 KB
/
jack_capture.c
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Kjetil Matheussen, 2005-2013.
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "das_config.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <sndfile.h>
#include <pthread.h>
#include <semaphore.h>
#include <math.h>
#include <stdarg.h>
#include <termios.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <jack/jack.h>
#include <libgen.h>
#include <sys/wait.h>
#if HAVE_LAME
#include <lame/lame.h>
#endif
#if HAVE_LIBLO
#include <lo/lo.h>
/* defined in osc.c */
int init_osc(int osc_port);
void shutdown_osc(void);
#endif
#include "vringbuffer.h"
#define JC_MAX(a,b) (((a)>(b))?(a):(b))
#define JC_MIN(a,b) (((a)<(b))?(a):(b))
#define ALIGN_UP(value,alignment) (((uintptr_t)value + alignment - 1) & -alignment)
#define ALIGN_UP_DOUBLE(p) ALIGN_UP(p,sizeof(double)) // Using double because double should always be very large.
#define OPTARGS_CHECK_GET(wrong,right) lokke==argc-1?(fprintf(stderr,"Must supply argument for '%s'\n",argv[lokke]),exit(-4),wrong):right
#define OPTARGS_BEGIN(das_usage) {int lokke;const char *usage=das_usage;for(lokke=0;lokke<argc;lokke++){char *a=argv[lokke];if(!strcmp("--help",a)||!strcmp("-h",a)){fprintf(stderr,"%s",usage);exit(0);
#define OPTARG(name,name2) }}else if(!strcmp(name,a)||!strcmp(name2,a)){{
#define OPTARG_GETINT() OPTARGS_CHECK_GET(0,atoll(argv[++lokke]))
//int optargs_inttemp;
//#define OPTARG_GETINT() OPTARGS_CHECK_GET(0,(optargs_inttemp=strtol(argv[++lokke],(char**)NULL,10),errno!=0?(perror("strtol"),0):optargs_inttemp))
#define OPTARG_GETFLOAT() OPTARGS_CHECK_GET(0.0f,atof(argv[++lokke]))
#define OPTARG_GETSTRING() OPTARGS_CHECK_GET("",argv[++lokke])
#define OPTARG_LAST() }}else if(lokke==argc-1 && argv[lokke][0]!='-'){lokke--;{
#define OPTARGS_ELSE() }else if(1){
#define OPTARGS_END }else{fprintf(stderr,"%s",usage);exit(-1);}}}
/* Arguments and their default values */
#define DEFAULT_MIN_BUFFER_TIME 4
#define DEFAULT_MIN_MP3_BUFFER_TIME 8
static float min_buffer_time=-1.0f;
static float max_buffer_time=40.0f;
static jack_client_t *client=NULL;
#define DEFAULT_NUM_CHANNELS 2
#define DEFAULT_NUM_CHANNELS_SDS 1
static int num_channels=-1;
static int bitdepth=0;
static char *base_filename=NULL;
static char *filename=NULL;
static bool use_vu=true;
static bool use_meterbridge=false;
static bool show_bufferusage=true;
static char *meterbridge_type="vu";
static char *meterbridge_reference="0";
//static const int vu_len=56;
#define vu_len 65
static int vu_dB=true;
static float vu_bias=1.0f;
static int leading_zeros=1;
static char *filename_prefix="jack_capture_";
static int64_t num_frames_recorded=0;
static int64_t num_frames_to_record=0;
static bool fixed_duration=false;
static bool no_stdin=false;
static double recording_time=0.0;
static const int disk_error_stop=0;
static int disk_errors=0;
static bool soundfile_format_is_set=false;
static char *soundfile_format="wav";
static char *soundfile_format_one_or_two="wav";
#define ONE_OR_TWO_CHANNELS_FORMAT SF_FORMAT_WAV
static char *soundfile_format_multi="wavex";
#define MORE_THAN_TWO_CHANNELS_FORMAT SF_FORMAT_WAVEX
bool silent=false;
bool verbose=false;
static bool absolutely_silent=false;
static bool create_tme_file=false;
static bool write_to_stdout=false;
static bool write_to_mp3 = false;
static int das_lame_quality = 2; // 0 best, 9 worst.
static int das_lame_bitrate = -1;
static bool use_jack_transport = false;
static bool use_jack_freewheel = false;
static bool use_manual_connections = false;
#if HAVE_LIBLO
static int osc_port = -1;
#endif
static char *hook_cmd_opened = NULL;
static char *hook_cmd_closed = NULL;
static char *hook_cmd_rotate = NULL;
static char *hook_cmd_timing = NULL;
static int64_t rotateframe=0;
static bool timemachine_mode = false;
static bool timemachine_recording = false;
static float timemachine_prebuffer = 8.0f;
static int64_t num_frames_written_to_disk = 0;
static bool program_ended_with_return = false;
/* JACK data */
static jack_port_t **ports;
static jack_port_t **ports_meterbridge=NULL;
typedef jack_default_audio_sample_t sample_t;
static float jack_samplerate;
static bool jack_has_been_shut_down=false;
static int64_t unreported_overruns=0;
static int total_overruns=0;
static int total_xruns=0;
static volatile int freewheel_mode=0;
/* Disk thread */
#if HAVE_LAME
static lame_global_flags *lame;
#endif
static bool disk_thread_has_high_priority=false;
/* Helper thread */
static pthread_t helper_thread={0};
static float *vu_vals=NULL;
static int *vu_times=NULL;
static int *vu_peaks=NULL;
static float *vu_peakvals=NULL;
static void print_message(const char *fmt, ...);
/* Synchronization between jack process thread and disk thread. */
static volatile int is_initialized=0; // This $@#$@#$ variable is needed because jack ports must be initialized _after_ (???) the client is activated. (stupid jack)
static volatile int is_running=1; // Mostly used to stop recording as early as possible.
/* Buffer */
static int block_size; // Set once only. Never changes value after that, even if jack buffer size changes.
static bool buffer_interleaved = true;
typedef struct buffer_t{
int overruns;
int pos;
sample_t data[];
} buffer_t;
//static pid_t mainpid;
static buffer_t *current_buffer;
/* Jack connecting thread. */
static pthread_t connect_thread={0} ;
// das stop semaphore
static sem_t stop_sem;
/////////////////////////////////////////////////////////////////////
//////////////////////// VARIOUS ////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#if 0
void get_free_mem(void){
struct sysinfo sys_info;
if(sysinfo(&sys_info) != 0)
perror("sysinfo");
printf("Total Ram ----- %uk\tFree Ram ------ %uk\n", sys_info.totalram
,
sys_info.freeram / 1024);
}
#endif
char *string_concat(char *s1,char *s2);
static void verbose_print(const char *fmt, ...){
if (absolutely_silent==true) return;
if(verbose==true){
va_list argp;
va_start(argp,fmt);
vfprintf(stderr,fmt,argp);
va_end(argp); } }
static void* my_calloc(size_t size1,size_t size2){
size_t size = size1*size2;
void* ret = malloc(size);
if(ret==NULL){
fprintf(stderr,"\nOut of memory. Try a smaller buffer.\n");
return NULL; }
memset(ret,0,size);
return ret; }
static bool set_high_priority(void){
static bool shown_warning = false;
static int prio = -20;
while(prio<0 && setpriority(PRIO_PROCESS,0,prio)==-1)
prio++;
if(prio==0 && shown_warning==false){
print_message("Warning. Could not set higher priority for a SCHED_OTHER process using setpriority().\n");
shown_warning=true; }
if(prio < 0)
return true;
else
return false; }
static int echo_turned_on=true;
static struct termios current;
static struct termios initial;
static void turn_off_echo(void){
tcgetattr( STDIN_FILENO, &initial );
current = initial;
current.c_lflag &= ~ECHO; // added
tcsetattr( STDIN_FILENO, TCSANOW, ¤t );
echo_turned_on=false; }
static void turn_on_echo(void){
if(echo_turned_on==true)
return;
tcsetattr( STDIN_FILENO, TCSANOW, &initial );
echo_turned_on=true; }
/////////////////////////////////////////////////////////////////////
//////////////////////// BUFFERS ////////////////////////////////////
/////////////////////////////////////////////////////////////////////
static vringbuffer_t *vringbuffer;
//static sample_t **buffers=NULL;
static sample_t *empty_buffer;
static int buffer_size_in_bytes;
// block size in bytes = jack block size (1 channel) * sizeof(float)
// buffer size in bytes = block size in bytes * num_channels
static int64_t seconds_to_frames(float seconds){
return (int64_t) (((long double)seconds)*((long double)jack_samplerate));
}
static float frames_to_seconds(int frames){
return ((float)frames)/jack_samplerate;
}
// round up.
static int seconds_to_blocks(float seconds){
return (int)ceilf((seconds*jack_samplerate/(float)block_size));
}
// same return value as seconds_to_blocks
static int seconds_to_buffers(float seconds){
return seconds_to_blocks(seconds);
}
#if 0
// not used
static int block_time_in_microseconds(void){
return (int)(((double)block_size*1000000.0)/(double)jack_samplerate);
}
#endif
static float blocks_to_seconds(int blocks){
return (float)blocks*(float)block_size/jack_samplerate;
}
// same return value as blocks_to_seconds
static float buffers_to_seconds(int buffers){
return blocks_to_seconds(buffers);
}
static int autoincrease_callback(vringbuffer_t *vrb, bool first_call, int reading_size, int writing_size){
(void)vrb;
(void)reading_size;
if(use_jack_freewheel)
return 0;
if(first_call){
set_high_priority();
return 0; }
if(timemachine_mode==true && timemachine_recording==false)
return 0;
#if 0
if(1){
static jack_transport_state_t prev_state=-1;
jack_position_t pos;
jack_transport_state_t state=jack_transport_query(client,&pos);
if(state!=prev_state){
print_message("frame: %d\n",pos.frame);
prev_state=state; } }
#endif
if(buffers_to_seconds(writing_size) < min_buffer_time)
return 2; // autoincrease_callback is called approx. at every block. So it should not be necessary to return a value higher than 2. Returning a very low number might also theoretically put a lower constant strain on the memory bus, thus theoretically lower the chance of xruns.
return 0;
}
static void buffers_init(){
verbose_print("bufinit1. sizeof(long): %u, sizeof(float): %u, sizeof(double):%u, sizeof(int):%u, sizeof(void*):%u\n",sizeof(long),sizeof(float),sizeof(double),sizeof(int),sizeof(void*));
vringbuffer = vringbuffer_create(JC_MAX(4,seconds_to_buffers(min_buffer_time)),
JC_MAX(4,seconds_to_buffers(max_buffer_time)),
buffer_size_in_bytes);
if(vringbuffer==NULL){
fprintf(stderr,"Unable to allocate memory for buffers\n");
exit(-1);
}
vringbuffer_set_autoincrease_callback(vringbuffer,autoincrease_callback,0);
current_buffer = vringbuffer_get_writing(vringbuffer);
empty_buffer = my_calloc(sizeof(sample_t),block_size*num_channels);
}
/////////////////////////////////////////////////////////////////////
//////////////////////// PORTNAMES //////////////////////////////////
/////////////////////////////////////////////////////////////////////
static const char **cportnames=NULL;
static int num_cportnames=0;
static int findnumports(const char **ports){
int ret=0;
while(ports && ports[ret]!=NULL)
ret++;
return ret;
}
static void portnames_add_defaults(void){
if(cportnames==NULL){
{
const char **portnames = jack_get_ports(client,NULL,NULL,JackPortIsPhysical|JackPortIsInput);
int num_ports = findnumports(portnames);
if(num_ports==0){
fprintf(stderr,"No physical output ports found in your jack setup. Exiting.\n");
exit(0);
}
cportnames = my_calloc(sizeof(char*), num_ports+1);
{
int i;
for(i=0;i<num_ports;i++)
cportnames[i] = portnames[i];
}
jack_free(portnames);
}
num_cportnames=JC_MAX(DEFAULT_NUM_CHANNELS,findnumports(cportnames));
if(num_channels==-1) {
if(!strcasecmp("sds",soundfile_format)){
num_channels=DEFAULT_NUM_CHANNELS_SDS;
}else{
num_channels=DEFAULT_NUM_CHANNELS;
}
}
}else
if(num_channels==-1)
num_channels=num_cportnames;
if(num_channels<=0){
fprintf(stderr,"No point recording 0 channels. Exiting.\n");
exit(0);
}
// At this point, the variable "num_channels" has a known and valid value.
vu_vals = my_calloc(sizeof(float),num_channels);
vu_times = my_calloc(sizeof(int),num_channels);
vu_peaks = my_calloc(sizeof(int),num_channels);
vu_peakvals = my_calloc(sizeof(float),num_channels);
buffer_size_in_bytes = ALIGN_UP_DOUBLE(sizeof(buffer_t) + block_size*num_channels*sizeof(sample_t));
verbose_print("buf_size_in_bytes: %d\n",buffer_size_in_bytes);
}
static void portnames_add(char *name){
const char **new_outportnames;
int add_ch;
if(name[strlen(name)-1]=='*'){
char *pattern=strdup(name);
pattern[strlen(name)-1]=0;
new_outportnames = jack_get_ports(client,pattern,"",0);
//char **new_outportnames = (char**)jack_get_ports(client,"system:capture_1$","",0);
add_ch = findnumports(new_outportnames);
free(pattern);
}else{
new_outportnames = my_calloc(1,sizeof(char*));
new_outportnames[0] = name;
add_ch = 1;
}
if(add_ch>0){
int ch;
cportnames=realloc(cportnames,(num_cportnames+add_ch)*sizeof(char*));
for(ch=0;ch<add_ch;ch++){
cportnames[num_cportnames]=new_outportnames[ch];
//fprintf(stderr,"ch: %d, num_ch: %d, new_outportnames[ch]: %s, %s\n",ch,num_cportnames,new_outportnames[ch],new_outportnames[ch+1]);
num_cportnames++;
}
}else{
fprintf(stderr,"\nWarning, no port(s) with name \"%s\".\n",name);
if(cportnames==NULL)
if(silent==false)
fprintf(stderr,"This could lead to using default ports instead.\n");
}
}
static const char **portnames_get_connections(int ch, bool *using_calloc){
if(ch>=num_cportnames)
return NULL;
else{
jack_port_t *port = jack_port_by_name(client,cportnames[ch]);
const char **ret;
if(port==NULL){
print_message("Error, port with name \"%s\" not found.\n",cportnames[ch]);
return NULL;
}
if(jack_port_flags(port) & JackPortIsInput){
ret = jack_port_get_all_connections(client,port);
*using_calloc = false;
}else{
ret = my_calloc(2,sizeof(char*));
ret[0] = cportnames[ch];
*using_calloc = true;
}
return ret;
}
}
/////////////////////////////////////////////////////////////////////
//////////////////////// console meter //////////////////////////////
/////////////////////////////////////////////////////////////////////
// Note that the name "vu" is used instead of "console meter".
// I know (now) it's not a vu at all. :-)
// Function iec_scale picked from meterbridge by Steve Harris.
static int iec_scale(float db) {
float def = 0.0f; /* Meter deflection %age */
if (db < -70.0f) {
def = 0.0f;
} else if (db < -60.0f) {
def = (db + 70.0f) * 0.25f;
} else if (db < -50.0f) {
def = (db + 60.0f) * 0.5f + 5.0f;
} else if (db < -40.0f) {
def = (db + 50.0f) * 0.75f + 7.5;
} else if (db < -30.0f) {
def = (db + 40.0f) * 1.5f + 15.0f;
} else if (db < -20.0f) {
def = (db + 30.0f) * 2.0f + 30.0f;
} else if (db < 0.0f) {
def = (db + 20.0f) * 2.5f + 50.0f;
} else {
def = 100.0f;
}
return (int)(def * 2.0f);
}
static void msleep(int n){
usleep(n*1000);
}
static void print_ln(void){
putchar('\n');
//msleep(3);
}
static void print_console_top(void){
if(use_vu){
int lokke=0;
char c='"';
// Set cyan color
printf("%c[36m",0x1b);
//printf("****");
printf(" |");
for(lokke=0;lokke<vu_len;lokke++)
putchar(c);
printf("|");print_ln();
printf("%c[0m",0x1b); // reset colors
fflush(stdout);
}else{
//print_ln();
}
}
static void init_vu(void){
//int num_channels=4;
int ch;
for(ch=0;ch<num_channels;ch++)
print_ln();
}
static void init_show_bufferusage(void){
print_ln();
}
static void move_cursor_to_top(void){
printf("%c[%dA",0x1b,
use_vu&&show_bufferusage
? num_channels+1
: use_vu
? num_channels
: show_bufferusage
? 1
: 0);
printf("%c[0m",0x1b); // reset colors
fflush(stdout);
}
static char *vu_not_recording="-----------Press <Return> to start recording------------";
// Console colors:
// http://www.linuxjournal.com/article/8603
static void print_console(bool move_cursor_to_top_doit,bool force_update){
//int num_channels=4;
int ch;
char vol[vu_len+50];
vol[2] = ':';
vol[3] = '|';
vol[4+vu_len+1] = 0;
// Values have not been updated since last time. Return.
if(force_update==false && vu_vals[0]==-1.0f)
return;
if(move_cursor_to_top_doit)
move_cursor_to_top();
if(use_vu){
// Set cyan color
printf("%c[36m",0x1b);
for(ch=0;ch<num_channels;ch++){
int i;
float val = vu_vals[ch];
int pos;
vu_vals[ch] = -1.0f;
if(vu_dB)
pos = iec_scale(20.0f * log10f(val * vu_bias)) * (vu_len) / 200;
else
pos = val*(vu_len);
if (pos > vu_peaks[ch]) {
vu_peaks[ch] = pos;
vu_peakvals[ch] = val;
vu_times[ch] = 0;
} else if (vu_times[ch]++ > 40) {
vu_peaks[ch] = pos;
vu_peakvals[ch] = val;
}
if(ch>9){
vol[0] = '0'+ch/10;
vol[1] = '0'+ch-(10*(ch/10));
}else{
vol[0] = '0';
vol[1] = '0'+ch;
}
if (timemachine_mode==true && timemachine_recording==false) {
for(i=0;i<pos && val>0.0f;i++)
vol[4+i] = vu_not_recording[i];
vol[4+pos]='\0';
if(vu_peakvals[ch]>=1.0f)
printf("%c[31m",0x1b); // Peaking, show red color
printf("%s", vol);
for(;i<vu_len;i++)
vol[4+i] = vu_not_recording[i];
printf("%c[33m",0x1b); // Yellow color
vol[i+4]='\0';
printf("%s", vol+4+pos);
printf("%c[36m",0x1b); // back to cyan
printf("|\n");
} else {
for(i=0;i<vu_len;i++)
if(vu_peaks[ch]==i && vu_peakvals[ch]>0.0f)
vol[4+i] = '*';
else if(i<=pos && val>0.0f)
vol[4+i] = '-';
else
vol[4+i] = ' ';
if(vu_peakvals[ch]>=1.0f){
vol[4+vu_len]='!';
printf("%c[31m",0x1b); //red color
puts(vol);
printf("%c[36m",0x1b); // back to cyan
}else{
vol[4+vu_len]='|';
puts(vol);
}
}
}
}
if(show_bufferusage){
int num_bufleft = vringbuffer_writing_size(vringbuffer);
int num_buffers = (vringbuffer_reading_size(vringbuffer)+ vringbuffer_writing_size(vringbuffer));
float buflen = buffers_to_seconds(num_buffers);
float bufleft = buffers_to_seconds(num_bufleft);
int recorded_seconds = (int)frames_to_seconds(num_frames_recorded);
if(timemachine_mode==true)
recorded_seconds = (int)frames_to_seconds(num_frames_written_to_disk);
int recorded_minutes = recorded_seconds/60;
char buffer_string[1000];
{
sprintf(buffer_string,"%.2fs./%.2fs",bufleft,buflen);
int len_buffer=strlen(buffer_string);
int i;
for(i=len_buffer;i<14;i++)
buffer_string[i]=' ';
buffer_string[i]='\0';
}
printf("%c[32m"
"Buffer: %s"
" Time: %d.%s%dm. %s"
"DHP: [%c] "
"Overruns: %d "
"Xruns: %d"
"%c[0m",
//fmaxf(0.0f,buflen-bufleft),buflen,
0x1b, // green color
buffer_string,
recorded_minutes, recorded_seconds%60<10?"0":"", recorded_seconds%60, recorded_minutes<10?" ":"",
disk_thread_has_high_priority?'x':' ',
total_overruns,
total_xruns,
0x1b // reset color
);
print_ln();
}else{
printf("%c[0m",0x1b); // reset colors
fprintf(stderr,"%c[0m",0x1b); // reset colors
}
fflush(stdout);
fflush(stderr);
}
/////////////////////////////////////////////////////////////////////
//////////////////////// Helper thread //////////////////////////////
/////////////////////////////////////////////////////////////////////
#define MESSAGE_PREFIX ">>> "
static char message_string[5000]={0};
static volatile int helper_thread_running=0;
static int init_meterbridge_ports();
static bool is_helper_running=true;
static void *helper_thread_func(void *arg){
(void)arg;
helper_thread_running=1;
if(use_vu||show_bufferusage)
print_console_top();
if(use_vu)
init_vu();
if(show_bufferusage)
init_show_bufferusage();
do{
bool move_cursor_to_top_doit=true;
if(message_string[0]!=0){
if(use_vu || show_bufferusage){
move_cursor_to_top();
if(!use_vu){
print_ln();
}
printf("%c[%dA",0x1b,1); // move up yet another line.
msleep(5);
printf("%c[31m",0x1b); // set red color
{ // clear line
int lokke;
for(lokke=0;lokke<vu_len+5;lokke++)
putchar(' ');
print_ln();
printf("%c[%dA",0x1b,1); // move up again
msleep(5);
}
}
printf(MESSAGE_PREFIX);
printf("%s",message_string);
message_string[0]=0;
move_cursor_to_top_doit=false;
if(use_vu || show_bufferusage)
print_console_top();
}
if(use_vu || show_bufferusage)
print_console(move_cursor_to_top_doit,false);
if(init_meterbridge_ports()==1 && use_vu==false && show_bufferusage==false) // Note, init_meterbridge_ports will usually exit at the top of the function, where it tests for ports_meterbridge!=NULL (this stuff needs to be handled better)
break;
msleep(1000/20);
}while(is_helper_running);
if(use_vu || show_bufferusage){
print_console(true,true);
}
message_string[0] = 0;
helper_thread_running = 0;
msleep(4);
return NULL;
}
static pthread_mutex_t print_message_mutex = PTHREAD_MUTEX_INITIALIZER;
static void print_message(const char *fmt, ...){
if (absolutely_silent==true) return;
if(helper_thread_running==0 || write_to_stdout==true){
va_list argp;
va_start(argp,fmt);
fprintf(stderr,"%c[31m" MESSAGE_PREFIX,0x1b); // set red color
vfprintf(stderr,fmt,argp);
fprintf(stderr,"%c[0m",0x1b); // reset colors
fflush(stderr);
va_end(argp);
}else{
pthread_mutex_lock(&print_message_mutex);{
while(message_string[0]!=0)
msleep(2);
va_list argp;
va_start(argp,fmt);
vsprintf(message_string,fmt,argp);
va_end(argp);
while(message_string[0]!=0)
msleep(2);
}pthread_mutex_unlock(&print_message_mutex);
}
}
void setup_helper_thread (void){
if(write_to_stdout==false){
pthread_create(&helper_thread, NULL, helper_thread_func, NULL);
}
}
static void stop_helper_thread(void){
//helper_thread_running=0;
is_helper_running=false;
if(write_to_stdout==false){
pthread_join(helper_thread, NULL);
}
/*
if(use_vu||show_bufferusage){
printf("%c[0m",0x1b); // reset colors
usleep(1000000/2); // wait for terminal
}
*/
}
/////////////////////////////////////////////////////////////////////
//////////////////////// DISK Thread hooks //////////////////////////
/////////////////////////////////////////////////////////////////////
#ifndef __USE_GNU
/* This code has been derived from an example in the glibc2 documentation.
* "asprintf() implementation for braindamaged operating systems"
* Copyright (C) 1991, 1994-1999, 2000, 2001 Free Software Foundation, Inc.
*/
#ifdef _WIN32
#define vsnprintf _vsnprintf
#endif
int asprintf(char **buffer, char *fmt, ...) {
/* Guess we need no more than 200 chars of space. */
int size = 200;
int nchars;
va_list ap;
*buffer = (char*)malloc(size);
if (*buffer == NULL) return -1;
/* Try to print in the allocated space. */
va_start(ap, fmt);
nchars = vsnprintf(*buffer, size, fmt, ap);
va_end(ap);
if (nchars >= size)
{
char *tmpbuff;
/* Reallocate buffer now that we know how much space is needed. */
size = nchars+1;
tmpbuff = (char*)realloc(*buffer, size);
if (tmpbuff == NULL) { /* we need to free it*/
free(*buffer);
return -1;
}
*buffer=tmpbuff;
/* Try again. */
va_start(ap, fmt);
nchars = vsnprintf(*buffer, size, fmt, ap);
va_end(ap);
}
if (nchars < 0) return nchars;
return size;
}
#endif
#define ARGS_ADD_ARGV(FMT,ARG) \
argv=(char**) realloc((void*)argv, (argc+2)*sizeof(char*)); \
asprintf(&argv[argc++], FMT, ARG); argv[argc] = 0;
#define PREPARE_ARGV(CMD,ARGC,ARGV) \
{\
char *bntmp = strdup(CMD); \
ARGC=0; \
ARGV=(char**) calloc(2,sizeof(char*)); \
ARGV[ARGC++] = strdup(basename(bntmp));\
free(bntmp); \
}
static void wait_child(int sig){
(void)sig;
wait(NULL);
}
static void call_hook(const char *cmd, int argc, char **argv){
/* invoke external command */
if (verbose==true) {
fprintf(stderr, "EXE: %s ", cmd);
for (argc=0;argv[argc];++argc) printf("'%s' ", argv[argc]);
printf("\n");
}
pid_t pid=fork();
if (pid==0) {
/* child process */
if(1){ /* redirect all output of child process*/
/* one day this if(1) could become a global option */
int fd;
if((fd = open("/dev/null", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR))==-1){
perror("open");
}else{
dup2(fd,STDOUT_FILENO);
dup2(fd,STDERR_FILENO);
close(fd);
}
}
execvp (cmd, (char *const *) argv);
print_message("EXE: error; exec returned.\n");
//pause();
exit(127);
}
/* parent/main process */
if (pid < 0 ) {
print_message("EXE: error; can not fork child process\n");
}
signal(SIGCHLD,wait_child);
/* clean up */
for (argc=0;argv[argc];++argc) {
free(argv[argc]);
}
free (argv);
}
static void hook_file_opened(char *fn){
char **argv; int argc;
const char *cmd = hook_cmd_opened;