Java Code Examples for javax.sound.sampled.SourceDataLine#write()

The following examples show how to use javax.sound.sampled.SourceDataLine#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SoftAudioPusher.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example 2
Source File: SoftAudioPusher.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example 3
Source File: bug6372428.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void playRecorded(AudioFormat format, byte[] data) throws Exception {
    //SourceDataLine line = AudioSystem.getSourceDataLine(format);
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info);

    line.open();
    line.start();

    int remaining = data.length;
    while (remaining > 0) {
        int avail = line.available();
        if (avail > 0) {
            if (avail > remaining)
                avail = remaining;
            int written = line.write(data, data.length - remaining, avail);
            remaining -= written;
            log("Playing: " + written + " bytes written");
        } else {
            delay(100);
        }
    }

    line.drain();
    line.stop();
}
 
Example 4
Source File: LocalPlayerDemo.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws LineUnavailableException, IOException {
  AudioPlayerManager manager = new DefaultAudioPlayerManager();
  AudioSourceManagers.registerRemoteSources(manager);
  manager.getConfiguration().setOutputFormat(COMMON_PCM_S16_BE);

  AudioPlayer player = manager.createPlayer();

  manager.loadItem("ytsearch: epic soundtracks", new FunctionalResultHandler(null, playlist -> {
    player.playTrack(playlist.getTracks().get(0));
  }, null, null));

  AudioDataFormat format = manager.getConfiguration().getOutputFormat();
  AudioInputStream stream = AudioPlayerInputStream.createStream(player, format, 10000L, false);
  SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat());
  SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

  line.open(stream.getFormat());
  line.start();

  byte[] buffer = new byte[COMMON_PCM_S16_BE.maximumChunkSize()];
  int chunkSize;

  while ((chunkSize = stream.read(buffer)) >= 0) {
    line.write(buffer, 0, chunkSize);
  }
}
 
Example 5
Source File: SoftAudioPusher.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }
}
 
Example 6
Source File: SoftAudioPusher.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example 7
Source File: SoftAudioPusher.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example 8
Source File: SoftAudioPusher.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example 9
Source File: SoundUtils.java    From Neural-Network-Programming-with-Java-SecondEdition with MIT License 6 votes vote down vote up
public static void tone(int hz, int msecs, double vol) throws LineUnavailableException {
	byte[] buf = new byte[1];
	AudioFormat af = new AudioFormat(SAMPLE_RATE, // sampleRate
			8, // sampleSizeInBits
			1, // channels
			true, // signed
			false); // bigEndian
	SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
	sdl.open(af);
	sdl.start();
	for (int i = 0; i < msecs * 8; i++) {
		double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
		buf[0] = (byte) (Math.sin(angle) * 127.0 * vol);
		sdl.write(buf, 0, 1);
	}

	sdl.drain();
	sdl.stop();
	sdl.close();
}
 
Example 10
Source File: SoftAudioPusher.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example 11
Source File: Test.java    From DTMF-Decoder with MIT License 6 votes vote down vote up
private static void rawplay(AudioFormat targetFormat, 
                                   AudioInputStream din) throws IOException, LineUnavailableException
{
   byte[] data = new byte[4096];
  SourceDataLine line = getLine(targetFormat);		
  if (line != null)
  {
     // Start
    line.start();
     int nBytesRead = 0, nBytesWritten = 0;
     while (nBytesRead != -1)
    {
        nBytesRead = din.read(data, 0, data.length);
         if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
    }
     // Stop
    line.drain();
    line.stop();
    line.close();
    din.close();
  }		
}
 
Example 12
Source File: SoftAudioPusher.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example 13
Source File: SoftAudioPusher.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    byte[] buffer = SoftAudioPusher.this.buffer;
    AudioInputStream ais = SoftAudioPusher.this.ais;
    SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;

    try {
        while (active) {
            // Read from audio source
            int count = ais.read(buffer);
            if(count < 0) break;
            // Write byte buffer to source output
            sourceDataLine.write(buffer, 0, count);
        }
    } catch (IOException e) {
        active = false;
        //e.printStackTrace();
    }

}
 
Example 14
Source File: AudioPlayer.java    From google-assistant-java-demo with GNU General Public License v3.0 5 votes vote down vote up
public void play(byte[] sound) throws AudioException {
    try {
        AudioFormat format = AudioUtil.getAudioFormat(audioConf);
        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
        SourceDataLine speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
        speakers.open(format);
        speakers.start();
        speakers.write(sound, 0, sound.length);
        speakers.drain();
        speakers.close();
    } catch (Exception e) {
        throw new AudioException("Unable to play the response", e);
    }
}
 
