Audio distortion while implementing a low-pass filter I attempted to implement a real-time audio filtering in a callback function using the simplest moving-average filter with coefficients [0.5 0.5] :

void process(void* in_, void* out_, DWORD length, void* user) {

    //data is float, mono
    int n = length/sizeof(float);
    float* in = (float*)in_;
    float* out = (float*)out_;
    float* buff = (float*)user;

    //buff holds the last input sample from the previous block
    //if this is a first block, it is 0
    out[0] = 0.5*in[0] + 0.5*buff;

    for (int i = 1; i &lt; n; ++i)
    {
         out[i] = 0.5*in[i] + 0.5*in[i - 1];
    }

    buff[0] = in[n-1];
}


however, i get a distortion in the output, like an aliasing of some sort or clipping. But, to my knowledge none of this is possible to occur in this case. I've implemented the same code in Matlab ( although not in real-time ) and everything was fine.

I am using a BASS library. 

I would like to know if i am missing something obvious here, cause i am running out of ideas. Thanks in advance!

EDIT: Thanks everyone for the answers. The above function is in the form for simplicity, but in actual from it looks like below (there is no output buffer, filtering must be done in-place) but it does exact same thing, although I will write it for convenience: 

void CALLBACK filterDSPMono(HDSP handle, DWORD channel, void* buffer, DWORD length, void* user)
{
    float* x = (float*) buffer;
    int n = length / sizeof(float);
    float* buff = (float*) user;
    float temp;


    int i = 0;
    for (; i &lt; n; ++i)
    {
        temp = x[i];
        x[i] = 0.5*x[i] + 0.5*buff[0];
        buff[0] = temp;
    }
}


EDIT2: Ok, so it turns out the problem was with the buffer size set in the library that my sound card drivers didn't like for some reason. After I changed that, everything is fine. Thank you for all the answers unfortunately I cant mark any as The Answer due to the fault being on my part. This question now can be considered as resolved.
I suspect that most likely the audio coming in is integer, and not floating point. Depending on your device, operating system and type of file input, you can get one or the other. Read documentation for audio input API you're using to see which format the audio is in. Also, if it's integer, it's likely to be 16 bit rather than 32 bit. In any case, again, read the documentation.

EDIT:
BASS library has a flag you can set to get floating point values everywhere:

BASS_SetConfig(
    BASS_CONFIG_FLOATDSP,
    BOOL floatdsp
);