Skip to content

Commit

Permalink
Convert bytes instead of unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
hwsmm committed Sep 21, 2024
1 parent 4f82123 commit 6ae2d8a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 55 deletions.
12 changes: 3 additions & 9 deletions osu.Framework/Audio/Track/TrackSDL3AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,11 @@ internal void PutSamplesInStream(byte[] next, int length)
if (audioDataLength + floatLen > AudioData.LongLength)
prepareArray(audioDataLength + floatLen);

unsafe // To directly put bytes as float in array
for (int i = 0; i < floatLen; i++)
{
fixed (float* dest = AudioData)
fixed (void* ptr = next)
{
float* src = (float*)ptr;
Buffer.MemoryCopy(src, dest + audioDataLength, (AudioData.LongLength - audioDataLength) * sizeof(float), length);
}
float src = BitConverter.ToSingle(next, i * sizeof(float));
AudioData[audioDataLength++] = src;
}

audioDataLength += floatLen;
}

internal void DonePutting()
Expand Down
83 changes: 37 additions & 46 deletions osu.Framework/Audio/Track/Waveform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,66 +120,57 @@ public Waveform(Stream? data)

do
{
int read = decoder.LoadFromStream(out byte[] currentBytes);
int read = decoder.LoadFromStream(out byte[] currentBytes) / bytes_per_sample;
int sampleIndex = 0;

unsafe
while (sampleIndex < read)
{
fixed (void* ptr = currentBytes)
// Each point is composed of multiple samples
for (; pointSamples < samplesPerPoint && sampleIndex < read; pointSamples += channels, sampleIndex += channels)
{
float* currentFloats = (float*)ptr;
int currentFloatsLength = read / bytes_per_sample;
// Find the maximum amplitude for each channel in the point
float left = BitConverter.ToSingle(currentBytes, sampleIndex * bytes_per_sample);
float right = BitConverter.ToSingle(currentBytes, (sampleIndex + 1) * bytes_per_sample);

while (sampleIndex < currentFloatsLength)
{
// Each point is composed of multiple samples
for (; pointSamples < samplesPerPoint && sampleIndex < currentFloatsLength; pointSamples += channels, sampleIndex += channels)
{
// Find the maximum amplitude for each channel in the point
float left = *(currentFloats + sampleIndex);
float right = *(currentFloats + sampleIndex + 1);

point.AmplitudeLeft = Math.Max(point.AmplitudeLeft, Math.Abs(left));
point.AmplitudeRight = Math.Max(point.AmplitudeRight, Math.Abs(right));

complexBuffer[complexBufferIndex].X = left + right;
complexBuffer[complexBufferIndex].Y = 0;
point.AmplitudeLeft = Math.Max(point.AmplitudeLeft, Math.Abs(left));
point.AmplitudeRight = Math.Max(point.AmplitudeRight, Math.Abs(right));

if (++complexBufferIndex >= fft_samples)
{
complexBufferIndex = 0;
complexBuffer[complexBufferIndex].X = left + right;
complexBuffer[complexBufferIndex].Y = 0;

FastFourierTransform.FFT(true, m, complexBuffer);
if (++complexBufferIndex >= fft_samples)
{
complexBufferIndex = 0;

point.LowIntensity = computeIntensity(sample_rate, complexBuffer, low_min, mid_min);
point.MidIntensity = computeIntensity(sample_rate, complexBuffer, mid_min, high_min);
point.HighIntensity = computeIntensity(sample_rate, complexBuffer, high_min, high_max);
FastFourierTransform.FFT(true, m, complexBuffer);

for (; fftPointIndex < pointList.Count; fftPointIndex++)
{
var prevPoint = pointList[fftPointIndex];
prevPoint.LowIntensity = point.LowIntensity;
prevPoint.MidIntensity = point.MidIntensity;
prevPoint.HighIntensity = point.HighIntensity;
pointList[fftPointIndex] = prevPoint;
}
point.LowIntensity = computeIntensity(sample_rate, complexBuffer, low_min, mid_min);
point.MidIntensity = computeIntensity(sample_rate, complexBuffer, mid_min, high_min);
point.HighIntensity = computeIntensity(sample_rate, complexBuffer, high_min, high_max);

fftPointIndex++; // current Point is going to be added
}
for (; fftPointIndex < pointList.Count; fftPointIndex++)
{
var prevPoint = pointList[fftPointIndex];
prevPoint.LowIntensity = point.LowIntensity;
prevPoint.MidIntensity = point.MidIntensity;
prevPoint.HighIntensity = point.HighIntensity;
pointList[fftPointIndex] = prevPoint;
}

if (pointSamples >= samplesPerPoint)
{
// There may be unclipped samples, so clip them ourselves
point.AmplitudeLeft = Math.Min(1, point.AmplitudeLeft);
point.AmplitudeRight = Math.Min(1, point.AmplitudeRight);
fftPointIndex++; // current Point is going to be added
}
}

if (pointSamples >= samplesPerPoint)
{
// There may be unclipped samples, so clip them ourselves
point.AmplitudeLeft = Math.Min(1, point.AmplitudeLeft);
point.AmplitudeRight = Math.Min(1, point.AmplitudeRight);

pointList.Add(point);
pointList.Add(point);

point = new Point();
pointSamples = 0;
}
}
point = new Point();
pointSamples = 0;
}
}
} while (decoder.Loading);
Expand Down

0 comments on commit 6ae2d8a

Please sign in to comment.