/** * Starts the mixer. */ final synchronized void start(Line line) { if (Printer.trace) Printer.trace(">> AbstractMixer: start(" + line + ")"); // $$kk: 06.11.99: ignore ourselves for now if (this.equals(line)) { if (Printer.trace) Printer.trace("<< AbstractMixer: start(" + line + ") nothing done"); return; } // we just start the mixer regardless of anything else here. if (!started) { if (Printer.debug) Printer.debug("AbstractMixer: start(line): starting the mixer"); implStart(); started = true; } if (Printer.trace) Printer.trace("<< AbstractMixer: start(" + line + ") succeeded"); }
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 AudioFormat getFirstLinearFormat(Line.Info[] infos) { for (int i = 0; i < infos.length; i++) { if (infos[i] instanceof DataLine.Info) { AudioFormat[] formats = ((DataLine.Info) infos[i]).getFormats(); for (int j = 0; j < formats.length; j++) { AudioFormat.Encoding encoding = formats[j].getEncoding(); int sampleSizeInBits = formats[j].getSampleSizeInBits(); if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED) && sampleSizeInBits == 16 || encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED) && sampleSizeInBits == 16) { return formats[j]; } } } } return null; }
/** * Constructs a new AbstractMixer. * @param mixer the mixer with which this line is associated * @param controls set of supported controls */ protected AbstractMixer(Mixer.Info mixerInfo, Control[] controls, Line.Info[] sourceLineInfo, Line.Info[] targetLineInfo) { // Line.Info, AbstractMixer, Control[] super(new Line.Info(Mixer.class), null, controls); // setup the line part this.mixer = this; if (controls == null) { controls = new Control[0]; } // setup the mixer part this.mixerInfo = mixerInfo; this.sourceLineInfo = sourceLineInfo; this.targetLineInfo = targetLineInfo; }
public final Line.Info[] getSourceLineInfo(Line.Info info) { int i; Vector vec = new Vector(); for (i = 0; i < sourceLineInfo.length; i++) { if (info.matches(sourceLineInfo[i])) { vec.addElement(sourceLineInfo[i]); } } Line.Info[] returnedArray = new Line.Info[vec.size()]; for (i = 0; i < returnedArray.length; i++) { returnedArray[i] = (Line.Info)vec.elementAt(i); } return returnedArray; }
public final boolean isLineSupported(Line.Info info) { int i; for (i = 0; i < sourceLineInfo.length; i++) { if (info.matches(sourceLineInfo[i])) { return true; } } for (i = 0; i < targetLineInfo.length; i++) { if (info.matches(targetLineInfo[i])) { return true; } } return false; }
/** * Close all lines and then close this mixer. */ public final synchronized void close() { if (Printer.trace) Printer.trace(">> AbstractMixer: close()"); if (isOpen()) { // close all source lines Line[] localLines = getSourceLines(); for (int i = 0; i<localLines.length; i++) { localLines[i].close(); } // close all target lines localLines = getTargetLines(); for (int i = 0; i<localLines.length; i++) { localLines[i].close(); } implClose(); // set the open state to false and send events setOpen(false); } manuallyOpened = false; if (Printer.trace) Printer.trace("<< AbstractMixer: close() succeeded"); }
/** * Returns the first complete Line.Info object it finds that * matches the one specified, or null if no matching Line.Info * object is found. */ final Line.Info getLineInfo(Line.Info info) { if (info == null) { return null; } // $$kk: 05.31.99: need to change this so that // the format and buffer size get set in the // returned info object for data lines?? for (int i = 0; i < sourceLineInfo.length; i++) { if (info.matches(sourceLineInfo[i])) { return sourceLineInfo[i]; } } for (int i = 0; i < targetLineInfo.length; i++) { if (info.matches(targetLineInfo[i])) { return targetLineInfo[i]; } } return null; }
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); } } }
@Override public final boolean isLineSupported(Line.Info info) { int i; for (i = 0; i < sourceLineInfo.length; i++) { if (info.matches(sourceLineInfo[i])) { return true; } } for (i = 0; i < targetLineInfo.length; i++) { if (info.matches(targetLineInfo[i])) { return true; } } return false; }
public final Line.Info[] getTargetLineInfo(Line.Info info) { int i; Vector vec = new Vector(); for (i = 0; i < targetLineInfo.length; i++) { if (info.matches(targetLineInfo[i])) { vec.addElement(targetLineInfo[i]); } } Line.Info[] returnedArray = new Line.Info[vec.size()]; for (i = 0; i < returnedArray.length; i++) { returnedArray[i] = (Line.Info)vec.elementAt(i); } return returnedArray; }
@Override public final Line.Info[] getSourceLineInfo(Line.Info info) { int i; Vector<Line.Info> vec = new Vector<>(); for (i = 0; i < sourceLineInfo.length; i++) { if (info.matches(sourceLineInfo[i])) { vec.addElement(sourceLineInfo[i]); } } Line.Info[] returnedArray = new Line.Info[vec.size()]; for (i = 0; i < returnedArray.length; i++) { returnedArray[i] = vec.elementAt(i); } return returnedArray; }
/** * Removes this line from the list of open source lines and * open target lines, if it exists in either. * If the list is now empty, closes the mixer. */ final synchronized void close(Line line) { if (Printer.trace) Printer.trace(">> AbstractMixer: close(" + line + ")"); // $$kk: 06.11.99: ignore ourselves for now if (this.equals(line)) { if (Printer.trace) Printer.trace("<< AbstractMixer: close(" + line + ") nothing done"); return; } sourceLines.removeElement(line); targetLines.removeElement(line); if (Printer.debug) Printer.debug("AbstractMixer: close(line): sourceLines.size() now: " + sourceLines.size()); if (Printer.debug) Printer.debug("AbstractMixer: close(line): targetLines.size() now: " + targetLines.size()); if (sourceLines.isEmpty() && targetLines.isEmpty() && !manuallyOpened) { if (Printer.trace) Printer.trace("AbstractMixer: close(" + line + "): need to close the mixer"); close(); } if (Printer.trace) Printer.trace("<< AbstractMixer: close(" + line + ") succeeded"); }
/** * 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; }
/** * Close all lines and then close this mixer. */ @Override public final synchronized void close() { if (Printer.trace) Printer.trace(">> AbstractMixer: close()"); if (isOpen()) { // close all source lines Line[] localLines = getSourceLines(); for (int i = 0; i<localLines.length; i++) { localLines[i].close(); } // close all target lines localLines = getTargetLines(); for (int i = 0; i<localLines.length; i++) { localLines[i].close(); } implClose(); // set the open state to false and send events setOpen(false); } manuallyOpened = false; if (Printer.trace) Printer.trace("<< AbstractMixer: close() succeeded"); }
@Override public final Line.Info[] getTargetLineInfo(Line.Info info) { int i; Vector<Line.Info> vec = new Vector<>(); for (i = 0; i < targetLineInfo.length; i++) { if (info.matches(targetLineInfo[i])) { vec.addElement(targetLineInfo[i]); } } Line.Info[] returnedArray = new Line.Info[vec.size()]; for (i = 0; i < returnedArray.length; i++) { returnedArray[i] = vec.elementAt(i); } return returnedArray; }
@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 Line[] getSourceLines() { Line[] localLines; synchronized (control_mutex) { if (mainmixer == null) return new Line[0]; SoftMixingDataLine[] sourceLines = mainmixer.getOpenLines(); localLines = new Line[sourceLines.length]; for (int i = 0; i < localLines.length; i++) { localLines[i] = sourceLines[i]; } } return localLines; }
public static void playSound(String filename) { URL resource = ClassLoader.getSystemClassLoader().getResource(filename); try { final Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class)); clip.addLineListener(event -> { if (event.getType() == LineEvent.Type.STOP) { clip.close(); } }); clip.open(AudioSystem.getAudioInputStream(resource)); clip.start(); } catch (Exception e) { logger.error("Failed to play sound " + filename, e); } }
public static HashMap<String, Line.Info> enumerateMicrophones() { HashMap<String, Line.Info> mics = new HashMap<String, Line.Info>(); Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo(); for (Mixer.Info info : mixerInfos) { Mixer m = AudioSystem.getMixer(info); Line.Info[] lineInfos = m.getTargetLineInfo(); if (lineInfos.length >= 1 && lineInfos[0].getLineClass().equals(TargetDataLine.class)) mics.put(info.getName(), lineInfos[0]); } return mics; }
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(); } } } }
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 Port.Info) { //return AudioSystem.NOT_SPECIFIED; // if several instances of PortMixerPort return 1; } return 0; }
/** * Constructs a new AbstractLine. * @param mixer the mixer with which this line is associated * @param controls set of supported controls */ protected AbstractLine(Line.Info info, AbstractMixer mixer, Control[] controls) { if (controls == null) { controls = new Control[0]; } this.info = info; this.mixer = mixer; this.controls = controls; }
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 int getMaxLines(Line.Info info) { if (info.getLineClass() == SourceDataLine.class) return AudioSystem.NOT_SPECIFIED; if (info.getLineClass() == Clip.class) return AudioSystem.NOT_SPECIFIED; return 0; }
public javax.sound.sampled.Line.Info[] getSourceLineInfo( javax.sound.sampled.Line.Info info) { int i; ArrayList<javax.sound.sampled.Line.Info> infos = new ArrayList<javax.sound.sampled.Line.Info>(); for (i = 0; i < sourceLineInfo.length; i++) { if (info.matches(sourceLineInfo[i])) { infos.add(sourceLineInfo[i]); } } return infos.toArray(new Line.Info[infos.size()]); }
public boolean isLineSupported(javax.sound.sampled.Line.Info info) { if (info != null) { for (int i = 0; i < sourceLineInfo.length; i++) { if (info.matches(sourceLineInfo[i])) { return true; } } } return false; }
/** * The default implementation of this method just determines whether * this line is a source or target line, calls open(no-arg) on the * mixer, and adds the line to the appropriate vector. * The mixer may be opened at a format different than the line's * format if it is a DataLine. */ final synchronized void open(Line line) throws LineUnavailableException { if (Printer.trace) Printer.trace(">> AbstractMixer: open(line = " + line + ")"); // $$kk: 06.11.99: ignore ourselves for now if (this.equals(line)) { if (Printer.trace) Printer.trace("<< AbstractMixer: open(" + line + ") nothing done"); return; } // source line? if (isSourceLine(line.getLineInfo())) { if (! sourceLines.contains(line) ) { // call the no-arg open method for the mixer; it should open at its // default format if it is not open yet open(false); // we opened successfully! add the line to the list sourceLines.addElement(line); } } else { // target line? if(isTargetLine(line.getLineInfo())) { if (! targetLines.contains(line) ) { // call the no-arg open method for the mixer; it should open at its // default format if it is not open yet open(false); // we opened successfully! add the line to the list targetLines.addElement(line); } } else { if (Printer.err) Printer.err("Unknown line received for AbstractMixer.open(Line): " + line); } } if (Printer.trace) Printer.trace("<< AbstractMixer: open(" + line + ") completed"); }
/** * Determines whether this is a source line for this mixer. * Right now this just checks whether it's supported, but should * check whether it actually belongs to this mixer.... */ final boolean isSourceLine(Line.Info info) { for (int i = 0; i < sourceLineInfo.length; i++) { if (info.matches(sourceLineInfo[i])) { return true; } } return false; }
public static void main(String argv[]) throws Exception { boolean success = true; Mixer.Info [] infos = AudioSystem.getMixerInfo(); for (int i=0; i<infos.length; i++) { Mixer mixer = AudioSystem.getMixer(infos[i]); System.out.println("Mixer is: " + mixer); Line.Info [] target_line_infos = mixer.getTargetLineInfo(); for (int j = 0; j < target_line_infos.length; j++) { try { System.out.println("Trying to get:" + target_line_infos[j]); mixer.getLine(target_line_infos[j]); } catch (IllegalArgumentException iae) { System.out.println("Unexpected IllegalArgumentException raised:"); iae.printStackTrace(); success = false; } catch (LineUnavailableException lue) { System.out.println("Unexpected LineUnavailableException raised:"); lue.printStackTrace(); success = false; } } } if (success) { System.out.println("Test passed"); } else { throw new Exception("Test FAILED"); } }
public static void main(String[] args) throws Exception { boolean res=true; Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); for (int i = 0; i < mixerInfo.length; i++) { Mixer mixer = AudioSystem.getMixer(mixerInfo[i]); System.out.println(mixer); Line.Info[] lineinfo = mixer.getSourceLineInfo(); for (int j = 0; j < lineinfo.length; j++) { System.out.println(" " + lineinfo[j]); try { AudioFormat[] formats = ((DataLine.Info)lineinfo[j]).getFormats(); for (int k = 0; k < formats.length; k++) { if ( (formats[k].getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED) || formats[k].getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) && (formats[k].getFrameSize() != AudioSystem.NOT_SPECIFIED) && ((formats[k].getSampleSizeInBits() == 16) || (formats[k].getSampleSizeInBits() == 8)) && ((((formats[k].getSampleSizeInBits() + 7) / 8) * formats[k].getChannels()) != formats[k].getFrameSize())) { System.out.println(" # " + formats[k] + ", getFrameSize() wrongly returns"+ formats[k].getFrameSize()); res=false; } } } catch (ClassCastException e) { } } } if (res) { System.out.println("Test passed"); } else { System.out.println("Test failed"); throw new Exception("Test failed"); } }
public static int run(String argv[], java.io.PrintStream out) { String testCaseID = "LineListener2001"; log.println("===== " + testCaseID + " ====="); boolean failed = false; Line l = null; // get the default SourceDataLine DataLine.Info s_info = new DataLine.Info(SourceDataLine.class, null); Line.Info infos[] = AudioSystem.getSourceLineInfo( s_info ); if( infos.length < 1 ) { log.println("Line.Info array == 0"); return STATUS_PASSED; } try { l = AudioSystem.getLine(infos[0]); } catch(SecurityException lue) { log.println("SecurityException"); return STATUS_PASSED; } catch (LineUnavailableException e1) { log.println("LUE"); return STATUS_PASSED; } catch (IllegalArgumentException iae) { log.println("IllegalArgumentException should not be thrown " + "for supported line"); iae.printStackTrace(log); return STATUS_FAILED; } out.println("Passed."); return STATUS_PASSED; }