Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Pixely.Audio/AudioSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class AudioSource : IDisposable
internal Pointer<MIX_Track> Pointer { get; set; }
private AudioClip? _clip;
private AudioGroup? _group;
private Vector3 _position;
private Vector3? _position;
private float _gain = 1.0f;
private bool _looping;

Expand Down Expand Up @@ -106,7 +106,9 @@ public bool Looping
}
}

public Vector3 Position
// Null plays the clip unspatialized, with its own channels. SDL_mixer spatializes a track with any position, Vector3.Zero
// included, and then mixes it down to mono.
public Vector3? Position
{
get
{
Expand Down
9 changes: 6 additions & 3 deletions src/Pixely.Audio/AudioSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ internal void UpdateSourcePositions()
{
foreach (AudioSource source in _sources.Copy())
{
source.ApplyPosition();
if (source.Position is not null)
{
source.ApplyPosition();
}
}
}

Expand Down Expand Up @@ -236,8 +239,8 @@ internal static void ThrowIfNegative(float value, string parameterName)
// -1 loops forever, 0 plays once.
internal abstract void SetTrackLoopsCore(Pointer<MIX_Track> track, int loopCount);

// Relative to the listener.
internal abstract void SetTrack3DPositionCore(Pointer<MIX_Track> track, Vector3 position);
// Relative to the listener. Null turns spatialization off.
internal abstract void SetTrack3DPositionCore(Pointer<MIX_Track> track, Vector3? position);

internal abstract void TagTrackCore(Pointer<MIX_Track> track, string tag);

Expand Down
1 change: 1 addition & 0 deletions src/Pixely.Audio/Pixely.Audio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<InternalsVisibleTo Include="Pixely.Testing" />
<InternalsVisibleTo Include="Pixely.Tests" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 10 additions & 4 deletions src/Pixely.Audio/SdlAudioSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,19 @@ internal override void SetTrackLoopsCore(Pointer<MIX_Track> track, int loopCount
SdlError.ThrowOnFalse(MIX_SetTrackLoops(track, loopCount), nameof(MIX_SetTrackLoops));
}

internal override void SetTrack3DPositionCore(Pointer<MIX_Track> track, Vector3 position)
internal override void SetTrack3DPositionCore(Pointer<MIX_Track> track, Vector3? position)
{
if (position is not Vector3 value)
{
SdlError.ThrowOnFalse(MIX_SetTrack3DPosition(track, null), nameof(MIX_SetTrack3DPosition));
return;
}

MIX_Point3D point = new()
{
x = position.X,
y = position.Y,
z = position.Z
x = value.X,
y = value.Y,
z = value.Z
};

SdlError.ThrowOnFalse(MIX_SetTrack3DPosition(track, &point), nameof(MIX_SetTrack3DPosition));
Expand Down
2 changes: 1 addition & 1 deletion src/Pixely.Testing/StubAudioSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ internal override void SetTrackLoopsCore(Pointer<MIX_Track> track, int loopCount
{
}

internal override void SetTrack3DPositionCore(Pointer<MIX_Track> track, Vector3 position)
internal override void SetTrack3DPositionCore(Pointer<MIX_Track> track, Vector3? position)
{
}

Expand Down
38 changes: 38 additions & 0 deletions tests/Pixely.Tests/AudioSystemTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Frozen;
using System.Collections.Immutable;
using System.Numerics;
using System.Runtime.InteropServices;
using Pixely.App;
using Pixely.Audio;
using Pixely.Content;
using XdlSharp;
using static XdlSharp.Xdl;

namespace Pixely.Tests;
Expand Down Expand Up @@ -89,6 +91,34 @@ public void Play_WithoutAClip_Throws()
Assert.That(source.Play, Throws.InvalidOperationException);
}

[Test]
public void Position_ByDefault_IsNullAndLeavesTheTrackUnspatialized()
{
AudioSource source = CreateSourceWithBuffer();
_audioSystem.Listener.Position = new Vector3(4, 0, 0);

source.Play();

Assert.That((source.Position, GetTrackPosition(source)), Is.EqualTo(((Vector3?)null, Vector3.Zero)));
}

[Test]
public void Position_SetWhilePlaying_SpatializesRelativeToTheListener_AndNullUndoesIt()
{
AudioSource source = CreateSourceWithBuffer();
_audioSystem.Listener.Position = new Vector3(4, 0, 0);
source.Play();

source.Position = new Vector3(1, 2, 3);
Vector3 positioned = GetTrackPosition(source);
_audioSystem.Listener.Position = new Vector3(0, 0, 1);
Vector3 listenerMoved = GetTrackPosition(source);
source.Position = null;
Vector3 cleared = GetTrackPosition(source);

Assert.That((positioned, listenerMoved, cleared, source.Position), Is.EqualTo((new Vector3(-3, 2, 3), new Vector3(1, 2, 2), Vector3.Zero, (Vector3?)null)));
}

[Test]
public void Gains_KeepTheirValues_AndRefuseNegativeOnes()
{
Expand Down Expand Up @@ -287,6 +317,14 @@ public void Dispose_OfTheAudioSystem_ReleasesEverything()
});
}

// SDL_mixer reports the origin for an unspatialized track.
private static unsafe Vector3 GetTrackPosition(AudioSource source)
{
MIX_Point3D point;
Assert.That(MIX_GetTrack3DPosition(source.Pointer, &point), Is.True);
return new Vector3(point.x, point.y, point.z);
}

private AudioSource CreateSourceWithBuffer()
{
AudioSource source = _audioSystem.CreateSource();
Expand Down