/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package compose;

import data.IPart;
import data.IPhrase;
import data.IScore;
import gui.ComposingDialog;
import java.util.ArrayList;
import jm.music.data.Note;
import jm.music.data.Part;
import jm.music.data.Phrase;
import jm.music.data.Score;
import jm.music.tools.PhraseMatrix;
import midi.MIDIAnalyser;

/**
 *
 * @author smuRf
 */
public class MarkovComposer extends Composer{
    private IPhrase basePhrase;
    private int depth, noteCount;
    private boolean pitch, rhythmvalue, dynamics;
    private int tempo;
    
    public MarkovComposer(ComposingDialog parent, ArrayList<IPhrase> iPhrases, int tempo, int depth, int noteCount, boolean pitch, boolean rhythmvalue, boolean dynamics)
    {
        this.parent = parent;
        this.depth = depth;
        this.noteCount = noteCount;
        this.pitch = pitch;
        this.rhythmvalue = rhythmvalue;
        this.dynamics = dynamics;
        this.tempo = tempo;
        
        IScore iScore = new IScore("newScore", 100);
        IPart iPart = new IPart("newPart", 0, 0);
        
        basePhrase = new IPhrase("newPhrase", 0);
        for(IPhrase iPhrase:iPhrases)
        {
            basePhrase.appendPhrase(iPhrase);
        }
        
        iScore.addIPart(iPart);
        iPart.addIPhrase(basePhrase);
    }
    
    public void compose()
    {
        PhraseMatrix matrix = new PhraseMatrix(basePhrase.getPhraseWithNotes(), depth);
        Phrase newPhrase = matrix.generate(pitch, rhythmvalue, dynamics, noteCount);
        
        Note[] noteArray = newPhrase.getNoteArray();
       
        Phrase transformedPhrase = new Phrase(noteArray);
        Phrase answer = createAnswerPhrase(transformedPhrase);
        answer.setMute(parent.getAnswerPhraseMute());
        Phrase counter = createCounterPhrase(transformedPhrase);
        counter.setMute(parent.getCounterPhraseMute());
        
        for(Note n:transformedPhrase.getNoteArray())
        {
            n.setRhythmValue(n.getRhythmValue() * 480);
            n.setDynamic(100);
        }

        for(Note n:answer.getNoteArray())
        {
            n.setRhythmValue(n.getRhythmValue() * 480);
            n.setDynamic(60);
        }

        for(Note n:counter.getNoteArray())
        {
            n.setRhythmValue(n.getRhythmValue() * 480);
            n.setDynamic(60);
        }
        
        Phrase drums = createDrumPhrase(transformedPhrase, parent.getDrumPhraseInstrument());
        drums.setMute(parent.getDrumPhraseMute());
        for(Note n:drums.getNoteArray())
        {
            n.setRhythmValue(n.getRhythmValue() * 480);
            n.setDynamic(80);
        }
        
        score = new Score(tempo);
        score.setPPQN(480);
        Part partOne = new Part(parent.getPhraseInstrument(), 0);
        Part partTwo = new Part(parent.getAnswerPhraseInstrument(), 1);
        Part partThree = new Part(parent.getCounterPhraseInstrument(), 2);
        Part partFour = new Part(0, 9);
        partOne.add(transformedPhrase);
        partTwo.add(answer);
        partThree.add(counter);
        partFour.add(drums);
        score.add(partOne);
        score.add(partTwo);
        score.add(partThree);
        score.add(partFour);
        
        MIDIAnalyser analyser = new MIDIAnalyser();
        IScore iScore = analyser.analyseTask(score);
        
        parent.composerFinished(iScore);
    }
}
