Audio Manager - System Sample Rate is ignored on Android

Hi all,

I’m encountering an issue whereby the system sample rate is not being respected on Android.

In Edit → Project Settings → Audio, the system sample rate is set to 44100. However, when running on a device, the sample rate is 48000.

AudioSettings.outputSampleRate states that “As of version 5.0 setting the sample rate from scripts is no longer supported. This has to be set in the Audio section of the project settings instead.

Additionally, attempting to force the sample rate with AudioSettings.Reset, as below, is also unsuccessful.

AudioConfiguration config = AudioSettings.GetConfiguration();
config.sampleRate = 44100;
AudioSettings.Reset(config);

I’m using a Google Nexus 5X (Android 6.0.1) with Unity version 5.6.2p3.

Has anyone encountered a similar issue and know of a fix?

Thanks,
Andy

1 Like

Hi,

I am having the same problem now, did you manage to overcome it? if so how?

Not easily I’m afraid. I ended up adjusting my audio synthesis code to interpolate samples (resample) when running at a different sample rate to the audio data - i.e. I never found a way to make Android respect the specified audio sample rate.

This could be a solution

int clipSampleRate = 44100;
        int outputSampleRate = AudioSettings.outputSampleRate;
        int clipChannelsCount = audioClip.channels;
        int clipSamplesCount = audioClip.samples;

        //Convert audio clip framerate to audio processor output framerate
        int outputSamplesCount = (int)(clipSamplesCount * (float)outputSampleRate / (float)clipSampleRate);
        float[] samplesOut = new float[outputSamplesCount * clipChannelsCount];

        for (int i = 0; i < outputSamplesCount; i++)
        {
            for (int channel = 0; channel < clipChannelsCount; channel++)
            {
                samplesOut[i * clipChannelsCount + channel] = samples[(int)(((float)i / (float)outputSamplesCount) * (float)clipSamplesCount) * clipChannelsCount + channel];
            }
        }