Example 15
Source File: SoundPlay.java    From Data_Processor with Apache License 2.0 5 votes vote down vote up
public void Play(byte[] secbytesarray, AudioFormat af, SourceDataLine line1, int sp, byte[] data) throws LineUnavailableException{
	for(int i=0;i<sp;i++){
		for(int j=0;j<data.length;j++){
			data[j]=secbytesarray[j+i*data.length];
		}
		line1.write(data, 0, data.length);
	}
}
 
Example 16
Source File: ChangingBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean doMixerSDL(Mixer mixer, AudioFormat format) {
    if (mixer==null) return false;
    try {
        System.out.println("Trying mixer "+mixer+":");
            DataLine.Info info = new DataLine.Info(
                                      SourceDataLine.class,
                                      format,
                                      (int) samplerate);

            SourceDataLine sdl = (SourceDataLine) mixer.getLine(info);
        System.out.println("  - got sdl: "+sdl);
        System.out.println("  - open with format "+format);
        sdl.open(format);
        System.out.println("  - start...");
        sdl.start();
        System.out.println("  - write...");
        sdl.write(buffer, 0, buffer.length);
        Thread.sleep(200);
        System.out.println("  - drain...");
        sdl.drain();
        System.out.println("  - stop...");
        sdl.stop();
        System.out.println("  - close...");
        sdl.close();
        System.out.println("  - closed");
    } catch (Throwable t) {
        System.out.println("  - Caught exception. Not failed.");
        System.out.println("  - "+t.toString());
        return false;
    }
    return true;
}
 
