feat: Port native Audio providers to C# #2454
feat: Port native Audio providers to C# #2454Jklawreszuk wants to merge 14 commits intostride3d:masterfrom
Conversation
There was a problem hiding this comment.
Looks like the whole file got "touched" in the latest commit (maybe the encoding change or the EOL character). Try to keep the encoding on all file to be UTF-8 and EOL to be LF when pushing (but can be CRLF when checking out on Windows). Please check whether the git config option on Windows and Linux are consistent.
There was a problem hiding this comment.
With that said, sometimes git does the strangest diff and it's out of our control (could be the case here).
|
Funnily enough, my OpenAL audio layer fixes a bug where when opening the audio a second time it crashes the entire game. |
fb4142f to
426a701
Compare
2a18ab9 to
6ec7068
Compare
|
|
||
| var data = stackalloc byte[maxBufferSizeBytes]; | ||
| buffer.Buffer.PAudioData = data; | ||
| return buffer; |
There was a problem hiding this comment.
Excuse me if I speak prematurely, but I stumble upon this and it hurts my eyes. It is never valid to stackalloc and let the pointer escape the current method. data is implicitly discarded and will be reused for method calls immediately upon return from this method. This buffer should probably be a rented buffer and perhaps also allocated from the pinned object heap, or allocated using NativeMemory, since I assume it will be used for native interop and/or hardware.
| var matrix = stackalloc float[AUDIO_CHANNELS]; | ||
| source.dsp_settings.pMatrixCoefficients = matrix; | ||
| var delay = stackalloc float[AUDIO_CHANNELS]; | ||
| source.dsp_settings.pDelayTimes = delay; |
| al.SourceQueueBuffers(source.Value, 1, &buffer.Value); | ||
| fixed (uint* bufferPtr = &buffer.Value) | ||
| { | ||
| al.SourceQueueBuffers(source.Value, 1, bufferPtr); |
There was a problem hiding this comment.
The buffer will be used asynchronously, but it won't be pinned when it is used anymore, since the fixed statement is exited. To keep the buffer pinned, allocate it pinned, or from native memory, or use GCHandle.Alloc(buffer.Value, GCHandleType.Pinned) and then keep the GCHandle around and call Free on it once the buffer no longer needs to be pinned.
There was a problem hiding this comment.
Sorry for derailing the conversation slightly, but since we have you here, @ericwj can you quickly take a look at this comment we left a while ago on something you wrote #2393 (comment)
d72cf78 to
b563530
Compare
|
I don't know if it is in the scope of this PR or should be its own issue, but can we lazy-load the audio layers and only have them if the loaded scene has an audio source and/or an audio listener? I stumbled upon an issue on WSL where I can't start a game because it crashes after failing to initialize the audio, while I don't use any audio in the game itself. edit: tracked here #2628 |
| { | ||
| throw new Exception("Failed to initialize the audio native layer."); | ||
| } | ||
| AudioLayer.Init(); |
There was a problem hiding this comment.
Maybe it's here that we could have a lazy loading. Just a thought, not to be done in that PR but in another one later for #2628.
There was a problem hiding this comment.
When I think about it it would be nice to lazy load other optional systems like VRDeviceSystem, DebugTextSystem, GameprofilingSystem etc. I'll try to concider such PR 🤔
|
I would like to start work from scratch so I am closing PR |
PR Details
The goal of this PR is to make the Stride.Audio module fully working in C#, without worring about C++ build tools.
Todo
Types of changes
Checklist