Skip to content

Commit

Permalink
Finished out audio API.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomspilman committed Sep 8, 2024
1 parent 87dd501 commit da6378f
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 10 deletions.
66 changes: 63 additions & 3 deletions MonoGame.Framework/Platform/Native/Audio.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using Microsoft.Xna.Framework.Audio;
using System;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;


namespace MonoGame.Interop;



[MGHandle]
internal readonly struct MGA_System { }

Expand All @@ -21,6 +20,51 @@ internal readonly struct MGA_Buffer { }
internal readonly struct MGA_Voice { }


internal struct ReverbSettings
{
public float ReflectionsDelayMs;
public float ReverbDelayMs;
public float PositionLeft;
public float PositionRight;
public float PositionLeftMatrix;
public float PositionRightMatrix;
public float EarlyDiffusion;
public float LateDiffusion;
public float LowEqGain;
public float LowEqCutoff;
public float HighEqGain;
public float HighEqCutoff;
public float RearDelayMs;
public float RoomFilterFrequencyHz;
public float RoomFilterMainDb;
public float RoomFilterHighFrequencyDb;
public float ReflectionsGainDb;
public float ReverbGainDb;
public float DecayTimeSec;
public float DensityPct;
public float RoomSizeFeet;
public float WetDryMixPct;
}

internal struct Listener
{
public Vector3 Position;
public Vector3 Forward;
public Vector3 Up;
public Vector3 Velocity;
};

internal struct Emitter
{
public Vector3 Position;
public Vector3 Forward;
public Vector3 Up;
public Vector3 Velocity;
public float DopplerScale;
};



/// <summary>
/// MonoGame native calls for platform audio features.
/// </summary>
Expand All @@ -42,6 +86,9 @@ internal static unsafe partial class MGA
[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGA_System_GetMaxInstances", StringMarshalling = StringMarshalling.Utf8)]
public static partial int System_GetMaxInstances();

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGA_System_SetReverbSettings", StringMarshalling = StringMarshalling.Utf8)]
public static partial void System_SetReverbSettings(MGA_System* system, in ReverbSettings settings);

#endregion

#region Buffer
Expand Down Expand Up @@ -73,6 +120,7 @@ public static partial void Buffer_InitializePCM(
int loopStart,
int loopLength);

// TODO: This should go away after we move to FAudio's Xact implementation.
[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGA_Buffer_InitializeXact", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Buffer_InitializeXact(
MGA_Buffer* buffer,
Expand Down Expand Up @@ -125,6 +173,18 @@ public static partial void Buffer_InitializeXact(
[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGA_Voice_SetVolume", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Voice_SetVolume(MGA_Voice* voice, float volume);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGA_Voice_SetReverbMix", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Voice_SetReverbMix(MGA_Voice* voice, float mix);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGA_Voice_SetFilterMode", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Voice_SetFilterMode(MGA_Voice* voice, FilterMode mode, float filterQ, float frequency);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGA_Voice_ClearFilterMode", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Voice_ClearFilterMode(MGA_Voice* voice);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGA_Voice_Apply3D", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Voice_Apply3D(MGA_Voice* voice, in Listener listener, in Emitter emitter, float distanceScale);

#endregion
}

Expand Down
31 changes: 29 additions & 2 deletions MonoGame.Framework/Platform/Native/SoundEffect.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ private unsafe void PlatformInitializeFormat(byte[] header, byte[] buffer, int b
MGA.Buffer_InitializeFormat(Buffer, header, buffer, bufferSize, loopStart, loopLength);
}

// TODO: This should go away after we move to FAudio's Xact implementation.
private unsafe void PlatformInitializeXact(MiniFormatTag codec, byte[] buffer, int channels, int sampleRate, int blockAlignment, int loopStart, int loopLength, out TimeSpan duration)
{
// This is only the platform specific non-streaming
Expand Down Expand Up @@ -125,8 +126,34 @@ private unsafe void PlatformDispose(bool disposing)
}
}

internal static void PlatformSetReverbSettings(ReverbSettings reverbSettings)
internal unsafe static void PlatformSetReverbSettings(ReverbSettings reverbSettings)
{
// TODO!
var settings = new MonoGame.Interop.ReverbSettings
{
ReflectionsDelayMs = reverbSettings.ReflectionsDelayMs,
ReverbDelayMs = reverbSettings.ReverbDelayMs,
PositionLeft = reverbSettings.PositionLeft,
PositionRight = reverbSettings.PositionRight,
PositionLeftMatrix = reverbSettings.PositionLeftMatrix,
PositionRightMatrix = reverbSettings.PositionRightMatrix,
EarlyDiffusion = reverbSettings.EarlyDiffusion,
LateDiffusion = reverbSettings.LateDiffusion,
LowEqGain = reverbSettings.LowEqGain,
LowEqCutoff = reverbSettings.LowEqCutoff,
HighEqGain = reverbSettings.HighEqGain,
HighEqCutoff = reverbSettings.HighEqCutoff,
RearDelayMs = reverbSettings.RearDelayMs,
RoomFilterFrequencyHz = reverbSettings.RoomFilterFrequencyHz,
RoomFilterMainDb = reverbSettings.RoomFilterMainDb,
RoomFilterHighFrequencyDb = reverbSettings.RoomFilterHighFrequencyDb,
ReflectionsGainDb = reverbSettings.ReflectionsGainDb,
ReverbGainDb = reverbSettings.ReverbGainDb,
DecayTimeSec = reverbSettings.DecayTimeSec,
DensityPct = reverbSettings.DensityPct,
RoomSizeFeet = reverbSettings.RoomSizeFeet,
WetDryMixPct = reverbSettings.WetDryMixPct
};

MGA.System_SetReverbSettings(System, in settings);
}
}
32 changes: 27 additions & 5 deletions MonoGame.Framework/Platform/Native/SoundEffectInstance.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file 'LICENSE.txt', which is part of this source code package.

using System;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Interop;


Expand All @@ -13,9 +14,27 @@ public partial class SoundEffectInstance : IDisposable
internal unsafe MGA_Voice* Voice;


private void PlatformApply3D(AudioListener listener, AudioEmitter emitter)
private unsafe void PlatformApply3D(AudioListener listener, AudioEmitter emitter)
{
// TODO: Implement me.
var listener_ = new Listener()
{
Position = listener.Position,
Forward = listener.Forward,
Up = listener.Up,
Velocity = listener.Velocity,
};

var emitter_ = new Emitter()
{
Position = emitter.Position,
Forward = emitter.Forward,
Up = emitter.Up,
Velocity = emitter.Velocity,
DopplerScale = emitter.DopplerScale
};

if (Voice != null)
MGA.Voice_Apply3D(Voice, in listener_, in emitter_, SoundEffect.DistanceScale);
}

private unsafe void PlatformPause()
Expand Down Expand Up @@ -70,17 +89,20 @@ private unsafe void PlatformSetVolume(float volume)

internal unsafe void PlatformSetReverbMix(float mix)
{
// TODO: Implement me.
if (Voice != null)
MGA.Voice_SetReverbMix(Voice, mix);
}

internal unsafe void PlatformSetFilter(FilterMode mode, float filterQ, float frequency)
{
// TODO: Implement me.
if (Voice != null)
MGA.Voice_SetFilterMode(Voice, mode, filterQ, frequency);
}

internal unsafe void PlatformClearFilter()
{
// TODO: Implement me.
if (Voice != null)
MGA.Voice_ClearFilterMode(Voice);
}

private unsafe void PlatformDispose(bool disposing)
Expand Down
25 changes: 25 additions & 0 deletions src/monogame/faudio/MGA_faudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ mgint MGA_System_GetMaxInstances()
return INT_MAX;
}

