Hi,
I am having trouble with this code. I am trying to create a Player class (GUI) for Oscillator classes, but the first one that I did will not work properly. It gives me a very distorted noise and I cannot control the frequency properly. Could someone have a look and see where I am going wrong?

Thanks
Jay

I have two seprate classes:

public class FinalSounds {
//---------------------------Globals------------------------------
JFrame myFrame;
JPanel centerPanel;
JButton startButton;

AudioInputStream audioInputStream; //used to handle the audio being written
SourceDataLine sourceDataLine; //audio is written to this and then is passed to the mixer
byte audioData[] = new byte[16000*4];
AudioFormat audioFormat = getAudioFormat();

//---------------------Main-------------------------------
public static void main(String[] args){
FinalSounds myProgram = new FinalSounds();
myProgram.makeGui();
}

//------------------------------Methods-----------------------
public void makeGui(){
myFrame = new JFrame("FinalSound");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);

centerPanel = new JPanel();

startButton = new JButton("Start");
startButton.addActionListener(new StartListener());

centerPanel.add(startButton);
myFrame.getContentPane().add(BorderLayout.CENTER, centerPanel);


myFrame.setLocation(100,100);
myFrame.pack();
myFrame.setVisible(true);
}

private AudioFormat getAudioFormat(){
float sampleRate = 16000.0F;
int sampleSizeInBits = 16;
int channels = 2;
boolean signed = true;
boolean bigEndian = false;

return new AudioFormat(sampleRate,sampleSizeInBits,channels,s igned,bigEndian);
}


public void playSound(){
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(audioData);

audioInputStream = new AudioInputStream(byteArrayInputStream,audioFormat,
audioData.length/audioFormat.getFrameSize());
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,audioFormat);
try{
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
}catch(Exception e){
e.printStackTrace();
}
new PlayThread().start();
}

//----------------------------ActionListeners------------------

class StartListener implements ActionListener{
public void actionPerformed(ActionEvent e){
SimpleOsc simpleOsc = new SimpleOsc();
simpleOsc.getSound(audioData);
playSound();
}
}

class PlayThread extends Thread{
byte playBuffer[] = new byte[16000];

public void run(){
try{
sourceDataLine.open(audioFormat);
sourceDataLine.start();

int counter;
while((counter = audioInputStream.read(playBuffer,0,playBuffer.leng th)) != -1){
if(counter > 0){
sourceDataLine.write(playBuffer,0,counter);
}
}
sourceDataLine.drain();
sourceDataLine.stop();
sourceDataLine.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
*
public class SimpleOsc {
ByteBuffer byteBuffer; //takes audioData[]
ShortBuffer shortBuffer; //turns audioData[] into shortBuffer
int byteLength; //hold length of audioData[]

public void getSound(byte[] audioData){
byteBuffer = ByteBuffer.wrap(audioData);
shortBuffer = byteBuffer.asShortBuffer();

byteLength = audioData.length;

makeWave();
}

public void makeWave(){
//int channels = 2;
int bytesPerSample = 2;
float sampleRate = 16000.0F;
int sampleLength = byteLength/bytesPerSample;
for(int i = 0; i< sampleLength;i++){
double time = i/sampleRate;
double freq = 440.0;
double sinWave = Math.sin(2*Math.PI*freq*time);
shortBuffer.put((short)(16000*sinWave));
}
}
}