Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import javax.sound.sampled.AudioFormat;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.SourceDataLine;
- public class Main
- {
- private static void playStuff()
- {
- melody.play(NOTE_A3);
- harmony.play(NOTE_CS4);
- delay(140);
- melody.stop();
- harmony.stop();
- delay(10);
- melody.play(NOTE_A3);
- harmony.play(NOTE_CS4);
- delay(290);
- melody.stop();
- harmony.stop();
- delay(10);
- melody.play(NOTE_A3);
- harmony.play(NOTE_CS4);
- delay(290);
- melody.stop();
- harmony.stop();
- delay(10);
- melody.play(NOTE_A3);
- harmony.play(NOTE_CS4);
- delay(290);
- melody.stop();
- harmony.stop();
- delay(10);
- melody.play(NOTE_A3);
- harmony.play(NOTE_CS4);
- delay(140);
- melody.stop();
- harmony.stop();
- delay(10);
- melody.play(NOTE_A3);
- harmony.play(NOTE_CS4);
- delay(290);
- melody.stop();
- harmony.stop();
- delay(10);
- melody.play(NOTE_GS3);
- harmony.play(NOTE_B3);
- delay(290);
- melody.stop();
- harmony.stop();
- delay(10);
- melody.play(NOTE_CS4);
- harmony.play(NOTE_E4);
- delay(290);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_FS4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_E4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_CS4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_B3);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_CS4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_FS4);
- delay(290);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_E4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_B3);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_CS4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_FS4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_E4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_CS4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_B3);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_CS4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_FS4);
- delay(140);
- harmony.stop();
- delay(10);
- harmony.play(NOTE_E4);
- delay(290);
- harmony.stop();
- delay(10);
- melody.stop();
- harmony.stop();
- }
- public static Music melody;
- public static Music harmony;
- public static void main(String[] args) throws LineUnavailableException
- {
- System.out.println("Compiling...");
- melody = new Music(0);
- harmony = new Music(1);
- playStuff();
- System.out.println("Playing...");
- melody.close();
- harmony.close();
- }
- public static void delay(int ms)
- {
- harmony.delay(ms);
- melody.delay(ms);
- }
- static int NOTE_B0 = 31;
- static int NOTE_C1 = 33;
- static int NOTE_CS1 = 35;
- static int NOTE_D1 = 37;
- static int NOTE_DS1 = 39;
- static int NOTE_E1 = 41;
- static int NOTE_F1 = 44;
- static int NOTE_FS1 = 46;
- static int NOTE_G1 = 49;
- static int NOTE_GS1 = 52;
- static int NOTE_A1 = 55;
- static int NOTE_AS1 = 58;
- static int NOTE_B1 = 62;
- static int NOTE_C2 = 65;
- static int NOTE_CS2 = 69;
- static int NOTE_D2 = 73;
- static int NOTE_DS2 = 78;
- static int NOTE_E2 = 82;
- static int NOTE_F2 = 87;
- static int NOTE_FS2 = 93;
- static int NOTE_G2 = 98;
- static int NOTE_GS2 = 104;
- static int NOTE_A2 = 110;
- static int NOTE_AS2 = 117;
- static int NOTE_B2 = 123;
- static int NOTE_C3 = 131;
- static int NOTE_CS3 = 139;
- static int NOTE_D3 = 147;
- static int NOTE_DS3 = 156;
- static int NOTE_E3 = 165;
- static int NOTE_F3 = 175;
- static int NOTE_FS3 = 185;
- static int NOTE_G3 = 196;
- static int NOTE_GS3 = 208;
- static int NOTE_A3 = 220;
- static int NOTE_AS3 = 233;
- static int NOTE_B3 = 247;
- static int NOTE_C4 = 262;
- static int NOTE_CS4 = 277;
- static int NOTE_D4 = 294;
- static int NOTE_DS4 = 311;
- static int NOTE_E4 = 330;
- static int NOTE_F4 = 349;
- static int NOTE_FS4 = 370;
- static int NOTE_G4 = 392;
- static int NOTE_GS4 = 415;
- static int NOTE_A4 = 440;
- static int NOTE_AS4 = 466;
- static int NOTE_B4 = 494;
- static int NOTE_C5 = 523;
- static int NOTE_CS5 = 554;
- static int NOTE_D5 = 587;
- static int NOTE_DS5 = 622;
- static int NOTE_E5 = 659;
- static int NOTE_F5 = 698;
- static int NOTE_FS5 = 740;
- static int NOTE_G5 = 784;
- static int NOTE_GS5 = 831;
- static int NOTE_A5 = 880;
- static int NOTE_AS5 = 932;
- static int NOTE_B5 = 988;
- static int NOTE_C6 = 1047;
- static int NOTE_CS6 = 1109;
- static int NOTE_D6 = 1175;
- static int NOTE_DS6 = 1245;
- static int NOTE_E6 = 1319;
- static int NOTE_F6 = 1397;
- static int NOTE_FS6 = 1480;
- static int NOTE_G6 = 1568;
- static int NOTE_GS6 = 1661;
- static int NOTE_A6 = 1760;
- static int NOTE_AS6 = 1865;
- static int NOTE_B6 = 1976;
- static int NOTE_C7 = 2093;
- static int NOTE_CS7 = 2217;
- static int NOTE_D7 = 2349;
- static int NOTE_DS7 = 2489;
- static int NOTE_E7 = 2637;
- static int NOTE_F7 = 2794;
- static int NOTE_FS7 = 2960;
- static int NOTE_G7 = 3136;
- static int NOTE_GS7 = 3322;
- static int NOTE_A7 = 3520;
- static int NOTE_AS7 = 3729;
- static int NOTE_B7 = 3951;
- static int NOTE_C8 = 4186;
- static int NOTE_CS8 = 4435;
- static int NOTE_D8 = 4699;
- static int NOTE_DS8 = 4978;
- }
- class Music
- {
- final AudioFormat af = new AudioFormat(Note.SAMPLE_RATE, 8, 2, true, true);
- SourceDataLine line = null;
- Note currentNote = new Note();
- byte[] song = new byte[0];
- int side;
- public Music(int side)
- {
- this.side = side;
- try {
- line = AudioSystem.getSourceDataLine(af);
- line.open(af, Note.SAMPLE_RATE);
- } catch (LineUnavailableException e) {
- e.printStackTrace();
- }
- line.start();
- }
- public void play(final int freq)
- {
- currentNote = new Note(freq);
- }
- public void delay(int ms)
- {
- int length = Note.SAMPLE_RATE * ms / 1000;
- byte[] newSong = Arrays.copyOf(song, song.length + length * 2);
- byte[] newData = currentNote.data();
- for (int i = 0; i < length * 2; ++i) {
- int pos = i / 2;
- if (i % 2 == side) {
- newSong[song.length + i] = newData[pos];
- } else {
- newSong[song.length + i] = 0;
- }
- }
- song = newSong;
- }
- public void stop()
- {
- // line.flush();
- currentNote = new Note();
- }
- public void close()
- {
- new Thread()
- {
- public void run()
- {
- line.write(song, 0, song.length);
- line.drain();
- line.stop();
- line.flush();
- line.close();
- }
- }.start();
- }
- }
- class Note
- {
- public static final int SAMPLE_RATE = 16 * 1024; // ~16KHz
- public static final int MAX_SECONDS = 15;
- private byte[] wave = new byte[MAX_SECONDS * SAMPLE_RATE];
- Note()
- {
- }
- Note(double f)
- {
- for (int i = 0; i < wave.length; i++) {
- double period = (double) SAMPLE_RATE / f;
- double angle = 2.0 * Math.PI * i / period;
- // Sin
- // wave[i] = (byte) (Math.sin(angle) * 127f);
- // Square
- wave[i] = (byte) ((Math.sin(angle) > 0 ? 1 : 0) * 127f);
- }
- }
- public byte[] data()
- {
- return wave;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement