git @ Cat's Eye Technologies JaC64 / master com / dreamfabric / jac64 / SIDMixer.java
master

Tree @master (Download .tar.gz)

SIDMixer.java @masterraw · history · blame

  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
/**
 * This file is a part of JaC64 - a Java C64 Emulator
 * Main Developer: Joakim Eriksson (JaC64.com Dreamfabric.com)
 * Contact: joakime@sics.se
 * Web: http://www.jac64.com/ 
 * http://www.dreamfabric.com/c64
 * ---------------------------------------------------
 */

package com.dreamfabric.jac64;

/**
 * This mixes four channels of SID sounds (or three)
 *
 * 0 - 2 => 16 bits channels from the SID chip
 *
 * 3 => 8 bits from PSID sample player
 *
 *
 * Created: Fri Oct 28 17:45:19 2005
 *
 * @author Joakim Eriksson
 * @version 1.0
 */
public class SIDMixer {

  public static int BYTES_PER_SAMPLE = 2;

  public static final boolean NO_SOUND = false;

  public static final boolean DEBUG = false;
  // 16 bits, 1 second buffer
  public static int DL_BUFFER_SIZE = SIDVoice6581.SAMPLE_RATE * 2;
  // public static final int SYNCH_TIME = 1000 / 50;
  public static final int SYNCH_BUFFER = ((SIDVoice6581.SAMPLE_RATE * 2) / 50);

  public static final int EFX_NONE = 0;
  public static final int EFX_FLANGER_1 = 1;
  public static final int EFX_FLANGER_2 = 2;
  public static final int EFX_FLANGER_3 = 3;
  public static final int EFX_PHASER_1 = 4;
  public static final int EFX_PHASER_2 = 5;
  public static final int EFX_CHORUS_1 = 6;
  public static final int EFX_CHORUS_2= 7;
  public static final int EFX_ECHO_1 = 8;
  public static final int EFX_ECHO_2 = 9;
  public static final int EFX_REV_SMALL = 10;
  public static final int EFX_REV_MED = 11;
  public static final int EFX_REV_LARGE = 12;
  public static final int EFX_FSWEEP = 13;
  public static final int EFX_FSWEEP_RES = 14;

  private String[] efxNames = {
      "EFX_NONE",
      "EFX_FLANGER_1",
      "EFX_FLANGER_2",
      "EFX_FLANGER_3",
      "EFX_PHASER_1",
      "EFX_PHASER_2",
      "EFX_CHORUS_1",
      "EFX_CHORUS_2",
      "EFX_ECHO_1",
      "EFX_ECHO_2",
      "EFX_REV_SMALL",
      "EFX_REV_MED",
      "EFX_REV_LARGE",
      "EFX_FSWEEP",
      "EFX_FSWEEP_RES",
  };

  // if set this will never block...
  public boolean fullSpeed = false;

  private SIDVoice psid;
  private SIDVoice6581[] channels;
  private boolean soundOn = true;

  private SIDMixerListener listener = null;
  private AudioDriver driver;

  // Fixed size buffers
  byte[] buffer = new byte[SIDVoice6581.GENLEN * 2];
  byte[] syncBuffer = new byte[4096];
  int[] intBuffer = new int[SIDVoice6581.GENLEN];
  int[] noFltBuffer = new int[SIDVoice6581.GENLEN];



  // Effects buffers and variables
  boolean effects = false;
//boolean moog = false;
  public static final int LFO_WAVELEN = 500;

  int[] echo;
  int echoSize = 0;
  int echoPos = 0;
  int echoLFODiff = 0;
  int echoLFODiffMax = 0;
  int echoLFODepth = 50;
  int echoFeedback = 0;
  int echoLFOSpeed = 0;
  int echoLFOPos = 0;
  int echoDW = 50;

  int maxefx;
  int minefx;

  int sidVol = 15;

  int sidVolArr[] = new int[SIDVoice6581.GENLEN];
  long lastCycles = 0;

  // Filter (some stuff public for viewing )
  int filterVal = 0;
  public int cutoff = 0;
  public int resonance = 0;
  int filterOn = 0;

  int masterVolume = 100;

  boolean lpOn = false;
  boolean hpOn = false;
  boolean bpOn = false;

  // SIDFilter
  long vlp;
  long vhp;
  long vbp;
  long w0;
  long div1024Q;

