use of javax.sound.sampled.SourceDataLine in project competitive-programming by ttvi-cse.
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.SourceDataLine in project competitive-programming by ttvi-cse.
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();
}
use of javax.sound.sampled.SourceDataLine in project Symphony by Vazkii.
the class JavaSoundAudioDevice method createSource.
// createSource fix.
protected void createSource() throws JavaLayerException {
Throwable t = null;
try {
Line line = AudioSystem.getLine(getSourceLineInfo());
if (line instanceof SourceDataLine) {
source = (SourceDataLine) line;
// source.open(fmt, millisecondsToBytes(fmt, 2000));
source.open(fmt);
/*
if (source.isControlSupported(FloatControl.Type.MASTER_GAIN))
{
FloatControl c = (FloatControl)source.getControl(FloatControl.Type.MASTER_GAIN);
c.setValue(c.getMaximum());
}*/
source.start();
}
} catch (RuntimeException ex) {
t = ex;
} catch (LinkageError ex) {
t = ex;
} catch (LineUnavailableException ex) {
t = ex;
}
if (source == null)
throw new JavaLayerException("cannot obtain source audio line", t);
}
use of javax.sound.sampled.SourceDataLine in project lionengine by b3dgs.
the class WavImpl method play.
/**
* Play sound.
*
* @param media The sound media.
* @param alignment The sound alignment.
*/
private void play(Media media, Align alignment) {
if (opened.containsKey(media)) {
try {
opened.get(media).close();
} catch (final IOException exception) {
Verbose.exception(exception, media.toString());
}
}
count++;
try (Playback playback = createPlayback(cache)) {
opened.put(media, playback);
final AudioInputStream input = playback.getInput();
final SourceDataLine dataLine = playback.getDataLine();
openLine(dataLine, input);
updateAlignment(dataLine, alignment);
updateVolume(dataLine, AudioFactory.getVolume() * volume / Constant.HUNDRED);
dataLine.start();
readSound(input, dataLine);
close(input, dataLine);
} catch (final IOException | LineUnavailableException exception) {
if (last == null || exception.getMessage() != null && !exception.getMessage().equals(last.getMessage())) {
Verbose.exception(exception, media.toString());
last = exception;
}
} finally {
count--;
}
}
use of javax.sound.sampled.SourceDataLine in project selenium_java by sergueik.
the class Tone method sound.
/*
* @param frequency frequency of the generated tone in Hz
* @param duration duration of the generated tone in milliseconds
*/
public static void sound(final int frequency, final int duration) {
final float sampleRate = 8000;
final int bitsPerSample = 8;
byte[] buf = new byte[duration * bitsPerSample];
for (int i = 0; i < buf.length; i++) {
double angle = i / (sampleRate / frequency) * 2.0 * Math.PI;
buf[i] = (byte) (Math.sin(angle) * 80.0);
}
// MONO
final int channels = 1;
final boolean signed = true;
// little-endian
final boolean bigEndian = false;
AudioFormat af = new AudioFormat(sampleRate, bitsPerSample, channels, signed, bigEndian);
try (SourceDataLine sdl = AudioSystem.getSourceDataLine(af)) {
sdl.open(af);
sdl.start();
sdl.write(buf, 0, buf.length);
sdl.drain();
} catch (LineUnavailableException e) {
Toolkit.getDefaultToolkit().beep();
}
}
Aggregations