view src/uk/ac/qmul/eecs/depic/daw/gui/AudioTrackParameters.java @ 0:3074a84ef81e

first import
author Fiore Martin <f.martin@qmul.ac.uk>
date Wed, 26 Aug 2015 16:16:53 +0100
parents
children c0412c81d274
line wrap: on
line source
/*  
 Cross-Modal DAW Prototype - Prototype of a simple Cross-Modal Digital Audio Workstation.

 Copyright (C) 2015  Queen Mary University of London (http://depic.eecs.qmul.ac.uk/)
	
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.qmul.eecs.depic.daw.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToggleButton;
import javax.swing.ListCellRenderer;
import javax.swing.border.MatteBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import uk.ac.qmul.eecs.depic.daw.Automation;
import uk.ac.qmul.eecs.depic.daw.Parameter;
import uk.ac.qmul.eecs.depic.daw.Parameter.Type;
import uk.ac.qmul.eecs.depic.daw.ParametersControl;
import uk.ac.qmul.eecs.depic.patterns.SequenceEvent;
import uk.ac.qmul.eecs.depic.patterns.SequenceListener;

public class AudioTrackParameters extends JPanel {
	private static final long serialVersionUID = 1L;
	private final static int SLIDER_SCALE = 100;
	
	private AudioTrack track;
	public final JLabel nameLabel;
	public final JLabel gainLabel;
	public final JToggleButton muteButton;
	public final JToggleButton soloButton;
	public final JButton fxButton;
	public final JComboBox<Parameter.Type> automationComboBox;
	public final JSlider gainSlider;
	public final JLabel panLabel;
	public final JSlider panSlider;
	
	/**
	 * Create the panel.
	 */
	public AudioTrackParameters(AudioTrack track, int index) {
		this.track = track;
		
		this.setPreferredSize(new Dimension(247, 130)); //FIXME const values 
		this.setMaximumSize(new Dimension(247, 130));
		
		
		nameLabel = new JLabel("New Track "+index);
		nameLabel.setBounds(6, 8, 233, 17);
		nameLabel.setBorder(new MatteBorder(0, 0, 1, 0, (Color) new Color(0, 0, 0)));
		
		soloButton = new JToggleButton("S");
		soloButton.setBounds(205, 84, 36, 20);
		soloButton.getAccessibleContext().setAccessibleName("Solo");
		soloButton.setMargin(new Insets(0, 0, 0, 0));
		soloButton.setMinimumSize(new Dimension(20, 20));
		soloButton.setMaximumSize(new Dimension(20, 20));
		soloButton.setPreferredSize(new Dimension(20, 20));
		
		muteButton = new JToggleButton("M");
		muteButton.setBounds(166, 84, 38, 20);
		muteButton.getAccessibleContext().setAccessibleName("Mute");
		muteButton.setMargin(new Insets(0, 0, 0, 0));
		muteButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
		muteButton.setPreferredSize(new Dimension(20, 20));
		muteButton.setMinimumSize(new Dimension(20, 20));
		muteButton.setMaximumSize(new Dimension(20, 20));
		
		fxButton = new JButton("Fx");
		fxButton.setBounds(12, 30, 40, 20);
		fxButton.setPreferredSize(new Dimension(20, 20));
		fxButton.setMinimumSize(new Dimension(20, 20));
		fxButton.setMaximumSize(new Dimension(20, 20));
		fxButton.setMargin(new Insets(0, 0, 0, 0));
		fxButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
		
		automationComboBox = new JComboBox<>();
		automationComboBox.setBounds(166, 29, 73, 18);
		
		JLabel lblAutomation = new JLabel("Automation:");
		lblAutomation.setBounds(95, 30, 65, 16);
		lblAutomation.setLabelFor(automationComboBox);
		
		gainLabel = new JLabel("");
		gainLabel.setBounds(6, 106, 87, 16);
		setLayout(null);
		add(nameLabel);
		add(fxButton);
		add(gainLabel);
		add(lblAutomation);
		add(automationComboBox);
		add(muteButton);
		add(soloButton);
		
		gainSlider = new JSlider();
		gainSlider.setBounds(103, 103, 136, 21);
		add(gainSlider);
		
		panLabel = new JLabel("Pan: 0.0 ");
		panLabel.setBounds(6, 84, 61, 16);
		add(panLabel);
		
		panSlider = new JSlider();
		panSlider.setBounds(68, 84, 78, 21);
		add(panSlider);
		
		initComponentsValues();
		addListeners();
	}
	
	private void addListeners(){
		gainSlider.addChangeListener(new GainListener());
		panSlider.addChangeListener(new PanListener());
		automationComboBox.addItemListener(new AutomationComboBoxListener());
		
	}
	
	private void initComponentsValues(){
		ParametersControl parametersControl = track.getSoundWave().getParametersControl();
		
		/* gain slider */
		Parameter volume = parametersControl.getGainParameter();
		gainSlider.setMinimum((int)(volume.getRange().getStart()*SLIDER_SCALE));
		gainSlider.setMaximum((int)(volume.getRange().getEnd()*SLIDER_SCALE));
		gainSlider.setValue((int)volume.getValue()*SLIDER_SCALE);
		
		gainLabel.setText(volume.getType().getLabel()+(volume.getInitialValue()+" "+volume.getType().getUnitofMeasurment()));
		gainLabel.setLabelFor(gainSlider);
		
		Parameter pan = parametersControl.getPanParameter();
		panSlider.setMinimum((int)(pan.getRange().getStart()*SLIDER_SCALE));
		panSlider.setMaximum((int)(pan.getRange().getEnd()*SLIDER_SCALE));
		panSlider.setValue((int)pan.getValue()*SLIDER_SCALE);
		
		
		float panValue = panSlider.getValue();
		panLabel.setText(pan.getType().getLabel()+(panValue/SLIDER_SCALE));
		panLabel.setLabelFor(panSlider);
		/* automation combo box */
		
		class ParameterTypeRenderer extends JLabel implements ListCellRenderer<Parameter.Type> {
			private static final long serialVersionUID = 1L;

			@Override
			public Component getListCellRendererComponent(
					JList<? extends Type> list, Type value, int index,
					boolean isSelected, boolean cellHasFocus) {
				if (isSelected) {
		            setBackground(list.getSelectionBackground());
		            setForeground(list.getSelectionForeground());
		        } else {
		            setBackground(list.getBackground());
		            setForeground(list.getForeground());
		        }

				setText("  "+value.getLabel());
				
				return this;
			}
			
		}
		
		automationComboBox.setModel(new DefaultComboBoxModel<Parameter.Type>(
				new Parameter.Type[] {Parameter.NONE_PARAMETER.getType(), Parameter.GAIN_TYPE, Parameter.PAN_TYPE}));
				
		automationComboBox.setRenderer(new ParameterTypeRenderer());
	}
	
	public void setLabel(String label){
		nameLabel.setText(label);
		
		track.getAccessibleContext().setAccessibleName(label);
	}
	
	/**
	 * Returns the text of this component label
	 * 
	 * @see #setLabel(String)
	 * @return the text of this component label
	 */
	@Override
	public String toString(){
		return nameLabel.getText();
	}
	
	private class GainListener implements ChangeListener {
		@Override
		public void stateChanged(ChangeEvent evt) {
			JSlider gain = (JSlider)evt.getSource();
			
			float newValue = (float)gain.getValue()/SLIDER_SCALE;
			Parameter gainParam = track.getSoundWave().getParametersControl().getGainParameter();
			gainLabel.setText(gainParam.getType().getLabel()+(newValue)+" "+gainParam.getType().getUnitofMeasurment());
			gainParam.setValue(newValue);
		}
	}
	
	private class PanListener implements ChangeListener {
		@Override
		public void stateChanged(ChangeEvent evt) {
			JSlider pan = (JSlider)evt.getSource();
			
			float newValue = (float)pan.getValue()/SLIDER_SCALE;
			Parameter panParam = track.getSoundWave().getParametersControl().getPanParameter();
			panParam.setValue(newValue);
			panLabel.setText(panParam.getType().getLabel()+(newValue));
		}
	}
	
	private class AutomationComboBoxListener implements ItemListener { 
		@Override
		public void itemStateChanged(ItemEvent evt) {
			if(evt.getStateChange() == ItemEvent.SELECTED){
				Parameter.Type type = (Parameter.Type)evt.getItem();
				track.getSoundWave().getParametersControl().setCurrentAutomation(type);
				
				if(Parameter.GAIN_TYPE.equals(type)){
					/* get the now current autiomation */
					Automation a = track.getSoundWave().getParametersControl().getCurrentAutomation();
					a.addSequenceListener(new SequenceListener(){

						@Override
						public <T extends SequenceEvent> void sequenceUpdated(T t) {
							if(t.getSource().getValuesNum() != 0){
								gainSlider.setEnabled(false);
							}else {
								gainSlider.setEnabled(true);
								float newValue = t.getSource().getBegin() * SLIDER_SCALE;
								gainSlider.setValue((int)newValue);
							}
						}
						
					});
				}else if(Parameter.PAN_TYPE.equals(type)) {
					/* get the now current autiomation */
					Automation a = track.getSoundWave().getParametersControl().getCurrentAutomation();
					a.addSequenceListener(new SequenceListener(){

						@Override
						public <T extends SequenceEvent> void sequenceUpdated(T t) {
							if(t.getSource().getValuesNum() != 0){
								panSlider.setEnabled(false);
							}else {
								panSlider.setEnabled(true);
								float newValue = (float)t.getSource().getBegin() * SLIDER_SCALE;
								panSlider.setValue((int) newValue);
							}
						}
						
					});
				}
			}
		}
	}
}