當前位置: 首頁>>代碼示例>>Java>>正文


Java SourceDataLine.open方法代碼示例

本文整理匯總了Java中javax.sound.sampled.SourceDataLine.open方法的典型用法代碼示例。如果您正苦於以下問題:Java SourceDataLine.open方法的具體用法?Java SourceDataLine.open怎麽用?Java SourceDataLine.open使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.sound.sampled.SourceDataLine的用法示例。


在下文中一共展示了SourceDataLine.open方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: init

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
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();
}
 
開發者ID:Scoutdrago3,項目名稱:MusicToGraph,代碼行數:24,代碼來源:StdAudio.java

示例2: playGradient

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
public static void playGradient(double fstart,double fend,double duration,double volume,byte fadeend,byte wave) {
	byte[] freqdata = new byte[(int)(duration * SAMPLE_RATE)];
	
	// Generate the sound
	for(int i = 0; i < freqdata.length; i++) {
		freqdata[i] = (byte)generateValue(i, duration, fstart + (fend-fstart) * (i/(double)freqdata.length), volume, fadeend, wave);
	}
		
	// Play it
	try {
		final AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
		SourceDataLine line = AudioSystem.getSourceDataLine(af);
		line.open(af, SAMPLE_RATE);
		line.start();
		line.write(freqdata, 0, freqdata.length);
	    line.drain();
	    line.close();
	}catch(LineUnavailableException e) {
		e.printStackTrace();
	}
}
 
開發者ID:vanyle,項目名稱:Explorium,代碼行數:22,代碼來源:SoundGenerator.java

示例3: playSound

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/**
 * Play a sound at a given frequency freq during duration (in seconds) with volume as strenght
 * <br/><br/>
 * <code>SoundGenerator.playSound(440.0,1.0,0.5,SoundGenerator.FADE_LINEAR,SoundGenerator.WAVE_SIN);</code><br/>
 * Available fades : FADE_NONE, FADE_LINEAR, FADE_QUADRATIC<br/>
 * Available waves : WAVE_SIN, WAVE_SQUARE, WAVE_TRIANGLE, WAVE_SAWTOOTH<br/>
 */
public static void playSound(double freq,double duration,double volume,byte fade,byte wave){
	
	double[] soundData = generateSoundData(freq,duration,volume,fade,wave);
	byte[] freqdata = new byte[soundData.length];
	
	for(int i = 0;i < soundData.length;i++) {
		freqdata[i] = (byte)soundData[i];
	}
		
	// Play it
	try {
		final AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
		SourceDataLine line = AudioSystem.getSourceDataLine(af);
		line.open(af, SAMPLE_RATE);
		line.start();
		line.write(freqdata, 0, freqdata.length);
	    line.drain();
	    line.close();
	}catch(LineUnavailableException e) {
		e.printStackTrace();
	}
}
 
開發者ID:vanyle,項目名稱:Explorium,代碼行數:30,代碼來源:SoundGenerator.java

示例4: main

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
	AbstractRcomArgs a=new AbstractRcomArgs();
	UtilCli.parse(a, args, true);
	File folder=new File("/home/rizsi/tmp/video");
	byte[] data=UtilFile.loadFile(new File(folder, "remote.sw"));
	AudioFormat format=ManualTestEchoCancel.getFormat();
	final Mixer mixer = AudioSystem.getMixer(null);
	DataLine.Info info2= new DataLine.Info(SourceDataLine.class, format);
	SourceDataLine s=(SourceDataLine) mixer.getLine(info2);
	s.open(format, framesamples*2);
	s.start();
	try(LoopInputStream lis=new LoopInputStream(data))
	{
		try(JitterResampler rs=new JitterResampler(a, 8000, framesamples, 2))
		{
			new FeedThread(lis, rs).start();
			final byte[] buffer=new byte[framesamples*2];;
			while(true)
			{
				rs.readOutput(buffer);
				s.write(buffer, 0, buffer.length);
			}
		}
	}
}
 
開發者ID:rizsi,項目名稱:rcom,代碼行數:26,代碼來源:JitterExample.java

示例5: playAudioStream

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/** Plays audio from the given audio input stream.
 * 
 * @param stream the AudioInputStream to play.
 * @param startTime the time to skip to when playing starts.
 * A value of zero means this plays from the beginning, 1 means it skips one second, etc.
 * @param listener an optional Listener to update.
 * @param cancellable an optional Cancellable to consult.
 * @param blocking whether this call is blocking or not.
 * @throws LineUnavailableException if a line is unavailable.
 * @throws UnsupportedOperationException if this static method doesn't support playing the stream argument
 **/
public static SourceDataLine playAudioStream(AudioInputStream stream,StartTime startTime,Listener listener,Cancellable cancellable,boolean blocking) throws UnsupportedOperationException, LineUnavailableException {
	AudioFormat audioFormat = stream.getFormat();
	DataLine.Info info = new DataLine.Info( SourceDataLine.class, audioFormat );
	if ( !AudioSystem.isLineSupported( info ) ) {
		throw new UnsupportedOperationException("AudioPlayback.playAudioStream: info="+info );
	}
	
	final SourceDataLine dataLine = (SourceDataLine) AudioSystem.getLine( info );
	dataLine.open( audioFormat );
	dataLine.start();

	PlayAudioThread thread = new PlayAudioThread(stream, startTime, dataLine, listener, cancellable);
	if(blocking) {
		thread.run();
	} else {
		thread.start();
	}
	
	return dataLine;
}
 
