Results 1 to 4 of 4
- 06-02-2011, 07:23 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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:
At this line: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.
That is mainly because the format Im using is made up. Its not taking by any audioinputstream.getFormat().auline = (SourceDataLine) AudioSystem.getLine(info);
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){} } }
- 06-02-2011, 07:33 PM #2
Why didn't you provide this if its so easy? A completely self contained test program is much better.To run this just write new PlayFrequency() in a main method.
- 06-02-2011, 07:47 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
After some tweeking, some flashback of recent physic classes I'v finaly found a solution:
THO I still need help.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){} } }
This only plays extremly high and annoying pitch sounds.
How can I make the sound go from like 0 Hz to 40MHz?
- 06-02-2011, 07:50 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Epic lol.
Just realised that this code:
Can be replaced by:abData[i] = (byte)(128*Math.sin(posX));
Where A is amplitude and P changes the period thus creatingabData[i] = (byte)(A*Math.sin(posX*P));
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
-
Get info from HTML file?
By ZeCute in forum NetworkingReplies: 0Last Post: 05-08-2011, 02:20 PM -
A problem for put the file info to arrary
By l0afer in forum New To JavaReplies: 11Last Post: 03-28-2011, 06:48 AM -
Concurrent timers are not getting invoked for same timer info (Serializable object co
By Neeraj in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-31-2008, 02:20 PM -
Anyone know how to take info from a txt file and convert it to a char array?
By 2potatocakes in forum New To JavaReplies: 9Last Post: 09-11-2008, 02:51 AM -
How do you read from a file, and then store the info in an array?
By szimme101 in forum New To JavaReplies: 5Last Post: 07-30-2008, 09:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks