Code example for SourceDataLine

Methods: getClassstartopen

0
	} 
 
	public void powerOn(){ 
		if (!triedToGetLine) getLine();
		if (dataLine == null) return;
		dataLine.start();
		clock.go();
	} 
 
	public void powerOff(){ 
		if (dataLine == null) return;
		clock.pause();
		dataLine.flush();
		dataLine.stop();
	} 
 
	public void destroy() { 
		if (dataLine == null) return;
		clock.terminate();
		dataLine.close();
		dataLine = null;
	} 
 
	@Override 
	public synchronized int nextSamples(byte[] buffer, int quant) {
		if (dataLine == null) return -1;
		if (buffer == null) {		// Signal is off
			dataLine.flush();
			return -1; 
		} 
		// Drop samples that don't fit the input buffer available capacity 
		int ava = inputBuffer.remaining();
		if (ava > quant)
			ava = quant;
		// else 
			// if (ava < quant) System.out.println(">>>> DROPPED: " + quant + " - " + ava + " = " + (quant - ava)); 
		inputBuffer.put(buffer, 0, ava);
 
		// System.out.println(">>>> Available: " + inputBuffer.position()); 
		return inputBuffer.position();
	} 
	 
	@Override 
	public void synchOutput() { 
		refresh(); 
	} 
 
	@Override 
	public void clockPulse() { 
		synchOutput(); 
	} 
 
	private void getLine() { 
		if (signal == null) return;
		try { 
			triedToGetLine = true;
			dataLine = AudioSystem.getSourceDataLine(AUDIO_FORMAT);
			dataLine.open(AUDIO_FORMAT, OUTPUT_BUFFER_SIZE);
			inputBuffer = ByteBuffer.allocateDirect(INPUT_BUFFER_SIZE);
			tempBuffer = new byte[inputBuffer.capacity()];
			if (ADDED_THREAD_PRIORITY != 0) clock.setPriority(Thread.NORM_PRIORITY + ADDED_THREAD_PRIORITY);
			System.out.println("Sound Mixer Line: " + dataLine.getClass().getSimpleName());
			clock = new Clock("Speaker", this, FPS);
		} catch (Exception ex) {
			System.out.println("Unable to acquire audio line:\n" + ex);
			dataLine = null;
		}