-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaymidi.cpp
More file actions
3173 lines (2830 loc) · 80.5 KB
/
playmidi.cpp
File metadata and controls
3173 lines (2830 loc) · 80.5 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
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
/*
$Id$
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
playmidi.c -- random stuff in need of rearrangement
*/
#include <stdio.h>
#ifndef __WIN32__
#include <unistd.h>
#endif
#include <stdlib.h>
#ifndef NO_STRING_H
# include <string.h>
#else
#include <strings.h>
#endif
#include "config.h"
#include "common.h"
#include "instrum.h"
#include "playmidi.h"
#ifndef ADAGIO
#include "readmidi.h"
#include <sys/time.h>
#else
#include <memory.h>
#endif /* ADAGIO */
#include "output.h"
#include "mix.h"
#include "controls.h"
#include "resample.h"
#include "tables.h"
/*<95GUI_MODIF_BEGIN>*/
#ifdef CHANNEL_EFFECT
extern void effect_ctrl_change( MidiEvent* pCurrentEvent );
extern void effect_ctrl_reset( int idChannel ) ;
int opt_effect = 0;
#endif /* CHANNEL_EFFECT */
/*<95GUI_MODIF_END>*/
#ifdef tplus
#define PORTAMENTO_TIME_TUNING (1.0 / 5000.0)
#define PORTAMENTO_CONTROL_RATIO 256 /* controls per sec */
#endif
#ifdef __WIN32__
extern int intr;
#endif
#ifdef ADAGIO
/* Minimal control mode */
extern ControlMode dumb_control_mode;
#define DEFAULT_CONTROL_MODE &dumb_control_mode
ControlMode *ctl_list[]={
&dumb_control_mode,
0
};
ControlMode *ctl=DEFAULT_CONTROL_MODE;
int free_instruments_afterwards=0;
/**static char def_instr_name[256]="";**/
static unsigned max_polyphony = 0;
#endif
#ifdef tplus
int dont_cspline=0;
#endif
int opt_dry = 1;
int opt_expression_curve = 1;
int opt_volume_curve = 1;
int opt_stereo_surround = 1;
int dont_filter_melodic=1;
int dont_filter_drums=1;
int dont_chorus=0;
int dont_reverb=0;
int current_interpolation=1;
int dont_keep_looping=0;
static int voice_reserve=0;
#ifndef ADAGIO
Channel channel[MAXCHAN];
signed char drumvolume[MAXCHAN][MAXNOTE];
signed char drumpanpot[MAXCHAN][MAXNOTE];
signed char drumreverberation[MAXCHAN][MAXNOTE];
signed char drumchorusdepth[MAXCHAN][MAXNOTE];
#else /* ADAGIO */
static void read_seq(unsigned char *from, unsigned char *to);
Channel channel[MAX_TONE_VOICES];
#endif /* ADAGIO */
Voice voice[MAX_VOICES];
int
voices=DEFAULT_VOICES;
int32
control_ratio=0,
amplification=DEFAULT_AMPLIFICATION;
FLOAT_T
master_volume;
int32 drumchannels=DEFAULT_DRUMCHANNELS;
int adjust_panning_immediately=1;
int GM_System_On=0;
int XG_System_On=0;
int GS_System_On=0;
int XG_System_reverb_type;
int XG_System_chorus_type;
int XG_System_variation_type;
static int32 lost_notes, cut_notes, played_notes;
#ifdef CHANNEL_EFFECT
int32 common_buffer[AUDIO_BUFFER_SIZE*2], /* stereo samples */
*buffer_pointer;
static uint32 buffered_count;
MidiEvent *event_list, *current_event;
uint32 sample_count;
uint32 current_sample;
#else
static int32 common_buffer[AUDIO_BUFFER_SIZE*2], /* stereo samples */
*buffer_pointer;
static uint32 buffered_count;
static MidiEvent *event_list, *current_event;
static uint32 sample_count, current_sample;
#endif
#ifdef tplus
static void update_portamento_controls(int ch);
#endif
static void kill_note(int i);
static void adjust_amplification(void)
{
master_volume = (double)(amplification) / 100.0L;
master_volume /= 4;
}
static void adjust_master_volume(int32 vol)
{
master_volume = (double)(vol*amplification) / 1638400.0L;
master_volume /= 4;
}
static void reset_voices(void)
{
int i;
for (i=0; i<MAX_VOICES; i++)
voice[i].status=VOICE_FREE;
}
/* Process the Reset All Controllers event */
static void reset_controllers(int c)
{
channel[c].volume=90; /* Some standard says, although the SCC docs say 0. */
channel[c].expression=127; /* SCC-1 does this. */
channel[c].sustain=0;
channel[c].pitchbend=0x2000;
channel[c].pitchfactor=0; /* to be computed */
channel[c].reverberation = global_reverb;
channel[c].chorusdepth = global_chorus;
#ifdef tplus
channel[c].modulation_wheel = 0;
channel[c].portamento_time_lsb = 0;
channel[c].portamento_time_msb = 0;
channel[c].tuning_lsb = 64;
channel[c].tuning_msb = 64;
channel[c].porta_control_ratio = 0;
channel[c].portamento = 0;
channel[c].last_note_fine = -1;
channel[c].vibrato_ratio = 0;
channel[c].vibrato_depth = 0;
channel[c].vibrato_delay = 0;
/*
memset(channel[c].envelope_rate, 0, sizeof(channel[c].envelope_rate));
*/
update_portamento_controls(c);
#endif
#ifdef CHANNEL_EFFECT
if (opt_effect) effect_ctrl_reset( c ) ;
#endif /* CHANNEL_EFFECT */
}
#ifndef ADAGIO
static void redraw_controllers(int c)
{
ctl->volume(c, channel[c].volume);
ctl->expression(c, channel[c].expression);
ctl->sustain(c, channel[c].sustain);
ctl->pitch_bend(c, channel[c].pitchbend);
}
#endif
static void reset_midi(void)
{
int i;
#ifndef ADAGIO
for (i=0; i<MAXCHAN; i++)
#else /* ADAGIO */
for (i=0; i<MAX_VOICES; i++)
#endif /* ADAGIO */
{
reset_controllers(i);
/* The rest of these are unaffected by the Reset All Controllers event */
channel[i].program=default_program;
channel[i].panning=NO_PANNING;
channel[i].pitchsens=2;
channel[i].bank=0; /* tone bank or drum set */
channel[i].harmoniccontent=64,
channel[i].releasetime=64,
channel[i].attacktime=64,
channel[i].brightness=64,
/*channel[i].kit=0;*/
channel[i].sfx=0;
/* channel[i].transpose and .kit are initialized in readmidi.c */
}
reset_voices();
}
static void select_sample(int v, Instrument *ip)
{
int32 f, cdiff, diff, midfreq;
int s,i;
Sample *sp, *closest;
s=ip->samples;
sp=ip->sample;
if (s==1)
{
voice[v].sample=sp;
return;
}
f=voice[v].orig_frequency;
#if 0
/*
Even if in range, a later patch may be closer. */
for (i=0; i<s; i++)
{
if (sp->low_freq <= f && sp->high_freq >= f)
{
voice[v].sample=sp;
return;
}
sp++;
}
#endif
/*
No suitable sample found! We'll select the sample whose root
frequency is closest to the one we want. (Actually we should
probably convert the low, high, and root frequencies to MIDI note
values and compare those.) */
cdiff=0x7FFFFFFF;
closest=sp=ip->sample;
midfreq = (sp->low_freq + sp->high_freq) / 2;
for(i=0; i<s; i++)
{
diff=sp->root_freq - f;
/* But the root freq. can perfectly well lie outside the keyrange
* frequencies, so let's try:
*/
/* diff=midfreq - f; */
if (diff<0) diff=-diff;
if (diff<cdiff)
{
cdiff=diff;
closest=sp;
}
sp++;
}
voice[v].sample=closest;
return;
}
static void select_stereo_samples(int v, InstrumentLayer *lp)
{
Instrument *ip;
InstrumentLayer *nlp, *bestvel;
int diffvel, midvel, mindiff;
/* select closest velocity */
bestvel = lp;
mindiff = 500;
for (nlp = lp; nlp; nlp = nlp->next) {
midvel = (nlp->hi + nlp->lo)/2;
if (!midvel) diffvel = 127;
else if (voice[v].velocity < nlp->lo || voice[v].velocity > nlp->hi)
diffvel = 200;
else diffvel = voice[v].velocity - midvel;
if (diffvel < 0) diffvel = -diffvel;
if (diffvel < mindiff) {
mindiff = diffvel;
bestvel = nlp;
}
}
ip = bestvel->instrument;
if (ip->right_sample) {
ip->sample = ip->right_sample;
ip->samples = ip->right_samples;
select_sample(v, ip);
voice[v].right_sample = voice[v].sample;
}
else voice[v].right_sample = 0;
ip->sample = ip->left_sample;
ip->samples = ip->left_samples;
select_sample(v, ip);
}
#ifndef tplus
static
#endif
void recompute_freq(int v)
{
int
sign=(voice[v].sample_increment < 0), /* for bidirectional loops */
pb=channel[voice[v].channel].pitchbend;
double a;
int32 tuning = 0;
if (!voice[v].sample->sample_rate)
return;
#ifdef tplus
#if 0
if(!opt_modulation_wheel)
voice[v].modulation_wheel = 0;
if(!opt_portamento)
voice[v].porta_control_ratio = 0;
#endif
voice[v].vibrato_control_ratio = voice[v].orig_vibrato_control_ratio;
if(voice[v].modulation_wheel > 0)
{
voice[v].vibrato_control_ratio =
(int32)(play_mode->rate * MODULATION_WHEEL_RATE
/ (2.0 * VIBRATO_SAMPLE_INCREMENTS));
voice[v].vibrato_delay = 0;
}
#endif
#ifdef tplus
if(voice[v].vibrato_control_ratio || voice[v].modulation_wheel > 0)
#else
if (voice[v].vibrato_control_ratio)
#endif
{
/* This instrument has vibrato. Invalidate any precomputed
sample_increments. */
int i=VIBRATO_SAMPLE_INCREMENTS;
while (i--)
voice[v].vibrato_sample_increment[i]=0;
#ifdef tplus29
if(voice[v].modulation_wheel > 0)
{
voice[v].vibrato_control_ratio =
(int32)(play_mode->rate * MODULATION_WHEEL_RATE
/ (2.0 * VIBRATO_SAMPLE_INCREMENTS));
voice[v].vibrato_delay = 0;
}
#endif
}
#ifdef tplus
/* fine: [0..128] => [-256..256]
* 1 coarse = 256 fine (= 1 note)
* 1 fine = 2^5 tuning
*/
tuning = (((int32)channel[voice[v].channel].tuning_lsb - 0x40)
+ 64 * ((int32)channel[voice[v].channel].tuning_msb - 0x40)) << 7;
#endif
#ifdef tplus
if(!voice[v].porta_control_ratio)
{
#endif
#ifdef tplus29
if(tuning == 0 && pb == 0x2000)
#else
if (pb==0x2000 || pb<0 || pb>0x3FFF)
#endif
voice[v].frequency=voice[v].orig_frequency;
else
{
pb-=0x2000;
if (!(channel[voice[v].channel].pitchfactor))
{
/* Damn. Somebody bent the pitch. */
int32 i=pb*channel[voice[v].channel].pitchsens + tuning;
#ifdef tplus29
tuning;
if(i >= 0)
channel[ch].pitchfactor =
bend_fine[(i>>5) & 0xFF] * bend_coarse[(i>>13) & 0x7F];
else
{
i = -i;
channel[ch].pitchfactor = 1.0 /
(bend_fine[(i>>5) & 0xFF] * bend_coarse[(i>>13) & 0x7F]);
}
#else
if (pb<0)
i=-i;
channel[voice[v].channel].pitchfactor=
bend_fine[(i>>5) & 0xFF] * bend_coarse[i>>13];
#endif
}
#ifndef tplus29
if (pb>0)
#endif
voice[v].frequency=
(int32)(channel[voice[v].channel].pitchfactor *
(double)(voice[v].orig_frequency));
#ifndef tplus29
else
voice[v].frequency=
(int32)((double)(voice[v].orig_frequency) /
channel[voice[v].channel].pitchfactor);
#endif
}
#ifdef tplus
}
else /* Portament */
{
int32 i;
FLOAT_T pf;
pb -= 0x2000;
i = pb * channel[voice[v].channel].pitchsens + (voice[v].porta_pb << 5) + tuning;
if(i >= 0)
pf = bend_fine[(i>>5) & 0xFF] * bend_coarse[(i>>13) & 0x7F];
else
{
i = -i;
pf = 1.0 / (bend_fine[(i>>5) & 0xFF] * bend_coarse[(i>>13) & 0x7F]);
}
voice[v].frequency = (int32)((double)(voice[v].orig_frequency) * pf);
/*voice[v].cache = NULL;*/
}
#endif
a = FRSCALE(((double)(voice[v].sample->sample_rate) *
(double)(voice[v].frequency)) /
((double)(voice[v].sample->root_freq) *
(double)(play_mode->rate)),
FRACTION_BITS);
/* what to do if incr is 0? --gl */
/* if (!a) a++; */
a += 0.5;
if ((int32)a < 1) a = 1;
if (sign)
a = -a; /* need to preserve the loop direction */
voice[v].sample_increment = (int32)(a);
}
static int vcurve[128] = {
0,0,18,29,36,42,47,51,55,58,
60,63,65,67,69,71,73,74,76,77,
79,80,81,82,83,84,85,86,87,88,
89,90,91,92,92,93,94,95,95,96,
97,97,98,99,99,100,100,101,101,102,
103,103,104,104,105,105,106,106,106,107,
107,108,108,109,109,109,110,110,111,111,
111,112,112,112,113,113,114,114,114,115,
115,115,116,116,116,116,117,117,117,118,
118,118,119,119,119,119,120,120,120,120,
121,121,121,122,122,122,122,123,123,123,
123,123,124,124,124,124,125,125,125,125,
126,126,126,126,126,127,127,127
};
static void recompute_amp(int v)
{
int32 tempamp;
int chan = voice[v].channel;
int vol = channel[chan].volume;
int expr = channel[chan].expression;
int vel = vcurve[voice[v].velocity];
FLOAT_T curved_expression, curved_volume;
/* TODO: use fscale */
if (channel[chan].kit)
{
int note = voice[v].sample->note_to_use;
if (note>0 && drumvolume[chan][note]>=0) vol = drumvolume[chan][note];
}
if (opt_expression_curve == 2) curved_expression = 127.0 * def_vol_table[expr];
else if (opt_expression_curve == 1) curved_expression = 127.0 * expr_table[expr];
else curved_expression = (FLOAT_T)expr;
if (opt_volume_curve == 2) curved_volume = 127.0 * def_vol_table[vol];
else if (opt_volume_curve == 1) curved_volume = 127.0 * expr_table[vol];
else curved_volume = (FLOAT_T)vol;
tempamp= (int32)((FLOAT_T)vel * curved_volume * curved_expression); /* 21 bits */
if (!(play_mode->encoding & PE_MONO))
{
/*if (voice[v].panning > 60 && voice[v].panning < 68)*/
if (voice[v].panning > 62 && voice[v].panning < 66)
{
voice[v].panned=PANNED_CENTER;
voice[v].left_amp=
FRSCALENEG((double)(tempamp) * voice[v].volume * master_volume,
21);
}
else if (voice[v].panning<5)
{
voice[v].panned = PANNED_LEFT;
voice[v].left_amp=
FRSCALENEG((double)(tempamp) * voice[v].volume * master_volume,
20);
}
else if (voice[v].panning>123)
{
voice[v].panned = PANNED_RIGHT;
voice[v].left_amp= /* left_amp will be used */
FRSCALENEG((double)(tempamp) * voice[v].volume * master_volume,
20);
}
else
{
voice[v].panned = PANNED_MYSTERY;
voice[v].left_amp=
FRSCALENEG((double)(tempamp) * voice[v].volume * master_volume,
27);
voice[v].right_amp=voice[v].left_amp * (voice[v].panning);
voice[v].left_amp *= (double)(127-voice[v].panning);
}
}
else
{
voice[v].panned=PANNED_CENTER;
voice[v].left_amp=
FRSCALENEG((double)(tempamp) * voice[v].volume * master_volume,
21);
}
}
static int current_polyphony = 0;
#ifdef POLYPHONY_COUNT
static int future_polyphony = 0;
#endif
#define NOT_CLONE 0
#define STEREO_CLONE 1
#define REVERB_CLONE 2
#define CHORUS_CLONE 3
/* just a variant of note_on() */
static int vc_alloc(int j, int clone_type)
{
int i=voices;
int vc_count = 0, vc_ret = -1;
while (i--)
{
if (i == j) continue;
if (voice[i].status == VOICE_FREE) {
vc_ret = i;
vc_count++;
}
}
if (vc_count > voice_reserve || clone_type == STEREO_CLONE) return vc_ret;
return -1;
}
static void kill_others(MidiEvent *e, int i)
{
int j=voices;
/*if (!voice[i].sample->exclusiveClass) return; */
while (j--)
{
if (voice[j].status & (VOICE_FREE|VOICE_OFF|VOICE_DIE)) continue;
if (i == j) continue;
if (voice[i].channel != voice[j].channel) continue;
if (voice[j].sample->note_to_use)
{
/* if (voice[j].sample->note_to_use != voice[i].sample->note_to_use) continue; */
if (!voice[i].sample->exclusiveClass) continue;
if (voice[j].sample->exclusiveClass != voice[i].sample->exclusiveClass) continue;
kill_note(j);
}
/*
else if (voice[j].note != (e->a &0x07f)) continue;
kill_note(j);
*/
}
}
static void clone_voice(Instrument *ip, int v, MidiEvent *e, uint8 clone_type, int variationbank)
{
int w, k, played_note, chorus, reverb, milli;
int chan = voice[v].channel;
if (clone_type == STEREO_CLONE) {
if (!voice[v].right_sample && variationbank != 3) return;
if (variationbank == 6) return;
}
chorus = ip->sample->chorusdepth;
reverb = ip->sample->reverberation;
if (channel[chan].kit) {
if ((k=drumreverberation[chan][voice[v].note]) >= 0) reverb = (reverb>k)? reverb : k;
if ((k=drumchorusdepth[chan][voice[v].note]) >= 0) chorus = (chorus>k)? chorus : k;
}
else {
if ((k=channel[chan].chorusdepth) >= 0) chorus = (chorus>k)? chorus : k;
if ((k=channel[chan].reverberation) >= 0) reverb = (reverb>k)? reverb : k;
}
if (clone_type == REVERB_CLONE) chorus = 0;
else if (clone_type == CHORUS_CLONE) reverb = 0;
else if (clone_type == STEREO_CLONE) reverb = chorus = 0;
if (reverb > 127) reverb = 127;
if (chorus > 127) chorus = 127;
if (clone_type == REVERB_CLONE) {
if ( (reverb_options & OPT_REVERB_EXTRA) && reverb < global_echo)
reverb = global_echo;
if (/*reverb < 8 ||*/ dont_reverb) return;
}
if (clone_type == CHORUS_CLONE) {
if (variationbank == 32) chorus = 30;
else if (variationbank == 33) chorus = 60;
else if (variationbank == 34) chorus = 90;
if ( (reverb_options & OPT_CHORUS_EXTRA) && chorus < global_detune)
chorus = global_detune;
if (/*chorus < 4 ||*/ dont_chorus) return;
}
if (!voice[v].right_sample) {
voice[v].right_sample = voice[v].sample;
}
else if (clone_type == STEREO_CLONE) variationbank = 0;
/* don't try to fake a second patch if we have a real one */
if ( (w = vc_alloc(v, clone_type)) < 0 ) return;
if (clone_type==STEREO_CLONE) voice[v].clone_voice = w;
voice[w].clone_voice = v;
voice[w].clone_type = clone_type;
voice[w].status = voice[v].status;
voice[w].channel = voice[v].channel;
voice[w].note = voice[v].note;
voice[w].sample = voice[v].right_sample;
voice[w].velocity= (e->b * (127 - voice[w].sample->attenuation)) / 127;
voice[w].left_sample = voice[v].left_sample;
voice[w].right_sample = voice[v].right_sample;
voice[w].orig_frequency = voice[v].orig_frequency;
voice[w].frequency = voice[v].frequency;
voice[w].sample_offset = voice[v].sample_offset;
voice[w].sample_increment = voice[v].sample_increment;
voice[w].current_x0 =
voice[w].current_x1 =
voice[w].current_y0 =
voice[w].current_y1 = 0;
voice[w].echo_delay = voice[v].echo_delay;
voice[w].starttime = voice[v].starttime;
voice[w].envelope_volume = voice[v].envelope_volume;
voice[w].envelope_target = voice[v].envelope_target;
voice[w].envelope_increment = voice[v].envelope_increment;
voice[w].modulation_volume = voice[v].modulation_volume;
voice[w].modulation_target = voice[v].modulation_target;
voice[w].modulation_increment = voice[v].modulation_increment;
voice[w].tremolo_sweep = voice[w].sample->tremolo_sweep_increment;
voice[w].tremolo_sweep_position = voice[v].tremolo_sweep_position;
voice[w].tremolo_phase = voice[v].tremolo_phase;
voice[w].tremolo_phase_increment = voice[w].sample->tremolo_phase_increment;
voice[w].lfo_sweep = voice[w].sample->lfo_sweep_increment;
voice[w].lfo_sweep_position = voice[v].lfo_sweep_position;
voice[w].lfo_phase = voice[v].lfo_phase;
voice[w].lfo_phase_increment = voice[w].sample->lfo_phase_increment;
voice[w].modLfoToFilterFc=voice[w].sample->modLfoToFilterFc;
voice[w].modEnvToFilterFc=voice[w].sample->modEnvToFilterFc;
voice[w].vibrato_sweep = voice[w].sample->vibrato_sweep_increment;
voice[w].vibrato_sweep_position = voice[v].vibrato_sweep_position;
voice[w].left_mix = voice[v].left_mix;
voice[w].right_mix = voice[v].right_mix;
voice[w].left_amp = voice[v].left_amp;
voice[w].right_amp = voice[v].right_amp;
voice[w].tremolo_volume = voice[v].tremolo_volume;
voice[w].lfo_volume = voice[v].lfo_volume;
for (k = 0; k < VIBRATO_SAMPLE_INCREMENTS; k++)
voice[w].vibrato_sample_increment[k] =
voice[v].vibrato_sample_increment[k];
for (k=ATTACK; k<MAXPOINT; k++)
{
voice[w].envelope_rate[k]=voice[v].envelope_rate[k];
voice[w].envelope_offset[k]=voice[v].envelope_offset[k];
}
voice[w].vibrato_phase = voice[v].vibrato_phase;
voice[w].vibrato_depth = voice[w].sample->vibrato_depth;
voice[w].vibrato_control_ratio = voice[w].sample->vibrato_control_ratio;
voice[w].vibrato_control_counter = voice[v].vibrato_control_counter;
voice[w].envelope_stage = voice[v].envelope_stage;
voice[w].modulation_stage = voice[v].modulation_stage;
voice[w].control_counter = voice[v].control_counter;
voice[w].panning = voice[v].panning;
if (voice[w].right_sample)
voice[w].panning = voice[w].right_sample->panning;
else voice[w].panning = 127;
if (clone_type == STEREO_CLONE) {
int left, right;
int panrequest = voice[v].panning;
if (variationbank == 3) {
voice[v].panning = 0;
voice[w].panning = 127;
}
else {
if (voice[v].sample->panning > voice[w].sample->panning) {
left = w;
right = v;
}
else {
left = v;
right = w;
}
if (panrequest < 64) {
if (opt_stereo_surround) {
voice[left].panning = 0;
voice[right].panning = panrequest;
}
else {
if (voice[left].sample->panning < panrequest)
voice[left].panning = voice[left].sample->panning;
else voice[left].panning = panrequest;
voice[right].panning = (voice[right].sample->panning + panrequest + 32) / 2;
}
}
else {
if (opt_stereo_surround) {
voice[left].panning = panrequest;
voice[right].panning = 127;
}
else {
if (voice[right].sample->panning > panrequest)
voice[right].panning = voice[right].sample->panning;
else voice[right].panning = panrequest;
voice[left].panning = (voice[left].sample->panning + panrequest - 32) / 2;
}
}
}
#ifdef DEBUG_CLONE_NOTES
fprintf(stderr,"STEREO_CLONE v%d vol%f pan%d\n", w, voice[w].volume, voice[w].panning);
#endif
}
#ifdef tplus
voice[w].porta_control_ratio = voice[v].porta_control_ratio;
voice[w].porta_dpb = voice[v].porta_dpb;
voice[w].porta_pb = voice[v].porta_pb;
/* ?? voice[w].vibrato_delay = 0; */
voice[w].vibrato_delay = voice[w].sample->vibrato_delay;
voice[w].modulation_delay = voice[w].sample->modulation_rate[DELAY];
#endif
voice[w].volume = voice[w].sample->volume;
milli = play_mode->rate/1000;
if (reverb) {
#if 0
if (voice[w].panning < 64) voice[w].panning = 127;
else voice[w].panning = 0;
#endif
if (opt_stereo_surround) {
if (voice[w].panning > 64) voice[w].panning = 127;
else voice[w].panning = 0;
}
else {
if (voice[v].panning < 64) voice[w].panning = 64 + reverb/2;
else voice[w].panning = 64 - reverb/2;
}
#ifdef DEBUG_REVERBERATION
printf("r=%d vol %f", reverb, voice[w].volume);
#endif
/* try 98->99 for melodic instruments ? (bit much for percussion) */
voice[w].volume *= vol_table[(127-reverb)/8 + 98];
#ifdef DEBUG_REVERBERATION
printf(" -> vol %f", voice[w].volume);
printf(" delay %d", voice[w].echo_delay);
#endif
/*voice[w].echo_delay += (reverb>>1) * milli;*/
voice[w].echo_delay += reverb * milli;
#ifdef DEBUG_REVERBERATION
printf(" -> delay %d\n", voice[w].echo_delay);
#endif
voice[w].envelope_rate[DECAY] *= 2;
voice[w].envelope_rate[RELEASE] /= 2;
if (XG_System_reverb_type >= 0) {
int subtype = XG_System_reverb_type & 0x07;
int rtype = XG_System_reverb_type >>3;
switch (rtype) {
case 0: /* no effect */
break;
case 1: /* hall */
if (subtype) voice[w].echo_delay += 100 * milli;
break;
case 2: /* room */
voice[w].echo_delay /= 2;
break;
case 3: /* stage */
voice[w].velocity = voice[v].velocity;
break;
case 4: /* plate */
voice[w].panning = voice[v].panning;
break;
case 16: /* white room */
voice[w].echo_delay = 0;
break;
case 17: /* tunnel */
voice[w].echo_delay *= 2;
voice[w].velocity /= 2;
break;
case 18: /* canyon */
voice[w].echo_delay *= 2;
break;
case 19: /* basement */
voice[w].velocity /= 2;
break;
default: break;
}
}
#ifdef DEBUG_CLONE_NOTES
fprintf(stderr,"REVERB_CLONE v%d vol=%f pan=%d reverb=%d delay=%dms\n", w, voice[w].volume, voice[w].panning, reverb,
voice[w].echo_delay / milli);
#endif
}
played_note = voice[w].sample->note_to_use;
if (!played_note) {
played_note = e->a & 0x7f;
if (variationbank == 35) played_note += 12;
else if (variationbank == 36) played_note -= 12;
else if (variationbank == 37) played_note += 7;
else if (variationbank == 36) played_note -= 7;
}
#ifdef ADAGIO
if (voice[w].sample->freq_scale == 1024)
played_note = played_note + voice[w].sample->freq_center - 60;
else if (voice[w].sample->freq_scale)
#endif
played_note = ( (played_note - voice[w].sample->freq_center) * voice[w].sample->freq_scale ) / 1024 +
voice[w].sample->freq_center;
voice[w].note = played_note;
voice[w].orig_frequency = freq_table[played_note];
if (chorus) {
/*voice[w].orig_frequency += (voice[w].orig_frequency/128) * chorus;*/
/*fprintf(stderr, "voice %d v sweep from %ld (cr %d, depth %d)", w, voice[w].vibrato_sweep,
voice[w].vibrato_control_ratio, voice[w].vibrato_depth);*/
if (opt_stereo_surround) {
if (voice[v].panning < 64) voice[w].panning = voice[v].panning + 32;
else voice[w].panning = voice[v].panning - 32;
}
if (!voice[w].vibrato_control_ratio) {
voice[w].vibrato_control_ratio = 100;
voice[w].vibrato_depth = 6;
voice[w].vibrato_sweep = 74;
}
/*voice[w].velocity = (voice[w].velocity * chorus) / 128;*/
#if 0
voice[w].velocity = (voice[w].velocity * chorus) / (128+96);
voice[v].velocity = voice[w].velocity;
voice[w].volume = (voice[w].volume * chorus) / (128+96);
voice[v].volume = voice[w].volume;
#endif
/* voice[w].volume *= 0.9; */
voice[w].volume *= 0.40;
voice[v].volume = voice[w].volume;
recompute_amp(v);
apply_envelope_to_amp(v);
voice[w].vibrato_sweep = chorus/2;
voice[w].vibrato_depth /= 2;
if (!voice[w].vibrato_depth) voice[w].vibrato_depth = 2;
voice[w].vibrato_control_ratio /= 2;
/*voice[w].vibrato_control_ratio += chorus;*/
/*fprintf(stderr, " to %ld (cr %d, depth %d).\n", voice[w].vibrato_sweep,
voice[w].vibrato_control_ratio, voice[w].vibrato_depth);
voice[w].vibrato_phase = 20;*/
voice[w].echo_delay += 30 * milli;
if (XG_System_chorus_type >= 0) {
int subtype = XG_System_chorus_type & 0x07;
int chtype = 0x0f & (XG_System_chorus_type >> 3);
switch (chtype) {
case 0: /* no effect */
break;
case 1: /* chorus */
chorus /= 3;
if(channel[ voice[w].channel ].pitchbend + chorus < 0x2000)
voice[w].orig_frequency =
(uint32)( (FLOAT_T)voice[w].orig_frequency * bend_fine[chorus] );
else voice[w].orig_frequency =
(uint32)( (FLOAT_T)voice[w].orig_frequency / bend_fine[chorus] );
if (subtype) voice[w].vibrato_depth *= 2;
break;
case 2: /* celeste */
voice[w].orig_frequency += (voice[w].orig_frequency/128) * chorus;
break;
case 3: /* flanger */
voice[w].vibrato_control_ratio = 10;
voice[w].vibrato_depth = 100;
voice[w].vibrato_sweep = 8;
voice[w].echo_delay += 200 * milli;
break;
case 4: /* symphonic : cf Children of the Night /128 bad, /1024 ok */
voice[w].orig_frequency += (voice[w].orig_frequency/512) * chorus;
voice[v].orig_frequency -= (voice[v].orig_frequency/512) * chorus;
recompute_freq(v);
break;
case 8: /* phaser */
break;
default:
break;
}
}
else {
chorus /= 3;
if(channel[ voice[w].channel ].pitchbend + chorus < 0x2000)
voice[w].orig_frequency =
(uint32)( (FLOAT_T)voice[w].orig_frequency * bend_fine[chorus] );
else voice[w].orig_frequency =
(uint32)( (FLOAT_T)voice[w].orig_frequency / bend_fine[chorus] );
}
#ifdef DEBUG_CLONE_NOTES
fprintf(stderr,"CHORUS_CLONE v%d vol%f pan%d chorus%d\n", w, voice[w].volume, voice[w].panning, chorus);
#endif
}
voice[w].loop_start = voice[w].sample->loop_start;
voice[w].loop_end = voice[w].sample->loop_end;
voice[w].echo_delay_count = voice[w].echo_delay;