  // ExtFilter
  long exVlp;
  long exVhp;
  long exVo;
  long exw0lp;
  long exw0hp;


  int irq = 0;

  int[] sine10Hz;

//private MoogFilter moogFilter;
//private int moogResonance = 0;
//private int moogCutoff = 0;
//private int mdep = 0;

  public SIDMixer() {
  }

  /**
   * Creates a new <code>SIDMixer</code> instance.
   *
   *
   */
  public SIDMixer(SIDVoice6581[] channels, SIDVoice sample, AudioDriver driver) {
    init(channels, sample, driver);
  }

  public void init(SIDVoice6581[] channels, SIDVoice sample, AudioDriver driver) {
    this.channels = channels;
    psid = sample;
    this.driver = driver;

//  moogFilter = new MoogFilter(SID6581.SAMPLE_RATE);
//  moogFilter.setFilterParams(120, 0.5);

    System.out.println("Micros per SIDGen: " + getMicrosPerGen());

    // From resid... (~1 Mhz clock...) Extfilter
    exw0hp = 105;
    exw0lp = 104858;

    sine10Hz = new int[LFO_WAVELEN];
    for (int i = 0, n = LFO_WAVELEN; i < n; i++) {
      sine10Hz[i] = (int)
      (500 + (500 * Math.sin(i * 2 * 3.1415 / LFO_WAVELEN)));
    }

    setEFX(EFX_ECHO_1);
    setEFX(EFX_NONE);
  }

  public void setListener(SIDMixerListener front) {
    listener = front;
  }

  // -------------------------------------------------------------------
  // Sound effects after the mixer!
  // -------------------------------------------------------------------
  public void setEchoTime(int millisDelay) {
    int sampleSize = millisDelay * SIDVoice6581.SAMPLE_RATE / 1000;
    System.out.println("SamplesDelay: " + sampleSize);
    echoSize = sampleSize;
    echo = new int[echoSize];
    echoLFODiffMax = (echoSize * echoLFODepth) / 110;
    echoLFODiff = 0;
    echoPos = 0;
  }

  public int getEchoTime() {
    return (1000 * echoSize) / SIDVoice6581.SAMPLE_RATE;
  }

  public int getEFXCount() {
    return efxNames.length;
  }

  public String getEFXName(int efx) {
    return efxNames[efx];
  }

  public void setEchoFeedback(int percen) {
    echoFeedback = percen;
  }

  public int getEchoFeedback() {
    return echoFeedback;
  }

  public void setEchoLFOSpeed(int hzd10) {
    // 10Hz wave => hz * 10 for speed in that...
    echoLFOSpeed = hzd10;
  }

  public int getEchoLFOSpeed() {
    return echoLFOSpeed;
  }

  public void setEchoDW(int percent) {
    echoDW = percent;
  }

  public int getEchoDW() {
    return echoDW;
  }

  public void setEchoLFODepth(int percent) {
    echoLFODepth = percent;
    echoLFODiffMax = (echoSize * echoLFODepth) / 110;
  }

  public int getEchoLFODepth() {
    return echoLFODepth;
  }

  public boolean getEffectsOn() {
    return effects;
  }

