FileDocCategorySizeDatePackage
JavaSoundOutput.javaAPI DocJMF 2.1.1e5872Mon May 12 12:20:48 BST 2003com.sun.media.renderer.audio.device

JavaSoundOutput

public class JavaSoundOutput extends Object implements AudioOutput

Fields Summary
static Mixer
mixer
static Object
initSync
protected SourceDataLine
dataLine
protected FloatControl
gc
protected FloatControl
rc
protected BooleanControl
mc
protected boolean
paused
protected int
bufSize
protected AudioFormat
format
long
lastPos
long
originPos
long
totalCount
Constructors Summary
public JavaSoundOutput()



      
    
Methods Summary
public intbufferAvailable()

	return dataLine.available();
    
public static javax.media.format.AudioFormatconvertFormat(javax.sound.sampled.AudioFormat fmt)
Convert a JavaSound format to a JMF format.

	Encoding type = fmt.getEncoding();

	String encoding;
	if (type == Encoding.PCM_SIGNED ||
	    type == Encoding.PCM_UNSIGNED)
	    encoding = javax.media.format.AudioFormat.LINEAR;
	else if (type == Encoding.ALAW)
	    encoding = javax.media.format.AudioFormat.ALAW;
	else if (type == Encoding.ULAW)
	    encoding = javax.media.format.AudioFormat.ULAW;
	else
	    encoding = null;

	return new javax.media.format.AudioFormat(
		encoding,
		fmt.getSampleRate(),
		fmt.getSampleSizeInBits(),
		fmt.getChannels(),
		(fmt.isBigEndian() ?
			javax.media.format.AudioFormat.BIG_ENDIAN :
			javax.media.format.AudioFormat.LITTLE_ENDIAN),
		(type == Encoding.PCM_SIGNED ? 
			javax.media.format.AudioFormat.SIGNED :
			javax.media.format.AudioFormat.UNSIGNED));
    
public static javax.sound.sampled.AudioFormatconvertFormat(javax.media.format.AudioFormat fmt)
Convert a JMF format to a JavaSound format.


	return new javax.sound.sampled.AudioFormat(
			(fmt.getSampleRate() == 
				javax.media.format.AudioFormat.NOT_SPECIFIED ?
					8000f : (float)fmt.getSampleRate()),
			(fmt.getSampleSizeInBits() == 
				javax.media.format.AudioFormat.NOT_SPECIFIED ?
					16 : fmt.getSampleSizeInBits()), 
			(fmt.getChannels() == 
				javax.media.format.AudioFormat.NOT_SPECIFIED ?
					1 : fmt.getChannels()),
			(fmt.getSigned() == 
				javax.media.format.AudioFormat.SIGNED ?
					true : false),
			(fmt.getEndian() == 
				javax.media.format.AudioFormat.BIG_ENDIAN ?
					true : false));
    
public voiddispose()

	dataLine.close();
    
public voiddrain()

	dataLine.drain();
    
public voidfinalize()

	super.finalize();
	dispose();
    
public voidflush()

	dataLine.flush();
    
public javax.media.format.AudioFormatgetFormat()

	return format;
    
public doublegetGain()

	return (gc != null ? gc.getValue() : 0);
    
public longgetMediaNanoseconds()


       
	if (dataLine == null || format == null)
	    return 0;

	long pos = dataLine.getFramePosition();

	if (pos < lastPos) {
	    // Wraps around.
	    totalCount += lastPos - originPos; 
	    originPos = pos;
	}

	lastPos = pos;

	return (long)(((totalCount + pos - originPos) * 1000 / format.getSampleRate()) * 1000000);
    
public booleangetMute()

	return (mc != null ? mc.getValue() : false);
    
public floatgetRate()

	if (rc == null)
	    return 1.0f;
	return (float)(rc.getValue() / format.getSampleRate());
    
public booleaninitialize(javax.media.format.AudioFormat format, int bufSize)


      synchronized (initSync) {

	DataLine.Info info;
	javax.sound.sampled.AudioFormat afmt;

	afmt = convertFormat(format);

	//System.err.println("Render buffer size = " + bufSize);

	info = new DataLine.Info(SourceDataLine.class, afmt, bufSize);

	try {

	    if (!AudioSystem.isLineSupported(info)) {
		Log.warning("DataLine not supported: " + format);
		return false;
	    }

	    dataLine = (SourceDataLine)AudioSystem.getLine(info);
	    dataLine.open(afmt, bufSize);

	} catch (Exception e) {
	    Log.warning("Cannot open audio device: " + e);
	    return false;
	}

	this.format = format;
	this.bufSize = bufSize;

	if (dataLine == null) {
	    Log.warning("JavaSound unsupported format: " + format);
	    return false;
	}

	try {
	    gc = (FloatControl)dataLine.getControl(FloatControl.Type.MASTER_GAIN);
	    mc = (BooleanControl)dataLine.getControl(BooleanControl.Type.MUTE);
	} catch (Exception e) {
	    Log.warning("JavaSound: No gain control");
	}

	try {
	    rc = (FloatControl)dataLine.getControl(FloatControl.Type.SAMPLE_RATE);
	} catch (Exception e) {
	    Log.warning("JavaSound: No rate control");
	}

	return true;
      }
    
public static booleanisOpen()

	Mixer mixer = AudioSystem.getMixer(null);
	Line lines[] = mixer.getSourceLines();

	return (lines != null && lines.length > 0);
    
public voidpause()

	if (dataLine != null)
	    dataLine.stop();
	paused = true;
    
public voidresume()

	if (dataLine != null)
	    dataLine.start();
	paused = false;
    
public voidsetGain(double g)

	if (gc != null)
	    gc.setValue((float)g);
    
public voidsetMute(boolean m)

	if (mc != null)
	    mc.setValue(m);
    
public floatsetRate(float r)

	if (rc == null)
	    return 1.0f;

	float rate = (float)(r * format.getSampleRate());

	if (rate > rc.getMaximum() || rate < rc.getMinimum())
	    return getRate();

	rc.setValue(rate);

	return r;
    
public intwrite(byte[] data, int off, int len)

	return dataLine.write(data, off, len);