javax.sound.sampled.SourceDataLine Java Examples

The following examples show how to use javax.sound.sampled.SourceDataLine. 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: SoundUtils.java    From Neural-Network-Programming-with-Java-SecondEdition with MIT License 6 votes vote down vote up
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 #2
Source File: JDK13Services.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/** Obtain the value of a default provider property.
    @param typeClass The type of the default provider property. This
    should be one of Receiver.class, Transmitter.class, Sequencer.class,
    Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
    Clip.class or Port.class.
    @return The complete value of the property, if available.
    If the property is not set, null is returned.
 */
private static synchronized String getDefaultProvider(Class typeClass) {
    if (!SourceDataLine.class.equals(typeClass)
            && !TargetDataLine.class.equals(typeClass)
            && !Clip.class.equals(typeClass)
            && !Port.class.equals(typeClass)
            && !Receiver.class.equals(typeClass)
            && !Transmitter.class.equals(typeClass)
            && !Synthesizer.class.equals(typeClass)
            && !Sequencer.class.equals(typeClass)) {
        return null;
    }
    String name = typeClass.getName();
    String value = AccessController.doPrivileged(
            (PrivilegedAction<String>) () -> System.getProperty(name));
    if (value == null) {
        value = getProperties().getProperty(name);
    }
    if ("".equals(value)) {
        value = null;
    }
    return value;
}
 
Example #3
Source File: SoftAudioPusher.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example #4
Source File: JavaSoundAudioClip.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private boolean createSourceDataLine() {
    if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
    try {
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
        if (!(AudioSystem.isLineSupported(info)) ) {
            if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
            // fail silently
            return false;
        }
        SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
        datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
    } catch (Exception e) {
        if (DEBUG || Printer.err)e.printStackTrace();
        // fail silently
        return false;
    }

    if (datapusher==null) {
        // fail silently
        return false;
    }

    if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
    return true;
}
 
Example #5
Source File: SoftAudioPusher.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example #6
Source File: JavaSoundAudioClip.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private boolean createSourceDataLine() {
    if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
    try {
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
        if (!(AudioSystem.isLineSupported(info)) ) {
            if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
            // fail silently
            return false;
        }
        SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
        datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
    } catch (Exception e) {
        if (DEBUG || Printer.err)e.printStackTrace();
        // fail silently
        return false;
    }

    if (datapusher==null) {
        // fail silently
        return false;
    }

    if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
    return true;
}
 
Example #7
Source File: JavaSoundAudioClip.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private boolean createSourceDataLine() {
    if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
    try {
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
        if (!(AudioSystem.isLineSupported(info)) ) {
            if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
            // fail silently
            return false;
        }
        SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
        datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
    } catch (Exception e) {
        if (DEBUG || Printer.err)e.printStackTrace();
        // fail silently
        return false;
    }

    if (datapusher==null) {
        // fail silently
        return false;
    }

    if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
    return true;
}
 
Example #8
Source File: JDK13Services.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** Obtain the value of a default provider property.
    @param typeClass The type of the default provider property. This
    should be one of Receiver.class, Transmitter.class, Sequencer.class,
    Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
    Clip.class or Port.class.
    @return The complete value of the property, if available.
    If the property is not set, null is returned.
 */
private static synchronized String getDefaultProvider(Class<?> typeClass) {
    if (!SourceDataLine.class.equals(typeClass)
            && !TargetDataLine.class.equals(typeClass)
            && !Clip.class.equals(typeClass)
            && !Port.class.equals(typeClass)
            && !Receiver.class.equals(typeClass)
            && !Transmitter.class.equals(typeClass)
            && !Synthesizer.class.equals(typeClass)
            && !Sequencer.class.equals(typeClass)) {
        return null;
    }
    String name = typeClass.getName();
    String value = AccessController.doPrivileged(
            (PrivilegedAction<String>) () -> System.getProperty(name));
    if (value == null) {
        value = getProperties().getProperty(name);
    }
    if ("".equals(value)) {
        value = null;
    }
    return value;
}
 
Example #9
Source File: SoundFormat.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
public boolean play(SWFInputStream sis) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (!decode(sis, baos)) {
        return false;
    }

    AudioFormat audioFormat = new AudioFormat(samplingRate, 16, stereo ? 2 : 1, true, false);
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    try (SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) {
        line.open(audioFormat);
        byte[] outData = baos.toByteArray();
        line.write(outData, 0, outData.length);
        line.drain();
        line.stop();
        return true;
    } catch (LineUnavailableException ex) {
        return false;
    }
}
 
