package auditory.described;

import java.io.*;
import java.util.StringTokenizer;
import javax.sound.midi.MidiUnavailableException;

/**
 * A factory that creates Score objects
 */
public class ScoreFactory
{
    
    /**
     * Create a Score object from a BufferedReader that
     * is reading a stream containing a string representation
     *
     * @param in     The BufferedReader
     */
    public static Score createScore(BufferedReader in) 
                        throws Exception
    {
       int                    denominator, numerator, tempo;       
       Note                   note;
       Part                   part;       
       Score                  score;       
       String                 line, voice;
       StringTokenizer        st;
       
       
       // Read the time signature and tempo
       line        = in.readLine();
       st          = new StringTokenizer(line, ",/");
       numerator   = Integer.parseInt(st.nextToken());
       denominator = Integer.parseInt(st.nextToken());
       tempo       = Integer.parseInt(st.nextToken());

       score = new Score();
       
       score.setTimeSignature(numerator, denominator);
       score.setTempo(tempo);       

       while ((voice = in.readLine()) != null)
       {
          part = PartFactory.createPart(in);
          score.addPart(part, voice);
       }
       
       return score;       
    }
    


    
    /**
     * Create a Score object from a file containing a string representation
     *
     * @param filename   The name of the file
     */
    public static Score createScore(String filename) 
                        throws Exception
    {
       BufferedReader         in;
       

       in = new BufferedReader(new FileReader(filename));

       return createScore(in);
    }
    
}
