Unity and audio sample rates

I’ve been working on a mobile app that involves recording audio via microphone input and then playing back that audio. As part of that I’ve been experimenting with how Unity handles sample rates. I wanted to investigate optimizing the audio filesize and also make sure there were no conflicts between mic sample rate, audio file sample rate, and speaker (output) sample rate. There didn’t seem to be a lot of info on this topic, so I thought I’d share what I’ve found. Some of it was expected. Some not so much.

1) Unity can play inputs of any sample rate – It doesn’t matter what the sample rate of an audio file is, Unity can play it and will seamlessly resample the audio to Unity’s selected output sample rate (which can be found in AudioSettings.outputSampleRate). This is true on PC and mobile.

2) Microphone.GetDeviceCaps lies – According to the docs, GetDeviceCaps is what you use to determine what sample rates are supported by the attached microphone. On both of my Android devices, Unity shows a minimum sample rate of 44100 and a maximum sample rate of 44100 for the built-in mic. However, I’ve found that I can actually record at many other sample rates. I was able to successfully record audio at 12k, 24k, and 48k sample rates by simply specifying those values in Microphone.Start(). I double checked the resulting samples and found that it does seem to truly record at those other rates. I also played back the various sample rates as a triple check and could hear the distortion/noise in the audio recorded at 12k. The reported min and max are a lie.

3) Setting AudioSettings.outputSampleRate won’t necessarily do what you think – The docs note that you can set outputSampleRate to change the output sample rate of Unity’s internal audio mixer. I thought maybe that was there as an optimization feature – allowing you to set a lower sample rate if you wanted performance over quality or if you wanted to minimize any up/downsampling. My Android devices default to an output rate of 24k and everything works fine if you leave that alone. But once I changed that to 12k, all audio plays twice as fast. This was regardless of the input sample rate of the audio file. Setting output rate to 48k made all audio play at half-speed. If I had to guess, I’d assume there’s maybe some kind of hardware limitation that restricts my device to only output at a 24k sample rate? Maybe Unity’s mixer actually downsamples the output to 12k and then something else down the chain says “Nope! I’ve gotta output this at 24k” so it plays it twice as fast. Regardless, it makes the outputSampleRate setting useless to me.

Anyway, I learned a lot playing with Unity’s audio system. Hope this helps someone!