// // ReadWave.java // 2008/1/30 初めて作成 // 2008/2/13 一木君にバグを指摘される。Byte は符号付きだった。 // 2008/2/15 やはりビット演算だけで記述することにした。 // import java.io.File; import javax.sound.sampled.*; public class ReadWave { private static final int EXTERNAL_BUFFER_SIZE = 128000; public static void main(String[] args) { int frameSize; // int sampleSizeInBits; // 音声データの数値のビット数 (16または8) int channels; // チャンネルの数 (2がステレオ, 1がモノラル) float sampleRate; // サンプリングレート (1秒間の...) boolean isStereo; // ステレオか boolean isBigEndian; // ビッグエンディアンか (上位バイトが先か) if (args.length == 0) System.exit(0); try { // Fileクラスのインスタンスを生成する File soundFile = new File(args[0]); // オーディオ入力ストリームを取得する AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile); // オーディオフォーマットを取得する AudioFormat audioFormat = audioInputStream.getFormat(); // データラインの情報オブジェクトを生成する DataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat); // 指定されたデータライン情報に一致するラインを取得する SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); // 指定されたオーディオ形式でラインを開きます line.open(audioFormat); // ラインでのデータ入出力を可能にします line.start(); // データの形式を読み取る channels = audioFormat.getChannels(); isStereo = (channels == 2); isBigEndian = audioFormat.isBigEndian(); frameSize = audioFormat.getFrameSize(); sampleSizeInBits = audioFormat.getSampleSizeInBits(); sampleRate = audioFormat.getSampleRate(); System.out.println("#original file: " + args[0]); System.out.println("#number of channels: " + channels); System.out.println("#sampling rate: " + sampleRate); System.out.println("#number of bits per sample: " + sampleSizeInBits); System.out.println("#FrameSize: " + frameSize); System.out.println("#isBigEndian: " + isBigEndian); if (audioFormat.getEncoding() == AudioFormat.Encoding.PCM_SIGNED) { System.out.println("#PCM Signed"); } else if (audioFormat.getEncoding() == AudioFormat.Encoding.PCM_UNSIGNED) { System.out.println("#PCM Unsigned!!!"); System.exit(0); } else { System.out.println("#NO PCM!!"); System.exit(0); } // 音声データを読み取り、鳴らして、数値を表示 int nBytesRead = 0; byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; if (isStereo) { if (sampleSizeInBits == 16) { // 16ビットステレオ (フツー) while (nBytesRead != -1) { // オーディオストリームからデータを読み込みます nBytesRead = audioInputStream.read(abData, 0, abData.length); if (nBytesRead >= 0) { // オーディオデータをミキサーに書き込みます int nBytesWritten = line.write(abData, 0, nBytesRead); for (int i = 0; i < nBytesRead; i += 4) { short left, right; left = (short)(abData[i] & 0xff | (abData[i+1] << 8)); right = (short)(abData[i+2] & 0xff | (abData[i+3] << 8)); System.out.println("" + left + " " + right); } } } } else { // sampleSizeInBits == 8 // 8ビットステレオ (フツー) while (nBytesRead != -1) { // オーディオストリームからデータを読み込みます nBytesRead = audioInputStream.read(abData, 0, abData.length); if (nBytesRead >= 0) { // オーディオデータをミキサーに書き込みます int nBytesWritten = line.write(abData, 0, nBytesRead); for (int i = 0; i < nBytesRead; i += 2) { short left, right; left = (short)(abData[i] & 0xff); right = (short)(abData[i+1] & 0xff); System.out.println("" + left + " " + right); } } } } } else { // モノラル if (sampleSizeInBits == 16) { // 16ビットモノラル while (nBytesRead != -1) { // オーディオストリームからデータを読み込みます nBytesRead = audioInputStream.read(abData, 0, abData.length); if (nBytesRead >= 0) { // オーディオデータをミキサーに書き込みます int nBytesWritten = line.write(abData, 0, nBytesRead); for (int i = 0; i < nBytesRead; i += 2) { short c; c = (short)(abData[i] & 0xff | (abData[i+1] << 8)); System.out.println("" + c); } } } } else { // sampleSizeInBits == 8 // 8ビットモノラル while (nBytesRead != -1) { // オーディオストリームからデータを読み込みます nBytesRead = audioInputStream.read(abData, 0, abData.length); if (nBytesRead >= 0) { // オーディオデータをミキサーに書き込みます int nBytesWritten = line.write(abData, 0, nBytesRead); for (int i = 0; i < nBytesRead; i++) { short c; c = (short)(abData[i] & 0xff); System.out.println("" + c); } } } } } // ラインからキューに入っているデータを排出します line.drain(); // ラインを閉じます line.close(); System.exit(0); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }