Java Code Examples for javax.sound.sampled.AudioSystem#getSourceDataLine()

The following examples show how to use javax.sound.sampled.AudioSystem#getSourceDataLine() . 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: 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 2
Source File: BufferSizeCheck.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one soundcard is correctly installed
 * on the system.
 */
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: "+e);
    }
    if (!result) {
        System.err.println("Soundcard does not exist or sound drivers not installed!");
        System.err.println("This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 3
Source File: DataLineInfoNegBufferSize.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one soundcard is correctly installed
 * on the system.
 */
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: "+e);
    }
    if (!result) {
        System.err.println("Soundcard does not exist or sound drivers not installed!");
        System.err.println("This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 4
Source File: SDLLinuxCrash.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one soundcard is correctly installed
 * on the system.
 */
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: "+e);
    }
    if (!result) {
        System.err.println("Soundcard does not exist or sound drivers not installed!");
        System.err.println("This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 5
Source File: ClipLinuxCrash2.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one soundcard is correctly installed
 * on the system.
 */
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: "+e);
    }
    if (!result) {
        System.err.println("Soundcard does not exist or sound drivers not installed!");
        System.err.println("This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 6
Source File: ClipDuration.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one soundcard is correctly installed
 * on the system.
 */
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: "+e);
    }
    if (!result) {
        System.err.println("Soundcard does not exist or sound drivers not installed!");
        System.err.println("This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 7
Source File: ClipDrain.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one soundcard is correctly installed
 * on the system.
 */
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: "+e);
    }
    if (!result) {
        System.err.println("Soundcard does not exist or sound drivers not installed!");
        System.err.println("This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 8
Source File: ClipCloseLoss.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
* Returns true if at least one soundcard is correctly installed
* on the system.
*/
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: "+e);
    }
    if (!result) {
        System.err.println("Soundcard does not exist or sound drivers not installed!");
        System.err.println("This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 9
Source File: ClipFlushCrash.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one soundcard is correctly installed
 * on the system.
 */
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: "+e);
    }
    if (!result) {
        System.err.println("Soundcard does not exist or sound drivers not installed!");
        System.err.println("This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 10
Source File: ClipSetEndPoint.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one soundcard is correctly installed
 * on the system.
 */
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: " + e);
    }
    if (!result) {
        System.err.println(
                "Soundcard does not exist or sound drivers not installed!");
        System.err.println(
                "This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 11
Source File: FloatControlBug.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one soundcard is correctly installed
 * on the system.
 */
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: " + e);
    }
    if (!result) {
        System.err.println(
                "Soundcard does not exist or sound drivers not installed!");
        System.err.println(
                "This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 12
Source File: ExtraCharInSoundbank.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one soundcard is correctly installed
 * on the system.
 */
public static boolean isSoundcardInstalled() {
    boolean result = false;
    try {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        if (mixers.length > 0) {
            result = AudioSystem.getSourceDataLine(null) != null;
        }
    } catch (Exception e) {
        System.err.println("Exception occured: "+e);
    }
    if (!result) {
        System.err.println("Soundcard does not exist or sound drivers not installed!");
        System.err.println("This test requires sound drivers for execution.");
    }
    return result;
}
 
Example 13
Source File: AudioSystemSoundOutput.java    From coffee-gb with MIT License 6 votes vote down vote up
@Override
public void start() {
    if (line != null) {
        LOG.debug("Sound already started");
        return;
    }
    LOG.debug("Start sound");
    try {
        line = AudioSystem.getSourceDataLine(FORMAT);
        line.open(FORMAT, BUFFER_SIZE);
    } catch (LineUnavailableException e) {
        throw new RuntimeException(e);
    }
    line.start();
    buffer = new byte[line.getBufferSize()];
    divider = (int) (Gameboy.TICKS_PER_SEC / FORMAT.getSampleRate());
}
 
Example 14
Source File: SoundLine.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
void init(int sr, int bufferSize, int inbyte) throws javax.sound.sampled.LineUnavailableException {
  nbytes=inbyte;
  if ( nbytes >2 | nbytes<1 ) { // check for a supported bit depth
		throw new javax.sound.sampled.LineUnavailableException();
  }
  final AudioFormat audioFormat = new AudioFormat(sr,    // sample rate
																  nbytes*8, // sample size in bits
																  1,     // channels
																  true,  // signed
																  false  // bigendian
																  );	 
  soundLine = AudioSystem.getSourceDataLine(audioFormat);
  soundLine.open(audioFormat, bufferSize); // set audio buffer size
  start();
  audBuf=new byte[bufferSize*nbytes]; // N.B. increase size by number bytes/sample
}
 
Example 15
Source File: LongFramePosition.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    boolean failed = false;
    try {
        AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, false);
        SourceDataLine sdl = AudioSystem.getSourceDataLine(format);
        try {
            sdl.open(format);
            sdl.start();
            sdl.write(new byte[16384], 0, 16384);
            Thread.sleep(1000);
            int intPos = sdl.getFramePosition();
            long longPos = sdl.getLongFramePosition();
            System.out.println("After 1 second: getFramePosition() = "+intPos);
            System.out.println("            getLongFramePosition() = "+longPos);
            if (intPos <= 0 || longPos <= 0) {
                failed = true;
                System.out.println("## FAILED: frame position did not advance, or negative!");
            }
            if (Math.abs(intPos - longPos) > 100) {
                failed = true;
                System.out.println("## FAILED: frame positions are not the same!");
            }
        } finally {
            sdl.close();
        }
    } catch (LineUnavailableException | IllegalArgumentException e) {
        System.out.println(e);
        System.out.println("Cannot execute test.");
        return;
    }
    if (failed) throw new RuntimeException("Test FAILED!");
    System.out.println("Test Passed.");
}
 
Example 16
Source File: Buzzer.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
public void StartThread() {
	// avoid crash (for example if you connect a clock at 4KHz to the enable pin)
	if (Thread.activeCount() > 100)
		return;
	thread = new Thread(new Runnable() {
		@Override
		public void run() {
			SourceDataLine line = null;
			AudioFormat format = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
			try {
				line = AudioSystem.getSourceDataLine(format);
				line.open(format, SAMPLE_RATE / 10);
			} catch (Exception e) {
				e.printStackTrace();
				System.err.println("Could not initialise audio");
				return;
			}
			line.start();
			byte[] audioData = new byte[1];
			while (is_on.get()) {
				for (int i = 0; is_on.get() && i < SAMPLE_RATE * 2; i += 2) {
					audioData[0] = (byte) Math.round(Math.sin(Math.PI * i * hz / SAMPLE_RATE) * vol);
					line.write(audioData, 0, 1);
				}
			}
			line.stop();
			line.drain();
			line.close();
		}
	});
	thread.start();
	thread.setName("Sound Thread");
}
 
Example 17
Source File: Sounds.java    From open-ig with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Add a new SourceDataLine with the specified format to the sound pool.
 * @param aft the audio format to add
 * @return the data line created
 */
SourceDataLine addLine(AudioFormatType aft) {
	try {
		synchronized (this) { // FIX for Linux PulseAudio Mixer threading issue
			SourceDataLine sdl = AudioSystem.getSourceDataLine(aft.format);
			sdl.open(aft.format);
			lines.add(sdl);
			return sdl;
		}
	} catch (LineUnavailableException ex) {
		Exceptions.add(ex);
	}
	return null;
}
 
Example 18
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 19
Source File: SuperDoomSoundDriver.java    From mochadoom with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean InitSound() {

    // Secure and configure sound device first.
    System.err.print("I_InitSound: ");

    // We only need a single data line.
    // PCM, signed, 16-bit, stereo, 22025 KHz, 2048 bytes per "frame",
    // maximum of 44100/2048 "fps"
    AudioFormat format = new AudioFormat(SAMPLERATE, 16, 2, true, true);

    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    if (AudioSystem.isLineSupported(info))
        try {
            line = (SourceDataLine) AudioSystem.getSourceDataLine(format);
            line.open(format, AUDIOLINE_BUFFER);
        }	catch (Exception e) {
            e.printStackTrace();
            System.err.print("Could not play signed 16 data\n");
            return false;
        }

    if (line != null) {
        System.err.print("configured audio device\n");
        line.start();
    } else {
    	 System.err.print("could not configure audio device\n");
    	 return false;
    }

    SOUNDSRV = new PlaybackServer(line);
    SOUNDTHREAD = new Thread(SOUNDSRV);
    SOUNDTHREAD.setDaemon(true);
    SOUNDTHREAD.start();
    // Vroom!        
    MIXTHREAD= new Thread(MIXSRV);
    MIXTHREAD.setDaemon(true);
    MIXTHREAD.start();
    
    // Initialize external data (all sounds) at start, keep static.
    System.err.print("I_InitSound: ");

    super.initSound8();

    System.err.print("pre-cached all sound data\n");

    // Finished initialization.
    System.err.print("I_InitSound: sound module ready\n");
    
    return true;

}
 
Example 20
Source File: ClassicDoomSoundDriver.java    From mochadoom with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean InitSound() {

    // Secure and configure sound device first.
    System.out.println("I_InitSound: ");

    // We only need a single data line.
    // PCM, signed, 16-bit, stereo, 22025 KHz, 2048 bytes per "frame",
    // maximum of 44100/2048 "fps"
    AudioFormat format = new AudioFormat(SAMPLERATE, 16, 2, true, true);

    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    if (AudioSystem.isLineSupported(info))
        try {
            line = (SourceDataLine) AudioSystem.getSourceDataLine(format);
            line.open(format,AUDIOLINE_BUFFER);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.print("Could not play signed 16 data\n");
            return false;
        }

    if (line != null) {
        System.err.print(" configured audio device\n");
        line.start();
    } else {
    	 System.err.print(" could not configure audio device\n");
    	 return false;
    }

    // This was here only for debugging purposes
    /*
     * try { fos=new FileOutputStream("test.raw"); dao=new
     * DataOutputStream(fos); } catch (FileNotFoundException e) {
     * Auto-generated catch block e.printStackTrace(); }
     */

    SOUNDSRV = new MixServer(line);
    SOUNDTHREAD = new Thread(SOUNDSRV);
    SOUNDTHREAD.start();

    // Initialize external data (all sounds) at start, keep static.
    System.err.print("I_InitSound: ");

    super.initSound8();

    System.err.print(" pre-cached all sound data\n");

    // Now initialize mixbuffer with zero.
    initMixBuffer();

    // Finished initialization.
    System.err.print("I_InitSound: sound module ready\n");

    return true;
}