package vermilion;

import java.util.ArrayList;

/**
 * A source machine that produces short sinewave note bursts.
 */
public class MachineMalady extends MachineBase
{
    // The note parameter that we read the melody from.
    private ParameterNote notes = null;

    // An array of simple oscillators which each produce one note.
    private ArrayList<UtilPlayingNote> playingNotes = new ArrayList<UtilPlayingNote>();

    /**
     * The type for this machine is 'SOURCE'.
     */
    @Override
    public String getType()
    {
        return "SOURCE";
    }

    /**
     * The name for this machine is 'Malady'.
     */
    @Override
    public String getName()
    {
        return "Malady";
    }

    /**
     * This plugin does not require initialisation and does nothing.
     */
    @Override
    public void initialise()
    {
    }

    /**
     * The mixing algorithm for this plugin simply asks all currently playing
     * notes to mix themselves to the output buffer.
     */
    @Override
    public void fillBuffer(int numSamples)
    {
        for (int x = 0; x < numSamples; x++)
        {
            output[0][x] = 0.0f;
            output[1][x] = 0.0f;
        }

        for (UtilPlayingNote note : playingNotes)
        {
            note.mix(output, numSamples);
        }

        for (int y = 0; y < playingNotes.size(); y++)
        {
            if (playingNotes.get(y).isPlaying == false)
            {
                playingNotes.remove(y);
                y--;
            }
        }
    }

    /**
     * This machine handles note change events and spawns notes.
     */
    @Override
    public void parameterChange(PlaybackEvent param)
    {
        if (param.parameterID.equals(notes.parameterName))
        {
            playingNotes.add(new UtilPlayingNote(notes.value, 0));
        }
    }

    /**
     * The setup for this machine creates the note parameter.
     */
    @Override
    public void setupMachine()
    {
        parameters = new ParameterBase[1];

        notes = new ParameterNote("Note", false);

        parameters[0] = notes;
    }

    /**
     * This machine doesn't print its parameter values - the note parameter is
     * not displayed on the machine parameter window.
     */
    @Override
    public String printParameter(ParameterBase p)
    {
        return "";
    }
}