Skip to content

Commit eafa846

Browse files
authored
Merge pull request #264 from SourceDotNET/lua
Lua (& more)
2 parents 62578bc + daaa2ff commit eafa846

87 files changed

Lines changed: 5415 additions & 167 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ FodyWeavers.xsd
387387
!.vscode/launch.json
388388
!.vscode/extensions.json
389389
*.code-workspace
390+
.forge/*
390391

391392
# Local History for Visual Studio Code
392393
.history/

‎Dependencies/LuaJIT‎

‎Directory.Build.props‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373

7474
<PropertyGroup Condition="'$(IsAnalyzer)' == 'true'">
7575
<PlatformTarget>AnyCPU</PlatformTarget>
76+
<NoWarn>$(NoWarn);RS2008</NoWarn>
7677
</PropertyGroup>
7778

7879
<PropertyGroup Condition="!$(MSBuildProjectName.StartsWith('Source.CodeAnalysis'))">

‎Directory.Packages.props‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<PackageVersion Include="FreeTypeSharp" Version="3.1.0" />
1616
<PackageVersion Include="K4os.Hash.xxHash" Version="1.0.8" />
1717
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="11.0.0" />
18+
<PackageVersion Include="ManagedBass" Version="4.0.2" />
1819
<PackageVersion Include="NLayer" Version="1.16.0" />
1920
<PackageVersion Include="Pastel" Version="6.0.1" />
2021
<PackageVersion Include="PInvoke.Gdi32" Version="0.7.124" />
854 KB
Binary file not shown.

‎Game.BASS/BassAudioStream.cs‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using ManagedBass;
2+
3+
using Source.Common.Audio;
4+
using Source.Common.GarrysMod;
5+
6+
using System.Runtime.InteropServices;
7+
8+
namespace Game.BASS;
9+
10+
public class BassAudioStream : IBassAudioStream, IDisposable
11+
{
12+
int Handle;
13+
IAudioStreamEvent? Event;
14+
uint Offset;
15+
GCHandle Self;
16+
17+
static readonly FileProcedures StreamProcs = new() {
18+
Close = static user => { },
19+
Length = static user => 0,
20+
Read = StreamRead,
21+
Seek = StreamSeek,
22+
};
23+
24+
static BassAudioStream Get(nint user) => (BassAudioStream)GCHandle.FromIntPtr(user).Target!;
25+
26+
static unsafe int StreamRead(nint buffer, int length, nint user) {
27+
BassAudioStream stream = Get(user);
28+
int read = stream.Event!.StreamRequestData(new Span<byte>((void*)buffer, length), length, (int)stream.Offset);
29+
stream.Offset += (uint)read;
30+
return read;
31+
}
32+
33+
static bool StreamSeek(long offset, nint user) {
34+
if ((ulong)offset > 0x4000000)
35+
return false;
36+
Get(user).Offset = (uint)offset;
37+
return true;
38+
}
39+
40+
public bool Init(IAudioStreamEvent? eventHandler) {
41+
Event = eventHandler;
42+
Self = GCHandle.Alloc(this);
43+
Handle = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode | BassFlags.StreamDownloadBlocks | BassFlags.RestrictDownloadRate, StreamProcs, GCHandle.ToIntPtr(Self));
44+
if (Handle == 0) {
45+
Warning($"Couldn't create BASS audio stream ({GMod_Audio.BassErrorDescription(Bass.LastError)})\n");
46+
return false;
47+
}
48+
return true;
49+
}
50+
51+
public void Dispose() {
52+
if (Handle != 0)
53+
Bass.StreamFree(Handle);
54+
if (Self.IsAllocated)
55+
Self.Free();
56+
}
57+
58+
public unsafe int Decode(Span<byte> buffer) {
59+
if (Handle == 0)
60+
return 0;
61+
int ret;
62+
fixed (byte* ptr = buffer)
63+
ret = Bass.ChannelGetData(Handle, (nint)ptr, buffer.Length);
64+
return ret < 0 ? 0 : ret;
65+
}
66+
67+
public int GetOutputBits() {
68+
Error("Not used");
69+
return 0;
70+
}
71+
72+
public int GetOutputRate() {
73+
Bass.ChannelGetInfo(Handle, out ChannelInfo info);
74+
return info.Frequency == 0 ? 44100 : info.Frequency;
75+
}
76+
77+
public int GetOutputChannels() {
78+
Bass.ChannelGetInfo(Handle, out ChannelInfo info);
79+
return info.Channels == 0 ? 2 : info.Channels;
80+
}
81+
82+
public uint GetPosition() {
83+
if (Handle == 0)
84+
return 0;
85+
return (uint)Bass.StreamGetFilePosition(Handle, FileStreamPosition.Current);
86+
}
87+
88+
public void SetPosition(uint distance) { }
89+
90+
public uint GetHandle() => (uint)Handle;
91+
}

‎Game.BASS/GModAudioChannel.cs‎

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
using ManagedBass;
2+
3+
using Source.Common.GarrysMod;
4+
5+
using System.Numerics;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
9+
namespace Game.BASS;
10+
11+
public class GModAudioChannel(int handle, bool isFile, int type, string? fileName) : IGModAudioChannel
12+
{
13+
int Handle = handle;
14+
readonly bool IsFile = isFile;
15+
readonly int Type = type;
16+
readonly string? FileName = fileName;
17+
18+
public void Destroy() {
19+
if (Bass.ChannelIsActive(Handle) == PlaybackState.Playing && !IsLooping() && Bass.ChannelGetLength(Handle, PositionFlags.Bytes) >= 0) {
20+
Bass.ChannelFlags(Handle, BassFlags.AutoFree, BassFlags.AutoFree);
21+
Handle = 0;
22+
return;
23+
}
24+
25+
if (Type == 0)
26+
Bass.StreamFree(Handle);
27+
else if (Type == 1)
28+
Bass.MusicFree(Handle);
29+
30+
Handle = 0;
31+
}
32+
33+
public void Stop() => Bass.ChannelStop(Handle);
34+
public void Pause() => Bass.ChannelPause(Handle);
35+
public void Play() => Bass.ChannelPlay(Handle, false);
36+
37+
public void SetVolume(float volume) => Bass.ChannelSetAttribute(Handle, ChannelAttribute.Volume, volume);
38+
39+
public float GetVolume() {
40+
Bass.ChannelGetAttribute(Handle, ChannelAttribute.Volume, out float volume);
41+
return volume;
42+
}
43+
44+
public void SetPlaybackRate(float rate) => Bass.ChannelSetAttribute(Handle, ChannelAttribute.Frequency, rate * GetSamplingRate());
45+
46+
public float GetPlaybackRate() {
47+
Bass.ChannelGetAttribute(Handle, ChannelAttribute.Frequency, out float frequency);
48+
if (frequency <= 0.0f)
49+
return 1.0f;
50+
return frequency / GetSamplingRate();
51+
}
52+
53+
public void SetPos(in Vector3 position, in Vector3 direction, in Vector3 velocity) {
54+
Bass.ChannelSet3DPosition(Handle, GMod_Audio.ToBass(position), GMod_Audio.ToBass(direction), GMod_Audio.ToBass(velocity));
55+
Bass.Apply3D();
56+
}
57+
58+
public void GetPos(out Vector3 position, out Vector3 direction, out Vector3 velocity) {
59+
Vector3D pos = new(), dir = new(), vel = new();
60+
Bass.ChannelGet3DPosition(Handle, ref pos, ref dir, ref vel);
61+
position = GMod_Audio.FromBass(pos);
62+
direction = GMod_Audio.FromBass(dir);
63+
velocity = GMod_Audio.FromBass(vel);
64+
}
65+
66+
public void SetTime(TimeUnit_t time, bool dontDecode) {
67+
long target = Bass.ChannelSeconds2Bytes(Handle, time);
68+
long current = Bass.ChannelGetPosition(Handle, PositionFlags.Bytes);
69+
PositionFlags flags = PositionFlags.Bytes;
70+
if (current < target && !dontDecode)
71+
flags = PositionFlags.DecodeTo;
72+
Bass.ChannelSetPosition(Handle, target, flags);
73+
}
74+
75+
public TimeUnit_t GetTime() => Bass.ChannelBytes2Seconds(Handle, Bass.ChannelGetPosition(Handle, PositionFlags.Bytes));
76+
77+
public TimeUnit_t GetBufferedTime() {
78+
if (IsFile)
79+
return GetLength();
80+
81+
long length = Bass.ChannelGetLength(Handle, PositionFlags.Bytes);
82+
long download = Bass.StreamGetFilePosition(Handle, FileStreamPosition.Download);
83+
long start = Bass.StreamGetFilePosition(Handle, FileStreamPosition.Start);
84+
long size = Bass.StreamGetFilePosition(Handle, (FileStreamPosition)8);
85+
double buffered = (double)(ulong)download / (double)(ulong)(size - start) * (double)(ulong)length;
86+
return Bass.ChannelBytes2Seconds(Handle, (long)(ulong)buffered);
87+
}
88+
89+
public void Set3DFadeDistance(float min, float max) {
90+
Bass.ChannelSet3DAttributes(Handle, Mode3D.LeaveCurrent, min, max, -1, -1, -1.0f);
91+
Bass.Apply3D();
92+
}
93+
94+
public void Get3DFadeDistance(out float min, out float max) {
95+
Mode3D mode = 0;
96+
int iangle = 0, oangle = 0;
97+
float outvol = 0;
98+
min = 0;
99+
max = 0;
100+
Bass.ChannelGet3DAttributes(Handle, ref mode, ref min, ref max, ref iangle, ref oangle, ref outvol);
101+
}
102+
103+
public void Set3DCone(int innerAngle, int outerAngle, float outerVolume) {
104+
if (innerAngle < 0 || outerAngle < 0) {
105+
innerAngle = -1;
106+
outerAngle = -1;
107+
}
108+
Bass.ChannelSet3DAttributes(Handle, Mode3D.LeaveCurrent, -1.0f, -1.0f, innerAngle, outerAngle, outerVolume);
109+
Bass.Apply3D();
110+
}
111+
112+
public void Get3DCone(out int innerAngle, out int outerAngle, out float outerVolume) {
113+
Mode3D mode = 0;
114+
float min = 0, max = 0;
115+
innerAngle = 0;
116+
outerAngle = 0;
117+
outerVolume = 0;
118+
Bass.ChannelGet3DAttributes(Handle, ref mode, ref min, ref max, ref innerAngle, ref outerAngle, ref outerVolume);
119+
}
120+
121+
public int GetState() => Bass.ChannelIsActive(Handle) switch {
122+
PlaybackState.Playing => 1,
123+
PlaybackState.Stalled => 3,
124+
PlaybackState.Paused => 2,
125+
_ => 0,
126+
};
127+
128+
public void SetLooping(bool looping) => Bass.ChannelFlags(Handle, looping ? BassFlags.Loop : BassFlags.Default, BassFlags.Loop);
129+
public bool IsLooping() => (Bass.ChannelFlags(Handle, BassFlags.Default, BassFlags.Default) & BassFlags.Loop) != 0;
130+
public bool IsOnline() => !IsFile;
131+
public bool Is3D() => (Bass.ChannelFlags(Handle, BassFlags.Default, BassFlags.Default) & BassFlags.Bass3D) != 0;
132+
public bool IsBlockStreamed() => (Bass.ChannelFlags(Handle, BassFlags.Default, BassFlags.Default) & BassFlags.StreamDownloadBlocks) != 0;
133+
public bool IsValid() => Handle != 0;
134+
135+
public double GetLength() => Bass.ChannelBytes2Seconds(Handle, Bass.ChannelGetLength(Handle, PositionFlags.Bytes));
136+
137+
public ReadOnlySpan<char> GetFileName() {
138+
if (FileName != null)
139+
return FileName;
140+
Bass.ChannelGetInfo(Handle, out ChannelInfo info);
141+
ChannelInfoRaw raw = Unsafe.As<ChannelInfo, ChannelInfoRaw>(ref info);
142+
if (raw.FileName == 0)
143+
return "";
144+
return ((raw.Flags & BassFlags.Unicode) != 0 ? Marshal.PtrToStringUni(raw.FileName) : Marshal.PtrToStringAnsi(raw.FileName)) ?? "";
145+
}
146+
147+
[StructLayout(LayoutKind.Sequential)]
148+
struct ChannelInfoRaw
149+
{
150+
public int Frequency;
151+
public int Channels;
152+
public BassFlags Flags;
153+
public ChannelType ChannelType;
154+
public int OriginalResolution;
155+
public int Plugin;
156+
public int Sample;
157+
public nint FileName;
158+
}
159+
160+
public int GetSamplingRate() {
161+
Bass.ChannelGetInfo(Handle, out ChannelInfo info);
162+
return info.Frequency;
163+
}
164+
165+
public int GetBitsPerSample() {
166+
Bass.ChannelGetInfo(Handle, out ChannelInfo info);
167+
return info.OriginalResolution;
168+
}
169+
170+
public float GetAverageBitRate() {
171+
Bass.ChannelGetAttribute(Handle, ChannelAttribute.Bitrate, out float bitrate);
172+
return bitrate;
173+
}
174+
175+
public void GetLevel(out float left, out float right) {
176+
int level = Bass.ChannelIsActive(Handle) == PlaybackState.Playing ? Bass.ChannelGetLevel(Handle) : 0;
177+
left = ((uint)level >> 16) * 3.0517578e-05f;
178+
right = (level & 0xffff) * 3.0517578e-05f;
179+
}
180+
181+
public int FFT(float[] data, GModChannelFFT size) {
182+
if (Bass.ChannelIsActive(Handle) != PlaybackState.Playing)
183+
return 0;
184+
185+
(int count, DataFlags flags) = size switch {
186+
GModChannelFFT.FFT_256 => (128, DataFlags.FFT256),
187+
GModChannelFFT.FFT_512 => (256, DataFlags.FFT512),
188+
GModChannelFFT.FFT_2048 => (1024, DataFlags.FFT2048),
189+
GModChannelFFT.FFT_4096 => (2048, DataFlags.FFT4096),
190+
GModChannelFFT.FFT_8192 => (4096, DataFlags.FFT8192),
191+
GModChannelFFT.FFT_16384 => (8192, DataFlags.FFT16384),
192+
GModChannelFFT.FFT_32768 => (16384, DataFlags.FFT32768),
193+
_ => (512, DataFlags.FFT1024),
194+
};
195+
Bass.ChannelGetData(Handle, data, (int)(flags | DataFlags.Float | DataFlags.FFTRemoveDC));
196+
return count;
197+
}
198+
199+
public void SetChannelPan(float pan) => Bass.ChannelSetAttribute(Handle, ChannelAttribute.Pan, pan);
200+
201+
public float GetChannelPan() {
202+
Bass.ChannelGetAttribute(Handle, ChannelAttribute.Pan, out float pan);
203+
return pan;
204+
}
205+
206+
public nint GetTags(int type) => Bass.ChannelGetTags(Handle, (TagType)type);
207+
208+
public void Set3DEnabled(bool enabled) {
209+
if (!Is3D())
210+
return;
211+
Bass.ChannelSet3DAttributes(Handle, enabled ? Mode3D.Normal : Mode3D.Off, -1.0f, -1.0f, -1, -1, -1.0f);
212+
Bass.Apply3D();
213+
}
214+
215+
public bool Get3DEnabled() {
216+
if (!Is3D())
217+
return false;
218+
Mode3D mode = 0;
219+
float min = 0, max = 0, outvol = 0;
220+
int iangle = 0, oangle = 0;
221+
Bass.ChannelGet3DAttributes(Handle, ref mode, ref min, ref max, ref iangle, ref oangle, ref outvol);
222+
return mode != Mode3D.Off;
223+
}
224+
225+
public void Restart() => Bass.ChannelPlay(Handle, true);
226+
}

0 commit comments

Comments
 (0)