import java.util.ArrayList;

/**
 * Class that models a piano, with characters mapped to piano keys.
 *   @author Dave Reed
 *   @version 8/28/12
 */
public class Piano {
    public final static int SAMPLING_RATE = 44100;
    public final static String KEYS = "q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' ";
               
    private ArrayList<PianoWire> wires;

    /**
     * Constructs a piano with 37 keys from across the keyboard.
     */
    public Piano() {
        this.wires = new ArrayList<PianoWire>();
        for (int i = 0; i < Piano.KEYS.length(); i++) {
            double freq = 440 * Math.pow(2, (i-24)/12.0);
            this.wires.add(new PianoWire(Piano.SAMPLING_RATE, freq));
        }
    }

    /**
     * Strikes the piano key (wire) corresponding to the specified character.
     *   @param key the character specifying the piano key (or wire)
     */
    public void strikeKey(char key) {
        int index = Piano.KEYS.indexOf(key);
        if (index != -1) {
            this.wires.get(index).strike();
        }
    }

    /**
     * Plays all of the vibrating keys (wires) at the current time step.
     */
    public void play() {
        double combined = 0.0;
        for (int i = 0; i < this.wires.size(); i++) {
            combined += this.wires.get(i).sample();
        }
        
        StdAudio.play(combined);      
    }
}
