アットウィキロゴ

音楽++

import javax.sound.sampled.*;

public class game{

int SAMPLE_RATE = 44100; // 44.1KHz
AudioFormat audio_format;
DataLine.Info info;
SourceDataLine line;  
  
public static void main(String[] args) {          
game test=new game();    
}


game(){

music();        
}


void music() {

try{
audio_format = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
        info = new DataLine.Info(
                SourceDataLine.class, audio_format);
        line = (SourceDataLine)AudioSystem.getLine(info);
        line.open();
        line.start();

        writeNote(523.25); // C
        writeNote(587.33); // D
        writeNote(659.26); // E
}
catch(Exception e){System.out.println("fail");}

}

void writeNote(double frequency) {
        byte[] b = new byte[SAMPLE_RATE];
        for (int i = 0; i < b.length; i++) {
            double r = i / (SAMPLE_RATE / frequency);
            b[i] = (byte)((Math.round(r) % 2 == 0) ? 100 : -100);
        }
        line.write(b, 0, b.length);
        line.drain();
    }
}
最終更新:2011年07月23日 19:00