Java Code Examples for javax.sound.sampled.DataLine#Info

The following examples show how to use javax.sound.sampled.DataLine#Info . 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: TestDataSetSource.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
protected void openLineIn() {
    final AudioFormat format = new AudioFormat(samplingRate, N_SYNTHESISER_BITS, 1, true, true);
    final DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

    // checks if system supports the data line
    if (!AudioSystem.isLineSupported(info)) {
        LOGGER.atError().addArgument(info).addArgument(format).log("Line not supported '{}' format was '{}'");
        throw new IllegalArgumentException("Line not supported");
    }

    try {
        line = (TargetDataLine) AudioSystem.getLine(info);
        line.open(format);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.atInfo().log("opened audio line-in, format = " + format);
        }
    } catch (final LineUnavailableException e) {
        LOGGER.atError().setCause(e).addArgument(DATA_SOURCE_FILE).log("'{}' does not seem to be recognised as a Midi file");
    }
}
 
Example 2
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
/**
 * WAVE�t�@�C�������[�h
 * @param url WAVE�t�@�C����URL
 */
public static void load(URL url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    // �I�[�f�B�I�X�g���[�����J��
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    // WAVE�t�@�C���̃t�H�[�}�b�g���擾
    AudioFormat format = ais.getFormat();
    // ���C�����擾
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);

    // WAVE�f�[�^���擾
    DataClip clip = new DataClip(ais);
    
    // WAVE�f�[�^��o�^
    clips[counter] = clip;
    lines[counter] = (SourceDataLine)AudioSystem.getLine(info);
    
    // ���C�����J��
    lines[counter].open(format);

    counter++;
}
 
Example 3
Source File: JavaSoundAudioDevice.java    From jsyn with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    if (!AudioSystem.isLineSupported(info)) {
        // Handle the error.
        logger.severe("JavaSoundOutputStream - not supported." + format);
    } else {
        try {
            line = (SourceDataLine) getDataLine(info);
            int bufferSize = calculateBufferSize(suggestedOutputLatency);
            line.open(format, bufferSize);
            logger.fine("Output buffer size = " + bufferSize + " bytes.");
            line.start();

        } catch (Exception e) {
            e.printStackTrace();
            line = null;
        }
    }
}
 
Example 4
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
/**
 * WAVE�t�@�C�������[�h
 * @param url WAVE�t�@�C����URL
 */
public static void load(URL url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    // �I�[�f�B�I�X�g���[�����J��
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    // WAVE�t�@�C���̃t�H�[�}�b�g���擾
    AudioFormat format = ais.getFormat();
    // ���C�����擾
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);

    // WAVE�f�[�^���擾
    DataClip clip = new DataClip(ais);
    
    // WAVE�f�[�^��o�^
    clips[counter] = clip;
    lines[counter] = (SourceDataLine)AudioSystem.getLine(info);
    
    // ���C�����J��
    lines[counter].open(format);

    counter++;
}
 
Example 5
Source File: JavaSoundAudioClip.java    From TencentKona-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 6
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 7
Source File: SoftMixingDataLine.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
SoftMixingDataLine(SoftMixingMixer mixer, DataLine.Info info) {
    this.mixer = mixer;
    this.info = info;
    this.control_mutex = mixer.control_mutex;

    controls = new Control[] { gain_control, mute_control, balance_control,
            pan_control, reverbsend_control, chorussend_control,
            apply_reverb };
    calcVolume();
}
 
Example 8
Source File: BackgroundMusicUtils.java    From WorldGrower with GNU General Public License v3.0 5 votes vote down vote up
public static Clip readMusicFile(InputStream audioFilePath, SoundOutput soundOutput) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
	AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFilePath);
	AudioFormat format = audioStream.getFormat();
	DataLine.Info info = new DataLine.Info(Clip.class, format);
	
	Mixer mixer = soundOutput.getMixer();
	Clip audioClip = (Clip) mixer.getLine(info);
	audioClip.open(audioStream);
	return audioClip;
}
 