開發者ID:mickleness,項目名稱:pumpernickel,代碼行數:32,代碼來源:AudioPlayer.java

示例6: init

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
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();
}
 
開發者ID:yimng,項目名稱:personal-stuff,代碼行數:22,代碼來源:StdAudio.java

示例7: main

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
public static void main(String[] args) throws LineUnavailableException, IOException {
  AudioPlayerManager manager = new DefaultAudioPlayerManager();
  AudioSourceManagers.registerRemoteSources(manager);
  manager.getConfiguration().setOutputFormat(new AudioDataFormat(2, 44100, 960, AudioDataFormat.Codec.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[format.bufferSize(2)];
  int chunkSize;

  while ((chunkSize = stream.read(buffer)) >= 0) {
    line.write(buffer, 0, chunkSize);
  }
}
 
開發者ID:sedmelluq,項目名稱:lavaplayer,代碼行數:27,代碼來源:LocalPlayerDemo.java

示例8: start

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/**
 * Starts the player
 */
public void start() {
    try {
        // start source line
        DataLine.Info info = new DataLine.Info(
                SourceDataLine.class, VideoFormat.getAudioFormat());
        mSourceLine = (SourceDataLine) AudioSystem.getLine(info);
        mSourceLine.open(VideoFormat.getAudioFormat());

        mPlayerThread = new Thread(this);
        mPlayerThread.setDaemon(true);
        mPlayerThread.start();
    } catch (LineUnavailableException ex) {
        Logs.error(getClass(), "Failed to start the audio line. ERROR: {0}", ex);
    }
}
 
開發者ID:dipu-bd,項目名稱:Tuntuni,代碼行數:19,代碼來源:AudioPlayer.java

示例9: reproduce

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
private static void reproduce( byte soundbytes[]) {
	try {
		DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, AudioFormatHelper.getAudioFormat());
		
		// El source data line se usa para escribir datos en el
		SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
		sourceDataLine.open(AudioFormatHelper.getAudioFormat());
		sourceDataLine.start();
		
		sourceDataLine.write(soundbytes, 0, soundbytes.length);
		sourceDataLine.drain();
		sourceDataLine.close();
	} catch (Exception e) {
		// Log and Handle exception
		e.printStackTrace();
	}
}
 
開發者ID:ldebello,項目名稱:javacuriosities,代碼行數:18,代碼來源:AudioReceiver.java

示例10: initializeOutput

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/**
 * FIXME:
 * specify the buffer size in the open(AudioFormat,int) method. A delay of 10ms-100ms will be acceptable for realtime audio. Very low latencies like will 
 * not work on all computer systems, and 100ms or more will probably be annoying for your users. A good tradeoff is, e.g. 50ms. For your audio format, 
 * 8-bit, mono at 44100Hz, a good buffer size is 2200 bytes, which is almost 50ms
 */
void initializeOutput() {
	
	DataLine.Info dataLineInfo = new DataLine.Info(  SourceDataLine.class, audioFormat);
	//line = (TargetDataLine) AudioSystem.getLine(info);
	//Mixer m = AudioSystem.getMixer(null);
	try {
		//sourceDataLine = (SourceDataLine)m.getLine(dataLineInfo);
		sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);
		sourceDataLine.open(audioFormat);
		sourceDataLine.start();
	} catch (LineUnavailableException e) {
		// TODO Auto-generated catch block
		e.printStackTrace(Log.getWriter());
	}

}
 
開發者ID:ac2cz,項目名稱:FoxTelem,代碼行數:23,代碼來源:SinkAudio.java

示例11: StreamingPlayback

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/**
 * Creates a new {@code StreamingPlayback}. StreamingPlayback objects will
 * always be created by their associated StreamingAudio object.
 *
 * @param audio
 *            The {@code Audio} that created this {@code StreamingPlayback}.
 * @param audioInStream
 *            The {@code AudioInputStream} used by this
 *            {@code StreamingPlayback}.
 * @param instanceID
 *            The {@code instanceID} of this {@code StreamingPlayback}.
 */
protected StreamingPlayback(Audio audio, AudioInputStream audioInStream,
		long instanceID) {

	super(audio, instanceID);
	this.audioInStream = audioInStream;

	AudioFormat audioFormat = audioInStream.getFormat();
	DataLine.Info info = new DataLine.Info(SourceDataLine.class,
			audioFormat);
	try {
		line = (SourceDataLine) AudioSystem.getLine(info);
		if (line != null) {
			line.open(audioFormat);
		}
		if (line.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
			volCtrl = (FloatControl) line
					.getControl(FloatControl.Type.MASTER_GAIN);
		} else {
			logger.warning("Master-Gain control is not supported."
					+ " Volume will be fixed at the default level.");
		}
	} catch (LineUnavailableException ex) {
		ex.printStackTrace();
	}
}
 
