aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/src/Godosa.Core/Audio/Pcm.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Godosa.Core/Audio/Pcm.cs')
-rw-r--r--src/Godosa.Core/Audio/Pcm.cs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/Godosa.Core/Audio/Pcm.cs b/src/Godosa.Core/Audio/Pcm.cs
new file mode 100644
index 0000000..2af554a
--- /dev/null
+++ b/src/Godosa.Core/Audio/Pcm.cs
@@ -0,0 +1,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);
+}