blob: 2af554abd2d2d615adb476df2546e6aad0f2a160 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
using System;
namespace Godosa.Core.Audio;
/// <summary>16-bit mono PCM helpers.</summary>
public static class Pcm
{
public const int SampleRate = 44100;
/// <summary>Interleaved float frames (e.g. decoded OGG) → mono 16-bit.</summary>
public static short[] Downmix(float[] interleaved, int channels)
{
var pcm = new short[interleaved.Length / channels];
for (int f = 0; f < pcm.Length; f++)
{
float sum = 0f;
for (int c = 0; c < channels; c++) sum += interleaved[f * channels + c];
pcm[f] = ToShort(sum / channels);
}
return pcm;
}
public static short ToShort(float sample) => (short)Math.Clamp(MathF.Round(sample * short.MaxValue), -short.MaxValue, short.MaxValue);
}
|