-
-
Notifications
You must be signed in to change notification settings - Fork 43
Description
Requirements
- I have searched the existing issues to ensure this feature has not already been requested.
- I have clearly described the problem and the proposed solution.
- I have considered alternative solutions.
1. Is your feature request related to a problem? Please describe.
When the SoundFlow.dll (compiled for .NET 8.0) is imported into a Unity project, the following two critical assembly resolution errors occur, preventing the library from loading:
Assemby 'Assets/Plugins/UniAudioKit/Runtime/Plugins/lib/net8.0/SoundFlow.dll' will not be loaded due to errors:
Unable to resolve reference 'System.Runtime.Intrinsics'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspectlor.
Unable to resolve reference 'System.Text.Json'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
These errors are caused by direct dependencies on two APIs that are not fully compatible with Unity's .NET runtime environment:
System.Runtime.Intrinsics: Used extensively in SoundFlow.Utils.MathHelper for hardware-accelerated (SIMD) calculations for FFT and windowing functions.
System.Text.Json: Used for JSON serialization tasks. While modern Unity versions have improved support, it is not universally available or is often less preferred than established solutions within the ecosystem.
2. Describe the Solution You'd Like
The proposed solution is to refactor the library to abstract these specific dependencies behind interfaces, allowing different implementations to be injected based on the host platform.
- Abstract System.Runtime.Intrinsics:
-
Action: Create an IMathOperations interface for methods that currently use SSE/AVX (e.g., Fft, HammingWindow).
-
Default Implementation: A NetCoreIntrinsicsProvider class will contain the existing, high-performance SSE/AVX code.
-
Unity Implementation: A UnityBurstProvider class will implement the same interface using Unity's Burst compiler and Unity.Burst.Intrinsics for equivalent, high-performance SIMD operations. A fallback to the existing scalar logic can be used for non-Burst scenarios.
- Abstract System.Text.Json:
-
Action: Create an IJsonSerializer interface with Serialize(T obj) and Deserialize(string json) methods.
-
Default Implementation: A SystemTextJsonProvider class will use System.Text.Json to handle serialization for standard .NET environments.
-
Unity Implementation: A UnityJsonProvider class will implement the interface using a Unity-compatible solution, such as UnityEngine.JsonUtility (for simple cases) or a third-party library like Newtonsoft.Json (which is standard in many Unity projects).
This dual-interface approach resolves all compatibility errors while making the entire library more modular, maintainable, and extensible.
3. Describe Alternatives You've Considered
The only alternative is a manual fork and rewrite of the library's internals, which is unsustainable. It would prevent users from receiving updates and would fragment the codebase. The proposed interface-based solution is the clean way to support multiple platforms.
4. Proposed API (if applicable)
The core idea is to use dependency injection to provide the correct implementation at runtime.
Global Configuration:
// In a central configuration class for SoundFlow
public static class SoundFlowConfig
{
// Set the provider at app startup. Defaults to .NET 8 versions.
public static IMathOperations MathProvider { get; set; } = new NetCoreIntrinsicsProvider();
public static IJsonSerializer JsonSerializer { get; set; } = new SystemTextJsonProvider();
}Example JSON Interface:
// The new abstraction layer for serialization
public interface IJsonSerializer
{
string Serialize<T>(T value);
T Deserialize<T>(string json);
}
// Default implementation
public class SystemTextJsonProvider : IJsonSerializer
{
public string Serialize<T>(T value) => System.Text.Json.JsonSerializer.Serialize(value);
public T Deserialize<T>(string json) => System.Text.Json.JsonSerializer.Deserialize<T>(json);
}
// Unity-specific implementation
public class UnityJsonProvider : IJsonSerializer
{
public string Serialize<T>(T value) => UnityEngine.JsonUtility.ToJson(value);
public T Deserialize<T>(string json) => UnityEngine.JsonUtility.FromJson<T>(json);
}This API design is clean, flexible, and completely decouples the core library logic from the platform-specific implementations.
5. Benefits
-
Full Unity Compatibility: Directly solves the blocking issues, making SoundFlow available to a vast new user base.
-
Enhanced Extensibility: Developers are no longer locked into System.Text.Json. They could, for instance, create their own NewtonsoftJsonProvider if they prefer its features, even in a standard .NET project.
-
Future-Proof Architecture: This modular design makes it trivial to add support for other platforms (e.g., MAUI, Godot) in the future by simply adding a new implementation of the interfaces.
-
Performance without Compromise: The use of Burst for math operations ensures that Unity developers still get the best possible performance.
6. Potential Drawbacks/Challenges
-
Initial Refactoring Effort: There is an upfront cost to refactoring MathHelper and any classes that use System.Text.Json.
-
Feature Parity: If the library uses advanced features of System.Text.Json (e.g., custom converters, reference handling), care must be taken to ensure the chosen Unity-compatible serializer (JsonUtility or Newtonsoft.Json) can support them.
-
Build/Distribution Strategy: A clear strategy for packaging the core library and the platform-specific provider packages will be needed (e.g., via separate NuGet packages or Unity packages).
7. Additional Context
The provided code for MathHelper is a clear example of where the System.Runtime.Intrinsics abstraction is needed. A similar pattern of direct dependency likely exists for System.Text.Json and would benefit equally from this proposed refactoring. Applying this strategy to both dependencies is the key to creating a truly portable and more robust SoundFlow library.
Metadata
Metadata
Assignees
Labels
Projects
Status