  public void setEFX(int fx) {
    fx = fx % efxNames.length;
    effects = true;
//  moog = false;
    switch (fx) {
    case  EFX_NONE:
      effects = false;
      break;
    case EFX_FLANGER_1:
      setEchoTime(5);
      setEchoFeedback(75);
      setEchoLFOSpeed(1);
      setEchoLFODepth(35);
      setEchoDW(33);
      break;
    case EFX_FLANGER_2:
      setEchoTime(15);
      setEchoFeedback(70);
      setEchoLFOSpeed(5);
      setEchoLFODepth(35);
      setEchoDW(35);
      break;
    case EFX_FLANGER_3:
      setEchoTime(2);
      setEchoFeedback(85);
      setEchoLFOSpeed(3);
      setEchoLFODepth(55);
      setEchoDW(30);
      break;
    case EFX_PHASER_1:
      setEchoTime(10);
      setEchoFeedback(0);
      setEchoLFOSpeed(1);
      setEchoLFODepth(75);
      setEchoDW(50);
      break;
    case EFX_PHASER_2:
      setEchoTime(3);
      setEchoFeedback(0);
      setEchoLFOSpeed(2);
      setEchoLFODepth(85);
      setEchoDW(40);
      break;
    case EFX_CHORUS_1:
      setEchoTime(25);
      setEchoFeedback(60);
      setEchoLFOSpeed(1);
      setEchoLFODepth(35);
      setEchoDW(35);
      break;
    case EFX_CHORUS_2:
      setEchoTime(30);
      setEchoFeedback(50);
      setEchoLFOSpeed(5);
      setEchoLFODepth(25);
      setEchoDW(35);
      break;
    case EFX_ECHO_1:
      setEchoTime(150);
      setEchoFeedback(0);
      setEchoLFOSpeed(0);
      setEchoLFODepth(0);
      setEchoDW(33);
      break;
    case EFX_ECHO_2:
      setEchoTime(300);
      setEchoFeedback(33);
      setEchoLFOSpeed(0);
      setEchoLFODepth(0);
      setEchoDW(45);
      break;
    case EFX_REV_SMALL:
      setEchoTime(70);
      setEchoFeedback(40);
      setEchoLFOSpeed(0);
      setEchoLFODepth(0);
      setEchoDW(33);
      break;
    case EFX_REV_MED:
      setEchoTime(130);
      setEchoFeedback(50);
      setEchoLFOSpeed(0);
      setEchoLFODepth(0);
      setEchoDW(33);
      break;
    case EFX_REV_LARGE:
      setEchoTime(100);
      setEchoFeedback(70);
      setEchoLFOSpeed(0);
      setEchoLFODepth(0);
      setEchoDW(40);
      break;
//    case EFX_FSWEEP:
//    effects = false;
//    moog = true;
//    moogResonance = 30;
//    mdep = 60;
//    mfrq = 1;
//    moogCutoff = 1000;
//    break;
//    case EFX_FSWEEP_RES:
//    effects = false;
//    moog = true;
//    moogResonance = 75;
//    mdep = 70;
//    mfrq = 4;
//    moogCutoff = 500;
//    break;
    }

    if (listener != null) {
      listener.updateValues();
    }
  }

  public void setEffectsOn(boolean fx) {
    effects = fx;
  }

  public boolean isEffectsOn() {
    return effects;
  }

  // -------------------------------------------------------------------
  // Control for the SID Mixer
  // -------------------------------------------------------------------

  public void setFilterCutoffLO(int data) {
    cutoff = cutoff & 0xff8 | (data & 0x07);
    recalcFilter();
  }

  public void setFilterCutoffHI(int data) {
    cutoff = cutoff & 0x07 | (data << 3);
    recalcFilter();
  }

  public void setFilterResonance(int data) {
    resonance = data;
    recalcFilter();
  }

  public void setFilterCtrl(int data) {
    lpOn = (data & 0x10) > 0;
    bpOn = (data & 0x20) > 0;
    hpOn = (data & 0x40) > 0;
    if (DEBUG) {
      System.out.println("Filter: " + data + " => " + (lpOn ? "LP " : "") +
          (bpOn ? "BP " : "") + (hpOn ? "HP " : ""));
    }
    recalcFilter();
  }

  public void setFilterOn(int on) {
    filterOn = on;
  }

  public void setMoogFilterOn(boolean on) {
//  moog = on;
  }

  public boolean isMoogFilterOn() {
    return false; // moog;
  }

  public void setMoogResonance(int percent) {
//  moogResonance = percent;
  }

  public int getMoogResonance() {
    return 0; //moogResonance;
  }

  public void setMoogCutoff(int frq) {
//  moogCutoff = frq;
  }

  public int getMoogCutoff() {
    return 0;// moogCutoff;
  }

  public void setMoogSpeed(int hz10) {
//  mfrq = hz10;
  }

  public int getMoogSpeed() {
    return 0;//mfrq;
  }

  public void setMoogDepth(int dep) {
    //mdep = dep;
  }

  public int getMoogDepth() {
    return 0; //mdep;
  }

  public void setVolume(int vol) {
    sidVol = vol;
  }

  public void setVolume(int vol, long cycles) {
    if (lastCycles > 0) {
      int pos = (int) ((cycles - lastCycles) /
          (SIDVoice6581.CYCLES_PER_SAMPLE + 10));
      sidVolArr[pos % SIDVoice6581.GENLEN] = vol;
    } else {
      sidVol = vol;
    }
  }

