Sourcedataline Write Example Essay

Java Code Examples for javax.sound.sampled.DataLine

The following are top voted examples for showing how to use javax.sound.sampled.DataLine. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

Example 1

Example 2

Example 3

Example 4

Example 5

Example 6

Example 7

Example 8

Example 9

Example 10

Example 11

Example 12

Example 13

Example 14

Example 15

Example 16

Example 17

Example 18

Example 19

Example 20

Example 21

Example 22

Example 23

Example 24

Example 25

Example 26

Example 27

Example 28

Example 29

Example 30

Example 31

Example 32

Example 33

Example 34

Example 35

Example 36

Example 37

Example 38

Example 39

Example 40

Example 41

Example 42

Example 43

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(); }
/** Creates a new instance of test. Opens the microphone input as the target line. * To start the reporting, {@link #start} the thread. * @throws LineUnavailableException if microphone input is not available */ public VirtualDrummerMicrophoneInput () throws LineUnavailableException{ // getAudioInfo(); // prints lots of useless information format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,sampleRate,8,1,1,sampleRate,false); DataLine.Info dlinfo = new DataLine.Info(TargetDataLine.class, format); if ( AudioSystem.isLineSupported(dlinfo) ){ targetDataLine = (TargetDataLine)AudioSystem.getLine(dlinfo); } targetDataLine.open(format,bufferSize); bufferSize=targetDataLine.getBufferSize(); gui = new DrumSoundDetectorDemo(); gui.setVirtualDrummerMicrophoneInput(this); }
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); } } } }
private boolean createSourceDataLine() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()"); try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat); // fail silently return false; } SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info); datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (datapusher==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine."); return true; }
public ExtendedClip(JuggleMasterPro objPjuggleMasterPro, byte bytPsoundFileIndex) { this.bytGsoundFileIndex = bytPsoundFileIndex; try { final AudioInputStream objLaudioInputStream = AudioSystem.getAudioInputStream(new File(Strings.doConcat( objPjuggleMasterPro.strS_CODE_BASE, Constants.strS_FILE_NAME_A[Constants.intS_FILE_FOLDER_SOUNDS], objPjuggleMasterPro.chrGpathSeparator, Constants.strS_FILE_SOUND_NAME_A[bytPsoundFileIndex]))); final AudioFormat objLaudioFormat = objLaudioInputStream.getFormat(); final DataLine.Info objLdataLineInfo = new DataLine.Info(Clip.class, objLaudioFormat, (int) objLaudioInputStream.getFrameLength() * objLaudioFormat.getFrameSize()); this.objGclip = (Clip) AudioSystem.getLine(objLdataLineInfo); this.objGclip.open(objLaudioInputStream); } catch (final Throwable objPthrowable) { Tools.err("Error while initializing sound : ", Constants.strS_FILE_SOUND_NAME_A[bytPsoundFileIndex]); this.objGclip = null; } }
private boolean createSourceDataLine() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()"); try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat); // fail silently return false; } SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info); datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (datapusher==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine."); return true; }
private boolean createSourceDataLine() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()"); try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat); // fail silently return false; } SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info); datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (datapusher==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine."); return true; }
@Override public int getMaxLines(Line.Info info) { Line.Info fullInfo = getLineInfo(info); // if it's not supported at all, return 0. if (fullInfo == null) { return 0; } if (fullInfo instanceof DataLine.Info) { // DirectAudioDevices should mix ! return getMaxSimulLines(); } return 0; }
@Override public void startPlayback(final ISyncAudioSource resampler) { final AudioFormat format = StreamSourceAudio.getFormat(); new Thread("Audio output") { private byte[] buffer; public void run() { try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); try(SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) { line.open(format, StreamSourceAudio.requestBufferSize); line.start(); buffer=new byte[line.getBufferSize()]; while(!resampler.isClosed()) { resampler.readOutput(buffer); line.write(buffer, 0, buffer.length); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); }
private static void test(final AudioFormat format, final byte[] data) throws Exception { final Line.Info info = new DataLine.Info(Clip.class, format); final Clip clip = (Clip) AudioSystem.getLine(info); go = new CountDownLatch(1); clip.addLineListener(event -> { if (event.getType().equals(LineEvent.Type.START)) { go.countDown(); } }); clip.open(format, data, 0, data.length); clip.start(); go.await(); while (clip.isRunning()) { // This loop should not hang } while (clip.isActive()) { // This loop should not hang } clip.close(); }
private static void doLine2(DataLine line, AudioFormat format) { try { System.out.println(" - call to open()"); line.open(); try { System.out.println(" - line has format: "+line.getFormat()); if (!line.getFormat().matches(format)) { System.out.println("## Error: expected this format: "+format); failed++; } else { passed++; } } finally { line.close(); System.out.println(" - closed"); } } catch (Throwable t) { System.out.println(" - Caught exception. Not failed."); System.out.println(" - "+t.toString()); } }
private static void doMixerClip(Mixer mixer, AudioFormat format) { if (mixer==null) return; try { System.out.println("Clip from mixer "+mixer+":"); System.out.println(" "+mixer.getMixerInfo()); DataLine.Info info = new DataLine.Info( Clip.class, format); if (mixer.isLineSupported(info)) { Clip clip = (Clip) mixer.getLine(info); doLine1(clip, format); } else { System.out.println(" - Line not supported"); } } catch (Throwable t) { System.out.println(" - Caught exception. Not failed."); System.out.println(" - "+t.toString()); } }
private static void doMixerSDL(Mixer mixer, AudioFormat format) { if (mixer==null) return; try { System.out.println("SDL from mixer "+mixer+":"); DataLine.Info info = new DataLine.Info( SourceDataLine.class, format); if (mixer.isLineSupported(info)) { SourceDataLine sdl = (SourceDataLine) mixer.getLine(info); doLine1(sdl, format); doLine2(sdl, format); } else { System.out.println(" - Line not supported"); } } catch (Throwable t) { System.out.println(" - Caught exception. Not failed."); System.out.println(" - "+t.toString()); } }
private static void doMixerTDL(Mixer mixer, AudioFormat format) { if (mixer==null) return; try { System.out.println("TDL from mixer "+mixer+":"); DataLine.Info info = new DataLine.Info( TargetDataLine.class, format); if (mixer.isLineSupported(info)) { TargetDataLine tdl = (TargetDataLine) mixer.getLine(info); doLine1(tdl, format); doLine2(tdl, format); } else { System.out.println(" - Line not supported"); } } catch (Throwable t) { System.out.println(" - Caught exception. Not failed."); System.out.println(" - "+t.toString()); } }
public static void checkLines(Mixer mixer, Line.Info[] infos) { for (int i = 0; i<infos.length; i++) { try { if (infos[i] instanceof DataLine.Info) { DataLine.Info info = (DataLine.Info) infos[i]; System.out.println(" Line "+info+" (max. "+mixer.getMaxLines(info)+" simultaneously): "); AudioFormat[] formats = info.getFormats(); for (int f = 0; f < formats.length; f++) { try { AudioFormat otherEndianOrSign = getOtherEndianOrSign(formats[f]); if (otherEndianOrSign != null) { checkFormat(formats, otherEndianOrSign); } } catch (Exception e1) { out(" Unexpected exception when getting a format: "+e1); } } } } catch (Exception e) { out(" Unexpected exception when getting a line: "+e); } } }
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(); }
private void enableHorn () throws UnsupportedAudioFileException, IOException, LineUnavailableException { if ( ( this.clip == null || !this.clip.isRunning () ) && Activator.getDefault ().getPreferenceStore ().getBoolean ( PreferenceConstants.BELL_ACTIVATED_KEY ) ) { final AudioInputStream sound = AudioSystem.getAudioInputStream ( this.soundFile ); final DataLine.Info info = new DataLine.Info ( Clip.class, sound.getFormat () ); this.clip = (Clip)AudioSystem.getLine ( info ); this.clip.open ( sound ); this.clip.loop ( Clip.LOOP_CONTINUOUSLY ); } if ( !this.bellIcon.isDisposed () ) { this.bellIcon.setImage ( getBellIcon () ); } }
protected DataLine.Info getSourceLineInfo() { AudioFormat fmt = getAudioFormat(); //DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt, 4000); DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt); return info; }
/** * Constructs a Microphone with the given InputStream. */ @Override public void initialize() { super.initialize(); audioList = new LinkedBlockingQueue<Data>(); DataLine.Info info = new DataLine.Info(TargetDataLine.class, desiredFormat); /* * If we cannot get an audio line that matches the desired * characteristics, shoot for one that matches almost everything we * want, but has a higher sample rate. */ if (!AudioSystem.isLineSupported(info)) { logger.info(desiredFormat + " not supported"); AudioFormat nativeFormat = DataUtil.getNativeAudioFormat(desiredFormat, getSelectedMixer()); if (nativeFormat == null) { logger.severe("couldn't find suitable target audio format"); } else { finalFormat = nativeFormat; /* convert from native to the desired format if supported */ doConversion = AudioSystem.isConversionSupported(desiredFormat, nativeFormat); if (doConversion) { logger.info("Converting from " + finalFormat.getSampleRate() + "Hz to " + desiredFormat.getSampleRate() + "Hz"); } else { logger.info("Using native format: Cannot convert from " + finalFormat.getSampleRate() + "Hz to " + desiredFormat.getSampleRate() + "Hz"); } } } else { logger.info("Desired format: " + desiredFormat + " supported."); finalFormat = desiredFormat; } }
void getAudioInfo (){ Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo(); log.info(mixerInfos.length + " mixers"); for ( int i = 0 ; i < mixerInfos.length ; i++ ){ Mixer mixer = AudioSystem.getMixer(mixerInfos[i]); System.out.println("Mixer " + mixer); // target data lines Line.Info[] lineInfos = mixer.getTargetLineInfo(); System.out.println("\t" + lineInfos.length + " lineInfos"); for ( int j = 0 ; j < lineInfos.length ; j++ ){ if ( lineInfos[j] instanceof DataLine.Info ){ AudioFormat[] formats = ( (DataLine.Info)lineInfos[j] ).getFormats(); System.out.println("\t\t\t" + formats.length + " formats"); for ( int k = 0 ; k < formats.length ; k++ ){ System.out.println("\t\tFormat " + formats[k]); } } Line line = null; try{ line = mixer.getLine(lineInfos[j]); System.out.println("\tLine " + line); } catch ( LineUnavailableException e ){ e.printStackTrace(); } } } }
/** Initialize local mixer: microphone */ public void initializeMixer() { audioFormat = new AudioFormat(References.SAMPLE_RATE, References.SAMPLE_SIZE_IN_BITS, References.CHANNELS, References.SIGNED, References.BIG_ENDIAN); dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat); try { targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo); targetDataLine.open(audioFormat); } catch (Exception e) { e.printStackTrace(); } }
public void openMixer() { InputStream byteArrayInputStream = new ByteArrayInputStream(playbuffer); audioInputStream = new AudioInputStream(byteArrayInputStream, audioFormat, playbuffer.length / audioFormat.getFrameSize()); DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat); try { sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); sourceDataLine.open(audioFormat); sourceDataLine.start(); } catch (LineUnavailableException e) { e.printStackTrace(); } }
private boolean createClip() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createClip()"); try { DataLine.Info info = new DataLine.Info(Clip.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Clip not supported: "+loadedAudioFormat); // fail silently return false; } Object line = AudioSystem.getLine(info); if (!(line instanceof AutoClosingClip)) { if (DEBUG || Printer.err)Printer.err("Clip is not auto closing!"+clip); // fail -> will try with SourceDataLine return false; } clip = (AutoClosingClip) line; clip.setAutoClosing(true); if (DEBUG || Printer.debug) clip.addLineListener(this); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (clip==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Loaded clip."); return true; }
SoftMixingDataLine(SoftMixingMixer mixer, DataLine.Info info) { this.mixer = mixer; this.info = info; this.control_mutex = mixer.control_mutex; controls = new Control[] { gain_control, mute_control, balance_control, pan_control, reverbsend_control, chorussend_control, apply_reverb }; calcVolume(); }
public Line getLine(Line.Info info) throws LineUnavailableException { if (!isLineSupported(info)) throw new IllegalArgumentException("Line unsupported: " + info); if ((info.getLineClass() == SourceDataLine.class)) { return new SoftMixingSourceDataLine(this, (DataLine.Info) info); } if ((info.getLineClass() == Clip.class)) { return new SoftMixingClip(this, (DataLine.Info) info); } throw new IllegalArgumentException("Line unsupported: " + info); }
private boolean createClip() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createClip()"); try { DataLine.Info info = new DataLine.Info(Clip.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Clip not supported: "+loadedAudioFormat); // fail silently return false; } Object line = AudioSystem.getLine(info); if (!(line instanceof AutoClosingClip)) { if (DEBUG || Printer.err)Printer.err("Clip is not auto closing!"+clip); // fail -> will try with SourceDataLine return false; } clip = (AutoClosingClip) line; clip.setAutoClosing(true); if (DEBUG || Printer.debug) clip.addLineListener(this); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (clip==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Loaded clip."); return true; }
SoftMixingDataLine(SoftMixingMixer mixer, DataLine.Info info) { this.mixer = mixer; this.info = info; this.control_mutex = mixer.control_mutex; controls = new Control[] { gain_control, mute_control, balance_control, pan_control, reverbsend_control, chorussend_control, apply_reverb }; calcVolume(); }
public Line getLine(Line.Info info) throws LineUnavailableException { if (!isLineSupported(info)) throw new IllegalArgumentException("Line unsupported: " + info); if ((info.getLineClass() == SourceDataLine.class)) { return new SoftMixingSourceDataLine(this, (DataLine.Info) info); } if ((info.getLineClass() == Clip.class)) { return new SoftMixingClip(this, (DataLine.Info) info); } throw new IllegalArgumentException("Line unsupported: " + info); }
public AsyncLineStopper(DataLine line, long delayMS) { this.line = line; this.delayMS = delayMS; thread = new Thread(this); thread.setDaemon(true); // starts the thread and waits until it becomes ready synchronized (readyEvent) { thread.start(); try { readyEvent.wait(); } catch (InterruptedException ex) { } } }
private boolean createClip() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createClip()"); try { DataLine.Info info = new DataLine.Info(Clip.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Clip not supported: "+loadedAudioFormat); // fail silently return false; } Object line = AudioSystem.getLine(info); if (!(line instanceof AutoClosingClip)) { if (DEBUG || Printer.err)Printer.err("Clip is not auto closing!"+clip); // fail -> will try with SourceDataLine return false; } clip = (AutoClosingClip) line; clip.setAutoClosing(true); if (DEBUG || Printer.debug) clip.addLineListener(this); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (clip==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Loaded clip."); return true; }
DirectAudioDevice(DirectAudioDeviceProvider.DirectAudioDeviceInfo portMixerInfo) { // pass in Line.Info, mixer, controls super(portMixerInfo, // Mixer.Info null, // Control[] null, // Line.Info[] sourceLineInfo null); // Line.Info[] targetLineInfo if (Printer.trace) Printer.trace(">> DirectAudioDevice: constructor"); // source lines DirectDLI srcLineInfo = createDataLineInfo(true); if (srcLineInfo != null) { sourceLineInfo = new Line.Info[2]; // SourcedataLine sourceLineInfo[0] = srcLineInfo; // Clip sourceLineInfo[1] = new DirectDLI(Clip.class, srcLineInfo.getFormats(), srcLineInfo.getHardwareFormats(), 32, // arbitrary minimum buffer size AudioSystem.NOT_SPECIFIED); } else { sourceLineInfo = new Line.Info[0]; } // TargetDataLine DataLine.Info dstLineInfo = createDataLineInfo(false); if (dstLineInfo != null) { targetLineInfo = new Line.Info[1]; targetLineInfo[0] = dstLineInfo; } else { targetLineInfo = new Line.Info[0]; } if (Printer.trace) Printer.trace("<< DirectAudioDevice: constructor completed"); }
protected DirectDL(DataLine.Info info, DirectAudioDevice mixer, AudioFormat format, int bufferSize, int mixerIndex, int deviceID, boolean isSource) { super(info, mixer, null, format, bufferSize); if (Printer.trace) Printer.trace("DirectDL CONSTRUCTOR: info: " + info); this.mixerIndex = mixerIndex; this.deviceID = deviceID; this.waitTime = 10; // 10 milliseconds default wait time this.isSource = isSource; }
private DirectSDL(DataLine.Info info, AudioFormat format, int bufferSize, DirectAudioDevice mixer) { super(info, mixer, format, bufferSize, mixer.getMixerIndex(), mixer.getDeviceID(), true); if (Printer.trace) Printer.trace("DirectSDL CONSTRUCTOR: completed"); }
private DirectTDL(DataLine.Info info, AudioFormat format, int bufferSize, DirectAudioDevice mixer) { super(info, mixer, format, bufferSize, mixer.getMixerIndex(), mixer.getDeviceID(), false); if (Printer.trace) Printer.trace("DirectTDL CONSTRUCTOR: completed"); }
public Mic(Mixer mixer, AudioFormat format, int frameSamples) throws LineUnavailableException { super(); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); t=(TargetDataLine) mixer.getLine(info); t.open(format, frameSamples*2); System.out.println("Bytebuffer t: "+t.getBufferSize()); }
SoftMixingDataLine(SoftMixingMixer mixer, DataLine.Info info) { this.mixer = mixer; this.info = info; this.control_mutex = mixer.control_mutex; controls = new Control[] { gain_control, mute_control, balance_control, pan_control, reverbsend_control, chorussend_control, apply_reverb }; calcVolume(); }
@Override public Line getLine(Line.Info info) throws LineUnavailableException { if (!isLineSupported(info)) throw new IllegalArgumentException("Line unsupported: " + info); if ((info.getLineClass() == SourceDataLine.class)) { return new SoftMixingSourceDataLine(this, (DataLine.Info) info); } if ((info.getLineClass() == Clip.class)) { return new SoftMixingClip(this, (DataLine.Info) info); } throw new IllegalArgumentException("Line unsupported: " + info); }
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(SpeexResampler resampler=new SpeexResampler(a, framesamples, new ResampledReceiver(s))) { final byte[] buffer=new byte[framesamples*2];; while(true) { UtilStream.readFully(buffer, lis, buffer.length); feed(resampler, buffer); } } // byte[] buffer=new byte[framesamples*2]; // while(true) // { // UtilStream.readFully(buffer, resampled, buffer.length); // } } }
protected DataLine.Info getSourceLineInfo() { AudioFormat fmt = getAudioFormat(); //DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt, 4000); DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt); return info; }
private static void doMixerClip(Mixer mixer) throws Exception { boolean waitedEnough=false; try { DataLine.Info info = new DataLine.Info(Clip.class, format); Clip clip = (Clip) mixer.getLine(info); clip.open(format, soundData, 0, soundData.length); // sanity if (clip.getMicrosecondLength()/1000 < 9900) { throw new Exception("clip's microsecond length should be at least 9900000, but it is "+clip.getMicrosecondLength()); } long start = System.currentTimeMillis(); System.out.println(" ---------- start --------"); clip.start(); // give time to actually start it. ALSA implementation needs that... Thread.sleep(300); System.out.println("drain ... "); clip.drain(); long elapsedTime = System.currentTimeMillis() - start; System.out.println("close ... "); clip.close(); System.out.println("... done"); System.out.println("Playback duration: "+elapsedTime+" milliseconds."); waitedEnough = elapsedTime >= ((clip.getMicrosecondLength() / 1000) - TOLERANCE_MS); } catch (Throwable t) { System.out.println(" - Caught exception. Not failed."); System.out.println(" - "+t.toString()); return; } if (!waitedEnough) { throw new Exception("Drain did not wait long enough to play entire clip."); } successfulTests++; }
public long start() throws Exception { AudioFormat format = new AudioFormat(44100, 16, 2, true, false); if (addLen) { staticLen+=(int) (staticLen/5)+1000; } else { staticLen-=(int) (staticLen/5)+1000; } if (staticLen>8*44100*4) { staticLen = 8*44100*4; addLen=!addLen; } if (staticLen<1000) { staticLen = 1000; addLen=!addLen; } int len = staticLen; len -= (len % 4); out(" Test program: preparing to play back "+len+" bytes == "+bytes2Ms(len, format)+"ms audio..."); byte[] fakedata=new byte[len]; InputStream is = new ByteArrayInputStream(fakedata); AudioInputStream ais = new AudioInputStream(is, format, fakedata.length/format.getFrameSize()); DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat()); clip = (Clip) AudioSystem.getLine(info); clip.addLineListener(this); out(" Test program: opening clip="+((clip==null)?"null":clip.toString())); clip.open(ais); ais.close(); out(" Test program: starting clip="+((clip==null)?"null":clip.toString())); clip.start(); return bytes2Ms(fakedata.length, format); }
public static SourceDataLine start() throws Exception { AudioFormat format = new AudioFormat(44100, 16, 2, true, false); if (addLen) { staticLen+=(int) (staticLen/5)+1000; } else { staticLen-=(int) (staticLen/5)+1000; } if (staticLen>8*44100*4) { staticLen = 8*44100*4; addLen=!addLen; } if (staticLen<1000) { staticLen = 1000; addLen=!addLen; } int len = staticLen; len -= (len % 4); out(" preparing to play back "+len+" bytes == "+bytes2Ms(len, format)+"ms audio..."); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); SourceDataLine sdl = (SourceDataLine) AudioSystem.getLine(info); sdl.addLineListener(new LineListener() { public void update(LineEvent e) { if (e.getType() == LineEvent.Type.STOP) { out(" calling close() from event dispatcher thread"); ((SourceDataLine) e.getSource()).close(); } else if (e.getType() == LineEvent.Type.CLOSE) { } } }); out(" opening..."); sdl.open(); out(" starting..."); sdl.start(); (new Thread(new SDLLinuxCrash(sdl, len))).start(); return sdl; }

Java Code Examples for javax.sound.sampled.SourceDataLine

The following are top voted examples for showing how to use javax.sound.sampled.SourceDataLine. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

Example 1

Example 2

Example 3

Example 4

Example 5

Example 6

Example 7

Example 8

Example 9

Example 10

Example 11

Example 12

Example 13

Example 14

Example 15

Example 16

Example 17

Example 18

Example 19

Example 20

Example 21

Example 22

Example 23

Example 24

Example 25

Example 26

Example 27

Example 28

Example 29

Example 30

Example 31

Example 32

Example 33

Example 34

Example 35

Example 36

Example 37

Example 38

Example 39

Example 40

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(); }
JSAudioRecording(JSMinim sys, byte[] samps, SourceDataLine sdl, AudioMetaData mdata) { system = sys; samples = samps; meta = mdata; format = sdl.getFormat(); finished = false; line = sdl; loop = false; play = false; numLoops = 0; loopBegin = 0; loopEnd = (int)AudioUtils.millis2BytesFrameAligned( meta.length(), format ); rawBytes = new byte[sdl.getBufferSize() / 8]; iothread = null; totalBytesRead = 0; bytesWritten = 0; shouldRead = true; }
/** * Finishes the recording process by closing the file. */ public AudioRecordingStream save() { try { aos.close(); } catch (IOException e) { Minim.error("AudioRecorder.save: An error occurred when trying to save the file:\n" + e.getMessage()); } String filePath = filePath(); AudioInputStream ais = system.getAudioInputStream(filePath); SourceDataLine sdl = system.getSourceDataLine(ais.getFormat(), 1024); // this is fine because the recording will always be // in a raw format (WAV, AU, etc). long length = AudioUtils.frames2Millis(ais.getFrameLength(), format); BasicMetaData meta = new BasicMetaData(filePath, length, ais.getFrameLength()); JSPCMAudioRecordingStream recording = new JSPCMAudioRecordingStream(system, meta, ais, sdl, 1024); return recording; }
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(); } }
/** * 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(); } }
/** * 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; }
private boolean createSourceDataLine() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()"); try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat); // fail silently return false; } SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info); datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (datapusher==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine."); return true; }
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(); } }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
private boolean createSourceDataLine() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()"); try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat); // fail silently return false; } SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info); datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (datapusher==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine."); return true; }
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(); } }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
private boolean createSourceDataLine() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()"); try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat); // fail silently return false; } SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info); datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (datapusher==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine."); return true; }
@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(); } }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class<?> typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
private static void doMixerSDL(Mixer mixer, AudioFormat format) { if (mixer==null) return; try { System.out.println("SDL from mixer "+mixer+":"); DataLine.Info info = new DataLine.Info( SourceDataLine.class, format); if (mixer.isLineSupported(info)) { SourceDataLine sdl = (SourceDataLine) mixer.getLine(info); doLine1(sdl, format); doLine2(sdl, format); } else { System.out.println(" - Line not supported"); } } catch (Throwable t) { System.out.println(" - Caught exception. Not failed."); System.out.println(" - "+t.toString()); } }
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(); }
/** * 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; }
@Override public void startPlayback(final ISyncAudioSource resampler) { final AudioFormat format = StreamSourceAudio.getFormat(); new Thread("Audio output") { private byte[] buffer; public void run() { try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); try(SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) { line.open(format, StreamSourceAudio.requestBufferSize); line.start(); buffer=new byte[line.getBufferSize()]; while(!resampler.isClosed()) { resampler.readOutput(buffer); line.write(buffer, 0, buffer.length); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); }
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); } } } }
/** * Returns all available mixers. * * @return A List of available Mixers */ public List<String> getMixers() { List<String> mixers = new ArrayList<>(); // Obtains an array of mixer info objects that represents the set of // audio mixers that are currently installed on the system. Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo(); if (mixerInfos != null) Arrays.stream(mixerInfos).forEach(mInfo -> { // line info Line.Info lineInfo = new Line.Info(SourceDataLine.class); Mixer mixer = AudioSystem.getMixer(mInfo); // if line supported if (mixer.isLineSupported(lineInfo)) mixers.add(mInfo.getName()); }); return mixers; }
private boolean createSourceDataLine() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()"); try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat); // fail silently return false; } SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info); datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (datapusher==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine."); return true; }
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(); } }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class<?> typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
/** 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; }
@Override public boolean isVisible(JList list, Object value, int row, boolean isSelected, boolean cellHasFocus) { SoundSource sound = (SoundSource)value; SoundUI ui = getSoundUI(sound); if(!isSelected) { SourceDataLine dataLine = ui.line; if(dataLine!=null) dataLine.close(); } boolean visible = isSelected; if(!visible) { ui.setTargetOpacity(0); } if(ui.targetOpacity>0) visible = true; return visible; }
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(); }
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); } }
private boolean createSourceDataLine() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()"); try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat); // fail silently return false; } SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info); datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (datapusher==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine."); return true; }
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(); } }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
private boolean createSourceDataLine() { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()"); try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat); if (!(AudioSystem.isLineSupported(info)) ) { if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat); // fail silently return false; } SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info); datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength); } catch (Exception e) { if (DEBUG || Printer.err)e.printStackTrace(); // fail silently return false; } if (datapusher==null) { // fail silently return false; } if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine."); return true; }
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(); } }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
public TGMixer() { super(new Line.Info(Mixer.class)); this.lines = new HashMap<Class<?>, Line>(); this.sourceLineInfo = new ArrayList<DataLine.Info>(); this.targetLineInfo = new ArrayList<DataLine.Info>(); List<AudioFormat> formats = new ArrayList<AudioFormat>(); for (int channels = 1; channels <= 2; channels++) { formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, 8, channels, channels, AudioSystem.NOT_SPECIFIED, false)); formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, 8, channels, channels, AudioSystem.NOT_SPECIFIED, false)); for (int bits = 16; bits < 32; bits += 8) { formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, false)); formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, false)); formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, true)); formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, true)); } formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4, AudioSystem.NOT_SPECIFIED, false)); formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4, AudioSystem.NOT_SPECIFIED, true)); formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8, AudioSystem.NOT_SPECIFIED, false)); formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8, AudioSystem.NOT_SPECIFIED, true)); } this.sourceLineInfo.add(new DataLine.Info(SourceDataLine.class, formats.toArray(new AudioFormat[formats.size()]), AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED)); }
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(); } }
/** Checks if a Mixer is appropriate. A Mixer is considered appropriate if it support the given line type. If isMixingRequired is true and the line type is an output one (SourceDataLine, Clip), the mixer is appropriate if it supports at least 2 (concurrent) lines of the given type. @return true if the mixer is considered appropriate according to the rules given above, false otherwise. */ private static boolean isAppropriateMixer(Mixer mixer, Line.Info lineInfo, boolean isMixingRequired) { if (! mixer.isLineSupported(lineInfo)) { return false; } Class lineClass = lineInfo.getLineClass(); if (isMixingRequired && (SourceDataLine.class.isAssignableFrom(lineClass) || Clip.class.isAssignableFrom(lineClass))) { int maxLines = mixer.getMaxLines(lineInfo); return ((maxLines == NOT_SPECIFIED) || (maxLines > 1)); } return true; }
/** * 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); } }
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(); } }

0 thoughts on “Sourcedataline Write Example Essay”

    -->

Leave a Comment

Your email address will not be published. Required fields are marked *