javax.sound.sampled.Clip Java Examples

The following examples show how to use javax.sound.sampled.Clip. 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: MetronomePlugin.java    From plugins with GNU General Public License v3.0 8 votes vote down vote up
private Clip GetAudioClip(String path)
{
	File audioFile = new File(path);
	if (!audioFile.exists())
	{
		return null;
	}

	try (AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile))
	{
		Clip audioClip = AudioSystem.getClip();
		audioClip.open(audioStream);
		FloatControl gainControl = (FloatControl) audioClip.getControl(FloatControl.Type.MASTER_GAIN);
		float gainValue = (((float) config.volume()) * 40f / 100f) - 35f;
		gainControl.setValue(gainValue);

		return audioClip;
	}
	catch (IOException | LineUnavailableException | UnsupportedAudioFileException e)
	{
		log.warn("Error opening audiostream from " + audioFile, e);
		return null;
	}
}
 
Example #2
Source File: WaveEngine.java    From javagame with MIT License 8 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example #3
Source File: WaveEngine.java    From javagame with MIT License 7 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example #4
Source File: SoundSystem.java    From Robot-Overlord-App with GNU General Public License v2.0 6 votes vote down vote up
static public void playSound(String url) {
	if (url.isEmpty()) return;

	try {
		Clip clip = AudioSystem.getClip();
		BufferedInputStream x = new BufferedInputStream(new FileInputStream(url));
		AudioInputStream inputStream = AudioSystem.getAudioInputStream(x);
		clip.open(inputStream);
		clip.start();
	} catch (Exception e) {
		e.printStackTrace();
		System.err.println(e.getMessage());
	}
}
 
Example #5
Source File: JDK13Services.java    From TencentKona-8 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 #6
Source File: WaveEngine.java    From nullpomino with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Playback
 * @param name Registered name
 */
public void play(String name) {
	Clip clip = clipMap.get(name);

	if(clip != null) {
		// Stop
		clip.stop();
		// Playback position back to the beginning
		clip.setFramePosition(0);
		// Playback
		clip.start();
	}
}
 
Example #7
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 #8
Source File: TestAudio.java    From Azzet with Open Software License 3.0 6 votes vote down vote up
private void doTest() throws AssetException, InterruptedException
{
	Clip clip = Assets.load("cowbell.wav");

	assertNotNull( clip );
	
	clip.start();

	Thread.sleep(1000);
	
	clip.stop();
	clip.close();
}
 
Example #9
Source File: InnerResource.java    From salty-engine with Apache License 2.0 6 votes vote down vote up
@Override
public Clip getAudioResource(final String relativePath) {

    AudioInputStream audioInput = null;

    final String arrangedPath = arrangePath(relativePath);

    try {

        final InputStream inputStream = classLoader.getResourceAsStream(arrangedPath);
        final InputStream bufferedIn = new BufferedInputStream(inputStream);
        audioInput = AudioSystem.getAudioInputStream(bufferedIn);
    } catch (final IOException | UnsupportedAudioFileException e) {
        e.printStackTrace();
    }

    return Resource.createClip(audioInput);
}
 
Example #10
Source File: JDK13Services.java    From hottub 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 #11
Source File: UI.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
private void playSirenSound() {
	try {
		File soundFile = new File(sirenFile);
		AudioInputStream soundIn = AudioSystem.getAudioInputStream(soundFile);
		AudioFormat format = soundIn.getFormat();
		DataLine.Info info = new DataLine.Info(Clip.class, format);
		clip = (Clip) AudioSystem.getLine(info);
		clip.addLineListener(new LineListener() {
			@Override
			public void update(LineEvent event) {
				if (event.getType() == LineEvent.Type.STOP) {
					soundOn = false;
				}
			}
		});
		clip.open(soundIn);
		clip.start();
		soundOn = true;
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #12
Source File: SoundTools.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static FloatControl getControl(AudioInputStream in) {
    try {

        AudioFormat baseFormat = in.getFormat();
        AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(),
                16,
                baseFormat.getChannels(),
                baseFormat.getChannels() * 2,
                baseFormat.getSampleRate(),
                false);
        AudioInputStream ain = AudioSystem.getAudioInputStream(decodedFormat, in);
        DataLine.Info info = new DataLine.Info(Clip.class, decodedFormat);
        try ( Clip clip = (Clip) AudioSystem.getLine(info)) {
            clip.open(ain);
            FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            return gainControl;
        }
    } catch (Exception e) {
        logger.debug(e.toString());
        return null;
    }
}
 
Example #13
Source File: FxmlControl.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static void playClip(final String file, final String userFile) {
    Task miaoTask = new Task<Void>() {
        @Override
        protected Void call() {
            try {
                File sound = FxmlControl.getInternalFile(file, "sound", userFile);
                FloatControl control = SoundTools.getControl(sound);
                Clip player = SoundTools.playback(sound, control.getMaximum() * 0.6f);
                player.start();
            } catch (Exception e) {
            }
            return null;
        }
    };
    Thread thread = new Thread(miaoTask);
    thread.setDaemon(true);
    thread.start();
}
 
Example #14
Source File: FxmlControl.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static void playClip(final File file) {
    Task miaoTask = new Task<Void>() {
        @Override
        protected Void call() {
            try {
                FloatControl control = SoundTools.getControl(file);
                Clip player = SoundTools.playback(file, control.getMaximum() * 0.6f);
                player.start();
            } catch (Exception e) {
            }
            return null;
        }
    };
    Thread thread = new Thread(miaoTask);
    thread.setDaemon(true);
    thread.start();
}
 
Example #15
Source File: BlorbSounds.java    From petscii-bbs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected boolean putToDatabase(final Chunk chunk, final int resnum) {

  final InputStream aiffStream =
    new  MemoryAccessInputStream(chunk.getMemoryAccess(), 0,
        chunk.getSize() + Chunk.CHUNK_HEADER_LENGTH);
  try {

    final AudioFileFormat aiffFormat =
      AudioSystem.getAudioFileFormat(aiffStream);
    final AudioInputStream stream = new AudioInputStream(aiffStream,
      aiffFormat.getFormat(), (long) chunk.getSize());
    final Clip clip = AudioSystem.getClip();
    clip.open(stream);      
    sounds.put(resnum, new DefaultSoundEffect(clip));
    return true;

  } catch (Exception ex) {

    ex.printStackTrace();
  }
  return false;
}
 
Example #16
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example #17
Source File: JDK13Services.java    From openjdk-jdk8u 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 #18
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
protected void loadAndPlayAudio(String path) {
  try (AudioInputStream sound = AudioSystem.getAudioInputStream(getClass().getResource(path));
       Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, sound.getFormat()))) {
    SecondaryLoop loop = Toolkit.getDefaultToolkit().getSystemEventQueue().createSecondaryLoop();
    clip.addLineListener(e -> {
      LineEvent.Type t = e.getType();
      if (Objects.equals(t, LineEvent.Type.STOP) || Objects.equals(t, LineEvent.Type.CLOSE)) {
        loop.exit();
      }
    });
    clip.open(sound);
    clip.start();
    loop.enter();
  } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
    ex.printStackTrace();
    Toolkit.getDefaultToolkit().beep();
  }
}
 
