-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsbagen.c
1911 lines (1677 loc) · 45 KB
/
sbagen.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
//
// SBaGen - Sequenced Binaural Beat Generator
//
// (c) 1999-2007 Jim Peters <[email protected]>. All Rights Reserved.
// For latest version see http://sbagen.sf.net/ or
// http://uazu.net/sbagen/. Released under the GNU GPL version 2.
// Use at your own risk.
//
// " 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, version 2.
//
// 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 file COPYING for details of this license.
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// Some code fragments in the Win32 audio handling are based on
// code from PLIB (c) 2001 by Steve Baker, originally released
// under the LGPL (slDSP.cxx and sl.h). For the original source,
// see the PLIB project: http://plib.sf.net
//
// The code for the Mac audio output was based on code from the
// FINK project's patches to ESounD, by Shawn Hsiao and Masanori
// Sekino. See: http://fink.sf.net
// 2010-07-18 - Nicolas George
// Ported to Java Native Interface for Android:
// Removed #ifdef soup.
// Unconditionnally removed soundcard related code.
// Hardcode Unix style functions, no fancy tty output.
// Remove Ogg/MP3 decoders.
// Remove most diagnosis.
// Remove input mixing.
// Always output to auxiliary funcion.
// Never sync to clock.
// Make most functions and variables static.
// Remove immediate and preprogrammed modes.
// Reject options in source file.
// Free all mallocated structures.
// Provide clean entry points.
// Make parser and loop return an error instead of exit.
// Return the error in a buffer.
// Removed helpful diagnosis.
/*
API of the librarized version of sbagen.c, in rough calling order.
All fonctions that can fail return 0 in case of success and -1 in case of
error.
int sbagen_init(void);
-> Computes the sin table; can fail if malloc fails.
int sbagen_set_parameters(int rate, int prate, int fade, const char *roll);
-> Sets decoding parameters; can fail if roll is invalid.
rate: sample rate; default: 44100; option -r
prate: frequency recalculation, default: 10, option -R
fade: fade time (ms), default: 60000, option -F
roll: headphone roll-off compensation, option -c (see sbagen doc)
int sbagen_parse_seq(const char *seq);
-> Parses the sequence; can fail on syntax error or out of memory.
seq: the text of the sequence, not the filename.
int sbagen_run(void);
-> Generates the waves; can fail on out of memory or if writeOut fails.
void sbagen_free_seq(void);
-> Frees the memory allocates by sbagen_parse_seq.
void
sbagen_exit(void);
-> Frees the sin table.
char *
sbagen_get_error(void);
-> Returns the error message; never fails.
static int writeOut(char *buf, int size);
-> To be implemented. Called by sbagen_run; can return -1 to fail.
buf: samples; actually "short (*buf)[2]"
size: size of buf in octets; divide by 4 (2 o/sample, 2 channels)
*/
#define VERSION "1.4.4"
// This should be built with one of the following target macros
// defined, which selects options for that platform, or else with some
// of the individual named flags #defined as listed later.
//
// Ogg and MP3 support is handled separately from the T_* macros.
#include <stdio.h>
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdint.h>
typedef int64_t S64;
#include <sys/times.h>
typedef struct Channel Channel;
typedef struct Voice Voice;
typedef struct Period Period;
typedef struct NameDef NameDef;
typedef struct BlockDef BlockDef;
typedef unsigned char uchar;
static inline int t_per24(int t0, int t1) ;
static inline int t_per0(int t0, int t1) ;
static inline int t_mid(int t0, int t1) ;
static int init_sin_table(void) ;
static void * Alloc(size_t len) ;
static char * StrDup(char *str) ;
static int loop() ;
static int outChunk() ;
static void corrVal(int ) ;
static int readLine() ;
static char * getWord(void) ;
static void badSeq(void) ;
static int readSeq(const char *text) ;
static int correctPeriods();
static int setup_device(void) ;
static int readNameDef();
static int readTimeLine();
static int voicesEq(Voice *, Voice *);
static void error(char *fmt, ...) ;
static int readTime(char *, int *);
static int writeOut(char *, int);
static int sinc_interpolate(double *, int, int *);
static int handleOptions(char *p);
static int setupOptC(const char *spec) ;
#define N_CH 16 // Number of channels
struct Voice {
int typ; // Voice type: 0 off, 1 binaural, 2 pink noise, 3 bell, 4 spin,
// 5 mix, 6 mixspin, 7 mixbeat, -1 to -100 wave00 to wave99
double amp; // Amplitude level (0-4096 for 0-100%)
double carr; // Carrier freq (for binaural/bell), width (for spin)
double res; // Resonance freq (-ve or +ve) (for binaural/spin)
};
struct Channel {
Voice v; // Current voice setting (updated from current period)
int typ; // Current type: 0 off, 1 binaural, 2 pink noise, 3 bell, 4 spin,
// 5 mix, 6 mixspin, 7 mixbeat, -1 to -100 wave00 to wave99
int amp, amp2; // Current state, according to current type
int inc1, off1; // :: (for binaural tones, offset + increment into sine
int inc2, off2; // :: table * 65536)
};
struct Period {
Period *nxt, *prv; // Next/prev in chain
int tim; // Start time (end time is ->nxt->tim)
Voice v0[N_CH], v1[N_CH]; // Start and end voices
int fi, fo; // Temporary: Fade-in, fade-out modes
};
struct NameDef {
NameDef *nxt;
char *name; // Name of definition
BlockDef *blk; // Non-zero for block definition
Voice vv[N_CH]; // Voice-set for it (unless a block definition)
};
struct BlockDef {
BlockDef *nxt; // Next in chain
char *lin; // StrDup'd line
};
#define ST_AMP 0x7FFFF // Amplitude of wave in sine-table
#define NS_ADJ 12 // Noise is generated internally with amplitude ST_AMP<<NS_ADJ
#define NS_DITHER 16 // How many bits right to shift the noise for dithering
#define NS_AMP (ST_AMP<<NS_ADJ)
#define ST_SIZ 16384 // Number of elements in sine-table (power of 2)
static int *sin_table;
#define AMP_DA(pc) (40.96 * (pc)) // Display value (%age) to ->amp value
#define AMP_AD(amp) ((amp) / 40.96) // Amplitude value to display %age
static int *waves[100]; // Pointers are either 0 or point to a sin_table[]-style array of int
static Channel chan[N_CH]; // Current channel states
static int now; // Current time (milliseconds from midnight)
static Period *per= 0; // Current period
static NameDef *nlist; // Full list of name definitions
static int *tmp_buf; // Temporary buffer for 20-bit mix values
static short *out_buf; // Output buffer
static int out_bsiz; // Output buffer size (bytes)
static int out_blen; // Output buffer length (samples) (1.0* or 0.5* out_bsiz)
static int out_bps; // Output bytes per sample (2 or 4)
static int out_buf_ms; // Time to output a buffer-ful in ms
static int out_buf_lo; // Time to output a buffer-ful, fine-tuning in ms/0x10000
static int out_fd; // Output file descriptor
static int out_rate= 44100; // Sample rate
static int out_prate= 10; // Rate of parameter change (for file and pipe output only)
static int fade_int= 60000; // Fade interval (ms)
static const char *in_text; // Input sequence text
static int in_lin; // Current input line
static char buf[4096]; // Buffer for current line
static char buf_copy[4096]; // Used to keep unmodified copy of line
static char *lin; // Input line (uses buf[])
static char *lin_copy; // Copy of input line
static double spin_carr_max; // Maximum 'carrier' value for spin (really max width in us)
static char error_message[256]; // Buffer for the error message
#define NS_BIT 10
//static int ns_tbl[1<<NS_BIT];
//static int ns_off= 0;
static int fast_tim0= -1; // First time mentioned in the sequence file (for -q and -S option)
static int fast_tim1= -1; // Last time mentioned in the sequence file (for -E option)
// output rate, with the multiplier indicated
static S64 byte_count= -1; // Number of bytes left to output, or -1 if unlimited
static int tty_erase; // Chars to erase from current line (for ESC[K emulation)
static int mix_flag= 0; // Has 'mix/*' been used in the sequence?
static int opt_c; // Number of -c option points provided (max 16)
static struct AmpAdj {
double freq, adj;
} ampadj[16]; // List of maximum 16 (freq,adj) pairs, freq-increasing order
//
// Time-keeping functions
//
#define H24 (86400000) // 24 hours
#define H12 (43200000) // 12 hours
inline int t_per24(int t0, int t1) { // Length of period starting at t0, ending at t1.
int td= t1 - t0; // NB for t0==t1 this gives 24 hours, *NOT 0*
return td > 0 ? td : td + H24;
}
inline int t_per0(int t0, int t1) { // Length of period starting at t0, ending at t1.
int td= t1 - t0; // NB for t0==t1 this gives 0 hours
return td >= 0 ? td : td + H24;
}
inline int t_mid(int t0, int t1) { // Midpoint of period from t0 to t1
return ((t1 < t0) ? (H24 + t0 + t1) / 2 : (t0 + t1) / 2) % H24;
}
//
// Handle an option string, disabled in the library
//
static int
handleOptions(char *str0) {
if(strcmp(str0, "-SE") == 0)
return 0;
error("Options not supported.\n");
return -1;
}
//
// Setup the ampadj[] array from the given -c spec-string
//
int
setupOptC(const char *spec) {
const char *p= spec, *q;
int a, b;
while (1) {
while (isspace(*p) || *p == ',') p++;
if (!*p) break;
if (opt_c >= sizeof(ampadj) / sizeof(ampadj[0])) {
error("Too many -c option frequencies; maxmimum is %d",
sizeof(ampadj) / sizeof(ampadj[0]));
return -1;
}
ampadj[opt_c].freq= strtod(p, (char **)&q);
if (p == q) goto bad;
if (*q++ != '=') goto bad;
ampadj[opt_c].adj= strtod(q, (char **)&p);
if (p == q) goto bad;
opt_c++;
}
// Sort the list
for (a= 0; a<opt_c; a++)
for (b= a+1; b<opt_c; b++)
if (ampadj[a].freq > ampadj[b].freq) {
double tmp;
tmp= ampadj[a].freq; ampadj[a].freq= ampadj[b].freq; ampadj[b].freq= tmp;
tmp= ampadj[a].adj; ampadj[a].adj= ampadj[b].adj; ampadj[b].adj= tmp;
}
return 0;
bad:
error("Bad -c option spec; expecting <freq>=<amp>[,<freq>=<amp>]...:\n %s", spec);
return -1;
}
static int
init_sin_table(void) {
int a;
int *arr= (int*)Alloc(ST_SIZ * sizeof(int));
if(arr == NULL)
return -1;
for (a= 0; a<ST_SIZ; a++)
arr[a]= (int)(ST_AMP * sin((a * 3.14159265358979323846 * 2) / ST_SIZ));
sin_table= arr;
return 0;
}
static void
error(char *fmt, ...) {
va_list ap; va_start(ap, fmt);
vsnprintf(error_message, sizeof(error_message), fmt, ap);
}
static void *
Alloc(size_t len) {
void *p= calloc(1, len);
if (!p) error("Out of memory");
return p;
}
static char *
StrDup(char *str) {
char *rv= strdup(str);
if (!rv) error("Out of memory");
return rv;
}
//
// Simple random number generator. Generates a repeating
// sequence of 65536 odd numbers in the range -65535->65535.
//
// Based on ZX Spectrum random number generator:
// seed= (seed+1) * 75 % 65537 - 1
//
#define RAND_MULT 75
static int seed= 2;
//inline int qrand() {
// return (seed= seed * 75 % 131074) - 65535;
//}
//
// Generate next sample for simulated pink noise, with same
// scaling as the sin_table[]. This version uses an inlined
// random number generator, and smooths the lower frequency bands
// as well.
//
#define NS_BANDS 9
typedef struct Noise Noise;
struct Noise {
int val; // Current output value
int inc; // Increment
};
static Noise ntbl[NS_BANDS];
static int nt_off;
static int noise_buf[256];
static uchar noise_off= 0;
static inline int
noise2() {
int tot;
int off= nt_off++;
int cnt= 1;
Noise *ns= ntbl;
Noise *ns1= ntbl + NS_BANDS;
tot= ((seed= seed * RAND_MULT % 131074) - 65535) * (NS_AMP / 65535 / (NS_BANDS + 1));
while ((cnt & off) && ns < ns1) {
int val= ((seed= seed * RAND_MULT % 131074) - 65535) * (NS_AMP / 65535 / (NS_BANDS + 1));
tot += ns->val += ns->inc= (val - ns->val) / (cnt += cnt);
ns++;
}
while (ns < ns1) {
tot += (ns->val += ns->inc);
ns++;
}
return noise_buf[noise_off++]= (tot >> NS_ADJ);
}
// //
// // Generate next sample for simulated pink noise, scaled the same
// // as the sin_table[]. This version uses a library random number
// // generator, and no smoothing.
// //
//
// inline double
// noise() {
// int tot= 0;
// int bit= ~0;
// int a;
// int off;
//
// ns_tbl[ns_off]= (rand() - (RAND_MAX / 2)) / (NS_BIT + 1);
// off= ns_off;
// for (a= 0; a<=NS_BIT; a++, bit <<= 1) {
// off &= bit;
// tot += ns_tbl[off];
// }
// ns_off= (ns_off + 1) & ((1<<NS_BIT) - 1);
//
// return tot * (ST_AMP / (RAND_MAX * 0.5));
// }
//
// Play loop
//
static int
loop() {
int c, cnt;
int now_lo= 0; // Low-order 16 bits of 'now' (fractional)
int err_lo= 0;
int ms_inc;
int r;
if(setup_device() < 0)
return -1;
spin_carr_max= 127.0 / 1E-6 / out_rate;
cnt= 1 + 1999 / out_buf_ms; // Update every 2 seconds or so
now= fast_tim0;
byte_count= out_bps * (S64)(t_per0(now, fast_tim1) * 0.001 * out_rate);
corrVal(0); // Get into correct period
while (1) {
for (c= 0; c < cnt; c++) {
corrVal(1);
r = outChunk();
if(r == 0)
goto break2; /* all done */
if(r < 0) {
free(tmp_buf);
free(out_buf);
return -1;
}
ms_inc= out_buf_ms;
now_lo += out_buf_lo + err_lo;
if (now_lo >= 0x10000) { ms_inc += now_lo >> 16; now_lo &= 0xFFFF; }
now += ms_inc;
if (now > H24) now -= H24;
}
}
break2:
free(tmp_buf);
free(out_buf);
return 0;
}
//
// Output a chunk of sound (a buffer-ful), then return
//
// Note: Optimised for 16-bit output. Eight-bit output is
// slower, but then it probably won't have to run at as high a
// sample rate.
//
static int rand0, rand1;
static int
outChunk() {
int off= 0;
while (off < out_blen) {
int ns= noise2(); // Use same pink noise source for everything
int tot1, tot2; // Left and right channels
int mix1, mix2; // Incoming mix signals
int val, a;
Channel *ch;
int *tab;
mix1= tmp_buf[off];
mix2= tmp_buf[off+1];
// Do default mixing at 100% if no mix/* stuff is present
if (!mix_flag) {
tot1= mix1 << 12;
tot2= mix2 << 12;
} else {
tot1= tot2= 0;
}
ch= &chan[0];
for (a= 0; a<N_CH; a++, ch++) switch (ch->typ) {
case 0:
break;
case 1: // Binaural tones
ch->off1 += ch->inc1;
ch->off1 &= (ST_SIZ << 16) - 1;
tot1 += ch->amp * sin_table[ch->off1 >> 16];
ch->off2 += ch->inc2;
ch->off2 &= (ST_SIZ << 16) - 1;
tot2 += ch->amp2 * sin_table[ch->off2 >> 16];
break;
case 2: // Pink noise
val= ns * ch->amp;
tot1 += val;
tot2 += val;
break;
case 3: // Bell
if (ch->off2) {
ch->off1 += ch->inc1;
ch->off1 &= (ST_SIZ << 16) - 1;
val= ch->off2 * sin_table[ch->off1 >> 16];
tot1 += val; tot2 += val;
if (--ch->inc2 < 0) {
ch->inc2= out_rate/20;
ch->off2 -= 1 + ch->off2 / 12; // Knock off 10% each 50 ms
}
}
break;
case 4: // Spinning pink noise
ch->off1 += ch->inc1;
ch->off1 &= (ST_SIZ << 16) - 1;
val= (ch->inc2 * sin_table[ch->off1 >> 16]) >> 24;
tot1 += ch->amp * noise_buf[(uchar)(noise_off+128+val)];
tot2 += ch->amp * noise_buf[(uchar)(noise_off+128-val)];
break;
case 5: // Mix level
tot1 += mix1 * ch->amp;
tot2 += mix2 * ch->amp;
break;
default: // Waveform-based binaural tones
tab= waves[-1 - ch->typ];
ch->off1 += ch->inc1;
ch->off1 &= (ST_SIZ << 16) - 1;
tot1 += ch->amp * tab[ch->off1 >> 16];
ch->off2 += ch->inc2;
ch->off2 &= (ST_SIZ << 16) - 1;
tot2 += ch->amp * tab[ch->off2 >> 16];
break;
}
// // Add pink noise as dithering
// tot1 += (ns >> NS_DITHER) + 0x8000;
// tot2 += (ns >> NS_DITHER) + 0x8000;
// // Add white noise as dithering
// tot1 += (seed >> 1) + 0x8000;
// tot2 += (seed >> 1) + 0x8000;
// White noise dither; you could also try (rand0-rand1) for a
// dither with more high frequencies
rand0= rand1;
rand1= (rand0 * 0x660D + 0xF35F) & 0xFFFF;
if (tot1 <= 0x7FFF0000) tot1 += rand0;
if (tot2 <= 0x7FFF0000) tot2 += rand0;
out_buf[off++]= tot1 >> 16;
out_buf[off++]= tot2 >> 16;
}
// Check and update the byte count if necessary
if (byte_count > 0) {
if (byte_count <= out_bsiz) {
if(writeOut((char*)out_buf, byte_count) < 0)
return -1;
return 0; // All done
}
else {
if(writeOut((char*)out_buf, out_bsiz) < 0)
return -1;
byte_count -= out_bsiz;
}
}
else {
if(writeOut((char*)out_buf, out_bsiz) < 0)
return -1;
}
return 1;
}
//
// Calculate amplitude adjustment factor for frequency 'freq'
//
static double
ampAdjust(double freq) {
int a;
struct AmpAdj *p0, *p1;
if (!opt_c) return 1.0;
if (freq <= ampadj[0].freq) return ampadj[0].adj;
if (freq >= ampadj[opt_c-1].freq) return ampadj[opt_c-1].adj;
for (a= 1; a<opt_c; a++)
if (freq < ampadj[a].freq)
break;
p0= &adj[a-1];
p1= &adj[a];
return p0->adj + (p1->adj - p0->adj) * (freq - p0->freq) / (p1->freq - p0->freq);
}
//
// Correct channel values and types according to current period,
// and current time
//
static void
corrVal(int running) {
int a;
int t0= per->tim;
int t1= per->nxt->tim;
Channel *ch;
Voice *v0, *v1, *vv;
double rat0, rat1;
int trigger= 0;
// Move to the correct period
while ((now >= t0) ^ (now >= t1) ^ (t1 > t0)) {
per= per->nxt;
t0= per->tim;
t1= per->nxt->tim;
if (running) {
if (tty_erase) {
fprintf(stderr, "%*s\r", tty_erase, "");
tty_erase= 0;
}
}
trigger= 1; // Trigger bells or whatever
}
// Run through to calculate voice settings for current time
rat1= t_per0(t0, now) / (double)t_per24(t0, t1);
rat0= 1 - rat1;
for (a= 0; a<N_CH; a++) {
ch= &chan[a];
v0= &per->v0[a];
v1= &per->v1[a];
vv= &ch->v;
if (vv->typ != v0->typ) {
switch (vv->typ= ch->typ= v0->typ) {
case 1:
ch->off1= ch->off2= 0; break;
case 2:
break;
case 3:
ch->off1= ch->off2= 0; break;
case 4:
ch->off1= ch->off2= 0; break;
case 5:
break;
default:
ch->off1= ch->off2= 0; break;
}
}
// Setup vv->*
switch (vv->typ) {
case 1:
vv->amp= rat0 * v0->amp + rat1 * v1->amp;
vv->carr= rat0 * v0->carr + rat1 * v1->carr;
vv->res= rat0 * v0->res + rat1 * v1->res;
break;
case 2:
vv->amp= rat0 * v0->amp + rat1 * v1->amp;
break;
case 3:
vv->amp= v0->amp; // No need to slide, as bell only rings briefly
vv->carr= v0->carr;
break;
case 4:
vv->amp= rat0 * v0->amp + rat1 * v1->amp;
vv->carr= rat0 * v0->carr + rat1 * v1->carr;
vv->res= rat0 * v0->res + rat1 * v1->res;
if (vv->carr > spin_carr_max) vv->carr= spin_carr_max; // Clipping sweep width
if (vv->carr < -spin_carr_max) vv->carr= -spin_carr_max;
break;
case 5:
vv->amp= rat0 * v0->amp + rat1 * v1->amp;
break;
default: // Waveform based binaural
vv->amp= rat0 * v0->amp + rat1 * v1->amp;
vv->carr= rat0 * v0->carr + rat1 * v1->carr;
vv->res= rat0 * v0->res + rat1 * v1->res;
break;
}
}
// Check and limit amplitudes if -c option in use
if (opt_c) {
double tot_beat= 0, tot_other= 0;
for (a= 0; a<N_CH; a++) {
vv= &chan[a].v;
if (vv->typ == 1) {
double adj1= ampAdjust(vv->carr + vv->res/2);
double adj2= ampAdjust(vv->carr - vv->res/2);
if (adj2 > adj1) adj1= adj2;
tot_beat += vv->amp * adj1;
} else if (vv->typ) {
tot_other += vv->amp;
}
}
if (tot_beat + tot_other > 4096) {
double adj_beat= (tot_beat > 4096) ? 4096 / tot_beat : 1.0;
double adj_other= (4096 - tot_beat * adj_beat) / tot_other;
for (a= 0; a<N_CH; a++) {
vv= &chan[a].v;
if (vv->typ == 1)
vv->amp *= adj_beat;
else if (vv->typ)
vv->amp *= adj_other;
}
}
}
// Setup Channel data from Voice data
for (a= 0; a<N_CH; a++) {
ch= &chan[a];
vv= &ch->v;
// Setup ch->* from vv->*
switch (vv->typ) {
double freq1, freq2;
case 1:
freq1= vv->carr + vv->res/2;
freq2= vv->carr - vv->res/2;
if (opt_c) {
ch->amp= vv->amp * ampAdjust(freq1);
ch->amp2= vv->amp * ampAdjust(freq2);
} else
ch->amp= ch->amp2= (int)vv->amp;
ch->inc1= (int)(freq1 / out_rate * ST_SIZ * 65536);
ch->inc2= (int)(freq2 / out_rate * ST_SIZ * 65536);
break;
case 2:
ch->amp= (int)vv->amp;
break;
case 3:
ch->amp= (int)vv->amp;
ch->inc1= (int)(vv->carr / out_rate * ST_SIZ * 65536);
if (trigger) { // Trigger the bell only on entering the period
ch->off2= ch->amp;
ch->inc2= out_rate/20;
}
break;
case 4:
ch->amp= (int)vv->amp;
ch->inc1= (int)(vv->res / out_rate * ST_SIZ * 65536);
ch->inc2= (int)(vv->carr * 1E-6 * out_rate * (1<<24) / ST_AMP);
break;
case 5:
ch->amp= (int)vv->amp;
break;
default: // Waveform based binaural
ch->amp= (int)vv->amp;
ch->inc1= (int)((vv->carr + vv->res/2) / out_rate * ST_SIZ * 65536);
ch->inc2= (int)((vv->carr - vv->res/2) / out_rate * ST_SIZ * 65536);
if (ch->inc1 > ch->inc2)
ch->inc2= -ch->inc2;
else
ch->inc1= -ch->inc1;
break;
}
}
}
//
// Setup audio device
//
static int
setup_device(void) {
// Handle output to files and pipes
out_fd= 1; // stdout
out_blen= out_rate * 2 / out_prate; // 10 fragments a second by default
while (out_blen & (out_blen-1)) out_blen &= out_blen-1; // Make power of two
out_bsiz= out_blen * 2;
out_bps= 4;
out_buf= (short*)Alloc(out_blen * sizeof(short));
if(out_buf == NULL)
return -1;
out_buf_lo= (int)(0x10000 * 1000.0 * 0.5 * out_blen / out_rate);
out_buf_ms= out_buf_lo >> 16;
out_buf_lo &= 0xFFFF;
tmp_buf= (int*)Alloc(out_blen * sizeof(int));
if(tmp_buf == NULL)
return -1;
return 0;
}
//
// Read a line, discarding blank lines and comments. Rets:
// Another line? Comments starting with '##' are displayed on
// stderr.
//
static int
readLine() {
char *p;
char *endl;
size_t llin;
while (1) {
lin= buf;
endl = strchr(in_text, '\n');
llin = endl == NULL ? strlen(in_text) : endl + 1 - in_text;
if(llin == 0)
return 0; /* EOF */
if(llin > sizeof(buf) - 1)
llin = sizeof(buf) - 1;
memcpy(buf, in_text, llin);
buf[llin] = 0;
in_text += llin;
in_lin++;
while (isspace(*lin)) lin++;
p= strchr(lin, '#');
p= p ? p : strchr(lin, 0);
while (p > lin && isspace(p[-1])) p--;
if (p != lin) break;
}
*p= 0;
lin_copy= buf_copy;
strcpy(lin_copy, lin);
return 1;
}
//
// Get next word at '*lin', moving lin onwards, or return 0
//
static char *
getWord() {
char *rv, *end;
while (isspace(*lin)) lin++;
if (!*lin) return 0;
rv= lin;
while (*lin && !isspace(*lin)) lin++;
end= lin;
if (*lin) lin++;
*end= 0;
return rv;
}
//
// Bad sequence file
//
static void
badSeq(void) {
error("Bad sequence file content at line: %d\n %s", in_lin, lin_copy);
}
//
// Read a list of sequence files, and generate a list of Period
// structures
//
static int
readSeq(const char *text) {
// Setup a 'now' value to use for NOW in the sequence file
int start= 1;
now= 0;
in_text = text;
in_lin= 0;
while (readLine()) {
char *p= lin;
// Blank lines
if (!*p) continue;
// Look for options
if (*p == '-') {
if (!start) {
error("Options are only permitted at start of sequence file:\n %s", p);
return -1;
}
if(handleOptions(p) < 0)
return -1;
continue;
}
// Check to see if it fits the form of <name>:<white-space>
start= 0;
if (!isalpha(*p))
p= 0;
else {
while (isalnum(*p) || *p == '_' || *p == '-') p++;
if (*p++ != ':' || !isspace(*p))
p= 0;
}
if (p) {
if(readNameDef() < 0)
return -1;
} else {
if(readTimeLine() < 0)
return -1;
}
}
return 0;
}
//
// Fill in all the correct information for the Periods, assuming
// they have just been loaded using readTimeLine()
//
static int
correctPeriods() {
// Get times all correct
{
Period *pp= per;
do {
if (pp->fi == -2) {
pp->tim= pp->nxt->tim;
pp->fi= -1;
}
pp= pp->nxt;
} while (pp != per);
}
// Make sure that the transitional periods each have enough time
{
Period *pp= per;
do {
if (pp->fi == -1) {
int per= t_per0(pp->tim, pp->nxt->tim);
if (per < fade_int) {
int adj= (fade_int - per) / 2, adj0, adj1;
adj0= t_per0(pp->prv->tim, pp->tim);
adj0= (adj < adj0) ? adj : adj0;
adj1= t_per0(pp->nxt->tim, pp->nxt->nxt->tim);
adj1= (adj < adj1) ? adj : adj1;
pp->tim= (pp->tim - adj0 + H24) % H24;
pp->nxt->tim= (pp->nxt->tim + adj1) % H24;
}
}
pp= pp->nxt;
} while (pp != per);
}
// Fill in all the voice arrays, and sort out details of
// transitional periods
{
Period *pp= per;
do {
if (pp->fi < 0) {
int fo, fi;
int a;
int midpt= 0;
Period *qq= (Period*)Alloc(sizeof(*qq));
if(qq == NULL)
return -1;
qq->prv= pp; qq->nxt= pp->nxt;
qq->prv->nxt= qq->nxt->prv= qq;
qq->tim= t_mid(pp->tim, qq->nxt->tim);
memcpy(pp->v0, pp->prv->v1, sizeof(pp->v0));
memcpy(qq->v1, qq->nxt->v0, sizeof(qq->v1));
// Special handling for bells
for (a= 0; a<N_CH; a++) {
if (pp->v0[a].typ == 3 && pp->fi != -3)
pp->v0[a].typ= 0;
if (qq->v1[a].typ == 3 && pp->fi == -3)
qq->v1[a].typ= 0;