  private void recalcFilter() {
    int fCutoff = 30 + (12000 * cutoff / 2048);
    w0 = (long) (2 * Math.PI * fCutoff * 1.048576);
    div1024Q = (int) (1024.0 / (0.707 + 1.0 * resonance / 15));
  }


  public void stop() {
    setVolume(0);
  }

  public void reset() {
    exVo = 0;
    exVhp = 0;
    exVlp = 0;

    cutoff = 0;
    resonance = 0;
    w0 = 0;
    div1024Q = 0;
    filterOn = 0;

    maxefx = 0;
    minefx = 0;

    for (int i = 0, n = SIDVoice6581.GENLEN; i < n; i++) {
      sidVolArr[i] = -1;
    }
    setVolume(15);
    recalcFilter();
  }


  public void printStatus() {
    System.out.println("SIDMixer  ----------------------------");
    System.out.println("Volume: " + sidVol);
    System.out.println("FilterOn: " + filterOn);
    System.out.println("Cutoff: " + cutoff);
    System.out.println("Resonance: " + resonance);

    System.out.println("Max Efx:" + maxefx);
    System.out.println("Min Efx:" + minefx);
//  moogFilter.printStatus();

  }

  public void setFullSpeed(boolean fs) {
    System.out.println("Set full speed: " + fs);
    fullSpeed = fs;
  }

  public boolean fullSpeed() {
    return fullSpeed;
  }

  public int[] getBuffer() {
    return intBuffer;
  }



  public static final int SLEEP_SYNC = 1;
  private int sleep = 100;
  private int syncMode = 0;//SLEEP_SYNC;
  private double avg = 10;
  private long lastTime = System.currentTimeMillis();
  private long micros = 0;

