package vermilion;

/**
 * A source machine that produces a 1 kHz test tone with a duty cycle.
 */
public class MachineMalady extends MachineBase
{
    private ParameterNote notes = null;

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

    /**
     * The name for this machine is 'Test Tone'.
     */
    @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 fills the output buffer
     * a sine wave at 1 kHz. The tone is gated by an on/off duty cycle
     * determined by the parameters on the machine.
     */
    @Override
    public void fillBuffer(int numSamples)
    {
        double cycleTime = 1000.0 / Main.audio.sampleRate;
        cycleTime *= Math.PI;

        int baseAddr = 0;
        for (int x = 0; x < numSamples; x++)
        {
            output[baseAddr++] = 0.0f;
            output[baseAddr++] = 0.0f;
        }
    }

    /**
     * This machine handles note changes and spawns notes.
     */
    @Override
    public void parameterChange(ParameterBase param, Object value)
    {
        if (param == notes)
        {
            PlaybackEvent evt = (PlaybackEvent) value;
            ParameterNote note = (ParameterNote) evt.commandVal;
            System.out.println(note.value);
        }
    }

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

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

        parameters[0] = notes;
    }

    /**
     * This machine prints the values for the parameters in seconds.
     */
    @Override
    public String printParameter(ParameterBase p)
    {
        ParameterSlider s = (ParameterSlider) p;
        String str = String.valueOf(s.value);
        if (str.length() == 1) str = "0.00" + str;
        else if (str.length() == 2) str = "0.0" + str;
        else if (str.length() == 3) str = "0." + str;
        else if (str.length() == 4) str = str.substring(0, 1) + "." + str.substring(1);
        return str;
    }
}