사인웨이브 만들기
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);
}
}
'개발' 카테고리의 다른 글
Failed to initialize NVML: Driver/library version mismatch (0) | 2023.08.08 |
---|---|
superset docker-compose 및 kakao oauth (0) | 2023.06.22 |
계약서 (0) | 2012.07.02 |
Java String 연산할때는 StringBuffer를 사용하자 (0) | 2012.03.13 |