  public boolean updateSound(long cycles) {
    // For all the normal SID generator channels generate the sound!
    // and mix it into the intBuffer

    // should return true when irq!!!
    irq++;
    boolean trueIRQ = (irq % SIDVoice6581.GENS_PER_IRQ) == 0;

    // How do we delay the short time that we should delay here???
    if (trueIRQ) {
      if (syncMode == SLEEP_SYNC && !fullSpeed) {
        long elapsed = System.currentTimeMillis() - lastTime;
        lastTime = System.currentTimeMillis();
        avg = 0.99 * avg + 0.01 * elapsed;
        // avg should be 20 =>
        // 20 = sleep + X
        // avg = sleep + X
        // sleep = avg - X
        if (avg < 20) sleep++;
        if (avg > 20) sleep--;
        System.out.println("Avg: " + avg + " Sleep: " + sleep / 10.0 + " " +
            (driver.getMicros() - micros));
        double slm = 19.75 - ((driver.getMicros() - micros) / 1000.0);
        try {
          Thread.sleep((int) slm);
        } catch (Exception e) {
          e.printStackTrace();
        }
        micros = driver.getMicros();
      }
    }


    if (!driver.hasSound()) {
      // No sound - no continue...
      return trueIRQ;
    }

    // If either fullspeed or SLEEP_SYNC then check if there are room
    // for more samples!
    if (fullSpeed || syncMode == SLEEP_SYNC) {
      if (driver.available() < SIDVoice6581.GENLEN * 2) {
        return false;
      }
    }

    lastCycles = cycles;

    if (soundOn) {
      int [] tBuf;
      if (psid != null) {
        tBuf = psid.generateSound(cycles);
        for (int j = 0, m = SIDVoice6581.GENLEN; j < m; j++) {
          intBuffer[j] = 0;
          // From a byte array (low 8 bits)
          noFltBuffer[j] = (tBuf[j] << 4);
        }
      } else {
        for (int j = 0, m = SIDVoice6581.GENLEN; j < m; j++) {
          intBuffer[j] = 0;
          noFltBuffer[j] = 0;
        }
      }

      for (int i = 0, n = channels.length; i < n; i++) {
        int[] buf = channels[i].generateSound(cycles);

        if ((filterOn & (1 << i)) > 0)
          tBuf = intBuffer;
        else
          tBuf = noFltBuffer;

        for (int j = 0, m = SIDVoice6581.GENLEN; j < m; j++) {
          tBuf[j] += buf[j] >> 2;
        }
      }

      // Internal SID emulation processing
      // Filters...

      for (int i = 0, n = SIDVoice6581.GENLEN; i < n; i++) {
        int inval = intBuffer[i];

        // SID filter - filter shuold be fed even if not on...
        if (filterOn > 0) {
          // 8 + 8 + 7 => 23 which is the number of cycles per sample
          vbp -= 8 * w0 * vhp >> 20;
      vlp -= 8 * w0 * vbp >> 20;
          vhp = (vbp * div1024Q >> 10) - vlp - inval;

          vbp -= 8 * w0 * vhp >> 20;
    vlp -= 8 * w0 * vbp >> 20;
    vhp = (vbp * div1024Q >> 10) - vlp - inval;

    vbp -= 7 * w0 * vhp >> 20;
    vlp -= 7 * w0 * vbp >> 20;
    vhp = (vbp * div1024Q >> 10) - vlp - inval;

    inval = (int)
    ((bpOn ? vbp : 0) + (hpOn ? vhp : 0) + (lpOn ? vlp : 0));
        }

        inval += noFltBuffer[i];
        // 13 bits * sidVol => 13 + 4 = 17 bits... take it down to 14...
        if (sidVolArr[i] != -1) {
          sidVol = sidVolArr[i];
          sidVolArr[i] = -1;
        }
        inval = (inval * sidVol) >> 2;

        // External filter
        // run 8 + 8 + 7 cycles
        exVlp += (8 * exw0lp >> 8) * (inval - exVlp) >> 12;
        exVhp += exw0hp * 8 * (exVlp - exVhp) >> 20;
        exVlp += (8 * exw0lp >> 8) * (inval - exVlp) >> 12;
        exVhp += exw0hp * 8 * (exVlp - exVhp) >> 20;

        exVo = exVlp - exVhp;
        exVlp += (7 * exw0lp >> 8) * (inval - exVlp) >> 12;
        exVhp += exw0hp * 7 * (exVlp - exVhp) >> 20;

        intBuffer[i] = (int) exVo;
      }

//    if (moog)
//    moogFilter.performFilter(intBuffer, SID6581.GENLEN);

      if (effects) {
        for (int i = 0, n = SIDVoice6581.GENLEN; i < n; i++) {
          int exVal = intBuffer[i];
          int echoRead = (echoPos + echoLFODiff) % echoSize;
          int out = (exVal * (100 - echoDW) + echo[echoRead] * echoDW) / 100;
          if (out > 32767) out = 32767;
          if (out < -32767) out = -32767;

          intBuffer[i] = out;


          exVal = exVal + ((echo[echoRead] * echoFeedback) / 100);

          if (exVal > maxefx) maxefx = exVal;
          if (exVal < minefx) minefx = exVal;

          if (exVal > 32767) exVal = 32767;
          if (exVal < -32767) exVal = -32767;

          echo[echoPos] = exVal;
          echoPos = (echoPos + 1) % echoSize;
        }
      }

      int bIndex = 0;
      if (BYTES_PER_SAMPLE == 2) {
        for (int i = 0, n = SIDVoice6581.GENLEN; i < n; i++) {
          buffer[bIndex++] = (byte) (intBuffer[i] & 0xff);
          buffer[bIndex++] = (byte) ((intBuffer[i] >> 8));
        }
      } else {
        for (int i = 0, n = SIDVoice6581.GENLEN; i < n; i++) {
          buffer[bIndex++] = (byte) ((intBuffer[i] >> 8));
        }
      }
    }

    driver.write(buffer);

    if (trueIRQ) {
      echoLFODiff = (echoLFODiffMax * sine10Hz[echoLFOPos]) / 1000;
      echoLFOPos = (echoLFOPos + echoLFOSpeed) % LFO_WAVELEN;
    }
    return trueIRQ;
  }

  public int getMicrosPerGen() {
    long mpg = 1000000l * SIDVoice6581.GENLEN;
    return (int) (mpg / SIDVoice6581.SAMPLE_RATE);
  }

  public boolean soundOn() {
    return soundOn;
  }

  public void setSoundOn(boolean on) {
    soundOn = on;
    if (soundOn == false) {
      for (int i = 0, n = buffer.length; i < n; i++) {
        buffer[i] = 0;
      }
    }
  }
}