Example #10
Source File: SoundTools.java    From MyBox with Apache License 2.0 6 votes vote down vote up
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 #11
Source File: Sounds.java    From open-ig with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** Close all audio lines. */
public void close() {
	if (exec != null) {
		exec.shutdown();
		for (SourceDataLine sdl : lines) {
			sdl.close();
		}
		try {
			exec.awaitTermination(60, TimeUnit.SECONDS);
		} catch (InterruptedException ex) {
			// ignored
		}
		exec.shutdownNow();
		lines.clear();
		soundPool.clear();
		soundMap.clear();
		soundFormat.clear();
		exec = null;
	}
	
}
 
Example #12
Source File: JavaSoundAudioClip.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private boolean createSourceDataLine() {
    if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
    try {
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
        if (!(AudioSystem.isLineSupported(info)) ) {
            if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
            // fail silently
            return false;
        }
        SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
        datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
    } catch (Exception e) {
        if (DEBUG || Printer.err)e.printStackTrace();
        // fail silently
        return false;
    }

    if (datapusher==null) {
        // fail silently
        return false;
    }

    if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
    return true;
}
 
Example #13
Source File: SoftAudioPusher.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example #14
Source File: SoftMixingMixer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public Line getLine(Line.Info info) throws LineUnavailableException {

        if (!isLineSupported(info))
            throw new IllegalArgumentException("Line unsupported: " + info);

        if ((info.getLineClass() == SourceDataLine.class)) {
            return new SoftMixingSourceDataLine(this, (DataLine.Info) info);
        }
        if ((info.getLineClass() == Clip.class)) {
            return new SoftMixingClip(this, (DataLine.Info) info);
        }

        throw new IllegalArgumentException("Line unsupported: " + info);
    }
 
Example #15
Source File: SoundSystem.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
public static Mixer tryToFindMixer(AudioFormat audioFormat)
  {
      Mixer.Info[]        mixerInfos   = AudioSystem.getMixerInfo();
      Mixer[]             mixers       = new Mixer[mixerInfos.length];
      final DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);

      if(mixers.length == 0) {
	return null;
}

      for(int i=0; i<mixerInfos.length; ++i) {
	mixers[i] = AudioSystem.getMixer(mixerInfos[i]);
}

      Arrays.sort(mixers, new Comparator<Mixer>()
      {
          @Override
	public int compare(Mixer mixer1, Mixer mixer2)
          {
              int numLines1 = mixer1.getMaxLines(dataLineInfo);
              int numLines2 = mixer2.getMaxLines(dataLineInfo);

              if(numLines1 == AudioSystem.NOT_SPECIFIED || numLines1 > numLines2) {
			return -1;
		}

              return 1;
          }
      });

      if(mixers[0].getMaxLines(dataLineInfo) == 0) {
	return null;
}

      return mixers[0];
  }
 
Example #16
Source File: SoftMixingMixer.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public Line getLine(Line.Info info) throws LineUnavailableException {

        if (!isLineSupported(info))
            throw new IllegalArgumentException("Line unsupported: " + info);

        if ((info.getLineClass() == SourceDataLine.class)) {
            return new SoftMixingSourceDataLine(this, (DataLine.Info) info);
        }
        if ((info.getLineClass() == Clip.class)) {
            return new SoftMixingClip(this, (DataLine.Info) info);
        }

        throw new IllegalArgumentException("Line unsupported: " + info);
    }
 
Example #17
Source File: SoftMixingMixer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public Line getLine(Line.Info info) throws LineUnavailableException {

        if (!isLineSupported(info))
            throw new IllegalArgumentException("Line unsupported: " + info);

        if ((info.getLineClass() == SourceDataLine.class)) {
            return new SoftMixingSourceDataLine(this, (DataLine.Info) info);
        }
        if ((info.getLineClass() == Clip.class)) {
            return new SoftMixingClip(this, (DataLine.Info) info);
        }

        throw new IllegalArgumentException("Line unsupported: " + info);
    }
 
Example #18
Source File: AudioPlayer.java    From pumpernickel with MIT License 5 votes vote down vote up
PlayAudioThread(AudioInputStream stream, StartTime startTime,
		SourceDataLine dataLine, Listener listener,
		Cancellable cancellable) {
	super("Play Audio " + (threadNameCtr++));
	if (stream == null)
		throw new NullPointerException();
	if (dataLine == null)
		throw new NullPointerException();
	this.stream = stream;
	this.dataLine = dataLine;
	this.startTime = startTime;
	this.listener = listener == null ? new NullListener() : listener;
	this.cancellable = cancellable == null ? new BasicCancellable()
			: cancellable;
}
 
Example #19
Source File: SoftMixingMixer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public int getMaxLines(Line.Info info) {
    if (info.getLineClass() == SourceDataLine.class)
        return AudioSystem.NOT_SPECIFIED;
    if (info.getLineClass() == Clip.class)
        return AudioSystem.NOT_SPECIFIED;
    return 0;
}
 
