Mailing Lists: Apple Mailing Lists

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

Re: Synthetic sound



Well Mail.app read it ok but for those that can't here is the plain text version...

On Mar 15, 2005, at 1:38 PM, Jason Smalridge wrote:
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_CLOSE);

                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,signed,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.length)) != -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));
                }
        }
}
_______________________________________________
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
References: 
 >Synthetic sound (From: Jason Smalridge <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.