package vermilion;

//TODO: Try to write an audio processing plugin or 'machine'. Any mathematical
//equation is an audio effect that someone will find useful one day. Look at the
//examples in the Extensions directory and just copy and modify an existing
//machine. (Trivial, fun)

/**
 * This class encapsulates a note that is currently playing.
 *
 * TODO: The idea of this class is to implement the concept of a note that
 * launches and plays either indefinitely or until its end. This will be managed
 * by a util class for mixing the notes, handling multiple track events, and
 * doing note-offs and other control tasks. Then there can be useful subclasses
 * of this class for tasks like ADSR oscillators, samples etc. Finally as the
 * sample bank and composer increase in complexity these classes can support
 * envelopes, pitch slides and such.
 */
public class UtilPlayingNote
{
    public boolean isPlaying = true;

    public int playbackPitch = 0;
    public int trackNum = 0;

    private int countDown;
    private double cycleTime;

    public UtilPlayingNote(int pitch, int track)
    {
        playbackPitch = pitch;
        trackNum = track;
        
        countDown = (int) (Main.audio.sampleRate * 0.2f);

        // TODO: Make note to frequency calculations a util function, and move
        // this calculation so the note pitch can be changed on the fly.
        double notePitch = UtilFunctions.notePitch(pitch);
        cycleTime = notePitch / Main.audio.sampleRate;
        cycleTime *= Math.PI;
    }

    void mix(float[][] output, int numSamples)
    {
        for (int x = 0; x < numSamples; x ++)
        {
            if (countDown > 0)
            {
                float funcVal = (float) Math.sin(cycleTime * countDown);
                output[0][x] += funcVal;
                output[1][x] += funcVal;
                countDown--;
            }
        }
    }
}
