Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Port native Audio providers to C# #2454

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

Jklawreszuk
Copy link
Collaborator

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

  • Port OpenAL to Silk.NET.OpenAL - complete and tested on SimpleAudio sample
  • Port Celt/Opus to .NET
  • Port XAudio to Silk.NET.XAudio - almost complete, but not tested yet
  • Port OpenSL ES to .NET - Silk.NET does not provide an OpenSLES wrapper, and Android considers it obsolete.

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My change requires a change to the documentation.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have built and run the editor to try this change out. (Only OpenAL)

@Jklawreszuk Jklawreszuk marked this pull request as draft September 17, 2024 21:34
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

With that said, sometimes git does the strangest diff and it's out of our control (could be the case here).

@Jklawreszuk
Copy link
Collaborator Author

Funnily enough, my OpenAL audio layer fixes a bug where when opening the audio a second time it crashes the entire game.
I'll try to get back to porting soon ♥️

buffer.Buffer.PContext = bufferPtr;

var data = stackalloc byte[maxBufferSizeBytes];
buffer.Buffer.PAudioData = data;
return buffer;
Copy link
Collaborator

Choose a reason for hiding this comment

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

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.

@@ -161,6 +167,11 @@ public unsafe Source SourceCreate(Listener listener, int sampleRate, int maxNumb
var delay = stackalloc float[AUDIO_CHANNELS];
source.dsp_settings.pDelayTimes = delay;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same.

al.SourceQueueBuffers(source.Value, 1, &buffer.Value);
fixed (uint* bufferPtr = &buffer.Value)
{
al.SourceQueueBuffers(source.Value, 1, bufferPtr);
Copy link
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants