Search in sources :

Example 26 with SourceDataLine

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();
        }
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) SourceDataLine(javax.sound.sampled.SourceDataLine) IOException(java.io.IOException) AudioFormat(javax.sound.sampled.AudioFormat)

Example 27 with SourceDataLine

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();
}
Also used : DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) AudioFormat(javax.sound.sampled.AudioFormat)

Example 28 with SourceDataLine

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);
}
Also used : Line(javax.sound.sampled.Line) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) SourceDataLine(javax.sound.sampled.SourceDataLine) JavaLayerException(vazkii.symphony.lib.javazoom.jl.decoder.JavaLayerException)

Example 29 with SourceDataLine

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--;
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) LineUnavailableException(javax.sound.sampled.LineUnavailableException) SourceDataLine(javax.sound.sampled.SourceDataLine) IOException(java.io.IOException)

Example 30 with SourceDataLine

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();
    }
}
Also used : LineUnavailableException(javax.sound.sampled.LineUnavailableException) SourceDataLine(javax.sound.sampled.SourceDataLine) AudioFormat(javax.sound.sampled.AudioFormat)

Aggregations

SourceDataLine (javax.sound.sampled.SourceDataLine)34 LineUnavailableException (javax.sound.sampled.LineUnavailableException)21 AudioFormat (javax.sound.sampled.AudioFormat)19 DataLine (javax.sound.sampled.DataLine)19 IOException (java.io.IOException)14 AudioInputStream (javax.sound.sampled.AudioInputStream)14 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)5 Line (javax.sound.sampled.Line)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Mixer (javax.sound.sampled.Mixer)3 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)3 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 TargetDataLine (javax.sound.sampled.TargetDataLine)2 AudioDataFormat (com.sedmelluq.discord.lavaplayer.format.AudioDataFormat)1 AudioPlayerInputStream (com.sedmelluq.discord.lavaplayer.format.AudioPlayerInputStream)1 COMMON_PCM_S16_BE (com.sedmelluq.discord.lavaplayer.format.StandardAudioDataFormats.COMMON_PCM_S16_BE)1 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)1 AudioPlayerManager (com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager)1 DefaultAudioPlayerManager (com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager)1