Skip to content

Commit

Permalink
Updated many things.
Browse files Browse the repository at this point in the history
  • Loading branch information
JunaMeinhold committed Jun 26, 2024
1 parent 2be33c8 commit 20f7678
Show file tree
Hide file tree
Showing 388 changed files with 519,945 additions and 256,623 deletions.
18 changes: 18 additions & 0 deletions AudioTest/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace XAudioTest
{
using HexaGen.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public static class Extensions
{
public static void ThrowIf(this int hresult)
{
HResult result = hresult;
result.ThrowIf();
}
}
}
74 changes: 58 additions & 16 deletions AudioTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Hexa.NET.XAudio2;
using HexaGen.Runtime.COM;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using XAudioTest;

unsafe
Expand All @@ -13,19 +15,20 @@

audio.StartEngine().ThrowIf();
ComPtr<IXAudio2MasteringVoice> master = default;
audio.CreateMasteringVoice(ref master, 2, 192000, 0, null, null, AudioStreamCategory.GameMedia).ThrowIf();
audio.CreateMasteringVoice(ref master, 8, 192000, 0, null, null, AudioStreamCategory.GameMedia).ThrowIf();

XAudio2VoiceDetails details;
master.GetVoiceDetails(&details);

ComPtr<IXAudio2SubmixVoice> submix = default;
audio.CreateSubmixVoice(ref submix, details.InputChannels, details.InputSampleRate, 0, 0, null, null).ThrowIf();

uint channelMask;
master.GetChannelMask(&channelMask);

X3DAudioHandle handle = new();
X3DAudio.X3DAudioInitialize(channelMask, X3DAudio.X3DAudio_SPEED_OF_SOUND, &handle).ThrowIf();

for (int i = 0; i < 20; i++)
{
Console.WriteLine(handle.Data[i]);
}

var fs = File.OpenRead("CantinaBand60.wav");

XAudio2WaveAudioStream stream = new(fs)
Expand All @@ -38,35 +41,74 @@
audio.CreateSourceVoice(ref source, ref waveFormat, 0, 1, (IXAudio2VoiceCallback*)null, null, null).ThrowIf();

stream.Initialize(source);
source.Start(0, 0).ThrowIf();
source.Start(0, 0).ThrowIf(); source.SetVolume(10, 0);

X3DAudioListener listener = new(Vector4.UnitZ, Vector4.UnitY, Vector4.Zero, Vector4.Zero);
X3DAudioListener listener = new(Vector3.UnitZ, Vector3.UnitY, Vector3.Zero, Vector3.Zero);

X3DAudioEmitter emitter = new(null, Vector3.UnitZ, Vector3.UnitY, Vector3.Zero, Vector3.Zero);

X3DAudioEmitter emitter = new(null, Vector4.UnitZ, Vector4.UnitY, Vector4.Zero, Vector4.Zero);
float* channelAzu = stackalloc float[waveFormat.Channels + 32];
emitter.PChannelAzimuths = channelAzu;
emitter.ChannelCount = 1;
emitter.CurveDistanceScaler = emitter.DopplerScaler = 1;
var pos = emitter.Position;

X3DAudioDspSettings settings = default;
float* matrix = stackalloc float[waveFormat.Channels + 32];
settings.SrcChannelCount = 1;
settings.DstChannelCount = waveFormat.Channels;
float* matrix = (float*)Marshal.AllocHGlobal((nint)(emitter.ChannelCount * details.InputChannels) * sizeof(float));

settings.SrcChannelCount = emitter.ChannelCount;
settings.DstChannelCount = details.InputChannels;
settings.PMatrixCoefficients = matrix;
X3DAudio.X3DAudioCalculate(&handle, &listener, &emitter, X3DAudio.X3DAudio_CALCULATE_MATRIX | X3DAudio.X3DAudio_CALCULATE_DOPPLER | X3DAudio.X3DAudio_CALCULATE_LPF_DIRECT | X3DAudio.X3DAudio_CALCULATE_REVERB, &settings);

X3DAudio.X3DAudioCalculate(handle, &listener, &emitter, X3DAudio.X3DAudio_CALCULATE_MATRIX | X3DAudio.X3DAudio_CALCULATE_DOPPLER | X3DAudio.X3DAudio_CALCULATE_LPF_DIRECT | X3DAudio.X3DAudio_CALCULATE_REVERB, &settings);

float angleXZ = 90.0f;
float angleY = 0.0f;
float radiusXZ = 1;
float radiusY = 1;
float lastX = radiusXZ * MathF.Cos(angleXZ);
float lastZ = radiusXZ * MathF.Sin(angleXZ);
float lastY = radiusY * MathF.Sin(angleY);

bool running = true;
while (running)
{
stream.Update(source);
running = !stream.ReachedEnd;

// Update emitter position to move in a circle around the listener on the XZ plane
emitter.Position.X = radiusXZ * MathF.Cos(angleXZ);
emitter.Position.Z = radiusXZ * MathF.Sin(angleXZ);
emitter.Position.Y = radiusY * MathF.Sin(angleY);

emitter.Velocity.X = (emitter.Position.X - lastX) / 0.01f;
emitter.Velocity.Z = (emitter.Position.Z - lastZ) / 0.01f;
emitter.Velocity.Y = (emitter.Position.Y - lastY) / 0.01f;

lastX = emitter.Position.X;
lastZ = emitter.Position.Z;
lastY = emitter.Position.Y;

angleXZ += 0.05f;
angleY += 0.075f;

X3DAudio.X3DAudioCalculate(handle, &listener, &emitter,
X3DAudio.X3DAudio_CALCULATE_MATRIX | X3DAudio.X3DAudio_CALCULATE_DOPPLER |
X3DAudio.X3DAudio_CALCULATE_LPF_DIRECT | X3DAudio.X3DAudio_CALCULATE_REVERB, &settings);

source.SetOutputMatrix((IXAudio2Voice*)master.Handle, waveFormat.Channels, details.InputChannels, settings.PMatrixCoefficients, 0u);
source.SetFrequencyRatio(settings.DopplerFactor, XAudio2.XAudio2_COMMIT_NOW);
source.SetOutputMatrix((IXAudio2Voice*)submix.Handle, 1, 1, &settings.ReverbLevel, 0);

XAudio2FilterParameters FilterParameters = new(XAudio2FilterType.LowPassFilter, 2.0f * MathF.Sin(float.Pi / 6.0f * settings.LPFDirectCoefficient), 1.0f);
source.SetFilterParameters(&FilterParameters, 0);

Thread.Sleep(10);
}

Marshal.FreeHGlobal((nint)settings.PMatrixCoefficients);

stream.Dispose();

source.Release();
submix.Release();
master.Release();
audio.Release();
}
2 changes: 1 addition & 1 deletion AudioTest/XAudioTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
8 changes: 8 additions & 0 deletions Hexa.NET.FreeType/Generated/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,5 +470,13 @@ public unsafe partial class FreeType
[NativeName(NativeNameType.Value, "2")]
public const int FREETYPE_PATCH = 2;

[NativeName(NativeNameType.Const, "FT_PALETTE_FOR_LIGHT_BACKGROUND")]
[NativeName(NativeNameType.Value, "0x01")]
public const int FT_PALETTE_FOR_LIGHT_BACKGROUND = 0x01;

[NativeName(NativeNameType.Const, "FT_PALETTE_FOR_DARK_BACKGROUND")]
[NativeName(NativeNameType.Value, "0x02")]
public const int FT_PALETTE_FOR_DARK_BACKGROUND = 0x02;

}
}
Loading

0 comments on commit 20f7678

Please sign in to comment.