diff options
author | bonmas14 <bonmas14@gmail.com> | 2023-10-26 14:17:12 +0300 |
---|---|---|
committer | bonmas14 <bonmas14@gmail.com> | 2023-10-26 14:17:12 +0300 |
commit | 0ee008ae77fb76cbe9fe151cc6c4bc9f89cb98a5 (patch) | |
tree | 3bd7d0169f38e38c53f16bba244381d5f348d0f3 | |
parent | 91d37e2f17ca0e3d9f122c54268356edb1fd2978 (diff) | |
download | RayRoom-0ee008ae77fb76cbe9fe151cc6c4bc9f89cb98a5.tar.gz RayRoom-0ee008ae77fb76cbe9fe151cc6c4bc9f89cb98a5.zip |
- code cleanup and settings change
-rw-r--r-- | AudioTester/Core/RenderStreamer.cs | 12 | ||||
-rw-r--r-- | AudioTester/Program.cs | 26 | ||||
-rw-r--r-- | AudioTester/RayRoomSettings.cs | 8 | ||||
-rw-r--r-- | RayRoom.Tests/MathTest.cs | 2 | ||||
-rw-r--r-- | RayRoom/Class1.cs | 7 | ||||
-rw-r--r-- | RayRoom/Core/AudioSimulator.cs | 56 | ||||
-rw-r--r-- | RayRoom/Core/AudioSource.cs | 32 | ||||
-rw-r--r-- | RayRoom/Core/CastInfo.cs | 1 | ||||
-rw-r--r-- | RayRoom/Core/Circle.cs (renamed from RayRoom/Core/Sphere.cs) | 8 | ||||
-rw-r--r-- | RayRoom/Core/CollisionHelpers.cs | 10 | ||||
-rw-r--r-- | RayRoom/Core/ICastObject.cs (renamed from RayRoom/Core/IStructure.cs) | 4 | ||||
-rw-r--r-- | RayRoom/Core/Line.cs | 6 | ||||
-rw-r--r-- | RayRoom/Core/Matrix2x2.cs | 8 | ||||
-rw-r--r-- | RayRoom/Core/Ray.cs | 11 | ||||
-rw-r--r-- | RayRoom/Core/Settings.cs | 27 | ||||
-rw-r--r-- | RayRoom/Core/Structure.cs | 17 | ||||
-rw-r--r-- | RayRoom/Platform/AVXSummator.cs | 6 | ||||
-rw-r--r-- | RayRoom/Platform/IRaySummator.cs | 3 |
18 files changed, 151 insertions, 93 deletions
diff --git a/AudioTester/Core/RenderStreamer.cs b/AudioTester/Core/RenderStreamer.cs index 85f76a5..c7d2595 100644 --- a/AudioTester/Core/RenderStreamer.cs +++ b/AudioTester/Core/RenderStreamer.cs @@ -1,6 +1,4 @@ -using NAudio.Codecs; -using NAudio.Wave; -using NAudio.Wave.SampleProviders; +using NAudio.Wave; namespace AudioTester.Core { @@ -22,16 +20,16 @@ namespace AudioTester.Core sample = 0; streamReader = new AudioFileReader(@"Resources\test.wav"); - + provider = streamReader.ToSampleProvider(); } public int Read(float[] buffer, int offset, int count) { - int length = provider.Read(buffer, offset, count); - + int length = provider.Read(buffer, offset, count); + Console.WriteLine("i: {0}; o:{1}", count, length); - + if (length < count) { streamReader.Position = 0; diff --git a/AudioTester/Program.cs b/AudioTester/Program.cs index 14212af..95d6049 100644 --- a/AudioTester/Program.cs +++ b/AudioTester/Program.cs @@ -9,35 +9,39 @@ namespace AudioTester { internal class Program { + private const int width = 1280; + private const int height = 720; + private const float scale = 100; #pragma warning disable CA1416 // Проверка совместимости платформы static void Main(string[] args) { - AudioSimulator a = new AudioSimulator(); + AudioSimulator a = new AudioSimulator(new Settings(44100, 5, 330)); - var array = a.Simulate(Vector2.Zero, 360); - var bitmap = new Bitmap(2000, 2000); + var array = a.Simulate(Vector2.Zero, 3600); + var bitmap = new Bitmap(width, height); - Matrix2x2 mat = new Matrix2x2(Vector2.UnitX * 100f, Vector2.UnitY * -100f); + Matrix2x2 mat = new(Vector2.UnitX * scale, Vector2.UnitY * -scale); - Pen background = new Pen(Color.FromArgb(5, Color.White), 4); - Pen foreground = new Pen(Color.FromArgb(25, Color.White), 1); + Pen foreground = new(Color.FromArgb(20, Color.White), 1); using (Graphics g = Graphics.FromImage(bitmap)) { g.Clear(Color.Black); + Vector2 offset = new(width / 2, height / 2); for (int i = 0; i < array.Length; i++) { - g.DrawLine(background, (array[i].a * mat + new Vector2(1000, 1000)).GetPoint(), (array[i].b * mat + new Vector2(1000, 1000)).GetPoint()); - g.DrawLine(foreground, (array[i].a * mat + new Vector2(1000, 1000)).GetPoint(), (array[i].b * mat + new Vector2(1000, 1000)).GetPoint()); + g.DrawLine(foreground, + (array[i].a * mat + offset).GetPoint(), + (array[i].b * mat + offset).GetPoint()); } } - bitmap.Save("bitmap.bmp"); + bitmap.Save($"bitmap.png"); #pragma warning restore CA1416 // Проверка совместимости платформы return; var device = new RenderStreamer(); - + using (var wo = new WasapiOut(NAudio.CoreAudioApi.AudioClientShareMode.Shared, 150)) { wo.Init(device); @@ -46,7 +50,7 @@ namespace AudioTester while (wo.PlaybackState == PlaybackState.Playing) { Thread.Sleep(100); - + if (Console.KeyAvailable) wo.Stop(); } diff --git a/AudioTester/RayRoomSettings.cs b/AudioTester/RayRoomSettings.cs index c469279..8b05bcd 100644 --- a/AudioTester/RayRoomSettings.cs +++ b/AudioTester/RayRoomSettings.cs @@ -7,11 +7,9 @@ namespace AudioTester { public static Settings GetSettingsFromWaveFromat(WaveFormat format, Settings baseSettings) { - return new Settings(format.SampleRate, - baseSettings.maxRayReflections, - baseSettings.maxDiffusionRays, - baseSettings.fadeFactorPerMeter, - baseSettings.speedOfSoundMetersPerSec); + return new Settings(format.SampleRate, + baseSettings.maxRayReflections, + baseSettings.speedOfSound); } } } diff --git a/RayRoom.Tests/MathTest.cs b/RayRoom.Tests/MathTest.cs index 804f32c..01f2137 100644 --- a/RayRoom.Tests/MathTest.cs +++ b/RayRoom.Tests/MathTest.cs @@ -21,7 +21,7 @@ namespace RayRoom.Tests Assert.IsFalse(CollisionHelpers.RaycastRayToRay(a, d, out info)); Console.WriteLine(info.ToString()); } - + [TestMethod] public void TestRayToLineCollision() { diff --git a/RayRoom/Class1.cs b/RayRoom/Class1.cs deleted file mode 100644 index 7c8766e..0000000 --- a/RayRoom/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace RayRoom -{ - public class Class1 - { - - } -}
\ No newline at end of file diff --git a/RayRoom/Core/AudioSimulator.cs b/RayRoom/Core/AudioSimulator.cs index 78cb356..c5a913c 100644 --- a/RayRoom/Core/AudioSimulator.cs +++ b/RayRoom/Core/AudioSimulator.cs @@ -1,6 +1,4 @@ -using System; -using System.Numerics; -using System.Xml; +using System.Numerics; namespace RayRoom.Core { @@ -8,8 +6,14 @@ namespace RayRoom.Core { private Settings simulationSettings; + public AudioSimulator(Settings simulationSettings) + { + this.simulationSettings = simulationSettings; + } + public Line[] Simulate(Vector2 position, int count) { + count += 1; Ray[] startupRays = new Ray[count]; for (int i = 0; i < count; i++) { @@ -17,39 +21,45 @@ namespace RayRoom.Core startupRays[i] = new Ray(position, UnitVectorFromAngle(angle)); } - List<IStructure> structures = new List<IStructure> + List<ICastObject> structures = new List<ICastObject> { - new Sphere(new Vector2(0, -3), 2f), - new Sphere(new Vector2(5, 3), 2f), - new Sphere(new Vector2(-0.5f, 0.5f), 0.5f), + new Circle(new Vector2(0, -3), 2f), + new Circle(new Vector2(5, 3), 2f), + new Circle(new Vector2(-0.5f, 0.5f), 0.5f), new Line(new Vector2(-10, -10), new Vector2(0, 10)), + new Line(new Vector2(5, 3), new Vector2(0, 10)), + new Line(new Vector2(-10, -10), new Vector2(0, -3)), + new Line(new Vector2(0, -3), new Vector2(5, 3)), }; - List<Ray> rays = new List<Ray>(startupRays); + List<Ray> rays = new(startupRays); - List<Line> output = new List<Line>(count); + List<Line> output = new(count); int castCount = count * 10; - while (rays.Count > 0 && castCount-- > 0) + while (rays.Count > 0 && castCount-- > 0) { var ray = rays[0]; + rays.Remove(ray); - { - CastInfo closest = CastInfo.Default; - foreach (var structure in structures) - { - if (structure.CastRay(ray, out CastInfo info)) - if (closest.distance > info.distance) - closest = info; - } + if (ray.bounces >= simulationSettings.maxRayReflections) + continue; - if (closest.collided) - { - output.Add(new Line(ray.position, closest.point)); - rays.Add(new Ray(closest.point, ray.direction - 2 * Vector2.Dot(ray.direction, closest.normal) * closest.normal)); - } + CastInfo closest = CastInfo.Default; + + foreach (var structure in structures) + { + if (structure.CastRay(ray, out CastInfo info)) + if (closest.distance > info.distance) + closest = info; + } + + if (closest.collided) + { + output.Add(new Line(ray.position, closest.point)); + rays.Add(new Ray(closest.point, ray.direction - 2 * Vector2.Dot(ray.direction, closest.normal) * closest.normal, closest.distance, ray.bounces + 1)); } } diff --git a/RayRoom/Core/AudioSource.cs b/RayRoom/Core/AudioSource.cs new file mode 100644 index 0000000..ff5766f --- /dev/null +++ b/RayRoom/Core/AudioSource.cs @@ -0,0 +1,32 @@ +using System.Numerics; + +namespace RayRoom.Core +{ + public interface ISampleHandler + { + int Read(float[] buffer, int offset, int count); + } + + + public class AudioSource : ICastObject + { + public const float size = 1.0f; + + public bool IsAudioSource => true; + + private Circle source; + private float strength; + + public AudioSource(Vector2 position, float strength) + { + source = new Circle(position, strength); + this.strength = strength; + } + + + public bool CastRay(Ray ray, out CastInfo info) + { + return source.CastRay(ray, out info); + } + } +} diff --git a/RayRoom/Core/CastInfo.cs b/RayRoom/Core/CastInfo.cs index 6c6d9a0..9dde7e3 100644 --- a/RayRoom/Core/CastInfo.cs +++ b/RayRoom/Core/CastInfo.cs @@ -8,6 +8,7 @@ namespace RayRoom.Core public readonly Vector2 normal; public readonly float distance; public readonly bool collided; + public static CastInfo Default => new CastInfo(Vector2.Zero, Vector2.Zero, float.MaxValue, false); public CastInfo(Vector2 point, Vector2 normal, float distance, bool collided) diff --git a/RayRoom/Core/Sphere.cs b/RayRoom/Core/Circle.cs index 892e46e..28aee45 100644 --- a/RayRoom/Core/Sphere.cs +++ b/RayRoom/Core/Circle.cs @@ -2,17 +2,19 @@ namespace RayRoom.Core { - public class Sphere : IStructure + public class Circle : ICastObject { - public readonly Vector2 center; + public readonly Vector2 center; public readonly float radius; - public Sphere(Vector2 center, float radius) + public Circle(Vector2 center, float radius) { this.center = center; this.radius = radius; } + public bool IsAudioSource => false; + public bool CastRay(Ray ray, out CastInfo info) { return CollisionHelpers.RaycastRayToSphere(this, ray, out info); diff --git a/RayRoom/Core/CollisionHelpers.cs b/RayRoom/Core/CollisionHelpers.cs index b0d0d40..d509a0b 100644 --- a/RayRoom/Core/CollisionHelpers.cs +++ b/RayRoom/Core/CollisionHelpers.cs @@ -45,14 +45,14 @@ namespace RayRoom.Core return new Distances(u, v); } - public static Distances TestSphereToRay(Sphere a, Ray b, out CastInfo info) + public static Distances TestSphereToRay(Circle a, Ray b, out CastInfo info) { Vector2 position = b.position - a.center; - + float vb = 2 * b.direction.X * position.X + 2 * b.direction.Y * position.Y; float va = MathF.Pow(b.direction.X, 2) + MathF.Pow(b.direction.Y, 2); float vc = MathF.Pow(position.X, 2) + MathF.Pow(position.Y, 2) - MathF.Pow(a.radius, 2); - + float discr = MathF.Pow(vb, 2) - 4 * va * vc; Distances result; @@ -96,8 +96,8 @@ namespace RayRoom.Core else return n1; } - - public static bool RaycastRayToSphere(Sphere sphere, Ray b, out CastInfo info) + + public static bool RaycastRayToSphere(Circle sphere, Ray b, out CastInfo info) { var distances = TestSphereToRay(sphere, b, out info); diff --git a/RayRoom/Core/IStructure.cs b/RayRoom/Core/ICastObject.cs index 9b42c92..292d26d 100644 --- a/RayRoom/Core/IStructure.cs +++ b/RayRoom/Core/ICastObject.cs @@ -1,7 +1,9 @@ namespace RayRoom.Core { - public interface IStructure + public interface ICastObject { + bool IsAudioSource { get; } + bool CastRay(Ray ray, out CastInfo info); } }
\ No newline at end of file diff --git a/RayRoom/Core/Line.cs b/RayRoom/Core/Line.cs index 84d82fe..a783878 100644 --- a/RayRoom/Core/Line.cs +++ b/RayRoom/Core/Line.cs @@ -2,17 +2,19 @@ namespace RayRoom.Core { - public class Line : IStructure + public class Line : ICastObject { public readonly Vector2 a; public readonly Vector2 b; - + public Line(Vector2 a, Vector2 b) { this.a = a; this.b = b; } + public bool IsAudioSource => false; + public bool CastRay(Ray ray, out CastInfo info) { return CollisionHelpers.RaycastRayToLine(this, ray, out info); diff --git a/RayRoom/Core/Matrix2x2.cs b/RayRoom/Core/Matrix2x2.cs index d2ef642..5eebd87 100644 --- a/RayRoom/Core/Matrix2x2.cs +++ b/RayRoom/Core/Matrix2x2.cs @@ -7,7 +7,7 @@ namespace AudioTester float a, b, c, d; public static Matrix2x2 Identity => new Matrix2x2(Vector2.UnitX, Vector2.UnitY); - + public Matrix2x2(float a, float b, float c, float d) { this.a = a; @@ -24,14 +24,14 @@ namespace AudioTester this.d = b.Y; } - public readonly float Determinant => (a*d) - (b*c); + public readonly float Determinant => (a * d) - (b * c); - public static Vector2 operator*(Vector2 vector, Matrix2x2 matrix) + public static Vector2 operator *(Vector2 vector, Matrix2x2 matrix) { return new Vector2(matrix.a, matrix.c) * vector.X + new Vector2(matrix.b, matrix.d) * vector.Y; } - public static Matrix2x2 operator*(Matrix2x2 a, Matrix2x2 b) + public static Matrix2x2 operator *(Matrix2x2 a, Matrix2x2 b) { return new Matrix2x2(new Vector2(a.a, a.c) * b, new Vector2(a.b, a.d) * b); } diff --git a/RayRoom/Core/Ray.cs b/RayRoom/Core/Ray.cs index 9cc8a6e..2650cec 100644 --- a/RayRoom/Core/Ray.cs +++ b/RayRoom/Core/Ray.cs @@ -1,5 +1,4 @@ -using System.ComponentModel; -using System.Numerics; +using System.Numerics; namespace RayRoom.Core { @@ -7,16 +6,20 @@ namespace RayRoom.Core { public readonly Vector2 position; public readonly Vector2 direction; + public readonly float distance; + public readonly int bounces; - public Ray(Vector2 position, Vector2 direction) + public Ray(Vector2 position, Vector2 direction, float distance = 0, int bounces = 0) { this.position = position; this.direction = direction; + this.distance = distance; + this.bounces = bounces; } public override string? ToString() { - return string.Format("p:{0} d:{1}", position.ToString(), direction.ToString()); + return string.Format("pos:{0} dir:{1} dist:{2}", position.ToString(), direction.ToString(), distance); } } }
\ No newline at end of file diff --git a/RayRoom/Core/Settings.cs b/RayRoom/Core/Settings.cs index f6064b8..72eaebc 100644 --- a/RayRoom/Core/Settings.cs +++ b/RayRoom/Core/Settings.cs @@ -1,25 +1,28 @@ -using System; - -namespace RayRoom.Core +namespace RayRoom.Core { public struct Settings { public readonly int frequency; public readonly int maxRayReflections; - public readonly int maxDiffusionRays; - - public readonly float fadeFactorPerMeter; //curve - public readonly float speedOfSoundMetersPerSec; + public readonly float speedOfSound; - public static Settings Default => new Settings(44100, 10, 15, 0.001f, 330f); + public static Settings Default => new Settings(44100, 10, 330f); - public Settings(int frequency, int maxRayReflections, int maxDiffusionRays, float fadeFactorPerMeter, float speedOfSoundMetersPerSec) + public Settings(int frequency, int maxRayReflections, float speedOfSound) { this.frequency = frequency; this.maxRayReflections = maxRayReflections; - this.maxDiffusionRays = maxDiffusionRays; - this.fadeFactorPerMeter = fadeFactorPerMeter; - this.speedOfSoundMetersPerSec = speedOfSoundMetersPerSec; + this.speedOfSound = speedOfSound; + } + + public float GetSecoundsDelay(float distance) + { + return distance / speedOfSound; + } + + public int GetSamplesDelay(float distance) + { + return (int)(GetSecoundsDelay(distance) * frequency); } } } diff --git a/RayRoom/Core/Structure.cs b/RayRoom/Core/Structure.cs new file mode 100644 index 0000000..9947d60 --- /dev/null +++ b/RayRoom/Core/Structure.cs @@ -0,0 +1,17 @@ +namespace RayRoom.Core +{ + internal class Structure + { + private List<ICastObject> structures; + + public void AddAudioSource(AudioSource audioSource) + { + + } + + public void AddCastObject(ICastObject castObject) + { + + } + } +} diff --git a/RayRoom/Platform/AVXSummator.cs b/RayRoom/Platform/AVXSummator.cs index 17db989..6c0a4b9 100644 --- a/RayRoom/Platform/AVXSummator.cs +++ b/RayRoom/Platform/AVXSummator.cs @@ -1,11 +1,5 @@ using RayRoom.Core; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; -using System.Text; -using System.Threading.Tasks; namespace RayRoom.Platform { diff --git a/RayRoom/Platform/IRaySummator.cs b/RayRoom/Platform/IRaySummator.cs index 177b327..8b3ec2d 100644 --- a/RayRoom/Platform/IRaySummator.cs +++ b/RayRoom/Platform/IRaySummator.cs @@ -1,5 +1,4 @@ -using System; -using RayRoom.Core; +using RayRoom.Core; namespace RayRoom.Platform { |