開發者ID:cmholton,項目名稱:QwickSound,代碼行數:38,代碼來源:StreamingPlayback.java

示例12: testOpenMidiDevice

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
@Test
public void testOpenMidiDevice() throws Exception
{
    FrinikaJVSTSynth synth = (FrinikaJVSTSynth) MidiSystem.getMidiDevice(new FrinikaJVSTSynthProvider.FrinikaJVSTSynthProviderInfo());
    final TargetDataLine line = (TargetDataLine)((Mixer)synth).getLine( new Line.Info(TargetDataLine.class));
    AudioFormat.Encoding PCM_FLOAT = new AudioFormat.Encoding("PCM_FLOAT");
    AudioFormat format = new AudioFormat(PCM_FLOAT, 44100, 32, 2, 4*2, 44100, ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN));
    line.open(format);

    AudioInputStream ais = new AudioInputStream(line);
    assertTrue(AudioSystem.isConversionSupported(Encoding.PCM_SIGNED, ais.getFormat()));

    AudioInputStream convertedAis = AudioSystem.getAudioInputStream(Encoding.PCM_SIGNED, ais);
    SourceDataLine sdl = AudioSystem.getSourceDataLine(convertedAis.getFormat());
    sdl.open();
    sdl.start();
    byte[] buf = new byte[16384];        
    ShortMessage shm = new ShortMessage();
    shm.setMessage(ShortMessage.NOTE_ON, 1, 40, 127);
    synth.getReceiver().send(shm,-1);
    for(int n=0;n<20;n++)
    {
        int read = convertedAis.read(buf);            
        sdl.write(buf, 0, read);
    }        
}
 
開發者ID:petersalomonsen,項目名稱:frinika,代碼行數:27,代碼來源:JVSTSynthMidiDeviceTest.java

示例13: startAudioOutput

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
public void startAudioOutput() {

		try {
			lineOut = (SourceDataLine) AudioSystem.getMixer(currentMixer)
					.getLine(infoOut);
			if (standardLatency)
				lineOut.open(format, bufferSize);
			else
				lineOut.open(format);

			lineOut.start();
			System.out.println("Buffersize: " + bufferSize + " / "
					+ lineOut.getBufferSize());
		} catch (Exception e) {
			lineOut = null;
			System.out
					.println("No audio output available. Use Audio Devices dialog to reconfigure.");
		}

		Thread thread = new Thread(this);
		thread.setPriority(Thread.MAX_PRIORITY);
		thread.start();
	}
 
開發者ID:petersalomonsen,項目名稱:frinika,代碼行數:24,代碼來源:OutputBufferTest.java

示例14: start

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
@Override
public void start() {
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    if (!AudioSystem.isLineSupported(info)) {
        // Handle the error.
        logger.severe("JavaSoundOutputStream - not supported." + format);
    } else {
        try {
            line = (SourceDataLine) getDataLine(info);
            int bufferSize = calculateBufferSize(suggestedOutputLatency);
            line.open(format, bufferSize);
            logger.fine("Output buffer size = " + bufferSize + " bytes.");
            line.start();

        } catch (Exception e) {
            e.printStackTrace();
            line = null;
        }
    }
}
 
開發者ID:philburk,項目名稱:jsyn,代碼行數:21,代碼來源:JavaSoundAudioDevice.java

示例15: playSounds

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/**
 * Same as playSound but plays several frequences at the same time<br/>
 * <code>
 * double[] freqs = {440.0,440*1.5}; <br/>
 * SoundGenerator.playSound(freqs,1.0,0.5,SoundGenerator.FADE_LINEAR,SoundGenerator.WAVE_SIN);
 * </code>
 * 
 */
public static void playSounds(double[] freqs,double duration,double volume,byte fade,byte wave) {
	if(freqs.length == 0) {
		System.err.println("No frequences to play !");
		return;
	}
	volume = volume / freqs.length; // ease volume to avoid overflow
	
	double[][] soundData = new double[freqs.length][];
	
	for(int i = 0;i < soundData.length;i++) {
		soundData[i] = generateSoundData(freqs[i],duration,volume,fade,wave);
	}
	byte[] freqdata = new byte[soundData[0].length];
	
	for(int i = 0;i < soundData[0].length;i++) {
		for(int j = 0;j < soundData.length;j++) {
			freqdata[i] += (byte)(soundData[j][i]);
		}
	}
		
	// Play it
	try {
		final AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
		SourceDataLine line = AudioSystem.getSourceDataLine(af);
		line.open(af, SAMPLE_RATE);
		line.start();
		line.write(freqdata, 0, freqdata.length);
	    line.drain();
	    line.close();
	}catch(LineUnavailableException e) {
		e.printStackTrace();
	}
}
 
開發者ID:vanyle,項目名稱:Explorium,代碼行數:42,代碼來源:SoundGenerator.java


注:本文中的javax.sound.sampled.SourceDataLine.open方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。