-
Notifications
You must be signed in to change notification settings - Fork 45
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
Use objc2
crates
#128
base: master
Are you sure you want to change the base?
Use objc2
crates
#128
Conversation
pub mod sys { | ||
#[cfg(feature = "audio_toolbox")] | ||
pub use objc2_audio_toolbox::*; | ||
#[cfg(feature = "core_audio")] | ||
pub use objc2_core_audio::*; | ||
#[cfg(feature = "core_audio")] | ||
pub use objc2_core_audio_types::*; | ||
#[cfg(feature = "core_midi")] | ||
pub use objc2_core_midi::*; | ||
|
||
// MacTypes.h | ||
pub type OSStatus = i32; | ||
} |
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.
I'm not a big fan of this module, ideally users should probably import the crates themselves, but it's useful to not break too much at once.
let mut audio_device_id: AudioDeviceID = 0; | ||
let data_size = mem::size_of::<AudioDeviceID>() as u32; | ||
let status = unsafe { | ||
AudioObjectGetPropertyData( | ||
kAudioObjectSystemObject, | ||
&property_address as *const _, | ||
kAudioObjectSystemObject as AudioObjectID, | ||
NonNull::from(&property_address), | ||
0, | ||
null(), | ||
&data_size as *const _ as *mut _, | ||
&audio_device_id as *const _ as *mut _, | ||
NonNull::from(&data_size), | ||
NonNull::from(&mut audio_device_id).cast(), | ||
) | ||
}; |
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.
I made sure to use &mut
on the out pointer audio_device_id
here (and in similar cases below), this was UB before (AudioObjectGetPropertyData
was writing through an immutable reference).
e8b03fa
to
8e5379e
Compare
objc2-core-foundation = { version = "0.3", optional = true, default-features = false, features = [ | ||
"std", | ||
"CFBase", | ||
"CFString", | ||
] } | ||
objc2-audio-toolbox = { version = "0.3", optional = true, default-features = false, features = [ | ||
"std", | ||
"bitflags", | ||
"libc", | ||
"objc2-core-foundation", | ||
"AUAudioUnit", | ||
"AUAudioUnitImplementation", | ||
"AUCocoaUIView", | ||
"AUComponent", | ||
"AUGraph", | ||
"AUParameters", | ||
"AudioCodec", | ||
"AudioComponent", | ||
"AudioConverter", | ||
"AudioFile", | ||
"AudioFileStream", | ||
"AudioFormat", | ||
"AudioOutputUnit", | ||
"AudioQueue", | ||
"AudioServices", | ||
"AudioSession", | ||
"AudioUnit", | ||
"AudioUnitCarbonView", | ||
"AudioUnitParameters", | ||
"AudioUnitProperties", | ||
"AudioUnitUtilities", | ||
"AudioWorkInterval", | ||
"CAFFile", | ||
"CAShow", | ||
"DefaultAudioOutput", | ||
"ExtendedAudioFile", | ||
"MusicDevice", | ||
"MusicPlayer", | ||
"objc2-core-audio", | ||
"objc2-core-audio-types", | ||
] } | ||
objc2-core-audio = { version = "0.3", optional = true, default-features = false, features = [ | ||
"std", | ||
"objc2-core-audio-types", | ||
"AudioHardware", | ||
"AudioHardwareDeprecated", | ||
"AudioServerPlugIn", | ||
"HostTime", | ||
] } | ||
objc2-core-audio-types = { version = "0.3", optional = true, default-features = false, features = [ | ||
"std", | ||
"bitflags", | ||
"AudioSessionTypes", | ||
"CoreAudioBaseTypes", | ||
] } | ||
objc2-core-midi = { version = "0.3", optional = true, default-features = false, features = [ | ||
"std", | ||
"objc2-core-foundation", | ||
"MIDIBluetoothConnection", | ||
"MIDICIDevice", | ||
"MIDICIDeviceManager", | ||
"MIDICapabilityInquiry", | ||
"MIDIDriver", | ||
"MIDIMessages", | ||
"MIDINetworkSession", | ||
"MIDIServices", | ||
"MIDISetup", | ||
"MIDIThruConnection", | ||
"MIDIUMPCI", | ||
"MIDIUMPCIProfile", | ||
"MIDIUMPEndpoint", | ||
"MIDIUMPEndpointManager", | ||
"MIDIUMPFunctionBlock", | ||
"MIDIUMPMutableEndpoint", | ||
"MIDIUMPMutableFunctionBlock", | ||
] } |
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 is unclear what features we want to enable here, so for now, I went with roughly "all features except block2
and objc2
".
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.
I need to do testing but I think this looks pretty good.
@mitchmindtree Would you be up to review this? You've got the most commit history on coreaudio-rs and coreaudio-sys so you might know this better than me. If you're unfamiliar with the objc2 crates, it's some work similar to bindgen but with higher level bindings. Mads has been working on the objective-c for a few years and slowly updating various crates. I find the bindings very nice. |
Nice work @madsmtm! Agree @simlay it definitely looks a lot nicer at a glance.
My only hesitation is the sheer number of libraries, audio tools, game engines (including bevy), etc that use coreaudio-rs and coreaudio-sys now, though I guess most do transitively via cpal. If we can test this with cpal and confirm that it's working nicely (e.g. all examples still work at least) and that we're not breaking anything major, and that we're not including any excessive number of extra deps, I think I'd be happy to see this happen 👍 |
Thanks for the feedback! Re deps,
After RustAudio/cpal#943.
Summing up, it's basically I suspect even the |
Use the
objc2-*
crates instead ofcoreaudio-sys
(you knew it was coming @simlay 😉):objc2-audio-toolbox
.objc2-core-audio
.objc2-core-audio-types
.objc2-core-midi
.Resolves every single
coreaudio-sys
issue1 and PR by no longer invokingbindgen
, and instead generating the headers ahead of time. We should probably deprecatecoreaudio-sys
and archive the repo once this lands.The bindings generated by
objc2
's toolheader-translator
are also a bit more type-safe as they include for example null-ability information, and is able to merge some enum constants into constants on the enum type.Tested with
sine
example on:Note: We've experienced problems with linking AudioUnix in the past, see RustAudio/coreaudio-sys#51, but that should be irrelevant since we now just always link AudioToolbox. This is fine, since AudioUnit has been an empty re-export of parts of AudioToolbox since macOS 10.10 (and
rustc
's minimum supported macOS version is 10.12) 2.Note: I am unsure of your MSRV, but this will at least bump it to 1.71 (required for
extern "C-unwind"
).Footnotes
Okay, maybe Docs failed to build for 0.2.5. coreaudio-sys#40 isn't quite resolved, since
objc2-core-audio
also failed building docs, but that's a minor issue. ↩If you really want to, we could add
#[link(name = "AudioUnit", kind = "framework")] extern "C" {}
to still support older macOS versions. ↩