Example #20
Source File: SoftMixingMixer.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public Line getLine(Line.Info info) throws LineUnavailableException {

    if (!isLineSupported(info))
        throw new IllegalArgumentException("Line unsupported: " + info);

    if ((info.getLineClass() == SourceDataLine.class)) {
        return new SoftMixingSourceDataLine(this, (DataLine.Info) info);
    }
    if ((info.getLineClass() == Clip.class)) {
        return new SoftMixingClip(this, (DataLine.Info) info);
    }

    throw new IllegalArgumentException("Line unsupported: " + info);
}
 
Example #21
Source File: SoundSystemNG.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
public void proceed(Time delay, SourceDataLine outputLine)
   {
	if(outputLine != null && !outputLine.getFormat().matches(mAudioFormat)) {
		throw new IllegalArgumentException("outputLine has the wrong audio format");
	}

	init(outputLine, mAudioFormat, mBufferDuration.get());
	changeSystemState(STATE_RUNNING, delay);
}
 
Example #22
Source File: SoundPlay.java    From Data_Processor with Apache License 2.0 5 votes vote down vote up
public void Play(byte[] secbytesarray, AudioFormat af, SourceDataLine line1, int sp, byte[] data) throws LineUnavailableException{
	for(int i=0;i<sp;i++){
		for(int j=0;j<data.length;j++){
			data[j]=secbytesarray[j+i*data.length];
		}
		line1.write(data, 0, data.length);
	}
}
 
Example #23
Source File: SoundSystemNG.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
public SoundSystemNG(SourceDataLine outputLine, Time bufferDuration)
  {
if(outputLine == null) {
	throw new IllegalArgumentException("outputLine argument must not be null");
}
if(bufferDuration == null) {
	throw new IllegalArgumentException("bufferDuration argument must not be null");
}

init(outputLine, outputLine.getFormat(), bufferDuration);
  }
 
Example #24
Source File: SoftMixingMixer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public int getMaxLines(Line.Info info) {
    if (info.getLineClass() == SourceDataLine.class)
        return AudioSystem.NOT_SPECIFIED;
    if (info.getLineClass() == Clip.class)
        return AudioSystem.NOT_SPECIFIED;
    return 0;
}
 
Example #25
Source File: SoftMixingMixer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public int getMaxLines(Line.Info info) {
    if (info.getLineClass() == SourceDataLine.class)
        return AudioSystem.NOT_SPECIFIED;
    if (info.getLineClass() == Clip.class)
        return AudioSystem.NOT_SPECIFIED;
    return 0;
}
 
Example #26
Source File: SoftMixingMixer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public int getMaxLines(Line.Info info) {
    if (info.getLineClass() == SourceDataLine.class)
        return AudioSystem.NOT_SPECIFIED;
    if (info.getLineClass() == Clip.class)
        return AudioSystem.NOT_SPECIFIED;
    return 0;
}
 
Example #27
Source File: SoundMixer.java    From jace with GNU General Public License v2.0 5 votes vote down vote up
public void returnLine(Object requester) {
        if (activeLines.containsKey(requester)) {
            SourceDataLine sdl = activeLines.remove(requester);
// Calling drain on pulse driver can cause it to freeze up (?)
//            sdl.drain();
            if (sdl.isRunning()) {
                sdl.flush();
                sdl.stop();
            }
            availableLines.add(sdl);
        }
    }
 
Example #28
Source File: SoftMixingMixer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public int getMaxLines(Line.Info info) {
    if (info.getLineClass() == SourceDataLine.class)
        return AudioSystem.NOT_SPECIFIED;
    if (info.getLineClass() == Clip.class)
        return AudioSystem.NOT_SPECIFIED;
    return 0;
}
 
Example #29
Source File: ChangingBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 #30
Source File: GetLine.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static int run(String argv[], java.io.PrintStream out) {
    String testCaseID = "LineListener2001";

    log.println("===== " + testCaseID + " =====");

    boolean failed = false;
    Line l = null;



    // get the default SourceDataLine

    DataLine.Info s_info = new DataLine.Info(SourceDataLine.class, null);
    Line.Info infos[] = AudioSystem.getSourceLineInfo( s_info );

    if( infos.length < 1 ) {
        log.println("Line.Info array == 0");
        return STATUS_PASSED;
    }
    try {
        l = AudioSystem.getLine(infos[0]);
    } catch(SecurityException lue) {
        log.println("SecurityException");
        return STATUS_PASSED;
    } catch (LineUnavailableException e1) {
        log.println("LUE");
        return STATUS_PASSED;
    } catch (IllegalArgumentException iae) {
        log.println("IllegalArgumentException should not be thrown "
                 + "for supported line");
        iae.printStackTrace(log);
        return STATUS_FAILED;
    }
    out.println("Passed.");
    return STATUS_PASSED;
}