移転しました。

Javaに「ド」を再生させる

PCMを使って音程を再生させる。ちゃんとクラスを考える時間がなかったので、とりあえず実行する箇所をmainメソッドにまとめたものを貼ってメモる。

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class HelloPCM {

    public static void main(String[] args) throws LineUnavailableException {

        float sampleRate = 11025f;
        int sampleSizeInBits = 8;
        int channels = 1;
        int frameSize = 1;
        float frameRate = 11025f;
        boolean bigEndian = true;

        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
            sampleRate, sampleSizeInBits,channels,frameSize, frameRate, bigEndian);

        SourceDataLine line = null;

        try {

            line = (SourceDataLine) AudioSystem.getLine(
                new DataLine.Info(SourceDataLine.class, format));

            line.open();
            line.start();

            float key = 260.7f; // C

            byte[] buffer = new byte[(int)sampleRate];
            int rate = (int) (sampleRate / key);
            int volume = 50;

            boolean isUp = true;

            for (int i = 0; i < buffer.length; i++) {
                if (i % rate == 0) {
                    isUp = isUp ? false : true;
                }
                buffer[i] = isUp
                        ? (byte) volume
                        : (byte) (volume * -1);
            }

            line.write(buffer, 0, buffer.length);

        } finally {
            if (line != null) {
                line.close();
            }
        }

    }

}