Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBrixican committed Feb 5, 2024
1 parent 7d3a08a commit a220473
Show file tree
Hide file tree
Showing 30 changed files with 118,838 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

# use tabs by default for C#/C code
[*.{cs,hpp,cpp,c,h}]
indent_style = tab
indent_size = 4

# use 2-spaces for csproj
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
indent_style = space
indent_size = 2
76 changes: 76 additions & 0 deletions .github/workflows/build-libs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Build Libs

on:
workflow_dispatch:
push:
branches:
- 'main'
paths:
- 'Platform/**'
- '!Platform/libs/**'
pull_request:
paths:
- 'Platform/**'
- '!Platform/libs/**'
jobs:
Build:
strategy:
matrix:
include:
- os: windows-latest
lib: x64/FosterAudioPlatform.dll
- os: macos-latest
lib: osx/libFosterAudioPlatform.dylib
- os: ubuntu-latest
lib: lib64/libFosterAudioPlatform.so
runs-on: ${{matrix.os}}
steps:
- name: Checkout
uses: actions/[email protected]
- name: Get CMake
uses: lukka/[email protected]
- name: Setup Linux dependencies
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install libsdl2-dev
- name: CMake Configure
run: cmake -B build -S Platform
- name: CMake Build
run: cmake --build build --config Release
- name: Publish Artifact
uses: actions/[email protected]
with:
name: ${{matrix.os}}-build
path: Platform/libs/${{matrix.lib}}
UpdateLibs:
if: github.ref == 'refs/heads/main'
needs: [Build]
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Download windows lib
uses: actions/download-artifact@v3
with:
name: windows-latest-build
path: Platform/libs/x64
- name: Download macos lib
uses: actions/download-artifact@v3
with:
name: macos-latest-build
path: Platform/libs/osx
- name: Download ubuntu lib
uses: actions/download-artifact@v3
with:
name: ubuntu-latest-build
path: Platform/libs/lib64
- name: Display structure of libs
run: ls -R
working-directory: Platform/libs
- name: Commit changes
uses: EndBug/[email protected]
with:
message: Updated platform libs
committer_name: GitHub Actions
committer_email: [email protected]
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.vscode
.vs
bin
obj
build
artifacts
25 changes: 25 additions & 0 deletions Foster.Audio.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34310.174
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foster.Audio", "Foster.Audio\Foster.Audio.csproj", "{0DE61728-C4E1-4798-864C-46823EFF2274}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0DE61728-C4E1-4798-864C-46823EFF2274}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0DE61728-C4E1-4798-864C-46823EFF2274}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DE61728-C4E1-4798-864C-46823EFF2274}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0DE61728-C4E1-4798-864C-46823EFF2274}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DE3B335B-C9A8-412D-BF50-B2FB40C9788E}
EndGlobalSection
EndGlobal
143 changes: 143 additions & 0 deletions Foster.Audio/Audio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;

namespace Foster.Audio;

public static class Audio
{
/// <summary>
/// Master volume
/// </summary>
public static float Volume
{
get => Platform.FosterAudioGetVolume();
set => Platform.FosterAudioSetVolume(value);
}

/// <summary>
/// Audio engine channel count
/// </summary>
public static int Channels { get; private set; }

/// <summary>
/// Audio engine sample rate
/// </summary>
public static int SampleRate { get; private set; }

/// <summary>
/// Audio engine clock in PCM frames (see <seealso cref="SampleRate"/>)
/// </summary>
public static ulong TimePcmFrames
{
get => Platform.FosterAudioGetTimePcmFrames();
set => Platform.FosterAudioSetTimePcmFrames(value);
}

/// <summary>
/// Audio engine clock
/// </summary>
public static TimeSpan Time
{
get => TimeSpan.FromSeconds(1.0 * TimePcmFrames / SampleRate);
set => TimePcmFrames = (ulong)Math.Floor(value.TotalSeconds * SampleRate);
}

/// <summary>
/// The maximum number of simultaneously active <see cref="SoundInstance"/>s allowed.
/// Instances created after this threshold are inactive and do not play any audio.
/// </summary>
public static int MaxActiveInstances { get; set; } = 100;

/// <summary>
/// The number of currently active <see cref="SoundInstance"/>s
/// </summary>
public static int ActiveInstances { get; internal set; }

/// <summary>
/// The primary (index 0) <see cref="AudioListener"/>
/// </summary>
public static AudioListener Listener { get; private set; } = null!;

/// <summary>
/// All available <see cref="AudioListener"/>s
/// </summary>
public static ReadOnlyCollection<AudioListener> Listeners { get; private set; } = null!;

/// <summary>
/// All instances currently being tracked, some may be inactive
/// </summary>
public static ReadOnlySpan<SoundInstance> Instances => CollectionsMarshal.AsSpan(instances);

private static readonly List<SoundInstance> instances = new();

public static void PlayAll() => ApplyAll(m => m.Play());

public static void PauseAll() => ApplyAll(m => m.Pause());

public static void StopAll() => ApplyAll(m => m.Stop());

public static void ReleaseAll() => ApplyAll(m => m.Release());

public static void ApplyAll(Action<SoundInstance> action)
{
foreach (var instance in Instances)
{
if(instance.Active)
{
action(instance);
}
}
}

internal static void Initialize()
{
Channels = Platform.FosterAudioGetChannels();
SampleRate = Platform.FosterAudioGetSampleRate();
Listeners = Enumerable.Range(0, Platform.FosterAudioGetListenerCount())
.Select(i => new AudioListener(i))
.ToList()
.AsReadOnly();
Listener = Listeners[0];
}

internal static void Step()
{
// Go through all instances and destroy all that are non-protected, finished
for (int i = 0;i<instances.Count;i++)
{
var instance = instances[i];
if (instance.Protected)
{
continue;
}

if (instance.Finished)
{
instance.Release();
}
}

// Remove inactive instances
lock (instances)
{
instances.RemoveAll(i => !i.Active);
}
}

internal static void Shutdown()
{
ReleaseAll();
lock (instances)
{
instances.Clear();
}
}

internal static void OnSoundInstanceCreate(SoundInstance instance)
{
lock(instances)
{
instances.Add(instance);
}
}
}
49 changes: 49 additions & 0 deletions Foster.Audio/AudioListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Numerics;

