Content Pipeline: ConversionQuality.Medium produces invalid AAC sample rate (44100 Hz -> 33075 Hz) for Android, forcing fallback to Low
Prerequisites
- I have verified this issue is present in the
developbranch - I have searched open and closed issues to ensure it has not already been reported.
MonoGame Version
3.8.5-preview.3
Which MonoGame platform are you using?
MonoGame Android Application (mgandroid)
Operating System
Windows & Linux
Description
When building audio files (e.g., MP3/OGG) as a Song for Android using ConversionQuality.Medium, the Content Pipeline silently fails the FFmpeg AAC conversion and falls back to Low quality.
The root cause seems to be a math oversight in AudioProfile.QualityToSampleRate. For a standard 44.100 Hz source file, Medium quality calculates the target sample rate as sourceSampleRate * 0.75, resulting in 33075 Hz.
However, 33075 Hz is not a valid sample rate according to the AAC standard. FFmpeg immediately crashes with the error [aac] Specified sample rate 33075 is not supported by the aac encoder.
Because DefaultAudioProfile.ConvertToFormat swallows the FFmpeg exception in its do { ... quality--; } while(...) loop, the build pipeline silently degrades the quality to Low (which calculates to 22050 Hz - a valid AAC sample rate) and only emits a generic warning at the end. This forces Android developers to use either Best (huge file sizes) or Low (terrible audio quality), making Medium impossible to use for standard 44.1kHz audio files.
Steps to Reproduce
- Create a new MonoGame Content Builder project.
- Add any standard 44.100 Hz audio file (e.g., music.mp3 or .ogg).
- Set the target platform to Android.
- Set the processor to SongProcessor.
- Set the Quality parameter of the processor to Medium.
- Build the content.
Minimal Example Repo
No response
Expected Behavior
The pipeline should successfully encode the audio file to an AAC (.m4a) format at Medium quality (approx. 128 kbps) without sample rate errors, snapping to the nearest valid AAC sample rate (e.g., 32000 Hz).
Resulting Behavior
The FFmpeg AAC encoder crashes internally. The pipeline catches the error, falls back to Low quality (22050 Hz / 96 kbps), and outputs the following warning in the build log: Failed to convert using "Medium" quality, used "Low" quality
Files
The issue is located in Microsoft.Xna.Framework.Content.Pipeline.Audio.AudioProfile:
protected static int QualityToSampleRate(ConversionQuality quality, int sourceSampleRate)
{
switch (quality)
{
case ConversionQuality.Low:
return Math.Max(8000, (int)Math.Floor(sourceSampleRate / 2.0));
case ConversionQuality.Medium:
// BUG: For 44100 Hz, this returns 33075, which crashes the AAC encoder in FFmpeg.
return Math.Max(8000, (int)Math.Floor((sourceSampleRate / 4.0) * 3));
}
return Math.Max(8000, sourceSampleRate);
}Proposed Fix: The calculated target sample rate should be snapped to the nearest valid AAC frequency (or standard audio frequency) to ensure FFmpeg compatibility.
protected static int QualityToSampleRate(ConversionQuality quality, int sourceSampleRate)
{
double targetRate = quality switch
{
ConversionQuality.Low => sourceSampleRate / 2.0,
ConversionQuality.Medium => sourceSampleRate * 0.75,
_ => sourceSampleRate
};
// Valid AAC sample rates
int[] validRates = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000 };
return validRates.OrderBy(rate => Math.Abs(rate - targetRate)).First();
}Maybe limit the AAC rate snapping to AAC conversions.
Source: MonoGame/MonoGame