Search in sources :

Example 1 with Clip

use of javax.sound.sampled.Clip in project JMRI by JMRI.

the class JavaSoundAudioSource method bindAudioBuffer.

@SuppressWarnings("SleepWhileInLoop")
@Override
boolean bindAudioBuffer(AudioBuffer audioBuffer) {
    // First check we've been initialised
    if (!initialised) {
        return false;
    }
    // Wait for AudioBuffer to be loaded, or 20 seconds
    long startTime = System.currentTimeMillis();
    while (audioBuffer.getState() != AudioBuffer.STATE_LOADED && System.currentTimeMillis() - startTime < 20000) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException ex) {
        }
    }
    if (audioBuffer instanceof JavaSoundAudioBuffer && audioBuffer.getState() == AudioBuffer.STATE_LOADED) {
        // Cast to JavaSoundAudioBuffer to enable easier access to specific methods
        JavaSoundAudioBuffer buffer = (JavaSoundAudioBuffer) audioBuffer;
        // Get a JavaSound DataLine and Clip
        DataLine.Info lineInfo;
        lineInfo = new DataLine.Info(Clip.class, buffer.getAudioFormat());
        Clip newClip;
        try {
            newClip = (Clip) mixer.getLine(lineInfo);
        } catch (LineUnavailableException ex) {
            log.warn("Error binding JavaSoundSource (" + this.getSystemName() + ") to AudioBuffer (" + this.getAssignedBufferName() + ") " + ex);
            return false;
        }
        this.clip = newClip;
        try {
            clip.open(buffer.getAudioFormat(), buffer.getDataStorageBuffer(), 0, buffer.getDataStorageBuffer().length);
        } catch (LineUnavailableException ex) {
            log.warn("Error binding JavaSoundSource (" + this.getSystemName() + ") to AudioBuffer (" + this.getAssignedBufferName() + ")" + ex);
        }
        if (log.isDebugEnabled()) {
            log.debug("Bind JavaSoundAudioSource (" + this.getSystemName() + ") to JavaSoundAudioBuffer (" + audioBuffer.getSystemName() + ")");
        }
        return true;
    } else {
        log.warn("AudioBuffer not loaded error when binding JavaSoundSource (" + this.getSystemName() + ") to AudioBuffer (" + this.getAssignedBufferName() + ")");
        return false;
    }
}
Also used : Clip(javax.sound.sampled.Clip) DataLine(javax.sound.sampled.DataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException)

Example 2 with Clip

use of javax.sound.sampled.Clip in project openblocks by mikaelhg.

the class SoundManager method loadSound.

public static Sound loadSound(String soundFileName) {
    final URL url = SoundManager.class.getResource(soundFileName);
    if (url == null) {
        System.out.println("Could not find resource " + soundFileName);
        return null;
    }
    AudioInputStream audioInputStream;
    try {
        audioInputStream = AudioSystem.getAudioInputStream(url);
    } catch (UnsupportedAudioFileException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
    final AudioFormat format = audioInputStream.getFormat();
    final Clip clip;
    try {
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        clip = (Clip) AudioSystem.getLine(info);
        clip.open(audioInputStream);
    } catch (LineUnavailableException e) {
        System.out.println("Sorry, sound is not available");
        return null;
    } catch (IOException e) {
        return null;
    }
    return new Sound(clip);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Clip(javax.sound.sampled.Clip) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) DataLine(javax.sound.sampled.DataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException) AudioFormat(javax.sound.sampled.AudioFormat) URL(java.net.URL)

Example 3 with Clip

use of javax.sound.sampled.Clip in project playn by threerings.

the class JavaAudio method createSound.

/**
   * Creates a sound instance from the audio data available via {@code in}.
   *
   * @param rsrc a resource via which the audio data can be read.
   * @param music if true, a custom {@link Clip} implementation will be used which can handle long
   * audio clips; if false, the default Java clip implementation is used which cannot handle long
   * audio clips.
   */
public JavaSound createSound(final JavaAssets.Resource rsrc, final boolean music) {
    final JavaSound sound = new JavaSound();
    ((JavaPlatform) platform).invokeAsync(new Runnable() {

        public void run() {
            try {
                AudioInputStream ais = rsrc.openAudioStream();
                Clip clip = AudioSystem.getClip();
                if (music) {
                    clip = new BigClip(clip);
                }
                AudioFormat baseFormat = ais.getFormat();
                if (baseFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
                    AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), // we have to force sample size to 16
                    16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), // big endian
                    false);
                    ais = AudioSystem.getAudioInputStream(decodedFormat, ais);
                }
                clip.open(ais);
                dispatchLoaded(sound, clip);
            } catch (Exception e) {
                dispatchLoadError(sound, e);
            }
        }
    });
    return sound;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Clip(javax.sound.sampled.Clip) AudioFormat(javax.sound.sampled.AudioFormat)

