view src/samer/audio/VLine.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents 5df24c91468d
children
line wrap: on
line source
/*
 *	VLine.java
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY;
 *	without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.audio;

import javax.sound.sampled.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import samer.core.*;
import samer.core.util.*;
import samer.core.util.swing.Meter;
import samer.core.types.*;

/**
	A Viewable for exposing a Javasound DataLine object.
	The Agent responds to these commands: "open", "close", "info"
	"start", "stop", "drain" and "flush".

	Changes:
	16/6/03: removed ClipAgent class. Don't care about clips.
		Also removed line members from agent classes: they now use
		local variables in the execute method instead.
*/

public abstract class VLine extends Viewable implements Agent
{
	public VLine(String name) {
		super(name); 
		setAgent(this);
		Shell.registerViewable(this);
	}

	abstract DataLine getLine();
	abstract void openImpl() throws Exception;
	abstract void closeImpl() throws Exception;

	/** Open the line using its default format */
	public void open() throws Exception { openImpl(); }

	/** Close the line */
	public void close() {
		try{ closeImpl(); }
		catch (Exception ex) {
			Shell.trace("line failed to close: "+ex);
		}
	}

	public void start() {
		Shell.status("starting line");
		getLine().start();
		Shell.status("line started");
	}

	public void stop() {
		Shell.status("stopping line");
		getLine().stop();
		Shell.status("line stopped");
	}

	public void dispose() { close(); super.dispose(); }

	public Viewer getViewer() { return new LineViewer(getLine()); }

	public void getCommands(Agent.Registry r) {
		r.add("start").add("stop").add("flush");
		r.add("open").add("close").add("info");
	}

	public void execute(String cmd, Environment env) throws Exception
	{
		if (cmd.equals("start")) start();
		else if (cmd.equals("stop")) stop();
		else if (cmd.equals("drain")) { getLine().drain(); changed(); }
		else if (cmd.equals("flush")) { getLine().flush(); changed(); }
		else if (cmd.equals("open")) open();
		else if (cmd.equals("close")) close();
		else if (cmd.equals("info")) {
			Shell.print("Line information: "+getLabel());
			Shell.print(getLine().getLineInfo().toString());
			Shell.print("Format: "+getLine().getFormat());
			Shell.print("Buffer size: "+getLine().getBufferSize());
		}
	}

	public String toString() { return super.toString()+"="+getLine().getFormat(); }

	class LineViewer extends BaseViewer
	{
		Meter			meter=null;
		DataLine		line;

		public LineViewer(DataLine line) {
			super(VLine.this);
			setLayout( new VLayout(1,4));
			setText(getLabel());

			if (Shell.getBoolean("meter",true)) {
				this.line = line;
				Shell.push("meter");
				meter = new Meter();
				meter.getMap().setDomain(0,line.getBufferSize());
				Shell.pop();
				add(meter);
			}
			Shell.pop();
		}

		public void update(Observable o, Object src) {
			if (src==Viewable.DISPOSING) super.update(o,src);
			else if (meter!=null) meter.next(line.available());
		}
	}
}