Example 9
Source File: JavaSoundAudioClip.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private boolean createClip() {

        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createClip()");

        try {
            DataLine.Info info = new DataLine.Info(Clip.class, loadedAudioFormat);
            if (!(AudioSystem.isLineSupported(info)) ) {
                if (DEBUG || Printer.err)Printer.err("Clip not supported: "+loadedAudioFormat);
                // fail silently
                return false;
            }
            Object line = AudioSystem.getLine(info);
            if (!(line instanceof AutoClosingClip)) {
                if (DEBUG || Printer.err)Printer.err("Clip is not auto closing!"+clip);
                // fail -> will try with SourceDataLine
                return false;
            }
            clip = (AutoClosingClip) line;
            clip.setAutoClosing(true);
            if (DEBUG || Printer.debug) clip.addLineListener(this);
        } catch (Exception e) {
            if (DEBUG || Printer.err)e.printStackTrace();
            // fail silently
            return false;
        }

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

        if (DEBUG || Printer.debug)Printer.debug("Loaded clip.");
        return true;
    }
 
Example 10
Source File: ClipDrain.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void doMixerClip(Mixer mixer) throws Exception {
    boolean waitedEnough=false;
    try {
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        Clip clip = (Clip) mixer.getLine(info);
        clip.open(format, soundData, 0, soundData.length);

        // sanity
        if (clip.getMicrosecondLength()/1000 < 9900) {
            throw new Exception("clip's microsecond length should be at least 9900000, but it is "+clip.getMicrosecondLength());
        }
        long start = System.currentTimeMillis();

        System.out.println(" ---------- start --------");
        clip.start();
        // give time to actually start it. ALSA implementation needs that...
        Thread.sleep(300);
        System.out.println("drain ... ");
        clip.drain();
        long elapsedTime = System.currentTimeMillis() - start;
        System.out.println("close ... ");
        clip.close();
        System.out.println("... done");
        System.out.println("Playback duration: "+elapsedTime+" milliseconds.");
        waitedEnough = elapsedTime >= ((clip.getMicrosecondLength() / 1000) - TOLERANCE_MS);
    } catch (Throwable t) {
            System.out.println("  - Caught exception. Not failed.");
            System.out.println("  - "+t.toString());
            return;
    }
    if (!waitedEnough) {
        throw new Exception("Drain did not wait long enough to play entire clip.");
    }
    successfulTests++;
}
 
Example 11
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 12
Source File: SoundTools.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static SourceDataLine getLine(AudioFormat audioFormat) throws
        LineUnavailableException {
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
    SourceDataLine res = (SourceDataLine) AudioSystem.getLine(info);
    res.open(audioFormat);
    return res;
}
 
Example 13
Source File: JavaSoundAudioDevice.java    From jsyn with Apache License 2.0 5 votes vote down vote up
private int scanMaxChannels(Line.Info[] lines) {
    int maxChannels = 0;
    for (Line.Info line : lines) {
        if (line instanceof DataLine.Info) {
            int numChannels = scanMaxChannels(((DataLine.Info) line));
            if (numChannels > maxChannels) {
                maxChannels = numChannels;
            }
        }
    }
    return maxChannels;
}
 
