-
Notifications
You must be signed in to change notification settings - Fork 576
[AudioToolbox] Add missing MusicSequence.SetUserCallback method #48
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a832718
[AudioToolbox] Add missing MusicSequence.SetUserCallback method
olegoid c2da173
[AudioToolbox] MusicEventUserData internal .ctor refactoring
olegoid aa775e7
[AudioToolbox] Correct callbacks storage
olegoid 8b393b3
[AudioToolbox] Remove deprecated checks and fix code style issues
olegoid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,6 +129,20 @@ internal override IntPtr ToUnmanaged () | |
|
|
||
| public class MusicEventUserData : MidiRawData { | ||
| public MusicEventUserData () {} | ||
|
|
||
| internal MusicEventUserData (IntPtr handle) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be a good idea to check |
||
| { | ||
| if (handle == IntPtr.Zero) | ||
| throw new ArgumentNullException (nameof (handle)); | ||
|
|
||
| int length = Marshal.ReadInt32 (handle); | ||
|
|
||
| var buffer = new byte [length]; | ||
| Marshal.Copy (handle + 4, buffer, 0, length); | ||
|
|
||
| len = length; | ||
| data = buffer; | ||
| } | ||
| } | ||
|
|
||
| // | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: do we know (sample?) how much time the callback can be called ? because this allocates memory each time it's called (and let the GC deal with it) so if it's called a lot, under a short amount of time, you could run out of memory and/or and less predictable GC pauses.
In such case using
Runtime.GetINativeObjectcould help, as it would reuse an existing instance (e.g.MusicTrack) but maybe it's better to always dispose after the call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with disposing after the call is if the callback keeps the objects around.
BTW
GetINativeObjectwill always return a new object unless it's anNSObjectsubclass.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the contract is unclear, even on the native side :-( so even undisposed it's not clear if it's correct to hang on the values (and that API not refcounted afaik). The only clear thing is that we're allocating memory for some things that might never be used (by the callback) and that it will have some, unknown effect.
Instead of having a callback with several parameters, some costly and some likely not used, we could expose a different API with a single parameter/type that would allow getting the
MusicTrack/MusicEventUserDatafrom properties (or methods) and lazily allocate memory only when needed... that makes the creation (and if needed the dispose) part of the callback code.@olegoid we really need a sample for this, minimally showing the rate of calls. Im fairly sure
MusicSequenceSetUserCallbackwas not implemented originally (along with the rest of the API) because the "how it's being used" was unanswered.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spouliot @rolfbjarne please take a look at MIDIPlayer sample.
To invoke callback user of this API have to set events to
MusicTrackand provide timestamp(when this event should be triggered)1.Theoretically user can set multiple events using the same timestamp. But most likely user events will be used to update UI(for instance draw sound waves while music track is playing).
In sample mentioned above user events are triggered once per second.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look like the callback gets any big chunks of memory, so I don't think there will be any memory issues. From what I can see these native objects aren't ref counted, and they're not owned by the managed wrapper either, which means native code can free them at any time (so the conclusion is that I don't think it's much better to bundle the callback parameters in a CallbackArgs class which only creates the managed wrappers upon request).