001package org.hl7.fhir.utilities;
002
003import javax.sound.sampled.AudioFormat;
004import javax.sound.sampled.AudioSystem;
005import javax.sound.sampled.SourceDataLine;
006
007/**
008 * Utiltities for working with system audio
009 */
010public class SoundUtilities {
011
012  /*
013   * Note: this functionality was removed from Utilities.java in order to prevent users of that
014   * class from needing to import unneeded libraries (sound, saxon, etc.)
015   */
016
017  // http://stackoverflow.com/questions/3780406/how-to-play-a-sound-alert-in-a-java-application
018  public static float SAMPLE_RATE = 8000f;
019  
020  public static void tone(int hz, int msecs) {
021      tone(hz, msecs, 1.0);
022   }
023
024  public static void tone(int hz, int msecs, double vol) {
025    try {
026      byte[] buf = new byte[1];
027      AudioFormat af = 
028          new AudioFormat(
029              SAMPLE_RATE, // sampleRate
030              8,           // sampleSizeInBits
031              1,           // channels
032              true,        // signed
033              false);      // bigEndian
034      SourceDataLine sdl;
035      sdl = AudioSystem.getSourceDataLine(af);
036      sdl.open(af);
037      sdl.start();
038      for (int i=0; i < msecs*8; i++) {
039        double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
040        buf[0] = (byte)(Math.sin(angle) * 127.0 * vol);
041        sdl.write(buf,0,1);
042      }
043      sdl.drain();
044      sdl.stop();
045      sdl.close();
046    } catch (Exception e) {
047    }
048  }
049
050
051}