Package edu.purdue.wind.util

Source Code of edu.purdue.wind.util.SineWaveGenerator

package edu.purdue.wind.util;

import edu.purdue.wind.Wave;
import edu.purdue.wind.SineGenerator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class SineWaveGenerator {
    static final int SAMPLE_RATE = 48000;
    static final short SAMPLE_BITS = 16;

    public static void main(String[] args) throws IOException {
  if (args.length != 2 && args.length != 3) {
      System.err.println("usage: SineWaveGenerator <file> <frequency> [duration]");
      System.exit(1);
  }

  File file = new File(args[0]);
  double freq = Double.parseDouble(args[1]);

  if (freq <= 0.0 || freq >= 20000) {
      System.err.println("Frequency must be between 0 Hz and 20 kHz, specified in Hz");
  }

  double duration = 30.0d;
  if (args.length == 3) {
      duration = Double.parseDouble(args[2]);
  }

  Wave wave = new Wave((short)1, SAMPLE_BITS, SAMPLE_RATE);
  int[][] samples = new int[1][(int)(SAMPLE_RATE * duration)];

  SineGenerator generator = new SineGenerator(SAMPLE_RATE,
                (int)SAMPLE_BITS,
                1, freq);
  generator.generate(samples);

  wave.setSampleData(samples);

  FileOutputStream fos = new FileOutputStream(file);
  wave.writeFile(fos);
  fos.close();
    }
}
TOP

Related Classes of edu.purdue.wind.util.SineWaveGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.