Exemplo n.º 1
0
  public static void bang() throws LineUnavailableException, InterruptedException {
    AudioFormat af =
        new AudioFormat(
            SAMPLE_RATE, // sampleRate
            8, // sampleSizeInBits
            1, // channels
            true, // signed
            false); // bigEndian
    SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
    sdl.open(af);
    sdl.start();

    byte[] buf = new byte[1];
    Random r = new Random();
    boolean silence = true;
    for (int i = 0; i < 8000; i++) {
      while (r.nextInt() % 10 != 0) {
        buf[0] =
            silence
                ? 0
                : (byte)
                    Math.abs(
                        r.nextInt()
                            % (int) (1. + 63. * (1. + Math.cos(((double) i) * Math.PI / 8000.))));
        i++;
        sdl.write(buf, 0, 1);
      }
      silence = !silence;
    }
    sdl.drain();
    sdl.stop();
    sdl.close();
  }
Exemplo n.º 2
0
  public static void warp(int repeat) throws LineUnavailableException, InterruptedException {
    AudioFormat af =
        new AudioFormat(
            SAMPLE_RATE, // sampleRate
            8, // sampleSizeInBits
            1, // channels
            true, // signed
            false); // bigEndian
    SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
    sdl.open(af);
    sdl.start();

    byte[] buf = new byte[1];
    int step;

    for (int j = 0; j < repeat; j++) {
      step = 25;
      for (int i = 0; i < 2000; i++) {
        if (i < 500) {
          buf[0] = ((i % step > 0) ? 32 : (byte) 0);
          if (i % 25 == 0) step--;
        } else {
          buf[0] = ((i % step > 0) ? 16 : (byte) 0);
          if (i % 50 == 0) step++;
        }
        sdl.write(buf, 0, 1);
      }
    }
    sdl.drain();
    sdl.stop();
    sdl.close();
  }
Exemplo n.º 3
0
  public void deactivate() {
    active = false;
    microphone.stop();
    microphone.flush();

    speaker.stop();
    speaker.flush();
  }
Exemplo n.º 4
0
 public boolean close() {
   boolean res = true;
   try {
     sdl.stop();
     sdl.close();
   } catch (Exception e) {
     Logger.warning("Could not close or stop SoundDataLine: " + sdl);
     res = false;
   }
   opened = false;
   return res;
 }
Exemplo n.º 5
0
 public static void tone(int hz, int msecs, double vol) throws LineUnavailableException {
   byte[] buf = new byte[1];
   AudioFormat af =
       new AudioFormat(
           SAMPLE_RATE, // sampleRate
           8, // sampleSizeInBits
           1, // channels
           true, // signed
           false); // bigEndian
   SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
   sdl.open(af);
   sdl.start();
   for (int i = 0; i < msecs * 8; i++) {
     double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
     buf[0] = (byte) (Math.sin(angle) * 127.0 * vol);
     sdl.write(buf, 0, 1);
   }
   sdl.drain();
   sdl.stop();
   sdl.close();
 }
Exemplo n.º 6
0
  public void run() {
    try {
      AudioFormat audioFormat = audioInputStream.getFormat();
      DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
      SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
      sourceDataLine.open(audioFormat);
      sourceDataLine.start();

      int cnt;
      while ((cnt = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) {
        if (cnt > 0) {
          sourceDataLine.write(tempBuffer, 0, cnt);
        }
      }
      sourceDataLine.drain();
      sourceDataLine.stop();
      sourceDataLine.close();
      sourceDataLine.close();
      audioInputStream.close();
    } catch (Exception e) {
      System.out.println("jMusic AudioFilePlayThread error");
      e.printStackTrace();
    }
  }
Exemplo n.º 7
0
 /** Close standard audio. */
 public static void close() {
   line.drain();
   line.stop();
 }
 /** Write data to the source data line. */
 public void run() {
   byte[] buffer = null;
   boolean useStream = (ais != null);
   if (useStream) {
     buffer = new byte[BUFFER_SIZE];
   } else {
     buffer = audioData;
   }
   while (wantedState != STATE_STOPPING) {
     // try {
     if (wantedState == STATE_WAITING) {
       // wait for 5 seconds - maybe the clip is to be played again
       if (DEBUG || Printer.debug) Printer.debug("DataPusher.run(): waiting 5 seconds");
       try {
         synchronized (this) {
           threadState = STATE_WAITING;
           wantedState = STATE_STOPPING;
           wait(AUTO_CLOSE_TIME);
         }
       } catch (InterruptedException ie) {
       }
       if (DEBUG || Printer.debug) Printer.debug("DataPusher.run(): waiting finished");
       continue;
     }
     if (newPos >= 0) {
       pos = newPos;
       newPos = -1;
     }
     threadState = STATE_PLAYING;
     int toWrite = BUFFER_SIZE;
     if (useStream) {
       try {
         pos = 0; // always write from beginning of buffer
         // don't use read(byte[]), because some streams
         // may not override that method
         toWrite = ais.read(buffer, 0, buffer.length);
       } catch (java.io.IOException ioe) {
         // end of stream
         toWrite = -1;
       }
     } else {
       if (toWrite > audioDataByteLength - pos) {
         toWrite = audioDataByteLength - pos;
       }
       if (toWrite == 0) {
         toWrite = -1; // end of "stream"
       }
     }
     if (toWrite < 0) {
       if (DEBUG || Printer.debug) Printer.debug("DataPusher.run(): Found end of stream");
       if (!useStream && looping) {
         if (DEBUG || Printer.debug) Printer.debug("DataPusher.run(): setting pos back to 0");
         pos = 0;
         continue;
       }
       if (DEBUG || Printer.debug) Printer.debug("DataPusher.run(): calling drain()");
       wantedState = STATE_WAITING;
       source.drain();
       continue;
     }
     if (DEBUG || Printer.debug)
       Printer.debug("> DataPusher.run(): Writing " + toWrite + " bytes");
     int bytesWritten = source.write(buffer, pos, toWrite);
     pos += bytesWritten;
     if (DEBUG || Printer.debug)
       Printer.debug("< DataPusher.run(): Wrote " + bytesWritten + " bytes");
   }
   threadState = STATE_STOPPING;
   if (DEBUG || Printer.debug) Printer.debug("DataPusher: closing device");
   if (Printer.trace) Printer.trace("DataPusher: source.flush()");
   source.flush();
   if (DEBUG || Printer.trace) Printer.trace("DataPusher: source.stop()");
   source.stop();
   if (DEBUG || Printer.trace) Printer.trace("DataPusher: source.flush()");
   source.flush();
   if (DEBUG || Printer.trace) Printer.trace("DataPusher: source.close()");
   source.close();
   threadState = STATE_STOPPED;
   synchronized (this) {
     pushThread = null;
     notifyAll();
   }
   if (DEBUG || Printer.debug) Printer.debug("DataPusher:end of thread");
 }