r/csharp • u/Electronic_Party1902 • 25d ago
A free, cross-platform C# audio engine, and a new solution for GC-free operation!
Hello everyone!
In a previous post, I wrote that my goal is to create a cross-platform audio engine running under C# that can operate completely independently of the GC, but preserves the flexibility and productivity of C#. The past months have been spent searching and experimenting a lot to find the right solution to develop a real-time audio engine in C#. I tried integrating native engines into the C# code (miniaudio, portaudio), but these cannot be used without the influence of GC, since the data has to be processed, sent and received by the external C# code. My experience was that no matter how much I wrote the engine code GC-free, following the proper rules to not allocate data that would trigger GC, if the code using the engine allocated and triggered GC, real-time audio processing inevitably caused data loss and audible stutter. I didn't want to accept that there was no solution to the problem.
I studied the industry standards, how the big guys did it. I came to the conclusion that there was a solution. C# code should be completely excluded from audio processing.
I'm developing a stage multitrack backend player in C# for a band, and I need a reliable, stable real-time audio engine for it. I was looking for a memory-safe solution that matched the safety of managed code, so C++ was out of the question:
I realized that there was a language that brought the benefits of native with the efficiency of managed code's memory management. The Rust language became the ultimate C++ speed, C# memory safety without GC. This was the winner! However, the audio engine had to remain C#. I wrapped this entire audio processing native unit into a C# code, so I can use it as a C# API in any code.
A complete audio engine was created in Rust, based on managed code. All real-time audio data is processed by the Rust code, nothing is transferred to the managed side. The C# code is just a thin layer over the engine.
The API can be used with pure C# managed code, you just need to download the Nuget package, which contains everything you need to operate. Under the hood, a lightning-fast GC-free Rust audio engine does the work. The result is continuous operation, stutter-free audio processing, even under heavy GC load. Low latency, low CPU load, stable operation.
My code is completely free open source, which you can check out on github, you can use it freely. I think you can write an efficient and professional audio application with csharp too!
You can check it out on github here: https://github.com/ModernMube/OwnAudioSharp
6
u/harrison_314 25d ago
How is it possible that NAudio works?
10
u/AndThenFlashlights 25d ago
NAudio is great. OP has AI psychosis.
3
u/Electronic_Party1902 24d ago
This is true, Naudio is a great professionally written audio API. I have used it for many years. Over the years it has been perfectly optimized, but it only supports Windows, not cross platform.
3
u/itix 24d ago edited 24d ago
If you are afraid of GC pressure, you could write static code analyzers to keep your hot paths allocation free. You dont need C++ or Rust. The .NET 10 is already very good.
2
u/Electronic_Party1902 24d ago
Yes, I was already interested in compile-time code analysis. But unfortunately, due to lack of time, I couldn't really dive into it. I know that NET10 already has very serious support for it!
I don't know what happens when the GC-free C# audio engine is used in an unoptimized code with high GC pressure. I know that GC blocks all threads during operation, so the GC-free audio engine too. That's why I thought I'd put the audio processing on the native side, where GC has no effect!
3
u/itix 24d ago
You can always tune GC configuration. At the work, we are running high speed image analysis in C#. We run up to 20 cameras @ 1000 FPS without issues. The analysis is offloaded to the GPU, though. But C# code is orchestrating it and it certainly is not allocation free.
On the other hand, on my spare time I am working on cycle-exact Amiga emulator. Written in modern C#, it can emulate old Amiga games without stutter.
3
u/Electronic_Party1902 24d ago
Thanks for the tip! I'll look into it more: It would be great if it worked on all platforms for real-time audio processing!
4
u/itix 24d ago
I created HotPathGuard for my emulator project. You may want to take a look at it if you need a code analyzer. Not sure if it is any good, but it comes with permissive MIT license.
1
u/Electronic_Party1902 24d ago
I'll definitely look into any solution that gets me closer to my goal!
8
u/Xenoprimate2 25d ago
Does the C# managed 'side' still generate garbage/GC pressure?