Example 17
Source File: ToneGenerator.java    From jmbe with GNU General Public License v3.0 4 votes vote down vote up
/**
     * Test harness
     * @param args not used
     */
    public static void main(String[] args)
    {
        ToneGenerator toneGenerator = new ToneGenerator();

        AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
            8000.0f, 16, 1, 2, 8000.0f, false);
        DataLine.Info datalineinfo = new DataLine.Info(SourceDataLine.class, audioFormat);

        if(AudioSystem.isLineSupported(datalineinfo))
        {
            try
            {
                SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine(audioFormat);
                sourceDataLine.open(audioFormat);

                for(Tone tone: Tone.DTMF_TONES)
//                for(Tone tone: Tone.KNOX_TONES)
//                for(Tone tone: Tone.CALL_PROGRESS_TONES)
//                for(Tone tone: Tone.DISCRETE_TONES)
//                for(Tone tone: Tone.values())
                {
                    for(int x = 0; x < 128; x++) //Amplitude levels 0 - 127
                    {
                        System.out.print("\rTONE [" + tone.name() + "]: " + tone + " " + tone.getFrequency1() +
                            (tone.hasFrequency2() ? " PLUS " + tone.getFrequency2() : "") + " AMPLITUDE:" + x);

                        ToneParameters toneParameters = new ToneParameters(tone, x);

                        float[] samples = toneGenerator.generate(toneParameters);

                        ByteBuffer converted = ByteBuffer.allocate(samples.length * 2);
                        converted.order(ByteOrder.LITTLE_ENDIAN);

                        for(float sample : samples)
                        {
                            converted.putShort((short)(sample * Short.MAX_VALUE));
                        }

                        byte[] bytes = converted.array();
                        sourceDataLine.write(bytes, 0, bytes.length);

                        if(x == 0)
                        {
                            sourceDataLine.start();
                        }
                    }

                    System.out.println("\rTONE [" + tone.name() + "]: " + tone + " " + tone.getFrequency1() +
                        (tone.hasFrequency2() ? " PLUS " + tone.getFrequency2() : ""));
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else
        {
            System.out.println("Audio Format Not Supported by Host Audio System: " + audioFormat);
        }
    }
 
Example 18
Source File: DirectSoundUnderrunSilence.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void play(Mixer mixer) {
    int res = 0;
    try {
        println("Getting SDL from mixer...");
        source = (SourceDataLine) mixer.getLine(info);
        println("Opening SDL...");
        source.open(audioFormat);
        println("Writing data to SDL...");
        source.write(audioData, 0, audioData.length);
        println("Starting SDL...");
        source.start();
        println("Now open your ears:");
        println("You should have heard a short tone,");
        println("followed by silence (no repeating tones).");
        key();
        source.write(audioData, 0, audioData.length);
        println("Now you should have heard another short tone.");
        println("If you did not hear a second tone, or more than 2 tones,");
        println("the test is FAILED.");
        println("otherwise, if you heard a total of 2 tones, the bug is fixed.");
        key();
    } catch (IllegalArgumentException iae) {
        println("IllegalArgumentException: "+iae.getMessage());
        println("Sound device cannot handle this audio format.");
        println("ERROR: Test environment not correctly set up.");
        if (source!=null) {
            source.close();
            source = null;
        }
        return;
    } catch (LineUnavailableException lue) {
        println("LineUnavailableException: "+lue.getMessage());
        println("This is normal for some mixers.");
    } catch (Exception e) {
        println("Unexpected Exception: "+e.toString());
    }
    if (source != null) {
        println("Stopping...");
        source.stop();
        println("Closing...");
        source.close();
        println("Closed.");
        source = null;
    }
}
 
Example 19
Source File: DirectSoundRepeatingBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void play(Mixer mixer) {
    int res = 0;
    try {
        println("Getting SDL from mixer...");
        source = (SourceDataLine) mixer.getLine(info);
        println("Opening SDL...");
        source.open(audioFormat);
        println("Writing data to SDL...");
        source.write(audioData, 0, audioData.length);
        println("Starting SDL...");
        source.start();
        println("Now open your ears:");
        println("- you should have heard a short tone,");
        println("  followed by silence.");
        println("- if after a while you hear repeated tones,");
        println("  the bug is NOT fixed.");
        println("- if the program remains silent after the ");
        println("  initial tone, the bug is fixed.");
        key();
    } catch (IllegalArgumentException iae) {
        println("IllegalArgumentException: "+iae.getMessage());
        println("Sound device cannot handle this audio format.");
        println("ERROR: Test environment not correctly set up.");
        if (source!=null) {
            source.close();
            source = null;
        }
        return;
    } catch (LineUnavailableException lue) {
        println("LineUnavailableException: "+lue.getMessage());
        println("This is normal for some mixers.");
    } catch (Exception e) {
        println("Unexpected Exception: "+e.toString());
    }
    if (source != null) {
        println("Stopping...");
        source.stop();
        println("Closing...");
        source.close();
        println("Closed.");
        source = null;
    }
}
 
Example 20
Source File: TickAtEndOfPlay.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.out.println("This test should only be run on Windows.");
    System.out.println("Make sure that the speakers are connected and the volume is up.");
    System.out.println("Close all other programs that may use the soundcard.");

    System.out.println("You'll hear a 2-second tone. when the tone finishes,");
    System.out.println("  there should be no noise. If you hear a short tick/noise,");
    System.out.println("  the bug still applies.");

    System.out.println("Press ENTER to continue.");
    System.in.read();

    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("1")) WorkAround1 = true;
        if (args[i].equals("2")) WorkAround2 = true;
    }
    if (WorkAround1) System.out.println("Using work around1: appending silence");
    if (WorkAround2) System.out.println("Using work around2: waiting before close");

    int zerolen = 0; // how many 0-bytes will be appended to playback
    if (WorkAround1) zerolen = 1000;
    int seconds = 2;
    int sampleRate = 8000;
    double frequency = 1000.0;
    double RAD = 2.0 * Math.PI;
    AudioFormat af = new AudioFormat((float)sampleRate,8,1,true,true);
    System.out.println("Format: "+af);
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,af);
    SourceDataLine source = (SourceDataLine)AudioSystem.getLine(info);
    System.out.println("Line: "+source);
    if (source.toString().indexOf("MixerSourceLine")>=0) {
        System.out.println("This test only applies to non-Java Sound Audio Engine!");
        return;
    }
    System.out.println("Opening...");
    source.open(af);
    System.out.println("Starting...");
    source.start();
    int datalen = sampleRate * seconds;
    byte[] buf = new byte[datalen+zerolen];
    for (int i=0; i<datalen; i++) {
        buf[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
    }
    System.out.println("Writing...");
    source.write(buf,0,buf.length);
    System.out.println("Draining...");
    source.drain();
    System.out.println("Stopping...");
    source.stop();
    if (WorkAround2) {
        System.out.println("Waiting 200 millis...");
        Thread.sleep(200);
    }
    System.out.println("Closing...");
    source.close();
    System.out.println("Done.");
}