javax.sound.sampled.AudioSystem#getSourceDataLine ( )源码实例Demo

下面列出了javax.sound.sampled.AudioSystem#getSourceDataLine ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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();
}
 
源代码2 项目: buffer_bci   文件: SoundLine.java
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
}
 
源代码3 项目: coffee-gb   文件: AudioSystemSoundOutput.java
@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());
}
 
源代码4 项目: openjdk-jdk9   文件: ExtraCharInSoundbank.java
/**
 * 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;
}
 
源代码5 项目: openjdk-jdk9   文件: FloatControlBug.java
/**
 * 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;
}
 
源代码6 项目: openjdk-jdk9   文件: ClipSetEndPoint.java
/**
 * 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;
}
 
源代码7 项目: openjdk-jdk9   文件: ClipFlushCrash.java
/**
 * 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;
}
 
源代码8 项目: openjdk-jdk9   文件: ClipCloseLoss.java
/**
* 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;
}
 
源代码9 项目: openjdk-jdk9   文件: ClipDrain.java
/**
 * 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;
}
 
源代码10 项目: openjdk-jdk9   文件: ClipDuration.java
/**
 * 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;
}
 
源代码11 项目: openjdk-jdk9   文件: ClipLinuxCrash2.java
/**
 * 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;
}
 
源代码12 项目: openjdk-jdk9   文件: SDLLinuxCrash.java
/**
 * 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;
}
 
源代码13 项目: openjdk-jdk9   文件: DataLineInfoNegBufferSize.java
/**
 * 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;
}
 
源代码14 项目: openjdk-jdk9   文件: BufferSizeCheck.java
/**
 * 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;
}
 
源代码15 项目: openjdk-jdk9   文件: LongFramePosition.java
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.");
}
 
源代码16 项目: Logisim   文件: Buzzer.java
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");
}
 
源代码17 项目: open-ig   文件: Sounds.java
/**
 * 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;
}
 
源代码18 项目: mochadoom   文件: ClassicDoomSoundDriver.java
@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;
}
 
源代码19 项目: mochadoom   文件: SuperDoomSoundDriver.java
@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;

}
 
源代码20 项目: jmbe   文件: ToneGenerator.java
/**
     * 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);
        }
    }