FileDocCategorySizeDatePackage
JavaSoundRenderer.javaAPI DocJMF 2.1.1e7920Mon May 12 12:20:48 BST 2003com.sun.media.renderer.audio

JavaSoundRenderer

public class JavaSoundRenderer extends AudioRenderer implements ExclusiveUse
JavaSoundRenderer
version

Fields Summary
static String
NAME
Codec
ulawDecoder
Format
ulawOutputFormat
Format
ulawFormat
Format
linearFormat
static int
METERHEIGHT
static boolean
available
Buffer
decodeBuffer
Constructors Summary
public JavaSoundRenderer()

    
     
	String javaVersion = null;
	String subver = null;
	int len;

	try {
	    javaVersion = (String)System.getProperty("java.version");
            if ( javaVersion.length() < 3 )
		len = javaVersion.length();
	    else 
		len = 3;
	    subver = javaVersion.substring(0,len);
	} catch (Throwable t) {
	    javaVersion = null;
	    subver = null;
	}

	if ( (subver == null) || (subver.compareTo("1.3") <  0)) {
	    try {
		JMFSecurityManager.loadLibrary("jmutil");
		JMFSecurityManager.loadLibrary("jsound");
		available = true;
	    } catch (Throwable t) {
	    }
	} else {
	    available = true;
	}
    
        super();

	if (!available)
	    throw new UnsatisfiedLinkError("No JavaSound library");
	
	ulawFormat = new javax.media.format.AudioFormat(
				AudioFormat.ULAW);
 	linearFormat = new javax.media.format.AudioFormat(
				AudioFormat.LINEAR);
	supportedFormats = new javax.media.Format[2];
	supportedFormats[0] = linearFormat;
	supportedFormats[1] = ulawFormat;

	gainControl = new GCA(this);
	peakVolumeMeter = new PeakVolumeMeter(this);
    
Methods Summary
protected com.sun.media.renderer.audio.device.AudioOutputcreateDevice(javax.media.format.AudioFormat format)

	return new JavaSoundOutput();
    
public java.lang.Object[]getControls()

	Control c[] = new Control[] { 
	    gainControl,
	    bufferControl,
	    peakVolumeMeter
	};
	return c;
    
public java.lang.StringgetName()

	return NAME;
    
protected booleaninitDevice(javax.media.format.AudioFormat in)


	javax.media.Format newInput = in;

	// Free the old ulaw decoder if there's one.
	if (ulawDecoder != null) {
	    ulawDecoder.close();
	    ulawDecoder = null;
	}

	// Initialize a ulaw decoder if the input format is ulaw.
	Format outs[] = new Format[1];
	if (ulawFormat.matches(in)) {

	    ulawDecoder = SimpleGraphBuilder.findCodec(in, linearFormat, null, outs);
	    if (ulawDecoder != null) {
		ulawOutputFormat = newInput = outs[0];
	    } else
		return false;
	}

	devFormat = in;

	return super.initDevice((javax.media.format.AudioFormat)newInput);
    
public booleanisExclusive()

	// JavaSound can mix audio
	return false;
    
public voidopen()

	if (device == null && inputFormat != null) {
	    if (!initDevice(inputFormat))
		throw new ResourceUnavailableException("Cannot intialize audio device for playback");
	    device.pause();
	}
    
public intprocessData(javax.media.Buffer buffer)


        

	if (!checkInput(buffer))
	    return BUFFER_PROCESSED_FAILED;

	// Processing linear data
	if (ulawDecoder == null) {
	    try {
		((PeakVolumeMeter)peakVolumeMeter).processData(buffer);
	    } catch (Throwable t) {
		t.printStackTrace();
	    }
	    return super.doProcessData(buffer);
	}

	// Pre-processing ulaw data, then feed it into JavaSound.
	if (decodeBuffer == null) {
	    decodeBuffer = new Buffer();
	    decodeBuffer.setFormat(ulawOutputFormat);
	}

	decodeBuffer.setLength(0);
	decodeBuffer.setOffset(0);
	decodeBuffer.setFlags(buffer.getFlags());
	decodeBuffer.setTimeStamp(buffer.getTimeStamp());
	decodeBuffer.setSequenceNumber(buffer.getSequenceNumber());

	int rc = ulawDecoder.process(buffer, decodeBuffer);

	if (rc == BUFFER_PROCESSED_OK) {
	    try {
		((PeakVolumeMeter)peakVolumeMeter).processData(decodeBuffer);
	    } catch (Throwable t) {
		System.err.println(t);
	    }
	    return super.doProcessData(decodeBuffer);
	}

	return BUFFER_PROCESSED_FAILED;