package org.jfugue.extras;

import java.io.File;

import org.jfugue.ChannelPressure;
import org.jfugue.Controller;
import org.jfugue.Instrument;
import org.jfugue.JFugueElement;
import org.jfugue.KeySignature;
import org.jfugue.Layer;
import org.jfugue.Measure;
import org.jfugue.Note;
import org.jfugue.Pattern;
import org.jfugue.PatternTool;
import org.jfugue.PitchBend;
import org.jfugue.Player;
import org.jfugue.PolyphonicPressure;
import org.jfugue.Tempo;
import org.jfugue.Time;
import org.jfugue.Voice;

/**
 * Returns all of the MusicString events that are played in the requested Voice (i.e., Channel)
 * 
 *@author David Koelle
 *@version 3.0
 * 
 */
public class GetPatternForVoiceTool extends PatternTool
{
    private byte voice = 0;
    private byte activeVoice = 0;
    private Pattern pattern;

    public GetPatternForVoiceTool(int voice)
    {
        this.voice = (byte)voice;
        reset();
    }
    
    public void reset()
    {
        pattern = new Pattern();
        activeVoice = 0;
    }
    
    public String getDescription()
    {
        return "Returns all of the MusicString events that are played in the requested Voice (i.e., Channel)";
    }

    public void voiceEvent(Voice voice)
    {
        if (activeVoice != voice.getVoice())
        {
            this.activeVoice = voice.getVoice();
            addElement(voice);
        }
    }

    /**
     * Tempo changes affect the voice regardless of what voice they appear to be in
     */
    public void tempoEvent(Tempo tempo) 
    {
        pattern.add(tempo.getMusicString());
    }

    /**
     * Key Signature changes affect the voice regardless of what voice they appear to be in
     */
    public void keySignatureEvent(KeySignature keySig)  
    {
        pattern.add(keySig.getMusicString());
    }
    
    /**
     * Called when the parser encounters an instrument event.
     * @param instrument the event that has been parsed
     * @see Instrument
     */
    public void instrumentEvent(Instrument instrument) 
    {
        addElement(instrument);
    }

    /**
     * Called when the parser encounters a layer event.
     * @param layer the event that has been parsed
     * @see Layer
     */
    public void layerEvent(Layer layer) 
    {
        addElement(layer);
    }

    /**
     * Called when the parser encounters a measure event.
     * @param measure the event that has been parsed
     * @see Measure
     */
    public void measureEvent(Measure measure) 
    {
        addElement(measure);
    }
    
    /**
     * Called when the parser encounters a time event.
     * @param time the event that has been parsed
     * @see Time
     */
    public void timeEvent(Time time) 
    {
        addElement(time);
    }
    
    
    /**
     * Called when the parser encounters a controller event.
     * @param controller the event that has been parsed
     */
    public void controllerEvent(Controller controller) 
    {
        addElement(controller);
    }
    
    /**
     * Called when the parser encounters a channel pressure event.
     * @param channelPressure the event that has been parsed
     * @see ChannelPressure
     */
    public void channelPressureEvent(ChannelPressure channelPressure) 
    {
        addElement(channelPressure);
    }
    
    /**
     * Called when the parser encounters a polyphonic pressure event.
     * @param polyphonicPressure the event that has been parsed
     * @see PolyphonicPressure
     */
    public void polyphonicPressureEvent(PolyphonicPressure polyphonicPressure) 
    {
        addElement(polyphonicPressure);
    }
    
    /**
     * Called when the parser encounters a pitch bend event.
     * @param pitchBend the event that has been parsed
     * @see PitchBend
     */
    public void pitchBendEvent(PitchBend pitchBend) 
    {
        addElement(pitchBend);
    }

    /**
     * Called when the parser encounters an initial note event.
     * @param note the event that has been parsed
     * @see Note
     */
    public void noteEvent(Note note)
    {
        addElement(note);
    }

    /**
     * Called when the parser encounters a sequential note event.
     * @param note the event that has been parsed
     * @see Note
     */
    public void sequentialNoteEvent(Note note) 
    { 
        addElement(note);
    }

    /**
     * Called when the parser encounters a parallel note event.
     * @param note the event that has been parsed
     * @see Note
     */
    public void parallelNoteEvent(Note note) 
    {
        addElement(note);
    }
    

    private void addElement(JFugueElement element)
    {
        if (activeVoice == voice)
        {
            System.out.println("Adding "+element.getMusicString());
            pattern.add(element.getMusicString());
        }
    }
    
    public Object getResult()
    {
        return pattern;
    }
    
    public static final void main(String[] args)
    {
        Pattern pattern = null;
        try {
            pattern = new Pattern(new File("billieje.mid"));
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println(pattern.getMusicString());
        GetPatternForVoiceTool tool = new GetPatternForVoiceTool(9); // Get the drum channel - lift the percussion rhythm from this file
        Pattern voice9 = (Pattern)tool.execute(pattern);
        Player player = new Player();
        System.out.println(voice9.getMusicString());
        player.play(voice9);
    }
}

