import javax.sound.sampled.LineUnavailableException; /** * @author: Sergej Borissov * @title: Tone Generator * @version: Prototyp Beta Version: 1.0.0.0 * * */ public class Main { public static void main(String[] args) throws LineUnavailableException, InterruptedException { AuswahlToene auswahlToene = new AuswahlToene(); ZufallsZahlenGenerator zahlen = new ZufallsZahlenGenerator(); TonQuelle tonQuelle = new TonQuelle(); TestTon testTon = new TestTon(); TonBass tonBass = new TonBass(); // tonBass.playBass2(); auswahlToene.spieleTon(); //testTon.playToneTest(); } } package loopExperiment; import javax.sound.sampled.LineUnavailableException; public class AuswahlToene { TonQuelle tonQuelle = new TonQuelle(); ZufallsZahlenGenerator zahlenGenerator = new ZufallsZahlenGenerator(); public void spieleTon() throws LineUnavailableException, InterruptedException { for (int i = 0; i < 10; i++) { switch (zahlenGenerator.gebeZufallsZahl()) { case 0: System.out.println("Spiele Ton 0"); tonQuelle.playTone3(); break; case 1: System.out.println("Spiele Ton 1"); tonQuelle.playTone1(); break; case 2: tonQuelle.playTone2(); System.out.println("Ton2"); break; case 3: System.out.println("Spiele Ton 3"); tonQuelle.playTone4(); case 4: System.out.println("Spiele Ton 4"); tonQuelle.playTone7(); break; case 5: System.out.println("Spiele Ton 5"); tonQuelle.playTone5(); break; case 6: System.out.println("Spiele Ton 6"); tonQuelle.playTone6(); break; case 7: System.out.println("Spiele Ton 7"); tonQuelle.playTone8(); break; case 8: System.out.println("SPiele ton 9"); tonQuelle.playTone9(); break; default: System.out.println("Über dem Zahlenbereich"); break; } } } } import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; public class TestTon { public void playToneTest() throws LineUnavailableException, InterruptedException { byte[] buf = new byte[ 1 ];; AudioFormat audioFormat = new AudioFormat( (float )44100, 8, 1, true, false ); SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine( audioFormat ); sourceDataLine.open(); sourceDataLine.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 40 ) * 2.0 * Math.PI; buf[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sourceDataLine.write( buf, 0, 1 ); } sourceDataLine.drain(); sourceDataLine.stop(); } } import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; public class TonBass { public void playBass1() throws LineUnavailableException, InterruptedException { for (int x = 0; x < 10; x++) { byte[] buf2 = new byte[1]; AudioFormat af2 = new AudioFormat((float) 45000, 8, 1, true, false); SourceDataLine sd2 = AudioSystem.getSourceDataLine(af2); sd2.open(); sd2.start(); for (int i = 0; i < 1000 * (float) 44100 / 1000; i++) { double angle = i / ((float) 44100 / 100) * 1.5 * Math.PI; buf2[0] = (byte) (Math.sin(angle) * 100); sd2.write(buf2, 0, 1); } sd2.drain(); sd2.stop(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } public void playBass2() throws LineUnavailableException, InterruptedException { for (int x = 0; x < 10; x++) { byte[] buf2 = new byte[1]; AudioFormat af2 = new AudioFormat((float) 45000, 8, 1, true, false); SourceDataLine sd2 = AudioSystem.getSourceDataLine(af2); sd2.open(); sd2.start(); for (int i = 0; i < 1000 * (float) 44100 / 1000; i++) { double angle = i / ((float) 44100 / 100) * 1.7 * Math.PI; buf2[0] = (byte) (Math.sin(angle) * 100); sd2.write(buf2, 0, 1); } sd2.drain(); sd2.stop(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; public class TonQuelle { public void playTone1() throws LineUnavailableException { byte[] buf3 = new byte[ 1 ];; AudioFormat af3 = new AudioFormat( (float )38000, 8, 1, true, false ); SourceDataLine sd3 = AudioSystem.getSourceDataLine( af3 ); sd3.open(); sd3.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 229) * 2.0 * Math.PI; buf3[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sd3.write( buf3, 0, 1 ); } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } public void playTone2() throws LineUnavailableException { byte[] buf2 = new byte[ 1 ];; AudioFormat af2 = new AudioFormat( (float )44100, 8, 1, true, false ); SourceDataLine sd2 = AudioSystem.getSourceDataLine( af2 ); sd2.open(); sd2.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 277 ) * 2.0 * Math.PI; buf2[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sd2.write( buf2, 0, 1 ); } sd2.drain(); sd2.stop(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } public void playTone3() throws LineUnavailableException { byte[] buf3 = new byte[ 1 ];; AudioFormat af3 = new AudioFormat( (float )38000, 8, 1, true, false ); SourceDataLine sd3 = AudioSystem.getSourceDataLine( af3 ); sd3.open(); sd3.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 220 ) * 2.0 * Math.PI; buf3[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sd3.write( buf3, 0, 1 ); } sd3.drain(); sd3.stop(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } public void playTone4() throws LineUnavailableException { byte[] buf3 = new byte[ 1 ];; AudioFormat af3 = new AudioFormat( (float )38000, 8, 1, true, false ); SourceDataLine sd3 = AudioSystem.getSourceDataLine( af3 ); sd3.open(); sd3.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 180 ) * 2.0 * Math.PI; buf3[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sd3.write( buf3, 0, 1 ); } sd3.drain(); sd3.stop(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } public void playTone5() throws LineUnavailableException { byte[] buf2 = new byte[ 1 ];; AudioFormat af2 = new AudioFormat( (float )54100, 8, 1, true, false ); SourceDataLine sd2 = AudioSystem.getSourceDataLine( af2 ); sd2.open(); sd2.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 330 ) * 2.0 * Math.PI; buf2[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sd2.write( buf2, 0, 1 ); } sd2.drain(); sd2.stop(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } public void playTone6() throws LineUnavailableException { byte[] buf3 = new byte[ 1 ];; AudioFormat af3 = new AudioFormat( (float )38000, 8, 1, true, false ); SourceDataLine sd3 = AudioSystem.getSourceDataLine( af3 ); sd3.open(); sd3.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 120 ) * 2.0 * Math.PI; buf3[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sd3.write( buf3, 0, 1 ); } sd3.drain(); sd3.stop(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } public void playTone7() throws LineUnavailableException { byte[] buf2 = new byte[ 1 ];; AudioFormat af2 = new AudioFormat( (float )74100, 8, 1, true, false ); SourceDataLine sd2 = AudioSystem.getSourceDataLine( af2 ); sd2.open(); sd2.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 139 ) * 2.0 * Math.PI; buf2[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sd2.write( buf2, 0, 1 ); } sd2.drain(); sd2.stop(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } public void playTone8() throws LineUnavailableException { byte[] buf2 = new byte[ 1 ];; AudioFormat af2 = new AudioFormat( (float )74100, 8, 1, true, false ); SourceDataLine sd2 = AudioSystem.getSourceDataLine( af2 ); sd2.open(); sd2.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 333 ) * 2.0 * Math.PI; buf2[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sd2.write( buf2, 0, 1 ); } sd2.drain(); sd2.stop(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } public void playTone9() throws LineUnavailableException { byte[] buf2 = new byte[ 1 ];; AudioFormat af2 = new AudioFormat( (float )45000, 8, 1, true, false ); SourceDataLine sd2 = AudioSystem.getSourceDataLine( af2 ); sd2.open(); sd2.start(); for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) { double angle = i / ( (float )44100 / 120 ) * 3.0 * Math.PI; buf2[ 0 ] = (byte )( Math.sin( angle ) * 100 ); sd2.write( buf2, 0, 1 ); } sd2.drain(); sd2.stop(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public class ZufallsZahlenGenerator { public int gebeZufallsZahl(){ Random random = new Random(); return random.nextInt(9); } }