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
|
import java.applet.Applet; import java.awt.Graphics; import java.awt.AWTEvent; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.DataLine;
public class SoundTemplate4k extends Applet implements Runnable {
private final static int SCREENWIDTH = 600; private final static int SCREENHEIGHT = 400;
private final static float RATE = 16000f; private final static float AMPLITUDE = 2048f;
private byte musicLoop[]; private Clip music;
private final static int maxNote = 730; private final static int length = 1018; private final static int tempo = 210; private final static String notes = "[42,#OB+^*(Un6*xG]]#io[%$\\_H]*eG:+#E7i^ W}T7 A'T7 UTR^ o^3^*)(R^ L@%7*/(E7 6@[7 /tx%$DUC^ 0y ,-)(R^ b|l]*0EX[*kKS^ (K;+#L7\\7 L3s]#/(E7 ='a^ #Un6*R<<^ 8%$A[2^*)(R^ S}a^ #Un6*`#5^ 8%$Mxw\\ o;'_ o;'_ rS9^*4L/,-qWf*#?rb<\"E$U%$:cP^*P<<^ .YK_ Aq]^*^#5^ # _ /yD^ 5q]^*IEv^ 1yD^ ll@^*3;2^*%8~] F7i^ qX2N!T#OM!LrXq#*LhR&po{]*+8~] qKF7 QzN]#uVT%$'8~] 7:>] .YK_ Bq]^*rDB7*grZ7 o+q7 g_/&$,q]^*-w%_ ^>[] 7Pp^ <r37*Bj\"^#0@h^ =\"+^*DUC^ 6Z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\\7 L3s]#/(E7 ='a^ s:S\\ fWi^ r*'] kKS^ >An]*\\_H]*l`%7*/(E7 6@[7 /tx%$DUC^ ,xa^ 9M]]#qS:+#F7i^ =\"+^*DUC^ ,xa^ 8M]]#(a2,#]l@^*yl@^*3HK] |\" _ K;u,#LC`6*)1O]#`w^]*tKS^ )+e^*/w%_ vKS^ R?O^*xl@^*ji-^ I'=_ [?O^*xP&^ :Pp^ IG6^ (;2^*%EB7*bYzM!{GeL!*7|y&T2_+#Q}a^ :#`6*c2~] [TE7 p\"D^ O?O^*{*e^*!1-_ +SE] &#r7 v71^#grZ7 btK] I'=_ ]?O^*1\"+^*FUC^ :I(] WZ!&$)(R^ S}a^ 3=eL!Q}a^ zq'M+^iGM!BZl!*SwR, )d;_ Pb|[*";
private final static int TRIANGLEHARMONICS = 7; // Number of harmonics private final static double TRIANGLEAMPLITUDE = 0.5; // Triangle Volume private final static double SINAMPLITUDE = 0.2; // Sinewave Volume private final static int ATTACK = 1000; // Note attack private final static int DECAY = 1000; // Note decay
@Override public void start() { enableEvents(AWTEvent.KEY_EVENT_MASK); new Thread(this).start(); }
public void run() {
// // Initialise and start background music //
// Create array of music frequencies int i, j, k; double[] freq = new double[100]; double f = 16.351562; // Frequency of C0 (C4 is middle C) for (i=0; i<7*12; i++) { freq[i] = f; f *= 1.0594630944; // Evenly tempered scale (12th root 2) }
// Create an array of bytes to store the background music musicLoop = new byte[2*(int)( length*15f/tempo*RATE )];
// Read each note and render it into the loop int semiQuaver = 0; // Start time for next note int byte0, byte1, twoNotes=0; for (i=0; i<maxNote; i++) { if ((i & 1)==0) { // Decode 5 chars to 1 int twoNotes = 0; for (j=4;j>=0;j--) twoNotes = twoNotes*95+(int)notes.charAt(5*(i>>1)+j)-0x20; } byte0 = twoNotes & 0xff; // Frequency byte1 = twoNotes>>8 & 0xff; // Delay & Duration twoNotes=twoNotes>>16; double frequency = freq[byte0 & 0x7f]; if (byte0 >127) // If bit7 is set, there is a delay before note semiQuaver += (byte1>>4 & 0x0f) + 1; // Note start time int duration = (byte1 & 0x0f) + 1; // Note duration // Calculate start and stop times in terms of samples int start = (int)( (semiQuaver)*15f/tempo*RATE ); int stop = (int)( (semiQuaver+duration)*15f/tempo*RATE );
// Calculate each sample and render it into the music loop double sample; for (j=start; j<stop; j++) { double time = (j-start)/RATE; // Time (seconds) // Triangle fundamental + sinusoidal 4th and 6th harmonics
sample = 0.0; for (k=1; k<=TRIANGLEHARMONICS; k+=2) { f = frequency*k; if (f<RATE/2.0) // Nyquist frequency sample += Math.sin(Math.PI*f*time*2.0)/(k*k); } sample = sample*TRIANGLEAMPLITUDE + (Math.sin(Math.PI*frequency*time*8.0) + Math.sin(Math.PI*frequency*time*12.0)) * SINAMPLITUDE;
// Apply attack and decay to avoid clicks if (j-start<ATTACK) sample *= (j-start)/(float)ATTACK; if (stop-j<DECAY) sample *= (stop-j)/(float)DECAY;
// Add the 16 bit sample to loop int data = (musicLoop[2*j]<<8)+(musicLoop[2*j+1] & 0xff); data += (int)(AMPLITUDE*sample); musicLoop[2*j+1] = (byte)(data & 0xff); musicLoop[2*j] = (byte)((data>>8) & 0xff); } } try { // Initialise Sound System AudioFormat audioFormat = new AudioFormat(RATE, 16, 1, true, true); DataLine.Info info = new DataLine.Info(Clip.class, audioFormat); music = (Clip)AudioSystem.getLine(info); music.open(audioFormat, musicLoop, 0, musicLoop.length); music.loop(Clip.LOOP_CONTINUOUSLY); } catch (Exception e) {}
// // End of Music Section //
BufferedImage screen = new BufferedImage( SCREENWIDTH, SCREENHEIGHT, BufferedImage.TYPE_INT_RGB);
while(!isActive()) Thread.yield(); Graphics g = getGraphics(); requestFocus(); do {
if (g!=null) { g.drawImage(screen, 0,0,null); } Thread.yield(); } while (isActive()); music.stop(); // Required to stop music when applet is not in foreground }
/** Process Key Event */ @Override protected void processKeyEvent(KeyEvent e) { // Key Handler ... }
}
|