public void run(String local, String remote, InputStream pin, OutputStream pout) { try { microphone = SoundMixerEnumerator.getInputLine(pcmformat, DefaultPhonePCMBlockSize); } catch (LineUnavailableException lue) { System.out.println( "\3b" + getClass().getName() + ".<init>:\n\tCould not create microphone input stream.\n\t" + lue); } try { speaker = SoundMixerEnumerator.getOutputLine(pcmformat, DefaultPhonePCMBlockSize); } catch (LineUnavailableException lue) { microphone.close(); System.out.println( "\3b" + getClass().getName() + ".<init>:\n\tCould not create speaker output stream.\n\t" + lue); } if ((speaker == null) || (microphone == null)) { super.run(local, remote, pin, pout); return; } try { recorder = new Recorder(pout); recorder.start(); gui = openMonitorGUI("Remote " + remote + " Local " + local); pin.skip(pin.available()); // waste whatever we couldn't process in time super.run(local, remote, new PhoneCallMonitorInputStream(pin), pout); recorder.interrupt(); gui.dispose(); } catch (Exception e) { System.out.println("3\b" + getClass().getName() + ".run:\n\t" + e); e.printStackTrace(); } finally { deactivate(); microphone.close(); speaker.close(); if (gui != null) { gui.dispose(); } } }
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(); }
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(); }
public void run() { try { AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile); AudioFormat format = ais.getFormat(); // System.out.println("Format: " + format); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info); source.open(format); source.start(); int read = 0; byte[] audioData = new byte[16384]; while (read > -1) { read = ais.read(audioData, 0, audioData.length); if (read >= 0) { source.write(audioData, 0, read); } } donePlaying = true; source.drain(); source.close(); } catch (Exception exc) { System.out.println("error: " + exc.getMessage()); exc.printStackTrace(); } }
public void play(InputStream source) { int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10); byte[] buffer = new byte[bufferSize]; SourceDataLine line; try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, bufferSize); } catch (LineUnavailableException e) { e.printStackTrace(); return; } line.start(); try { int numBytesRead = 0; while (numBytesRead != -1) { numBytesRead = source.read(buffer, 0, buffer.length); if (numBytesRead != -1) line.write(buffer, 0, numBytesRead); } } catch (IOException e) { e.printStackTrace(); } line.drain(); line.close(); }
/** Signals that a PooledThread has stopped. Drains and closes the Thread's Line. */ protected void threadStopped() { SourceDataLine line = (SourceDataLine) localLine.get(); if (line != null) { line.drain(); line.close(); } }
@Override public void onInactive() { if (audioLine != null) { audioLine.flush(); audioLine.close(); audioLine = null; } }
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; }
@Override public void onActive() { if (audioLine != null) audioLine.close(); try { if (player.getMixer() != null) { log.debug("Custom mixer " + player.getMixer().getName()); audioLine = AudioSystem.getSourceDataLine(PCM, player.getMixer()); } else { audioLine = AudioSystem.getSourceDataLine(PCM); } audioLine.open(PCM, 1048576); onVolumeChanged(player.getVolume()); if (isMuted && audioLine.isControlSupported(BooleanControl.Type.MUTE)) ((BooleanControl) audioLine.getControl(BooleanControl.Type.MUTE)).setValue(true); } catch (LineUnavailableException e) { log.error("onActive error", e); } }
// 播放au,aiff,wav音乐流, 这个函数基本完全为帖子上的代码 private synchronized void play() { ByteArrayInputStream aMusicInputStream; AudioFormat format; AudioInputStream musicInputStream; byte[] audioSamples; SourceDataLine line; try { File MusicFile = new File(m_filename); musicInputStream = AudioSystem.getAudioInputStream(MusicFile); // 取得文件的音频输入流 format = musicInputStream.getFormat(); // 取得音频输入流的格式 audioSamples = getAudioSamples(musicInputStream, format); // 取得音频样本 aMusicInputStream = new ByteArrayInputStream(audioSamples); int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10); byte[] buffer = new byte[bufferSize]; try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, bufferSize); } catch (LineUnavailableException e) { e.printStackTrace(); return; } if (!line.isRunning()) { line.start(); } int numBytesRead = 0; while (numBytesRead != -1 && !m_stopped) { numBytesRead = aMusicInputStream.read(buffer, 0, buffer.length); if (numBytesRead != -1) { line.write(buffer, 0, numBytesRead); } } line.drain(); line.close(); } catch (Exception e) { e.printStackTrace(); } }
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(); }
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(); } }
public void run() { SourceDataLine line = null; try { try { line = (SourceDataLine) AudioSystem.getLine(new DataLine.Info(SourceDataLine.class, fmt)); line.open(fmt, bufsize); line.start(); } catch (Exception e) { e.printStackTrace(); return; } byte[] buf = new byte[1024]; //noinspection InfiniteLoopStatement while (true) { if (Thread.interrupted()) throw (new InterruptedException()); synchronized (queuemon) { Collection<Runnable> queue = Audio.queue; Audio.queue = new LinkedList<Runnable>(); for (Runnable r : queue) r.run(); } synchronized (ncl) { for (CS cs : ncl) clips.add(cs); ncl.clear(); } fillbuf(buf, 0, 1024); //noinspection StatementWithEmptyBody for (int off = 0; off < buf.length; off += line.write(buf, off, buf.length - off)) ; } } catch (InterruptedException ignored) { } finally { synchronized (Audio.class) { player = null; } if (line != null) line.close(); } }
/** 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"); }
synchronized void close() { if (source != null) { if (DEBUG || Printer.trace) Printer.trace("DataPusher.close(): source.close()"); source.close(); } }
public String toString() { Mixer.Info info = mixer.getMixerInfo(); String s = "\nMixer [" + id + "]"; s += "\n\t Name: " + info.getName(); s += "\n\t Desc: " + info.getDescription(); s += "\n\t Ven : " + info.getVendor(); s += "\n\t Ver : " + info.getVersion(); s += "\n\t Str : " + info.toString(); Line.Info[] infos = mixer.getSourceLineInfo(); s += "\n\nSourceLine count : " + infos.length; for (int i = 0; i < infos.length; i++) { if (infos[i] instanceof DataLine.Info) { s += "\n\t\tData Line Source [" + i + "]"; s += "\n\t\t\t Str : " + infos[i].toString(); } else if (infos[i] instanceof Port.Info) { s += "\n\t\tPort Source [" + i + "]"; s += "\n\t\t\t Name: " + ((Port.Info) infos[i]).getName(); s += "\n\t\t\t is Src: " + ((Port.Info) infos[i]).isSource(); s += "\n\t\t\t Str : " + infos[i].toString(); } else /*if(infos[i]!=null)*/ { s += "\n\t\tSource [" + i + "]"; s += "\n\t\t\t Str : " + infos[i].toString(); } } s += "\n\nOUTPUT\n"; for (int i = 0; i < formats.length; i++) { try { SourceDataLine out = getOutputLine(formats[i]); out.close(); s += "\n" + formats[i].toString(); } catch (Exception e) { // s+="\n"+e.getMessage(); } } infos = mixer.getTargetLineInfo(); s += "\n\nTargetLine count : " + infos.length; for (int i = 0; i < infos.length; i++) { if (infos[i] instanceof DataLine.Info) { s += "\n\t\tData Line Target [" + i + "]"; s += "\n\t\t\t Str : " + infos[i].toString(); } else if (infos[i] instanceof Port.Info) { s += "\n\t\tPort Target [" + i + "]"; s += "\n\t\t\t Name: " + ((Port.Info) infos[i]).getName(); s += "\n\t\t\t is Src: " + ((Port.Info) infos[i]).isSource(); s += "\n\t\t\t Str : " + infos[i].toString(); } else /*if(infos[i]!=null)*/ { s += "\n\t\tTarget [" + i + "]"; s += "\n\t\t\t Str : " + infos[i].toString(); } } s += "\n\nINPUT\n"; for (int i = 0; i < formats.length; i++) { try { TargetDataLine out = getInputLine(formats[i]); out.close(); s += "\n" + formats[i].toString(); } catch (Exception e) { // s+="\n"+e.getMessage(); } } return s; }