Search in sources :

Example 36 with AudioInputStream

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

the class BigClip method open.

@Override
public void open(AudioFormat format, byte[] data, int offset, int bufferSize) throws LineUnavailableException {
    byte[] input = new byte[bufferSize];
    for (int ii = 0; ii < input.length; ii++) {
        input[ii] = data[offset + ii];
    }
    ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    try {
        AudioInputStream ais1 = AudioSystem.getAudioInputStream(inputStream);
        AudioInputStream ais2 = AudioSystem.getAudioInputStream(format, ais1);
        open(ais2);
    } catch (UnsupportedAudioFileException uafe) {
        throw new IllegalArgumentException(uafe);
    } catch (IOException ioe) {
        throw new IllegalArgumentException(ioe);
    }
// TODO - throw IAE for invalid frame size, format.
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException)

Example 37 with AudioInputStream

use of javax.sound.sampled.AudioInputStream 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 38 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project openhab1-addons by openhab.

the class TTSServiceMaryTTS method say.

/**
     * {@inheritDoc}
     */
public void say(String text, String voiceName, String outputDevice) {
    if (marytts == null) {
        logger.error("Mary TTS is not available");
        return;
    }
    if (text == null) {
        return;
    }
    Voice voice = null;
    if (StringUtils.isBlank(voiceName)) {
        logger.debug("Mary TTS: {} (Voice not set. Using default voice {}).", new String[] { text, defaultVoice.toString() });
        voice = defaultVoice;
    } else {
        voice = Voice.getVoice(voiceName);
        logger.debug("Mary TTS: {} (Voice: {})", new String[] { text, voiceName });
    }
    if (voice != null) {
        // Workaround: we have to set the Locale first, because only in the LocalMaryInterface.setLocale() method
        // the required private method
        // LocalMaryInterface.setAudioFileFormatForVoice() method is called. After that we can set the voice,
        // otherwise an NPE occurs
        marytts.setLocale(voice.getLocale());
        marytts.setVoice(voice.getName());
        try {
            AudioInputStream audio = marytts.generateAudio(text);
            AudioPlayer player = new AudioPlayer(audio);
            player.start();
            player.join();
        } catch (SynthesisException e) {
            logger.error("Error during tts generation: {}", e.getLocalizedMessage(), e);
        } catch (InterruptedException e) {
            logger.error("Error during tts playback: {}", e.getLocalizedMessage(), e);
        }
    } else {
        logger.error("Could not find voice: {}", voiceName);
        logger.info("Available Voices are {} ", StringUtils.join(marytts.getAvailableVoices(), ", "));
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) AudioPlayer(marytts.util.data.audio.AudioPlayer) Voice(marytts.modules.synthesis.Voice) SynthesisException(marytts.exceptions.SynthesisException)

Example 39 with AudioInputStream

use of javax.sound.sampled.AudioInputStream 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 40 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project d54 by mitrisdev.

the class AudioProcessor method openChannel.

/**
	 * Open the audio channel so data can be captured.  This must be called once before frameUpdate or the
	 * data accessors are called.
	 */
public void openChannel() {
    try {
        line.open();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    line.start();
    line.drain();
    input = new AudioInputStream(line);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) LineUnavailableException(javax.sound.sampled.LineUnavailableException)

Aggregations

AudioInputStream (javax.sound.sampled.AudioInputStream)66 AudioFormat (javax.sound.sampled.AudioFormat)29 IOException (java.io.IOException)25 InputStream (java.io.InputStream)20 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)14 ByteArrayInputStream (java.io.ByteArrayInputStream)11 AudioFileFormat (javax.sound.sampled.AudioFileFormat)11 FileInputStream (java.io.FileInputStream)10 BufferedInputStream (java.io.BufferedInputStream)9 File (java.io.File)9 LineUnavailableException (javax.sound.sampled.LineUnavailableException)9 SourceDataLine (javax.sound.sampled.SourceDataLine)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DataInputStream (java.io.DataInputStream)5 SequenceInputStream (java.io.SequenceInputStream)5 Clip (javax.sound.sampled.Clip)5 DataLine (javax.sound.sampled.DataLine)5 AudioSynthesizer (com.sun.media.sound.AudioSynthesizer)3 SoftSynthesizer (com.sun.media.sound.SoftSynthesizer)3 AudioMetaData (ddf.minim.AudioMetaData)3