Java Code Examples for javax.sound.sampled.SourceDataLine#drain()
The following examples show how to use
javax.sound.sampled.SourceDataLine#drain() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SoundTools.java From MyBox with Apache License 2.0 | 6 votes |
public static void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException { byte[] data = new byte[CommonValues.IOBufferLength]; DataLine.Info info = new DataLine.Info(SourceDataLine.class, targetFormat); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(targetFormat); if (line != null) { // Start FloatControl vol = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN); logger.debug(vol.getValue() + vol.getUnits()); line.start(); int nBytesRead = 0, nBytesWritten = 0; while (nBytesRead != -1) { nBytesRead = din.read(data, 0, data.length); if (nBytesRead != -1) { nBytesWritten = line.write(data, 0, nBytesRead); } } // Stop line.drain(); line.stop(); line.close(); din.close(); } }
Example 2
Source File: SoundUtils.java From Neural-Network-Programming-with-Java-SecondEdition with MIT License | 6 votes |
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(); }
Example 3
Source File: SoundUtils.java From Neural-Network-Programming-with-Java-SecondEdition with MIT License | 6 votes |
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(); }
Example 4
Source File: bug6372428.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void playRecorded(AudioFormat format, byte[] data) throws Exception { //SourceDataLine line = AudioSystem.getSourceDataLine(format); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info); line.open(); line.start(); int remaining = data.length; while (remaining > 0) { int avail = line.available(); if (avail > 0) { if (avail > remaining) avail = remaining; int written = line.write(data, data.length - remaining, avail); remaining -= written; log("Playing: " + written + " bytes written"); } else { delay(100); } } line.drain(); line.stop(); }
Example 5
Source File: PlayerTest.java From DTMF-Decoder with MIT License | 6 votes |
private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException { byte[] data = new byte[4096]; SourceDataLine line = getLine(targetFormat); if (line != null) { // Start line.start(); int nBytesRead = 0, nBytesWritten = 0; while (nBytesRead != -1) { nBytesRead = din.read(data, 0, data.length); if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead); } // Stop line.drain(); line.stop(); line.close(); din.close(); } }
Example 6
Source File: Test.java From DTMF-Decoder with MIT License | 6 votes |
private static void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException { byte[] data = new byte[4096]; SourceDataLine line = getLine(targetFormat); if (line != null) { // Start line.start(); int nBytesRead = 0, nBytesWritten = 0; while (nBytesRead != -1) { nBytesRead = din.read(data, 0, data.length); if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead); } // Stop line.drain(); line.stop(); line.close(); din.close(); } }
Example 7
Source File: AudioPlayer.java From google-assistant-java-demo with GNU General Public License v3.0 | 5 votes |
public void play(byte[] sound) throws AudioException { try { AudioFormat format = AudioUtil.getAudioFormat(audioConf); DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format); SourceDataLine speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo); speakers.open(format); speakers.start(); speakers.write(sound, 0, sound.length); speakers.drain(); speakers.close(); } catch (Exception e) { throw new AudioException("Unable to play the response", e); } }
Example 8
Source File: SoundPlayer.java From freecol with GNU General Public License v2.0 | 5 votes |
/** * Play a sound. * * @param sound The {@code File} to play. * @return True if the sound was played without incident. * @exception IOException if unable to read or write the sound data. */ private boolean playSound(File sound) throws IOException { boolean ret = false; try (AudioInputStream in = getAudioInputStream(sound)) { SourceDataLine line = openLine(in.getFormat(), getMixer(), data.length); if (line == null) return false; changeVolume(line, getVolume()); try { this.playDone = false; int rd; while (!this.playDone && (rd = in.read(data)) > 0) { line.write(data, 0, rd); } ret = true; //logger.finest("Played " + sound); } finally { this.playDone = true; line.drain(); line.stop(); line.close(); } } catch (IOException ioe) { logger.log(Level.WARNING, "Can not open " + sound + " as audio stream", ioe); return false; } return ret; }
Example 9
Source File: ChangingBuffer.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private static boolean doMixerSDL(Mixer mixer, AudioFormat format) { if (mixer==null) return false; try { System.out.println("Trying mixer "+mixer+":"); DataLine.Info info = new DataLine.Info( SourceDataLine.class, format, (int) samplerate); SourceDataLine sdl = (SourceDataLine) mixer.getLine(info); System.out.println(" - got sdl: "+sdl); System.out.println(" - open with format "+format); sdl.open(format); System.out.println(" - start..."); sdl.start(); System.out.println(" - write..."); sdl.write(buffer, 0, buffer.length); Thread.sleep(200); System.out.println(" - drain..."); sdl.drain(); System.out.println(" - stop..."); sdl.stop(); System.out.println(" - close..."); sdl.close(); System.out.println(" - closed"); } catch (Throwable t) { System.out.println(" - Caught exception. Not failed."); System.out.println(" - "+t.toString()); return false; } return true; }
Example 10
Source File: PlaySine.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public static int play(boolean shouldPlay) { int res = 0; DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); try { println("Getting line from mixer..."); source = (SourceDataLine) mixer.getLine(info); println("Opening line..."); println(" -- if the program is hanging here, kill the process that has blocks the audio device now."); source.open(audioFormat); println("Starting line..."); source.start(); println("Writing audio data for 1 second..."); long startTime = System.currentTimeMillis(); while (System.currentTimeMillis() - startTime < 1000) { writeData(); Thread.sleep(100); } res = 1; } catch (IllegalArgumentException iae) { println("IllegalArgumentException: "+iae.getMessage()); println("Sound device cannot handle this audio format."); println("ERROR: Test environment not correctly set up."); if (source!=null) { source.close(); } return 3; } catch (LineUnavailableException lue) { println("LineUnavailableException: "+lue.getMessage()); if (shouldPlay) { println("ERROR: the line should be available now!."); println(" Verify that you killed the other audio process."); } else { println("Correct behavior! the bug is fixed."); } res = 2; } catch (Exception e) { println("Unexpected Exception: "+e.toString()); } if (source != null) { println("Draining..."); try { source.drain(); } catch (NullPointerException npe) { println("(NullPointerException: bug fixed in J2SE 1.4.2"); } println("Stopping..."); source.stop(); println("Closing..."); source.close(); source = null; } return res; }
Example 11
Source File: TickAtEndOfPlay.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Exception { System.out.println("This test should only be run on Windows."); System.out.println("Make sure that the speakers are connected and the volume is up."); System.out.println("Close all other programs that may use the soundcard."); System.out.println("You'll hear a 2-second tone. when the tone finishes,"); System.out.println(" there should be no noise. If you hear a short tick/noise,"); System.out.println(" the bug still applies."); System.out.println("Press ENTER to continue."); System.in.read(); for (int i = 0; i < args.length; i++) { if (args[i].equals("1")) WorkAround1 = true; if (args[i].equals("2")) WorkAround2 = true; } if (WorkAround1) System.out.println("Using work around1: appending silence"); if (WorkAround2) System.out.println("Using work around2: waiting before close"); int zerolen = 0; // how many 0-bytes will be appended to playback if (WorkAround1) zerolen = 1000; int seconds = 2; int sampleRate = 8000; double frequency = 1000.0; double RAD = 2.0 * Math.PI; AudioFormat af = new AudioFormat((float)sampleRate,8,1,true,true); System.out.println("Format: "+af); DataLine.Info info = new DataLine.Info(SourceDataLine.class,af); SourceDataLine source = (SourceDataLine)AudioSystem.getLine(info); System.out.println("Line: "+source); if (source.toString().indexOf("MixerSourceLine")>=0) { System.out.println("This test only applies to non-Java Sound Audio Engine!"); return; } System.out.println("Opening..."); source.open(af); System.out.println("Starting..."); source.start(); int datalen = sampleRate * seconds; byte[] buf = new byte[datalen+zerolen]; for (int i=0; i<datalen; i++) { buf[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0); } System.out.println("Writing..."); source.write(buf,0,buf.length); System.out.println("Draining..."); source.drain(); System.out.println("Stopping..."); source.stop(); if (WorkAround2) { System.out.println("Waiting 200 millis..."); Thread.sleep(200); } System.out.println("Closing..."); source.close(); System.out.println("Done."); }