/*
  ==============================================================================

    granule.h
    Created: 29 Jan 2023 11:25:35am
    Author:  Tomáš Pospíšil

  ==============================================================================
*/

/*
* This class represents the grain object for granular synthesis. The values given
* to the constructor are assigned to the variables of this object. Grain object 
* also contains remove function, that removes grain from memory.
*/

#pragma once

class Grain {
public:
    /*
    * This function is the constructor of the Grain object. It assigns the function
    * parameters to object variables.
    * 
    * @param timeLength     Parameter, which tells the length of the grain in miliseconds.
    * @param envelope       Parameter, which tells what envelope to use on dataBuffer.
    * @param density        Parameter, which tells how many grains is expected in one second.
    * @param timeOffset     Parameter, which tells how many seconds the grain stream waits before start.
    * @param volume         Parameter, which tells the volume of the grain in the resulting sound in percents.
    * @param dataBuffer     Buffer with grain sound data, that need to be stored for reproduction.
    * @param silenceBuffer  Buffer with zerosamples. The amount of zero samples responds to time offset of the grain stream.
    */
    Grain(int timeLength, int envelope, int density, int timeOffset, int volume, juce::AudioSampleBuffer dataBuffer, juce::AudioSampleBuffer silenceBuffer) {
        this->delka = timeLength;
        this->obalka = envelope;
        this->pocet = density;
        this->offset = timeOffset;
        this->hlasitost = volume;
        this->grinOwnBuffer = dataBuffer;
        this->offsetBuffer = silenceBuffer;
        this->next = NULL;
    }

    /*
    * Object varaibles, which are being assigned in constructor
    */
    int delka;
    int obalka;
    int pocet;
    int offset;
    int hlasitost;
    juce::AudioSampleBuffer grinOwnBuffer;
    juce::AudioSampleBuffer offsetBuffer;
    Grain* next;

    /*
    * Function, that removes all the grains from a certain Channel object. 
    * It clears the memory at the end of the program.
    */
    void remove() {
        if (next != nullptr) {
            next->remove();
            delete next;
            next = nullptr;
        }
        delete this;
    }
};
