Skip to content
This repository has been archived by the owner on Dec 24, 2020. It is now read-only.

Commit

Permalink
Optimize AudioClipPreview rendering for very large clips
Browse files Browse the repository at this point in the history
  • Loading branch information
mafiesto4 committed Dec 18, 2018
1 parent 7d9cc71 commit f51c650
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions FlaxEditor/Viewport/Previews/AudioClipPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public override void Draw()
// Sample count
uint numSamplesPerChannel = info.NumSamples / info.NumChannels;
uint samplesPerIndex = (uint)(numSamplesPerChannel / Width);
const uint maxSamplesPerIndex = 64;
uint actualSamplesPerIndex = Math.Min(samplesPerIndex, maxSamplesPerIndex);
uint samplesPerIndexDiff = Math.Max(1, samplesPerIndex / actualSamplesPerIndex);

// Render each channel separately so outer loop is the sound wave channel index
for (uint channelIndex = 0; channelIndex < info.NumChannels; channelIndex++)
Expand All @@ -95,21 +98,20 @@ public override void Draw()
int numSamplesInPixel = 0;

// Loop through all pixels in this x-frame, sum all audio data. Track total frames rendered to avoid writing past buffer boundary
for (uint sampleIndex = 0; sampleIndex < samplesPerIndex && currentSample < numSamplesPerChannel; sampleIndex++)
uint samplesEnd = Math.Min(currentSample + samplesPerIndex, numSamplesPerChannel);
for (uint sampleIndex = currentSample; sampleIndex < samplesEnd; sampleIndex += samplesPerIndexDiff)
{
// Get the sample value
uint index = currentSample + channelIndex;
uint index = sampleIndex + channelIndex;
float value = _pcmData[index];

// Sum the sample value with the running sum
samplesSum += Mathf.Abs(value);

// Track the number of samples we're actually summing to get an accurate average
numSamplesInPixel++;

// Move to the next sample
currentSample++;
}
currentSample = samplesEnd;

// If we actually added any audio data in this pixel
if (numSamplesInPixel > Mathf.Epsilon)
Expand Down

0 comments on commit f51c650

Please sign in to comment.