본문 바로가기
개발

JAVA Sinewave로 소리 재생하기

by reikop.com 2013. 11. 26.

사인웨이브 만들기

 

사인웨이브 그래프


public static byte[] getSineWaveByte(int hz, float sampleRate, int volume){
  int period = (int) (sampleRate/hz);
  System.out.println(period);
  byte[] b = new byte[(int) (period)];
  for (int i=0; i < period; i++) {
   double angle = i / (sampleRate/hz) * 2.0 * Math.PI;
   b[i] = (byte)(Math.sin(angle) * 127.0 * volume);
  }
  return b;
 }

 

재생하기

public static void playbyte(byte[] bytes) throws LineUnavailableException
 {
  byte[] buf = new byte[1];
  AudioFormat af =
    new AudioFormat(
      SAMPLE_RATE, // sampleRate
      8,           // sampleSizeInBits
      1,           // channels
      true,        // signed
      false);      // bigEndian
  SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
  sdl.open(af);
  sdl.start();
  while(true){
   sdl.write(bytes,0,bytes.length);
  }
 }





'개발' 카테고리의 다른 글

계약서  (0) 2012.07.02
Java String 연산할때는 StringBuffer를 사용하자  (0) 2012.03.13