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
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ on:
jobs:
build:
uses: KSPModdingLibs/KSPBuildTools/.github/workflows/build.yml@0.0.4
with:
dependency-identifiers: Harmony2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be use-ckan: true and set the properties appropriately in the csproj

17 changes: 17 additions & 0 deletions Source/RocketSoundEnhancement/Patches/AudioSource_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using HarmonyLib;
using UnityEngine;
using System;

namespace RocketSoundEnhancement.Patches
{
[HarmonyPatch(typeof(AudioSource), MethodType.Constructor, argumentTypes: new Type[0])]
internal static class AudioSource_Ctor_Patch
{
static void Postfix(AudioSource __instance)
{
RocketSoundEnhancement.Instance?.newAudioSources?.Add(__instance);
}
}
}


32 changes: 25 additions & 7 deletions Source/RocketSoundEnhancement/RocketSoundEnhancement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ public static AudioMixer Mixer
private float lastInteriorCutoffFreq;

private HashSet<int> managedSources = new HashSet<int>();
internal readonly List<AudioSource> newAudioSources = new List<AudioSource>();

private bool gamePaused;

private void Awake()
{
if (instance != null) { Destroy(instance); }

// Get all the currently existing audio sources.
newAudioSources.AddRange((AudioSource[])FindObjectsOfType(typeof(AudioSource)));
instance = this;
}

Expand Down Expand Up @@ -162,27 +165,42 @@ public float WindModulation()
return Mathf.Lerp(1, windModulation, Mathf.Clamp01((float)FlightGlobals.ActiveVessel.atmDensity));
}

bool GetNextAudioSource(out AudioSource source)
{
if (newAudioSources.Count == 0)
{
source = null;
return false;
}

// This makes sure things still work even if a new audio source ends
// up being created while we are running.
source = newAudioSources[newAudioSources.Count - 1];
Copy link
Member

@JonnyOThan JonnyOThan Nov 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should check if the source is destroyed here and only return non-destroyed ones?

Oh never mind I thought you might be spreading them out over several frames.

newAudioSources.RemoveAt(newAudioSources.Count - 1);
return true;
}

public void LateUpdate()
{
if (gamePaused || !HighLogic.LoadedSceneIsFlight) return;

if (!Settings.EnableAudioEffects || Mixer == null)
{
// TODO: some kind of latch so this only runs once
foreach (var sourceObj in FindObjectsOfType(typeof(AudioSource)))
while (GetNextAudioSource(out var source))
{
AudioSource source = (AudioSource)sourceObj;
if (source != null) source.outputAudioMixerGroup = null;
if (source != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a faster null check, see KSPCF

source.outputAudioMixerGroup = null;
}

managedSources.Clear();

return;
}

// NOTE: the generic (strongly typed) version of FindObjectsOfType is slower!
foreach (var sourceObj in FindObjectsOfType(typeof(AudioSource)))
while (GetNextAudioSource(out var source))
{
AudioSource source = (AudioSource)sourceObj;
if (source == null)
continue;

int instanceID = source.GetInstanceID();

Expand Down
38 changes: 23 additions & 15 deletions Source/RocketSoundEnhancement/RocketSoundEnhancement.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,34 @@
<Publicize Include="Assembly-CSharp" IncludeCompilerGeneratedMembers="false" />
</ItemGroup>

<PropertyGroup>
<BinariesOutputRelativePath>GameData\RocketSoundEnhancement\Plugins</BinariesOutputRelativePath>
<GenerateKSPAssemblyAttribute>true</GenerateKSPAssemblyAttribute>
<GenerateKSPAssemblyDependencyAttributes>true</GenerateKSPAssemblyDependencyAttributes>
</PropertyGroup>
<PropertyGroup>
<BinariesOutputRelativePath>GameData\RocketSoundEnhancement\Plugins</BinariesOutputRelativePath>
<GenerateKSPAssemblyAttribute>true</GenerateKSPAssemblyAttribute>
<GenerateKSPAssemblyDependencyAttributes>true</GenerateKSPAssemblyDependencyAttributes>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\RocketSoundEnhancement.Unity\RocketSoundEnhancement.Unity.csproj">
<Project>{5dff0176-7ba8-49f4-8155-f5d9b37d7d3d}</Project>
<Name>RocketSoundEnhancement.Unity</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ProjectReference
Include="..\RocketSoundEnhancement.Unity\RocketSoundEnhancement.Unity.csproj">
<Project>{5dff0176-7ba8-49f4-8155-f5d9b37d7d3d}</Project>
<Name>RocketSoundEnhancement.Unity</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Krafs.Publicizer" Version="2.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="KSPBuildTools" Version="0.0.4" />

<Reference Include="HarmonyKSP">
<HintPath>$(KSPRoot)/GameData/000_Harmony/0Harmony.dll</HintPath>
<Private>false</Private>
<KSPAssembly>HarmonyKSP</KSPAssembly>
<KSPAssemblyVersion>2.2.1</KSPAssemblyVersion>
</Reference>
</ItemGroup>

</Project>
</Project>
11 changes: 5 additions & 6 deletions Source/RocketSoundEnhancement/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
using UnityEngine;

namespace RocketSoundEnhancement
Expand All @@ -15,14 +11,17 @@ void Awake()
AudioConfiguration audioConfig = AudioSettings.GetConfiguration();
audioConfig.numRealVoices = Settings.VoiceCount;

if(AudioSettings.Reset(audioConfig)) {
if (AudioSettings.Reset(audioConfig)) {
Debug.Log("[RSE]: Audio Settings Applied");
Debug.Log("[RSE]: DSP Buffer Size : " + AudioSettings.GetConfiguration().dspBufferSize);
Debug.Log("[RSE]: Real Voices : " + AudioSettings.GetConfiguration().numRealVoices);
Debug.Log("[RSE]: Virtual Voices : " + AudioSettings.GetConfiguration().numVirtualVoices);
Debug.Log("[RSE]: Samplerate : " + AudioSettings.GetConfiguration().sampleRate);
Debug.Log("[RSE]: Spearker Mode : " + AudioSettings.GetConfiguration().speakerMode);
}

Harmony harmony = new Harmony("RocketSoundEnhancement");
harmony.PatchAll(typeof(Startup).Assembly);
}
}
}
Loading