Example 4 with Clip

use of javax.sound.sampled.Clip in project intellij-community by JetBrains.

the class UIUtil method playSoundFromStream.

public static void playSoundFromStream(final Factory<InputStream> streamProducer) {
    new Thread(new Runnable() {

        // The wrapper thread is unnecessary, unless it blocks on the
        // Clip finishing; see comments.
        @Override
        public void run() {
            try {
                Clip clip = AudioSystem.getClip();
                InputStream stream = streamProducer.create();
                if (!stream.markSupported())
                    stream = new BufferedInputStream(stream);
                AudioInputStream inputStream = AudioSystem.getAudioInputStream(stream);
                clip.open(inputStream);
                clip.start();
            } catch (Exception ignore) {
                LOG.info(ignore);
            }
        }
    }, "play sound").start();
}
Also used : Clip(javax.sound.sampled.Clip) AudioInputStream(javax.sound.sampled.AudioInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Example 5 with Clip

use of javax.sound.sampled.Clip in project jdk8u_jdk by JetBrains.

the class ClipSetPos method main.

public static void main(String[] args) {
    boolean testPassed = true;
    Clip clip = null;
    try {
        clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, audioFormat));
        clip.open(audioFormat, dataBuffer, 0, dataBuffer.length);
    } catch (LineUnavailableException ex) {
        log(ex);
        log("Cannot test (this is not failure)");
        return;
    } catch (IllegalArgumentException ex) {
        log(ex);
        log("Cannot test (this is not failure)");
        return;
    }
    log("clip: " + clip.getClass().getName());
    int len = clip.getFrameLength();
    for (int pos = 0; pos < len; pos += (len / 100)) {
        clip.setFramePosition(pos);
        int curPos = clip.getFramePosition();
        if (Math.abs(pos - curPos) > MAX_FRAME_DELTA) {
            log("Tried to set pos to " + pos + ", but got back " + curPos);
            testPassed = false;
        } else {
            log("Sucessfully set pos to " + pos);
        }
    }
    clip.close();
    if (testPassed) {
        log("Test PASSED.");
    } else {
        log("Test FAILED.");
        throw new RuntimeException("Test FAILED (see log)");
    }
}
Also used : Clip(javax.sound.sampled.Clip) DataLine(javax.sound.sampled.DataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException)

Aggregations

Clip (javax.sound.sampled.Clip)6 AudioInputStream (javax.sound.sampled.AudioInputStream)4 DataLine (javax.sound.sampled.DataLine)4 LineUnavailableException (javax.sound.sampled.LineUnavailableException)4 IOException (java.io.IOException)3 AudioFormat (javax.sound.sampled.AudioFormat)3 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)2 AudioMetaData (ddf.minim.AudioMetaData)1 BufferedInputStream (java.io.BufferedInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 SourceDataLine (javax.sound.sampled.SourceDataLine)1 TargetDataLine (javax.sound.sampled.TargetDataLine)1 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)1