CoplayDev/unity-mcp · error · Exception

Could not retrieve audio samples

Error message

Could not retrieve audio samples

What it means

Thrown when AudioClip.GetData returns false, meaning the raw PCM sample array could not be retrieved from the loaded audio clip. GetData requires the audio data to already be loaded in memory; if it isn't, or if the clip format doesn't support sample access, this call fails.

Source

Thrown at TestProjects/AssetStoreUploads/Packages/com.unity.asset-store-tools/Editor/Previews/Scripts/Generators/Custom/TypeGenerators/AudioTypePreviewGenerator.cs:166

            for (int i = 0; i < audioClip.channels; i++)
            {
                var channelMaxY = (_texture.height - 1) - i * sectionSize;
                var channelMinY = _texture.height - (i + 1) * sectionSize;
                var channel = new AudioChannel(channelMinY, channelMaxY, channelSamples[i]);
                channels.Add(channel);
            }

            return channels;
        }

        private List<List<float>> GetChannelSamples(AudioClip audioClip)
        {
            var channelSamples = new List<List<float>>();
            var allSamples = new float[audioClip.samples * audioClip.channels];

            if (!audioClip.GetData(allSamples, 0))
                throw new Exception("Could not retrieve audio samples");

            for (int i = 0; i < audioClip.channels; i++)
            {
                var samples = new List<float>();
                var sampleIndex = i;
                while (sampleIndex < allSamples.Length)
                {
                    samples.Add(allSamples[sampleIndex]);
                    sampleIndex += audioClip.channels;
                }

                channelSamples.Add(samples);
            }

            return channelSamples;
        }

        private void DrawChannel(AudioChannel channel)

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Ensure LoadAudioData() has completed before calling GetData — yield a frame or poll audioClip.loadState until it is Loaded.
  2. Verify the AudioClip supports sample retrieval (some compressed/streaming formats do not).
  3. Handle large clips by checking that samples * channels does not exceed reasonable memory limits before allocating.

Example fix

// before
if (!audioClip.LoadAudioData())
    throw new Exception("Could not load audio data");
// immediately accesses samples
if (!audioClip.GetData(allSamples, 0))
    throw new Exception("Could not retrieve audio samples");

// after — wait for load completion
if (!audioClip.LoadAudioData())
    throw new Exception("Could not load audio data");
while (audioClip.loadState != AudioDataLoadState.Loaded)
    await Task.Yield();
if (!audioClip.GetData(allSamples, 0))
    throw new Exception("Could not retrieve audio samples");
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure data is fully loaded before requesting samples
if (audioClip.loadState != AudioDataLoadState.Loaded)
{
    audioClip.LoadAudioData();
    while (audioClip.loadState == AudioDataLoadState.Loading)
        await Task.Yield();
}
// Pre-validate buffer size to avoid OOM
var sampleCount = audioClip.samples * audioClip.channels;
if (sampleCount <= 0 || sampleCount > 100_000_000)
{
    Debug.LogWarning($"Skipping {audioClip.name}: invalid or excessive sample count ({sampleCount}).");
    continue;
}

Type guard

static bool IsAudioDataAccessible(AudioClip clip)
    => clip != null && clip.loadState == AudioDataLoadState.Loaded && clip.samples > 0;

Try / catch

try
{
    var samples = GetChannelSamples(audioClip);
}
catch (Exception ex) when (ex.Message.Contains("Could not retrieve audio samples"))
{
    ASDebug.LogWarning($"Skipping {audioClip.name}: sample data unavailable.");
    continue;
}

Prevention

When it happens

Trigger: Calling GetChannelSamples(audioClip) where audioClip.GetData(allSamples, 0) returns false. The sample buffer is pre-allocated as float[clip.samples * clip.channels].

Common situations: Audio data was not preloaded (LoadAudioData was called but is async and hasn't finished); clip is a tracker/mod format that doesn't expose PCM samples directly; clip was unloaded between LoadAudioData and GetData; very large clip where the float buffer allocation fails or Unity refuses to fill it.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/03d5c7c3aa5bff29. Report an issue: GitHub.