Java beep sound example 2013

View previous topic View next topic Go down

Java beep sound example 2013

Post by Admin on Sun Sep 15, 2013 4:03 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on">There are different ways to generate a beep sound in java. The general basic way to generate is to use java.awt.Toolkit class which has a default method to generate the beep sound. The other implementation is also shown below on how to generate beep sound in java. <br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package test; <br /> import java.awt.Toolkit; <br /> import javax.sound.sampled.AudioFormat; <br /> import javax.sound.sampled.AudioSystem; <br /> import javax.sound.sampled.LineUnavailableException; <br /> import javax.sound.sampled.SourceDataLine; <br /> public class Beep { <br /> public static float SAMPLE_RATE = 8000f; <br /> public static void tone(int hz, int msecs) throws LineUnavailableException { <br /> tone(hz, msecs, 1.0); <br /> } <br /> public static void tone(int hz, int msecs, double vol) <br /> throws LineUnavailableException { <br /> byte[] buf = new byte[1]; <br /> AudioFormat af = new AudioFormat(SAMPLE_RATE, // sampleRate <br /> 8, // sampleSizeInBits <br /> 1, // channels <br /> true, // signed <br /> false); // bigEndian <br /> SourceDataLine sdl = AudioSystem.getSourceDataLine(af); <br /> sdl.open(af); <br /> sdl.start(); <br /> for (int i = 0; i &lt; msecs * 8; i++) { <br /> double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI; <br /> buf[0] = (byte) (Math.sin(angle) * 127.0 * vol); <br /> sdl.write(buf, 0, 1); <br /> } <br /> sdl.drain(); <br /> sdl.stop(); <br /> sdl.close(); <br /> } <br /> public static void main(String[] args) throws Exception { <br /> // Normal Beep sound <br /> Toolkit.getDefaultToolkit().beep(); <br /> Beep.tone(1000, 100); <br /> Thread.sleep(1000); <br /> Beep.tone(100, 1000); <br /> Thread.sleep(1000); <br /> Beep.tone(5000, 100); <br /> Thread.sleep(1000); <br /> Beep.tone(400, 500); <br /> Thread.sleep(1000); <br /> Beep.tone(400, 500, 0.2); <br /> } <br /> } <br /></code></pre></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on">There are different ways to generate a beep sound in java. The general basic way to generate is to use java.awt.Toolkit class which has a default method to generate the beep sound. The other implementation is also shown below on how to generate beep sound in java. <br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package test; <br /> import java.awt.Toolkit; <br /> import javax.sound.sampled.AudioFormat; <br /> import javax.sound.sampled.AudioSystem; <br /> import javax.sound.sampled.LineUnavailableException; <br /> import javax.sound.sampled.SourceDataLine; <br /> public class Beep { <br /> public static float SAMPLE_RATE = 8000f; <br /> public static void tone(int hz, int msecs) throws LineUnavailableException { <br /> tone(hz, msecs, 1.0); <br /> } <br /> public static void tone(int hz, int msecs, double vol) <br /> throws LineUnavailableException { <br /> byte[] buf = new byte[1]; <br /> AudioFormat af = new AudioFormat(SAMPLE_RATE, // sampleRate <br /> 8, // sampleSizeInBits <br /> 1, // channels <br /> true, // signed <br /> false); // bigEndian <br /> SourceDataLine sdl = AudioSystem.getSourceDataLine(af); <br /> sdl.open(af); <br /> sdl.start(); <br /> for (int i = 0; i &lt; msecs * 8; i++) { <br /> double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI; <br /> buf[0] = (byte) (Math.sin(angle) * 127.0 * vol); <br /> sdl.write(buf, 0, 1); <br /> } <br /> sdl.drain(); <br /> sdl.stop(); <br /> sdl.close(); <br /> } <br /> public static void main(String[] args) throws Exception { <br /> // Normal Beep sound <br /> Toolkit.getDefaultToolkit().beep(); <br /> Beep.tone(1000, 100); <br /> Thread.sleep(1000); <br /> Beep.tone(100, 1000); <br /> Thread.sleep(1000); <br /> Beep.tone(5000, 100); <br /> Thread.sleep(1000); <br /> Beep.tone(400, 500); <br /> Thread.sleep(1000); <br /> Beep.tone(400, 500, 0.2); <br /> } <br /> } <br /></code></pre></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum