当前位置: 首页>>代码示例>>Java>>正文


Java SourceDataLine.write方法代码示例

本文整理汇总了Java中javax.sound.sampled.SourceDataLine.write方法的典型用法代码示例。如果您正苦于以下问题:Java SourceDataLine.write方法的具体用法?Java SourceDataLine.write怎么用?Java SourceDataLine.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.sound.sampled.SourceDataLine的用法示例。


在下文中一共展示了SourceDataLine.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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

示例2: 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

示例3: playSound

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
/**
 * Play a sound.
 *
 * @param in The {@code AudioInputStream} to play.
 * @return True if the stream was played without incident.
 * @exception IOException if unable to read or write the sound data.
 */
private boolean playSound(AudioInputStream in) throws IOException {
    boolean ret = false;

    SourceDataLine line = openLine(in.getFormat());
    if (line == null) return false;
    try {
        startPlaying();
        int rd;
        while (keepPlaying() && (rd = in.read(data)) > 0) {
            line.write(data, 0, rd);
        }
        ret = true;
    } finally {
        stopPlaying();
        line.drain();
        line.stop();
        line.close();
    }
    return ret;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:28,代码来源:SoundPlayer.java

示例4: run

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
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();
    }

}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:20,代码来源:SoftAudioPusher.java

示例5: run

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
@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();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:SoftAudioPusher.java

示例6: playRecorded

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:bug6372428.java

示例7: 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

示例8: 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

示例9: rawplay

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private 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();
  } 
}
 
开发者ID:debmalya,项目名称:JavaVAD,代码行数:22,代码来源:MP3Player.java

示例10: 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

示例11: rawplay

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private 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();
	}		
}
 
开发者ID:fredsa,项目名称:forplay,代码行数:22,代码来源:PlayerTest.java

示例12: rawplay

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private 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();
    }       
}
 
开发者ID:fredsa,项目名称:forplay,代码行数:22,代码来源:SkipTest.java

示例13: rawplay

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
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();
  }		
}
 
开发者ID:tino1b2be,项目名称:DTMF-Decoder,代码行数:23,代码来源:Test.java

示例14: 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

示例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.write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。