Example 14
Source File: PhantomMixers.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {
    int SDLformats = 0;
    int TDLformats = 0;
    Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
    for(int i=0; i<mixerInfo.length; i++){
        Mixer.Info thisMixerInfo = mixerInfo[i];
        System.out.println("Mixer #"+i+": "
                           + thisMixerInfo.getName()
                           + ": " + thisMixerInfo.getDescription());
        Mixer mixer = AudioSystem.getMixer(thisMixerInfo);
        Line.Info[] srcLineInfo = mixer.getSourceLineInfo();
        Line.Info[] dstLineInfo = mixer.getTargetLineInfo();
        int count = srcLineInfo.length + dstLineInfo.length;
        System.out.print(" -> " + (srcLineInfo.length + dstLineInfo.length) + " line");
        switch (count) {
            case 0: System.out.println("s"); break;
            case 1: System.out.println(""); break;
            default: System.out.println("s:"); break;
        }
        int l;
        for (l = 0; l < srcLineInfo.length; l++) {
            System.out.println("    "+srcLineInfo[l].toString());
            if (srcLineInfo[l].getLineClass() == SourceDataLine.class
                && (srcLineInfo[l] instanceof DataLine.Info)) {
                SDLformats += ((DataLine.Info) srcLineInfo[l]).getFormats().length;
            }
        }
        for (l = 0; l < dstLineInfo.length; l++) {
            System.out.println("    "+dstLineInfo[l].toString());
            if (dstLineInfo[l].getLineClass() == TargetDataLine.class
                && (dstLineInfo[l] instanceof DataLine.Info)) {
                TDLformats += ((DataLine.Info) dstLineInfo[l]).getFormats().length;
            }
        }
    }
    if (mixerInfo.length == 0) {
        System.out.println("[no mixers present]");
    }
    System.out.println(""+SDLformats+" total formats for SourceDataLines");
    System.out.println(""+TDLformats+" total formats for TargetDataLines");
    System.out.println("");
    System.out.println("If there are audio devices correctly installed on your");
    System.out.println("system, you should see at least one Mixer, and in total");
    System.out.println("at least each one SourceDataLine and TargetDataLine, both");
    System.out.println("providing at least one format.");
    System.out.println("");
    System.out.println("Now disable your soundcard and repeat the test.");
    System.out.println("The corresponding mixer(s) should not provide any formats");
    System.out.println("anymore. If you disable all available soundcards");
    System.out.println("on your computer, the number of formats above should be");
    System.out.println("0 for both line types (although mixers are allowed to exist).");
}
 
Example 15
Source File: DummySourceDataLine.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public DummySourceDataLine()
{
    ArrayList<AudioFormat> formats = new ArrayList<AudioFormat>();
    for (int channels = 1; channels <= 2; channels++) {
        formats.add(new AudioFormat(Encoding.PCM_SIGNED,
                AudioSystem.NOT_SPECIFIED, 8, channels, channels,
                AudioSystem.NOT_SPECIFIED, false));
        formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
                AudioSystem.NOT_SPECIFIED, 8, channels, channels,
                AudioSystem.NOT_SPECIFIED, false));
        for (int bits = 16; bits < 32; bits += 8) {
            formats.add(new AudioFormat(Encoding.PCM_SIGNED,
                    AudioSystem.NOT_SPECIFIED, bits, channels, channels
                            * bits / 8, AudioSystem.NOT_SPECIFIED, false));
            formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
                    AudioSystem.NOT_SPECIFIED, bits, channels, channels
                            * bits / 8, AudioSystem.NOT_SPECIFIED, false));
            formats.add(new AudioFormat(Encoding.PCM_SIGNED,
                    AudioSystem.NOT_SPECIFIED, bits, channels, channels
                            * bits / 8, AudioSystem.NOT_SPECIFIED, true));
            formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
                    AudioSystem.NOT_SPECIFIED, bits, channels, channels
                            * bits / 8, AudioSystem.NOT_SPECIFIED, true));
        }
        formats.add(new AudioFormat(Encoding.PCM_FLOAT,
                AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4,
                AudioSystem.NOT_SPECIFIED, false));
        formats.add(new AudioFormat(Encoding.PCM_FLOAT,
                AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4,
                AudioSystem.NOT_SPECIFIED, true));
        formats.add(new AudioFormat(Encoding.PCM_FLOAT,
                AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8,
                AudioSystem.NOT_SPECIFIED, false));
        formats.add(new AudioFormat(Encoding.PCM_FLOAT,
                AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8,
                AudioSystem.NOT_SPECIFIED, true));
    }
    AudioFormat[] formats_array = formats.toArray(new AudioFormat[formats
            .size()]);
    sourceLineInfo = new DataLine.Info(SourceDataLine.class,
            formats_array, AudioSystem.NOT_SPECIFIED,
            AudioSystem.NOT_SPECIFIED);

}
 
