FileDocCategorySizeDatePackage
DirectSoundAuto.javaAPI DocJMF 2.1.1e3234Mon May 12 12:20:34 BST 2003None

DirectSoundAuto

public class DirectSoundAuto extends Object

Fields Summary
private static final String
detectClass
CaptureDeviceInfo[]
devices
Constructors Summary
public DirectSoundAuto()

	boolean supported = false;
	// instance JavaSoundDetector to check is javasound's capture is availabe
	try {
	    Class cls = Class.forName(detectClass);
	    supported = true;
	} catch (Throwable t) {
	    supported = false;
	    // t.printStackTrace();
	}
	
	System.out.println("DirectSound Capture Supported = " + supported);

	if (supported) {
	    // It's there, start to register JavaSound with CaptureDeviceManager
	    Vector devices = (Vector) CaptureDeviceManager.getDeviceList(null).clone();
	    
	    // remove the old direct sound capturers
	    String name;
	    Enumeration enum = devices.elements();
	    while (enum.hasMoreElements()) {
		CaptureDeviceInfo cdi = (CaptureDeviceInfo) enum.nextElement();	
		name = cdi.getName();
		if (name.startsWith(com.sun.media.protocol.dsound.DataSource.NAME))
		    CaptureDeviceManager.removeDevice(cdi);
	    }
	    int LE = AudioFormat.LITTLE_ENDIAN;
	    int SI = AudioFormat.SIGNED;
	    int US = AudioFormat.UNSIGNED;
	    int UN = AudioFormat.NOT_SPECIFIED;
	    float [] Rates = new float[] {
		48000, 44100, 32000, 22050, 16000, 11025, 8000
	    };
	    Vector formats = new Vector(4);
	    for (int rateIndex = 0; rateIndex < Rates.length; rateIndex++) {
		float rate = Rates[rateIndex];
		AudioFormat af;
		af = new AudioFormat(AudioFormat.LINEAR, rate, 16, 2, LE, SI);
		if (supports(af)) formats.addElement(af);
		af = new AudioFormat(AudioFormat.LINEAR, rate, 16, 1, LE, SI);
		if (supports(af)) formats.addElement(af);
		af = new AudioFormat(AudioFormat.LINEAR, rate, 8, 2, UN, US);
		if (supports(af)) formats.addElement(af);
		af = new AudioFormat(AudioFormat.LINEAR, rate, 8, 1, UN, US);
		if (supports(af)) formats.addElement(af);
	    }

	    AudioFormat [] formatArray = new AudioFormat[formats.size()];
	    for (int fa = 0; fa < formatArray.length; fa++)
		formatArray[fa] = (AudioFormat) formats.elementAt(fa);

	    CaptureDeviceInfo cdi = new CaptureDeviceInfo(
		com.sun.media.protocol.dsound.DataSource.NAME,
		new MediaLocator("dsound://"),
		formatArray);
	    CaptureDeviceManager.addDevice(cdi);
	    try {
		CaptureDeviceManager.commit();
		System.out.println("DirectSoundAuto: Committed ok");
	    } catch (java.io.IOException ioe) {
		System.err.println("DirectSoundAuto: error committing cdm");
	    }
	}
    
Methods Summary
public static voidmain(java.lang.String[] args)


         
       new DirectSoundAuto();
       System.exit(0);
    
private booleansupports(javax.media.format.AudioFormat af)

	try {
	    com.sun.media.protocol.dsound.DSound ds;
	    ds = new com.sun.media.protocol.dsound.DSound(af, 1024);
	    ds.open();
	    ds.close();
	} catch (Exception e) {
	    System.err.println(e);
	    return false;
	}
	return true;