namespace Foster.Audio;

public class AudioListener
{
public int Index { get; }

public bool Enabled
{
get => Platform.FosterAudioListenerGetEnabled(Index);
set => Platform.FosterAudioListenerSetEnabled(Index, value);
}

public Vector3 Position
{
get => Platform.FosterAudioListenerGetPosition(Index);
set => Platform.FosterAudioListenerSetPosition(Index, value);
}

public Vector3 Velocity
{
get => Platform.FosterAudioListenerGetVelocity(Index);
set => Platform.FosterAudioListenerSetVelocity(Index, value);
}

public Vector3 Direction
{
get => Platform.FosterAudioListenerGetDirection(Index);
set => Platform.FosterAudioListenerSetDirection(Index, value);
}

public SoundCone Cone
{
get => Platform.FosterAudioListenerGetCone(Index);
set => Platform.FosterAudioListenerSetCone(Index, value);
}

public Vector3 WorldUp
{
get => Platform.FosterAudioListenerGetWorldUp(Index);
set => Platform.FosterAudioListenerSetWorldUp(Index, value);
}

internal AudioListener(int index)
{
Index = index;
}
}
28 changes: 28 additions & 0 deletions Foster.Audio/Enums/AudioFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Foster.Audio;

public enum AudioFormat
{
Unknown = 0,
U8 = 1,
S16 = 2,
S24 = 3,
S32 = 4,
F32 = 5
}

public static class AudioFormatExtensions
{
public static int GetSampleSize(this AudioFormat format)
{
return format switch
{
AudioFormat.Unknown => 0,
AudioFormat.U8 => 1,
AudioFormat.S16 => 2,
AudioFormat.S24 => 3,
AudioFormat.S32 => 4,
AudioFormat.F32 => 4,
_ => 0
};
}
}
9 changes: 9 additions & 0 deletions Foster.Audio/Enums/SoundAttenuationModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Foster.Audio;

public enum SoundAttenuationModel
{
None,
Inverse,
Linear,
Exponential
}
26 changes: 26 additions & 0 deletions Foster.Audio/Enums/SoundLoadingMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Foster.Audio;

public enum SoundLoadingMethod
{
/// <summary>
/// Loads encoded data into memory
/// </summary>
Preload,
/// <summary>
/// Loads decoded data into memory
/// </summary>
PreloadDecoded,
/// <summary>
/// While at least one instance is active, loads encoded data into memory
/// </summary>
LoadOnDemand,
/// <summary>
/// While at least one instance is active, loads decoded data into memory
/// </summary>
LoadOnDemandDecoded,
/// <summary>
/// Streams data from file system <br/>
/// Limitation: ogg files do not work properly in respect to <see cref="SoundInstance.Length"/>, <see cref="SoundInstance.LengthPcmFrames"/>, <see cref="SoundInstance.Cursor"/>, and <see cref="SoundInstance.CursorPcmFrames"/>
/// </summary>
Stream
}
Loading

0 comments on commit a220473

Please sign in to comment.