I'm really confused about why I get a 0kb file when I use that code showing below. As the instruction of Java website about this class, I should be worked. but... And I want to generate a sine wave and output its result in to a txt fill in double. That is the first step of my code, and I'm stuck in such simple problem. Maybe I was not pretty understand how to use class file and datastream as I learned from offical website.
public class audioplayThread implements Runnable {
private File file;
private FileOutputStream ops;
private BufferedOutputStream bos;
private DataOutputStream dos;
private double Omega;
private int f = 18*1000;
private double pi;
private int samplenumber = 84; //Assume OFDM symbol has 64 real value and
private static final int Encording = AudioFormat.ENCODING_PCM_16BIT; //Data size for each frame = 16 bytes
private static final int Sample_rate = 48000; //Sample rate = 48000 HZ
private static final int Channel = AudioFormat.CHANNEL_OUT_MONO; //Set as single track
private static final int Buffersize = AudioTrack.getMinBufferSize(Sample_rate, Channel,Encording);
@Override
public void run() {
// TODO Auto-generated method stub
file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(),"mmm.txt");
if(file.exists()){
file.delete();
}
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
/*Create a datastream*/
ops = new FileOutputStream(file);
bos = new BufferedOutputStream(ops);
dos = new DataOutputStream(bos);
/*Set sine wave parameter*/
pi = Math.PI;
Omega = 2*pi*f/Sample_rate;
/*Build instance for audiotrack*/
//AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC,Sample_rate, Channel, Encording, Buffersize, AudioTrack.MODE_STREAM);
/*build sine wave*/
double[] buffer = new double[samplenumber];
for(int i=0;i<samplenumber;i++){
buffer[i] = Math.sin(Omega*i);
dos.writeDouble(buffer[i]);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}