
#include "signalpath.h"

float * runSignalPath(signalStep *root,float *in, float *out, int buffSize)
{
    int noSteps = 0;
    float *tIn = in;
    float *tOut = out;
    int newBufSize = buffSize;
    while(root != 0)
    {
        newBufSize = root->function(in,out,newBufSize,root->userParam);
        float *temp = in;
        in = out;
        out = temp;
        root = root->nxt;
    }
   
    return tOut;
}

//PCM in, single channel stream out
int extractChannelFromStereo(float *in,float *out,int buffSize,void *channel)
{
    char channelOffset =*(char*)channel;
    for(int i =0;i<buffSize/2;i++)
    {
        out[i] = in[i*2+channelOffset];
    }
    return buffSize/2;
}

//buffSize is the amount stereo samples.
int stereoToMono(float *in, float *out,int buffSize ,void*channel)
{
    
    for(int i =0;i<buffSize/2;i++)
    {
        float sample = (in[i*2] + in[i*2+1])/2;
        out[i*2] = sample;
        out[i*2+1] = sample;
    }
    return buffSize;
}

//single channel stream in, pcm stream out. Only write to selected channel. (max 2 channels)
//buffsize is the amount of samples in the input stream, out stream needs to be twice as big as input
int writeToChannel(float *in, float *out,int buffSize, void *channel)
{
    char channelOffset =*(char*)channel;
    for(int i =0;i<buffSize;i++)
    {
        out[i*2+channelOffset] = in[i];
    }
    return buffSize*2+channelOffset;
}