Results 1 to 4 of 4
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default Getting a new DataLine.info object NOT using a wav file help?

    Hi.
    I'm trying to create a sound that should play a certain frequency (Hopefully 40 MHz)
    But Im having problems creating the DataLine.info object.

    I get this error:
    java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_UNSIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian is supported.
    At this line:
    auline = (SourceDataLine) AudioSystem.getLine(info);
    That is mainly because the format Im using is made up. Its not taking by any audioinputstream.getFormat().
    This is because I have no wav file that I wish to play.

    What I want to do is create a sound of a frequency and that is all.

    To run this just write new PlayFrequency() in a main method.
    Heres my whole code:
    PHP Code:
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
     
    
    public class PlayFrequency extends Thread { 
        private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 
        float sampleRate = 44100.0f;
        int sampleSizeInBits = 16;
        int channels = 2;
        boolean signed = false;
        boolean bigEndian = true;
        
        enum Position { 
            LEFT, RIGHT, NORMAL
        };
     
        public PlayFrequency() { 
        	this.start();
        } 
     
     
        public void run() { 
     
           
           
     
            AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); 
            SourceDataLine auline = null;
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
     
            try { 
                auline = (SourceDataLine) AudioSystem.getLine(info);
                auline.open(format);
            } catch (LineUnavailableException e) { 
                e.printStackTrace();
                return;
            } catch (Exception e) { 
                e.printStackTrace();
                return;
            } 
     
            System.out.println("Start");
            auline.start();
            
            int nBytesRead = 1000;
            byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
            boolean ok = true;
            try { 
                while (true) { 
                	nBytesRead = getFrequency(abData,5000,10000);
                	System.out.println(abData);
                    if (nBytesRead >= 0){
                    	auline.write(abData, 0, nBytesRead);
                    }
                } 
            } finally { 
                auline.drain();
                auline.close();
            } 
        } 
        
        private int getFrequency(byte[] abData, int i, int j) {
        	boolean ok = true;
        	int bytesRead = 0;
    		for(byte b : abData){
    			if (ok){ //Jumps between 2 diffrent levels creating the sound..
    				b = (byte) i;
    			} else {
    				b = (byte) j;
    			}
    			ok = !ok;
    			bytesRead++;
    		}
    		return bytesRead;
    	}
    
    
    	public void wait(int time){
    		try{
    			Thread.sleep(time);
    		} catch(Exception e){}
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    To run this just write new PlayFrequency() in a main method.
    Why didn't you provide this if its so easy? A completely self contained test program is much better.

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    After some tweeking, some flashback of recent physic classes I'v finaly found a solution:

    PHP Code:
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
     
    
    public class PlayFrequency extends Thread { 
        private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 
        float sampleRate = 44100.0f;
        int sampleSizeInBits = 16;
        int channels = 2;
        boolean signed = true;
        boolean bigEndian = false;
        int tot = 0;
        
        public static void main(String args[]){
        	new PlayFrequency();
        }
        
        enum Position { 
            LEFT, RIGHT, NORMAL
        };
     
        public PlayFrequency() { 
        	this.start();
        } 
     
     
        public void run() { 
     
           
           
     
            AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); 
            SourceDataLine auline = null;
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
     
            try { 
                auline = (SourceDataLine) AudioSystem.getLine(info);
                auline.open(format);
            } catch (LineUnavailableException e) { 
                e.printStackTrace();
                return;
            } catch (Exception e) { 
                e.printStackTrace();
                return;
            } 
     
            System.out.println("Start");
            auline.start();
            
            int nBytesRead = 1000;
            byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
            boolean ok = true;
            try { 
                while (true) { 
                	nBytesRead = abData.length;
                	abData = getFrequency(50000,90000);
                	printAbdata(abData);
                    if (nBytesRead >= 0){
                    	auline.write(abData, 0, nBytesRead);
                    }
                } 
            } finally { 
                auline.drain();
                auline.close();
            } 
        } 
        
        private void printAbdata(byte[] abData) {
        	for(int i = 0; i <10; i++){
        		byte b = abData[i];
        		System.out.print(b+"-");
        		
        	}
        	System.out.println(abData[10]);
    	}
    
    
    	private byte[] getFrequency( int low, int high) {
        	boolean ok = true;
        	byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
        	int bytesRead = 0;
        	int posX = 0;
        	
    		for(int i = 0; i <EXTERNAL_BUFFER_SIZE; i++){
    			if (ok){
    				abData[i] = -1;
    			} else {
    				abData[i] = (byte)(128*Math.sin(posX));
    			}
    			
    			ok = !ok;
    			bytesRead++;
    			posX++;
    		}
    		return abData;
    	}
    
    
    	public void wait(int time){
    		try{
    			Thread.sleep(time);
    		} catch(Exception e){}
    	}
    }
    THO I still need help.
    This only plays extremly high and annoying pitch sounds.
    How can I make the sound go from like 0 Hz to 40MHz?

  4. #4
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Epic lol.
    Just realised that this code:
    abData[i] = (byte)(128*Math.sin(posX));
    Can be replaced by:
    abData[i] = (byte)(A*Math.sin(posX*P));
    Where A is amplitude and P changes the period thus creating
    higher or lower pitches :D

    For all those reading this now who needs this code:
    Feel free to use! No restrictions whatsoever :P

Similar Threads

  1. Get info from HTML file?
    By ZeCute in forum Networking
    Replies: 0
    Last Post: 05-08-2011, 02:20 PM
  2. A problem for put the file info to arrary
    By l0afer in forum New To Java
    Replies: 11
    Last Post: 03-28-2011, 06:48 AM
  3. Replies: 0
    Last Post: 12-31-2008, 02:20 PM
  4. Replies: 9
    Last Post: 09-11-2008, 02:51 AM
  5. Replies: 5
    Last Post: 07-30-2008, 09:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •