2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

javaでsin波を鳴らすだけ(javax.sound.sampled.SourceDataLine)

Last updated at Posted at 2016-04-12
audio.java
import	javax.sound.sampled.*;
import	java.lang.Math;

public class Audio {
	static	final	int	freq=44100;//サンプリングレートは44100(=CD並
	static	final	double	hz=400;//音は400hz
	public	Audio(){
		try{
			AudioFormat	fmt=new	AudioFormat(freq, 8, 1, true, false);
			SourceDataLine	audio=(SourceDataLine)AudioSystem.getSourceDataLine(fmt);
			audio.open(fmt);
			byte[]	data=new	byte[freq];
			for(int i=0;i<freq;i++){
				data[i]=(byte)(127*Math.sin(2*Math.PI*hz*i/freq));
			}
			audio.start();
			audio.write(data, 0, freq);
		}catch(Exception e){
			e.printStackTrace();
			System.exit(1);
		}
	}
	public static void main(String[] args){
		new	Audio();
	}
}

audio.start()を忘れてて梃子摺った。
一サンプルあたりのバイト数をどうやれば上げられるのか分からない。
short[]とかわwrite関数が受け取ってくれないしどうしようか?

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?