use of javax.sound.sampled.AudioFormat in project Minim by ddf.
the class JSMinim method getAudioRecordingClip.
/** @deprecated */
public AudioRecording getAudioRecordingClip(String filename) {
Clip clip = null;
AudioMetaData meta = null;
AudioInputStream ais = getAudioInputStream(filename);
if (ais != null) {
AudioFormat format = ais.getFormat();
if (format instanceof MpegAudioFormat) {
AudioFormat baseFormat = format;
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
// converts the stream to PCM audio from mp3 audio
ais = getAudioInputStream(format, ais);
}
DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat());
if (AudioSystem.isLineSupported(info)) {
// Obtain and open the line.
try {
clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
} catch (Exception e) {
error("Error obtaining Javasound Clip: " + e.getMessage());
return null;
}
Map<String, Object> props = getID3Tags(filename);
long lengthInMillis = -1;
if (props.containsKey("duration")) {
Long dur = (Long) props.get("duration");
lengthInMillis = dur.longValue() / 1000;
}
meta = new MP3MetaData(filename, lengthInMillis, props);
} else {
error("File format not supported.");
return null;
}
}
if (meta == null) {
// this means we're dealing with not-an-mp3
meta = new BasicMetaData(filename, clip.getMicrosecondLength() / 1000, -1);
}
return new JSAudioRecordingClip(clip, meta);
}
use of javax.sound.sampled.AudioFormat in project JWildfire by thargor6.
the class JWFAudioDevice method getSourceLineInfo.
protected DataLine.Info getSourceLineInfo() {
AudioFormat fmt = getAudioFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt);
return info;
}
use of javax.sound.sampled.AudioFormat in project JWildfire by thargor6.
the class JWFAudioDevice method getAudioFormat.
protected AudioFormat getAudioFormat() {
if (audioFormat == null) {
Decoder decoder = getDecoder();
audioFormat = new AudioFormat(decoder.getOutputFrequency(), 16, decoder.getOutputChannels(), true, false);
}
return audioFormat;
}
use of javax.sound.sampled.AudioFormat in project algs4 by kevin-wayne.
the class StdAudio method stream.
// https://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
// play a wav or aif file
// javax.sound.sampled.Clip fails for long clips (on some systems)
private static void stream(String filename) {
SourceDataLine line = null;
// 4K buffer
int BUFFER_SIZE = 4096;
try {
InputStream is = StdAudio.class.getResourceAsStream(filename);
AudioInputStream ais = AudioSystem.getAudioInputStream(is);
AudioFormat audioFormat = ais.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat);
line.start();
byte[] samples = new byte[BUFFER_SIZE];
int count = 0;
while ((count = ais.read(samples, 0, BUFFER_SIZE)) != -1) {
line.write(samples, 0, count);
}
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} finally {
if (line != null) {
line.drain();
line.close();
}
}
}
use of javax.sound.sampled.AudioFormat in project algs4 by kevin-wayne.
the class StdAudio method init.
// open up an audio stream
private static void init() {
try {
// 44,100 samples per second, 16-bit audio, mono, signed PCM, little Endian
AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);
// the internal buffer is a fraction of the actual buffer size, this choice is arbitrary
// it gets divided because we can't expect the buffered data to line up exactly with when
// the sound card decides to push out its samples.
buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE / 3];
} catch (LineUnavailableException e) {
System.out.println(e.getMessage());
}
// no sound gets made before this call
line.start();
}
Aggregations