Sound2Light (S2L)

   Print  Previous  Next

Functionality

As described above, sound data refers mainly to frequency-based data. First of all, the function GetSoundLevel() retrieves the volume of the audio channels (left and right channel). The float value returned ranges from 0.0 to 1.0, the lowest and highest level possible, respectively.
Moreover, other available data is frequency values. They are stored in the arrays SOUND_DATA_LEFT and SOUND_DATA_RIGHT, which are both of the data type float. There are up to 511 values and each describes the volume of a »well-defined frequency.
The length-operator of the »arrays tells how much valid data they contain. A check may be necessary and then just take the values that are actually available/provided to have a proper effect. But it is secure to always assume 511 values.
In contrast to other dynamic arrays, in MADRIX Script those arrays will not grow because their size is fixed. Trying to get an invalid element, always results in 0.

 

The First S2L Example

The following example for the MAS Script Effect analyses the left and the right audio channel. Lines will be drawn onto the matrix to indicate the average frequency.

const color LEFT_CHANNEL = {255, 0, 0, 128};

const color RIGHT_CHANNEL= {0, 255, 0, 128};

 

void InitEffect()

{

}

 

float avgFrequ(float array[])

{

    float result;

 

    //to avoid division by zero later on

    if(array.length > 0)

    {

        for(int i = 0; i < array.length; i++)

            result += array[i];

 

        result /= (float)array.length;

    }

 

    return(result);

}

 

void RenderEffect()

{

    int valL = (int)(GetSoundLevel(0)*255.0);

    int valR = (int)(GetSoundLevel(1)*255.0);

    color c = {valL, valR, (valL * valR) % 255, (valR + valL) / 2, 0};

 

    float iHL = (float)avgFrequ(SOUND_DATA_LEFT);

    float iHR = (float)avgFrequ(SOUND_DATA_RIGHT);

 

    Clear();

 

    DrawVectorLine(c, 0.0, iHL, 1.0, iHL);

    DrawVectorLine(c, iHL, 0.0, iHL, 1.0);

 

    DrawVectorLine(c, 0.0, 1.0-iHL, 1.0, 1.0-iHL);

    DrawVectorLine(c, 1.0-iHL, 0.0, 1.0-iHL, 1.0);

 

    DrawVectorLine(c, 0.0, iHR, 1.0, iHR);

    DrawVectorLine(c, iHR, 0.0, iHR, 1.0);

 

    DrawVectorLine(c, 0.0, 1.0-iHR, 1.0, 1.0-iHR);

    DrawVectorLine(c, 1.0-iHR, 0.0, 1.0-iHR, 1.0);

}

 

MADRIX Version: 3.6g | Script Version: 2.21
[Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100%
Print   Previous   Next