NeighborBlaster, die Waffe gegen den Nachbarsfeind!

Habt ihr auch so liebe Nachbarn, die einen stundenlang mit übelsten Bässen aus überdimensionierten Subwoofern den letzten Nerv / Schlaf rauben? Und das obwohl sie schon x mal vom Vermieter angesprochen, abgemahnt, etc. wurden?
Aus lauter Wut entstand gestern abend folgender einfacher -aber effektiver :-) – Tongenerator.

Der Tongenerator spielt einen Dauerton ab (im Beispielcode ekelhafte 3500Hz). Der Dauerton ändert auch ständig seine Lautstärke um den Nachbar-NervFaktor (N-NF) zu verdoppeln :-D

import java.util.logging.Level;
  1. import java.util.logging.Logger;
  2. import javax.sound.sampled.AudioFormat;
  3. import javax.sound.sampled.AudioSystem;
  4. import javax.sound.sampled.LineUnavailableException;
  5. import javax.sound.sampled.SourceDataLine;
  6.  
  7. /**
  8.  *
  9.  * @author Michael Kabdebo
  10.  */
  11. public class NeighborBlaster {
  12.  
  13.     /** Bandbreite in Hz*/
  14.     private static final float FREQUENCY = 44100;
  15.     /** Tonlage in Hz*/
  16.     private static final int HERTZ = 3500;
  17.     private AudioFormat af;
  18.     private SourceDataLine sdl;
  19.     private Thread thread;
  20.     private boolean isAlive;
  21.     private int volume;
  22.  
  23.     public NeighborBlaster() {
  24.         volume = 80;
  25.         af = new AudioFormat(FREQUENCY, 8, 1, true, false);
  26.         try {
  27.             sdl = AudioSystem.getSourceDataLine(af);
  28.             sdl.open(af);
  29.             sdl.start();
  30.  
  31.         } catch (LineUnavailableException ex) {
  32.             Logger.getLogger(NeighborBlaster.class.getName()).log(Level.SEVERE, null, ex);
  33.             System.exit(-1);
  34.         }
  35.     }
  36.  
  37.     private void initThread() {
  38.         thread = new Thread(new Runnable() {
  39.  
  40.             public void run() {
  41.                 isAlive = true;
  42.                 byte[] buf = new byte[1];
  43.  
  44.                 while (isAlive) {
  45.                     for (int i = 0; i < 1000 * FREQUENCY / 1000; i++) {
  46.                         double angle = i / (FREQUENCY / HERTZ) * 2.0 * Math.PI;
  47.                         buf[0] = (byte) (Math.sin(angle) * (volume * 1.28));
  48.                         sdl.write(buf, 0, 1);
  49.                     }
  50.  
  51.                 }
  52.                 sdl.drain();
  53.                 sdl.stop();
  54.                 sdl.close();
  55.             }
  56.         });
  57.     }
  58.  
  59.     private void stop() {
  60.         isAlive = false;
  61.     }
  62.  
  63.     public void start() throws LineUnavailableException {
  64.         initThread();
  65.         thread.start();
  66.     }
  67.  
  68.     public void setVolume(int volume) {
  69.         this.volume = volume;
  70.     }
  71.  
  72.     public int getVolume() {
  73.         return volume;
  74.     }
  75.  
  76.     public static void main(String[] args) throws LineUnavailableException, InterruptedException {
  77.         NeighborBlaster nb = new NeighborBlaster();
  78.         nb.start();
  79.         int direction = 1;
  80.         while (true) {
  81.  
  82.             // schwankende Lautstärke
  83.             if (nb.getVolume() &lt;= 20) {
  84.                 direction = 1;
  85.             }
  86.             if (nb.getVolume() &gt;= 100) {
  87.                 direction = -1;
  88.             }
  89.             nb.setVolume(nb.getVolume() + direction);
  90.             Thread.sleep(100);
  91.         }
  92.     }
  93. }