This repository was archived by the owner on Jun 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSound.java
More file actions
764 lines (597 loc) · 18.2 KB
/
Sound.java
File metadata and controls
764 lines (597 loc) · 18.2 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
/*
JavaBoy
COPYRIGHT (C) 2001 Neil Millstone and The Victoria University of Manchester
;;;
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.
*/
import java.awt.*;
import java.awt.image.*;
import java.lang.*;
import java.io.*;
import java.applet.*;
import java.net.*;
import java.awt.event.KeyListener;
import java.awt.event.WindowListener;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.util.StringTokenizer;
import javax.sound.sampled.*;
import java.util.*;
import java.util.concurrent.*;
/** This class can mix a square wave signal with a sound buffer.
* It supports all features of the Gameboys sound channels 1 and 2.
*/
class NoiseGenerator {
/** Indicates sound is to be played on the left channel of a stereo sound */
public static final int CHAN_LEFT = 1;
/** Indictaes sound is to be played on the right channel of a stereo sound */
public static final int CHAN_RIGHT = 2;
/** Indicates that sound is mono */
public static final int CHAN_MONO = 4;
/** Indicates the length of the sound in frames */
int totalLength;
int cyclePos;
/** The length of one cycle, in samples */
int cycleLength;
/** Amplitude of the wave function */
int amplitude;
/** Channel being played on. Combination of CHAN_LEFT and CHAN_RIGHT, or CHAN_MONO */
int channel;
/** Sampling rate of the output channel */
int sampleRate;
/** Initial value of the envelope */
int initialEnvelope;
int numStepsEnvelope;
/** Whether the envelope is an increase/decrease in amplitude */
boolean increaseEnvelope;
int counterEnvelope;
/** Stores the random values emulating the polynomial generator (badly!) */
boolean randomValues[];
int dividingRatio;
int polynomialSteps;
int shiftClockFreq;
int finalFreq;
int cycleOffset;
/** Creates a white noise generator with the specified wavelength, amplitude, channel, and sample rate */
public NoiseGenerator(int waveLength, int ampl, int chan, int rate) {
cycleLength = waveLength;
amplitude = ampl;
cyclePos = 0;
channel = chan;
sampleRate = rate;
cycleOffset = 0;
randomValues = new boolean[32767];
Random rand = new java.util.Random();
for (int r = 0; r < 32767; r++) {
randomValues[r] = rand.nextBoolean();
}
cycleOffset = 0;
}
/** Creates a white noise generator with the specified sample rate */
public NoiseGenerator(int rate) {
cyclePos = 0;
channel = CHAN_LEFT | CHAN_RIGHT;
cycleLength = 2;
totalLength = 0;
sampleRate = rate;
amplitude = 32;
randomValues = new boolean[32767];
Random rand = new java.util.Random();
for (int r = 0; r < 32767; r++) {
randomValues[r] = rand.nextBoolean();
}
cycleOffset = 0;
}
public void setSampleRate(int sr) {
sampleRate = sr;
}
/** Set the channel that the white noise is playing on */
public void setChannel(int chan) {
channel = chan;
}
/** Setup the envelope, and restart it from the beginning */
public void setEnvelope(int initialValue, int numSteps, boolean increase) {
initialEnvelope = initialValue;
numStepsEnvelope = numSteps;
increaseEnvelope = increase;
amplitude = initialValue * 2;
}
/** Set the length of the sound */
public void setLength(int gbLength) {
if (gbLength == -1) {
totalLength = -1;
} else {
totalLength = (64 - gbLength) / 4;
}
}
public void setParameters(float dividingRatio, boolean polynomialSteps, int shiftClockFreq) {
this.dividingRatio = (int) dividingRatio;
if (!polynomialSteps) {
this.polynomialSteps = 32767;
cycleLength = 32767 << 8;
cycleOffset = 0;
} else {
this.polynomialSteps = 63;
cycleLength = 63 << 8;
java.util.Random rand = new java.util.Random();
cycleOffset = (int) (rand.nextFloat() * 1000);
}
this.shiftClockFreq = shiftClockFreq;
if (dividingRatio == 0) dividingRatio = 0.5f;
finalFreq = ((int) (4194304 / 8 / dividingRatio)) >> (shiftClockFreq + 1);
// System.out.println("dr:" + dividingRatio + " steps: " + this.polynomialSteps + " shift:" + shiftClockFreq + " = Freq:" + finalFreq);
}
/** Output a single frame of samples, of specified length. Start at position indicated in the
* output array.
*/
public void play(byte[] b, int length, int offset) {
int val;
if (totalLength != 0) {
totalLength--;
counterEnvelope++;
if (numStepsEnvelope != 0) {
if (((counterEnvelope % numStepsEnvelope) == 0) && (amplitude > 0)) {
if (!increaseEnvelope) {
if (amplitude > 0) amplitude-=2;
} else {
if (amplitude < 16) amplitude+=2;
}
}
}
int step = ((finalFreq) / (sampleRate >> 8));
// System.out.println("Step=" + step);
for (int r = offset; r < offset + length; r++) {
boolean value = randomValues[((cycleOffset ) + (cyclePos >> 8)) & 0x7FFF];
int v = value? (amplitude / 2): (-amplitude / 2);
if ((channel & CHAN_LEFT) != 0) b[r * 2] += v;
if ((channel & CHAN_RIGHT) != 0) b[r * 2 + 1] += v;
if ((channel & CHAN_MONO) != 0) b[r] += v;
cyclePos = (cyclePos + step) % cycleLength;
}
/*
for (int r = offset; r < offset + length; r++) {
val = (int) ((Math.random() * amplitude * 2) - amplitude);
if ((channel & CHAN_LEFT) != 0) b[r * 2] += val;
if ((channel & CHAN_RIGHT) != 0) b[r * 2 + 1] += val;
if ((channel & CHAN_MONO) != 0) b[r] += val;
// System.out.print(val + " ");
cyclePos = (cyclePos + 256) % cycleLength;
}*/
}
}
}
class VoluntaryWaveGenerator {
public static final int CHAN_LEFT = 1;
public static final int CHAN_RIGHT = 2;
public static final int CHAN_MONO = 4;
int totalLength;
int cyclePos;
int cycleLength;
int amplitude;
int channel;
int sampleRate;
int volumeShift;
byte[] waveform = new byte[32];
public VoluntaryWaveGenerator(int waveLength, int ampl, int duty, int chan, int rate) {
cycleLength = waveLength;
amplitude = ampl;
cyclePos = 0;
channel = chan;
sampleRate = rate;
}
public VoluntaryWaveGenerator(int rate) {
cyclePos = 0;
channel = CHAN_LEFT | CHAN_RIGHT;
cycleLength = 2;
totalLength = 0;
sampleRate = rate;
amplitude = 32;
}
public void setSampleRate(int sr) {
sampleRate = sr;
}
public void setFrequency(int gbFrequency) {
// cyclePos = 0;
float frequency = (int) ((float) 65536 / (float) (2048 - gbFrequency));
// System.out.println("gbFrequency: " + gbFrequency + "");
cycleLength = (int) ((float) (256f * sampleRate) / (float) frequency);
if (cycleLength == 0) cycleLength = 1;
// System.out.println("Cycle length : " + cycleLength + " samples");
}
public void setChannel(int chan) {
channel = chan;
}
public void setLength(int gbLength) {
if (gbLength == -1) {
totalLength = -1;
} else {
totalLength = (256 - gbLength) / 4;
}
}
public void setSamplePair(int address, int value) {
waveform[address * 2] = (byte) ((value & 0xF0) >> 4);
waveform[address * 2 + 1] = (byte) ((value & 0x0F)) ;
}
public void setVolume(int volume) {
switch (volume) {
case 0 : volumeShift = 5;
break;
case 1 : volumeShift = 0;
break;
case 2 : volumeShift = 1;
break;
case 3 : volumeShift = 2;
break;
}
// System.out.println("A:"+volume);
}
public void play(byte[] b, int length, int offset) {
int val;
if (totalLength != 0) {
totalLength--;
for (int r = offset; r < offset + length; r++) {
int samplePos = (31 * cyclePos) / cycleLength;
val = waveform[samplePos % 32] >> volumeShift << 1;
// System.out.print(" " + val);
if ((channel & CHAN_LEFT) != 0) b[r * 2] += val;
if ((channel & CHAN_RIGHT) != 0) b[r * 2 + 1] += val;
if ((channel & CHAN_MONO) != 0) b[r] += val;
// System.out.print(val + " ");
cyclePos = (cyclePos + 256) % cycleLength;
}
}
}
}
class SquareWaveGenerator {
/** Sound is to be played on the left channel of a stereo sound */
public static final int CHAN_LEFT = 1;
/** Sound is to be played on the right channel of a stereo sound */
public static final int CHAN_RIGHT = 2;
/** Sound is to be played back in mono */
public static final int CHAN_MONO = 4;
/** Length of the sound (in frames) */
int totalLength;
/** Current position in the waveform (in samples) */
int cyclePos;
/** Length of the waveform (in samples) */
int cycleLength;
/** Amplitude of the waveform */
int amplitude;
/** Amount of time the sample stays high in a single waveform (in eighths) */
int dutyCycle;
/** The channel that the sound is to be played back on */
int channel;
/** Sample rate of the sound buffer */
int sampleRate;
/** Initial amplitude */
int initialEnvelope;
/** Number of envelope steps */
int numStepsEnvelope;
/** If true, envelope will increase amplitude of sound, false indicates decrease */
boolean increaseEnvelope;
/** Current position in the envelope */
int counterEnvelope;
/** Frequency of the sound in internal GB format */
int gbFrequency;
/** Amount of time between sweep steps. */
int timeSweep;
/** Number of sweep steps */
int numSweep;
/** If true, sweep will decrease the sound frequency, otherwise, it will increase */
boolean decreaseSweep;
/** Current position in the sweep */
int counterSweep;
/** Create a square wave generator with the supplied parameters */
public SquareWaveGenerator(int waveLength, int ampl, int duty, int chan, int rate) {
cycleLength = waveLength;
amplitude = ampl;
cyclePos = 0;
dutyCycle = duty;
channel = chan;
sampleRate = rate;
}
/** Create a square wave generator at the specified sample rate */
public SquareWaveGenerator(int rate) {
dutyCycle = 4;
cyclePos = 0;
channel = CHAN_LEFT | CHAN_RIGHT;
cycleLength = 2;
totalLength = 0;
sampleRate = rate;
amplitude = 32;
counterSweep = 0;
}
/** Set the sound buffer sample rate */
public void setSampleRate(int sr) {
sampleRate = sr;
}
/** Set the duty cycle */
public void setDutyCycle(int duty) {
switch (duty) {
case 0 : dutyCycle = 1;
break;
case 1 : dutyCycle = 2;
break;
case 2 : dutyCycle = 4;
break;
case 3 : dutyCycle = 6;
break;
}
// System.out.println(dutyCycle);
}
/** Set the sound frequency, in internal GB format */
public void setFrequency(int gbFrequency) {
try {
float frequency = 131072 / 2048;
if (gbFrequency != 2048) {
frequency = ((float) 131072 / (float) (2048 - gbFrequency));
}
// System.out.println("gbFrequency: " + gbFrequency + "");
this.gbFrequency = gbFrequency;
if (frequency != 0) {
cycleLength = (256 * sampleRate) / (int) frequency;
} else {
cycleLength = 65535;
}
if (cycleLength == 0) cycleLength = 1;
// System.out.println("Cycle length : " + cycleLength + " samples");
} catch (ArithmeticException e) {
// Skip ip
}
}
/** Set the channel for playback */
public void setChannel(int chan) {
channel = chan;
}
/** Set the envelope parameters */
public void setEnvelope(int initialValue, int numSteps, boolean increase) {
initialEnvelope = initialValue;
numStepsEnvelope = numSteps;
increaseEnvelope = increase;
amplitude = initialValue * 2;
}
/** Set the frequency sweep parameters */
public void setSweep(int time, int num, boolean decrease) {
timeSweep = (time + 1) / 2;
numSweep = num;
decreaseSweep = decrease;
counterSweep = 0;
// System.out.println("Sweep: " + time + ", " + num + ", " + decrease);
}
public void setLength(int gbLength) {
if (gbLength == -1) {
totalLength = -1;
} else {
totalLength = (64 - gbLength) / 4;
}
}
public void setLength3(int gbLength) {
if (gbLength == -1) {
totalLength = -1;
} else {
totalLength = (256 - gbLength) / 4;
}
}
public void setVolume3(int volume) {
switch (volume) {
case 0 : amplitude = 0;
break;
case 1 : amplitude = 32;
break;
case 2 : amplitude = 16;
break;
case 3 : amplitude = 8;
break;
}
// System.out.println("A:"+volume);
}
/** Output a frame of sound data into the buffer using the supplied frame length and array offset. */
public void play(byte[] b, int length, int offset) {
int val = 0;
if (totalLength != 0) {
totalLength--;
if (timeSweep != 0) {
counterSweep++;
if (counterSweep > timeSweep) {
if (decreaseSweep) {
setFrequency(gbFrequency - (gbFrequency >> numSweep));
} else {
setFrequency(gbFrequency + (gbFrequency >> numSweep));
}
counterSweep = 0;
}
}
counterEnvelope++;
if (numStepsEnvelope != 0) {
if (((counterEnvelope % numStepsEnvelope) == 0) && (amplitude > 0)) {
if (!increaseEnvelope) {
if (amplitude > 0) amplitude-=2;
} else {
if (amplitude < 16) amplitude+=2;
}
}
}
for (int r = offset; r < offset + length; r++) {
if (cycleLength != 0) {
if (((8 * cyclePos) / cycleLength) >= dutyCycle) {
val = amplitude;
} else {
val = -amplitude;
}
}
/* if (cyclePos >= (cycleLength / 2)) {
val = amplitude;
} else {
val = -amplitude;
}*/
if ((channel & CHAN_LEFT) != 0) b[r * 2] += val;
if ((channel & CHAN_RIGHT) != 0) b[r * 2 + 1] += val;
if ((channel & CHAN_MONO) != 0) b[r] += val;
// System.out.print(val + " ");
cyclePos = (cyclePos + 256) % cycleLength;
}
}
}
}
/** This is the central controlling class for the sound.
* It interfaces with the Java Sound API, and handles the
* calsses for each sound channel.
*/
class Sound extends Thread {
/** The DataLine for outputting the sound */
SourceDataLine soundLine;
private final Semaphore sem;
SquareWaveGenerator channel1;
SquareWaveGenerator channel2;
VoluntaryWaveGenerator channel3;
NoiseGenerator channel4;
boolean soundEnabled = false;
/** If true, channel is enabled */
boolean channel1Enable = true, channel2Enable = true,
channel3Enable = true, channel4Enable = true;
/** Current sampling rate that sound is output at */
int sampleRate = 44100;
/** Amount of sound data to buffer before playback */
int bufferLengthMsec = 200;
FloatControl volControl = null;
/** Initialize sound emulation, and allocate sound hardware */
public void setChan1(boolean channel1Enable)
{
this.channel1Enable = channel1Enable;
}
public void setChan2(boolean channel2Enable)
{
this.channel2Enable = channel2Enable;
}
public void setChan3(boolean channel3Enable)
{
this.channel3Enable = channel3Enable;
}
public void setChan4(boolean channel4Enable)
{
this.channel4Enable = channel4Enable;
}
public void setSoundEnable(boolean soundEnabled)
{
this.soundEnabled = soundEnabled;
}
public float getVolume()
{
return (volControl.getValue());
}
public Sound(Semaphore sem) {
this.sem = sem;
soundLine = initSoundHardware();
if(soundEnabled)
volControl = (FloatControl) soundLine.getControl(FloatControl.Type.MASTER_GAIN);
channel1 = new SquareWaveGenerator(sampleRate);
channel2 = new SquareWaveGenerator(sampleRate);
channel3 = new VoluntaryWaveGenerator(sampleRate);
channel4 = new NoiseGenerator(sampleRate);
}
/** Initialize sound hardware if available */
public SourceDataLine initSoundHardware() {
try {
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
sampleRate, 8, 2, 2, sampleRate, true);
DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, format);
if (!AudioSystem.isLineSupported(lineInfo)) {
System.out.println("Error: Can't find audio output system!");
soundEnabled = false;
} else {
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(lineInfo);
int bufferLength = (sampleRate / 1000) * bufferLengthMsec;
line.open(format, bufferLength);
line.start();
// System.out.println("Initialized audio successfully.");
soundEnabled = true;
return line;
}
} catch (Exception e) {
System.out.println("Error: Audio system busy!");
soundEnabled = false;
}
return null;
}
/** Change the sample rate of the playback */
public void setSampleRate(int sr) {
sampleRate = sr;
soundLine.flush();
soundLine.close();
soundLine = initSoundHardware();
channel1.setSampleRate(sr);
channel2.setSampleRate(sr);
channel3.setSampleRate(sr);
channel4.setSampleRate(sr);
}
/** Change the sound volume **/
public void setVolume(double gain)
{
// Now we need to convert to decibels...
gain /= 100; // dB is lograthmic
float dB = (float) (Math.log(gain == 0.0 ? 0.0001 : gain) / Math.log(10.0)*20.0);
//Now we set the level
volControl.setValue(dB);
}
public float getMaxVol()
{
return (volControl.getMaximum());
}
public float getMinVol()
{
return (volControl.getMinimum());
}
/** Change the sound buffer length */
public void setBufferLength(int time) {
bufferLengthMsec = time;
soundLine.flush();
soundLine.close();
soundLine = initSoundHardware();
}
/** Adds a single frame of sound data to the buffer */
public void run() {
for(;;)
{
synchronized (this) {
try
{
sem.acquire();
}
catch(Throwable e)
{
e.printStackTrace();
}
if (soundEnabled) {
int numSamples;
if (sampleRate / 28 >= soundLine.available() * 2) {
numSamples = soundLine.available() * 2;
} else {
numSamples = (sampleRate / 28) & 0xFFFE;
}
byte[] b = new byte[numSamples];
if (channel1Enable) channel1.play(b, numSamples / 2, 0);
if (channel2Enable) channel2.play(b, numSamples / 2, 0);
if (channel3Enable) channel3.play(b, numSamples / 2, 0);
if (channel4Enable) channel4.play(b, numSamples / 2, 0);
soundLine.write(b, 0, numSamples);
}
}
}
}
}