void MGA_System_SetReverbSettings(MGA_System* system, ReverbSettings& settings)
{
assert(system != nullptr);
}

MGA_Buffer* MGA_Buffer_Create()
{
auto buffer = new MGA_Buffer();
Expand Down Expand Up @@ -154,3 +159,23 @@ void MGA_Voice_SetVolume(MGA_Voice* voice, mgfloat volume)
assert(voice != nullptr);
}

void MGA_Voice_SetReverbMix(MGA_Voice* voice, mgfloat mix)
{
assert(voice != nullptr);
}

void MGA_Voice_SetFilterMode(MGA_Voice* voice, MGFilterMode mode, mgfloat filterQ, mgfloat frequency)
{
assert(voice != nullptr);
}

void MGA_Voice_ClearFilterMode(MGA_Voice* voice)
{
assert(voice != nullptr);
}

void MGA_Voice_Apply3D(MGA_Voice* voice, Listener& listener, Emitter& emitter, mgfloat distanceScale)
{
assert(voice != nullptr);
}

5 changes: 5 additions & 0 deletions src/monogame/include/csharp_MGA.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct MGA_Voice;
MG_EXPORT MGA_System* MGA_System_Create();
MG_EXPORT void MGA_System_Destroy(MGA_System* system);
MG_EXPORT mgint MGA_System_GetMaxInstances();
MG_EXPORT void MGA_System_SetReverbSettings(MGA_System* system, ReverbSettings& settings);
MG_EXPORT MGA_Buffer* MGA_Buffer_Create();
MG_EXPORT void MGA_Buffer_Destroy(MGA_Buffer* buffer);
MG_EXPORT void MGA_Buffer_InitializeFormat(MGA_Buffer* buffer, mgbyte* waveHeader, mgbyte* waveData, mgint length, mgint loopStart, mgint loopLength);
Expand All @@ -38,3 +39,7 @@ MG_EXPORT MGSoundState MGA_Voice_GetState(MGA_Voice* voice);
MG_EXPORT void MGA_Voice_SetPan(MGA_Voice* voice, mgfloat pan);
MG_EXPORT void MGA_Voice_SetPitch(MGA_Voice* voice, mgfloat pitch);
MG_EXPORT void MGA_Voice_SetVolume(MGA_Voice* voice, mgfloat volume);
MG_EXPORT void MGA_Voice_SetReverbMix(MGA_Voice* voice, mgfloat mix);
MG_EXPORT void MGA_Voice_SetFilterMode(MGA_Voice* voice, MGFilterMode mode, mgfloat filterQ, mgfloat frequency);
MG_EXPORT void MGA_Voice_ClearFilterMode(MGA_Voice* voice);
MG_EXPORT void MGA_Voice_Apply3D(MGA_Voice* voice, Listener& listener, Emitter& emitter, mgfloat distanceScale);

0 comments on commit da6378f

Please sign in to comment.