Resampling?

Hello,
Okay, so it seems I totally underestimated the complexity of "downsampling" pcm data.
Since I have to mix all of my audio in one place and feed it to a single playout mechanism, I've been trying to achieve resampling functionality for (1) dealing with audio files that are sampled differently from my system's native sample rate and (2) (unfortunately) having to use it for handling pitch fluctuation.
Within an hour or so I managed to get something that gets the size and speed of the new audio data exactly correct, but the high frequencies are distorted (I'm assuming this is aliasing)?
As of now, I'm just calculating how many samples would have to be removed per second given the source and requested rates, then from there calculating a fraction so to know to delete one out of every N samples.
Is anyone able to comment on how the filters work which prevent this kind of distortion or point me to some resource that helped you understand it?
This is what I have thus far:


short* downsample(short* src_data, int src_size, int src_rate, int channels, int dst_rate, int* dst_size)
{
    float div = (float)src_rate / dst_rate;

    int target_size = src_size / div;//this computes the number of shorts required to hold the result.
   
    *dst_size = target_size;//return output size value.

    short* result = new short[target_size];
    memset(result, 0, target_size * 2);
    float interval = (float)dst_rate / (src_rate - dst_rate)*2;//this computes the number of sample groups (1 times the number of channels) which are to be kept before one is to be dropped


    short* src_pos = src_data;//keeps track of where in the source data we're drawing from.
    short* dst_pos = result;//keeps track of where in the destination buffer we're writing to.
    float tracker = interval;//keeps track of deletion intervals.


    for (int i = 0; i < src_size / 2; i++)
    {
        if (tracker > 0)
        {
            for (int x = 0; x < channels; x++)
            {
                    *dst_pos = *src_pos;//copy one short from source to destination.

                dst_pos++;
                src_pos++;

                tracker--;

            }

        }
        else
        {
            tracker += interval;//+= used because in terval is not a hole number, so sometimes copy ceil(interval) and other times copy floor(interval) to maintain averages.
            src_pos += channels;//skip copying (2Channels) samples.

        }

    }





    return result;

}


What I really don't understand about this is that if I'm downsampling by 50 percent (where every other sample per channel is being dropped), I don't hear any distortion in the result; however, lowering from say 44800 down to 44100 (where only one out of every 22 ~ 23 samples are being dropped) results in high frequencies sounding distorted.
I can provide some test audio if interested,
but what do I have to do to accomplish this cleanly?

Thanks.

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Trajectory via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Trajectory via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector

Reply via email to