Example #19
Source File: Utils.java    From ripme with MIT License 6 votes vote down vote up
/**
 * Plays a sound from a file.
 *
 * @param filename Path to the sound file
 */
public static void playSound(String filename) {
    URL resource = ClassLoader.getSystemClassLoader().getResource(filename);
    try {
        final Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
        clip.addLineListener(event -> {
            if (event.getType() == LineEvent.Type.STOP) {
                clip.close();
            }
        });
        clip.open(AudioSystem.getAudioInputStream(resource));
        clip.start();
    } catch (Exception e) {
        LOGGER.error("Failed to play sound " + filename, e);
    }
}
 
Example #20
Source File: MultiClip.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Stops the clip, if active.
 */
public void stop() {
	try {
		Clip clip = getClip();
		if (clip == null)
			return;

		if (clip.isActive())
			clip.stop();
	} catch (LineUnavailableException e) {}
}
 
Example #21
Source File: MultiClip.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Plays the clip with the specified volume.
 * @param volume the volume the play at
 * @param listener the line listener
 * @throws LineUnavailableException if a clip object is not available or
 *         if the line cannot be opened due to resource restrictions
 */
public void start(float volume, LineListener listener) throws LineUnavailableException {
	Clip clip = getClip();
	if (clip == null)
		return;

	// PulseAudio does not support Master Gain
	if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
		// set volume
		FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
		float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
		gainControl.setValue(dB);
	}

	if (listener != null)
		clip.addLineListener(listener);
	clip.setFramePosition(0);
	clip.start();
}
 
Example #22
Source File: JDK13Services.java    From openjdk-jdk8u-backup 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 #23
Source File: SoundNotifier.java    From MercuryTrade with MIT License 6 votes vote down vote up
private void play(String wavPath, float db) {
    if (!dnd && db > -40) {
        ClassLoader classLoader = getClass().getClassLoader();
        try (AudioInputStream stream = AudioSystem.getAudioInputStream(classLoader.getResource(wavPath))) {
            Clip clip = AudioSystem.getClip();
            clip.open(stream);
            if (db != 0.0) {
                FloatControl gainControl =
                        (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
                gainControl.setValue(db);
            }
            clip.start();
        } catch (Exception e) {
            logger.error("Cannot start playing wav file: ", e);
        }
    }
}
 
Example #24
Source File: IsRunningHang.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void test(final AudioFormat format, final byte[] data)
        throws Exception {
    final Line.Info info = new DataLine.Info(Clip.class, format);
    final Clip clip = (Clip) AudioSystem.getLine(info);

    go = new CountDownLatch(1);
    clip.addLineListener(event -> {
        if (event.getType().equals(LineEvent.Type.START)) {
            go.countDown();
        }
    });

    clip.open(format, data, 0, data.length);
    clip.start();
    go.await();
    while (clip.isRunning()) {
        // This loop should not hang
    }
    while (clip.isActive()) {
        // This loop should not hang
    }
    clip.close();
}
 
Example #25
Source File: JDK13Services.java    From jdk8u-dev-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 #26
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example #27
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example #28
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 #29
Source File: SoftMixingMixer.java    From jdk8u_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 #30
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;
}