Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Synthetic sound



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:

FinalSounds


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

audioData[] = [16000*4];
AudioFormat audioFormat = getAudioFormat();





main(String[] args)
FinalSounds myProgram =
FinalSounds();
myProgram.makeGui();






makeGui()
myFrame =
JFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


centerPanel =
JPanel();


startButton =
JButton();

startButton.addActionListener(
StartListener());


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




myFrame.setLocation(100,100);
myFrame.pack();

myFrame.setVisible(
);




AudioFormat getAudioFormat()

sampleRate = 16000.0F;

sampleSizeInBits = 16;

channels = 2;

signed = ;

bigEndian = ;



AudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian);






playSound()
ByteArrayInputStream byteArrayInputStream =
ByteArrayInputStream(audioData);


audioInputStream =
AudioInputStream(byteArrayInputStream,audioFormat,

audioData.length/audioFormat.getFrameSize());
DataLine.Info dataLineInfo =
DataLine.Info(SourceDataLine.class,audioFormat);


sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);

}(Exception e)
e.printStackTrace();



PlayThread().start();








StartListener ActionListener

actionPerformed(ActionEvent e)
SimpleOsc simpleOsc =
SimpleOsc();
simpleOsc.getSound(audioData);
playSound();






PlayThread Thread

playBuffer[] = [16000];



run()


sourceDataLine.open(audioFormat);
sourceDataLine.start();



counter;

((counter = audioInputStream.read(playBuffer,0,playBuffer.length)) != -1)

(counter > 0)

sourceDataLine.write(playBuffer,0,counter);




sourceDataLine.drain();
sourceDataLine.stop();
sourceDataLine.close();

}(Exception e)
e.printStackTrace();






 
SimpleOsc
ByteBuffer byteBuffer;
//takes audioData[]
ShortBuffer shortBuffer;
//turns audioData[] into shortBuffer

byteLength; //hold length of audioData[]



getSound([] audioData)
byteBuffer = ByteBuffer.wrap(audioData);
shortBuffer = byteBuffer.asShortBuffer();


byteLength = audioData.length;


makeWave();





makeWave()

//int channels = 2;

bytesPerSample = 2;

sampleRate = 16000.0F;

sampleLength = byteLength/bytesPerSample;

( i = 0; i< sampleLength;i++)

time = i/sampleRate;

freq = 440.0;

sinWave = Math.sin(2*Math.PI*freq*time);

shortBuffer.put((
)(16000*sinWave));




 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.