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.
}
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;
}
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(), ", "));
}
}
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);
}
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);
}
Aggregations