Example 16
Source File: ClassicDoomSoundDriver.java    From mochadoom with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean InitSound() {

    // Secure and configure sound device first.
    System.out.println("I_InitSound: ");

    // We only need a single data line.
    // PCM, signed, 16-bit, stereo, 22025 KHz, 2048 bytes per "frame",
    // maximum of 44100/2048 "fps"
    AudioFormat format = new AudioFormat(SAMPLERATE, 16, 2, true, true);

    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    if (AudioSystem.isLineSupported(info))
        try {
            line = (SourceDataLine) AudioSystem.getSourceDataLine(format);
            line.open(format,AUDIOLINE_BUFFER);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.print("Could not play signed 16 data\n");
            return false;
        }

    if (line != null) {
        System.err.print(" configured audio device\n");
        line.start();
    } else {
    	 System.err.print(" could not configure audio device\n");
    	 return false;
    }

    // This was here only for debugging purposes
    /*
     * try { fos=new FileOutputStream("test.raw"); dao=new
     * DataOutputStream(fos); } catch (FileNotFoundException e) {
     * Auto-generated catch block e.printStackTrace(); }
     */

    SOUNDSRV = new MixServer(line);
    SOUNDTHREAD = new Thread(SOUNDSRV);
    SOUNDTHREAD.start();

    // Initialize external data (all sounds) at start, keep static.
    System.err.print("I_InitSound: ");

    super.initSound8();

    System.err.print(" pre-cached all sound data\n");

    // Now initialize mixbuffer with zero.
    initMixBuffer();

    // Finished initialization.
    System.err.print("I_InitSound: sound module ready\n");

    return true;
}
 
Example 17
Source File: SoftMixingClip.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
SoftMixingClip(SoftMixingMixer mixer, DataLine.Info info) {
    super(mixer, info);
}
 
Example 18
Source File: AbstractDataLine.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a new AbstractLine.
 */
protected AbstractDataLine(DataLine.Info info, AbstractMixer mixer, Control[] controls) {
    this(info, mixer, controls, null, AudioSystem.NOT_SPECIFIED);
}
 
Example 19
Source File: DummySourceDataLine.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public DummySourceDataLine()
{
    ArrayList<AudioFormat> formats = new ArrayList<AudioFormat>();
    for (int channels = 1; channels <= 2; channels++) {
        formats.add(new AudioFormat(Encoding.PCM_SIGNED,
                AudioSystem.NOT_SPECIFIED, 8, channels, channels,
                AudioSystem.NOT_SPECIFIED, false));
        formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
                AudioSystem.NOT_SPECIFIED, 8, channels, channels,
                AudioSystem.NOT_SPECIFIED, false));
        for (int bits = 16; bits < 32; bits += 8) {
            formats.add(new AudioFormat(Encoding.PCM_SIGNED,
                    AudioSystem.NOT_SPECIFIED, bits, channels, channels
                            * bits / 8, AudioSystem.NOT_SPECIFIED, false));
            formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
                    AudioSystem.NOT_SPECIFIED, bits, channels, channels
                            * bits / 8, AudioSystem.NOT_SPECIFIED, false));
            formats.add(new AudioFormat(Encoding.PCM_SIGNED,
                    AudioSystem.NOT_SPECIFIED, bits, channels, channels
                            * bits / 8, AudioSystem.NOT_SPECIFIED, true));
            formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
                    AudioSystem.NOT_SPECIFIED, bits, channels, channels
                            * bits / 8, AudioSystem.NOT_SPECIFIED, true));
        }
        formats.add(new AudioFormat(Encoding.PCM_FLOAT,
                AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4,
                AudioSystem.NOT_SPECIFIED, false));
        formats.add(new AudioFormat(Encoding.PCM_FLOAT,
                AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4,
                AudioSystem.NOT_SPECIFIED, true));
        formats.add(new AudioFormat(Encoding.PCM_FLOAT,
                AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8,
                AudioSystem.NOT_SPECIFIED, false));
        formats.add(new AudioFormat(Encoding.PCM_FLOAT,
                AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8,
                AudioSystem.NOT_SPECIFIED, true));
    }
    AudioFormat[] formats_array = formats.toArray(new AudioFormat[formats
            .size()]);
    sourceLineInfo = new DataLine.Info(SourceDataLine.class,
            formats_array, AudioSystem.NOT_SPECIFIED,
            AudioSystem.NOT_SPECIFIED);

}
 
Example 20
Source File: AudioThread.java    From open-ig with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * The audio line specification.
 * @param af the audio format
 * @return the appropriate DataLine.Info object
 */
public static DataLine.Info createAudioInfo(AudioFormat af) {
	return new DataLine.Info(SourceDataLine.class, af);
}