/* * MakeWaveFile.java --- 自分で作成した数値データで WAVE ファイルを作る * version 1 (2008/3/5) by mk * version 1.1 (2008/3/7) by mk * 参考: http://okwave.jp/qa1942478.html http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/javax/sound/sampled/AudioSystem.html */ import java.io.IOException; import java.io.File; import java.io.InputStream; import java.io.ByteArrayInputStream; import javax.sound.sampled.*; public class MakeWaveFile { public static void main(String[] args) throws IOException { // 時間 (秒数) int t = 3; // 音のPCMデータ (44.1kHz, 量子化ビット数16, ステレオ, t=3秒) byte[] data = new byte[44100 * 2 * 2 * t]; // 適当なデータ (良い子は自分が考えるデータに変えてみよう) for (int i = 0; i < data.length; i++) { data[i] = (byte) (((i & (1 << 6)) == 0) ? -18 : 18); } // オーディオフォーマットを決める (44.1kHz, 16bit, stereo) AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, // サンプル・レート 16, // サンプルのビット数 2, // チャンネルの数 4, // フレームのサイズ(単位バイト) 44100.0F, // フレーム・レート false); // ビッグエンディアンか? // ByteArrayInputStream を AudioInputStream にする InputStream in = new ByteArrayInputStream(data); AudioInputStream ais = new AudioInputStream(in, audioFormat, 44100 * t); //フレームの数 // オーディオファイルの種類を指定 AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE; // 出力ファイル名 File outputFile = new File("test.wav"); // えいっ、と書く AudioSystem.write(ais, targetType, outputFile); } }