objc2_avf_audio/generated/
AVAudioEngine.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6#[cfg(feature = "objc2-audio-toolbox")]
7#[cfg(not(target_os = "watchos"))]
8use objc2_audio_toolbox::*;
9#[cfg(feature = "objc2-core-audio-types")]
10use objc2_core_audio_types::*;
11use objc2_foundation::*;
12
13use crate::*;
14
15/// Error codes that could be returned from AVAudioEngine manual rendering mode methods,
16/// e.g. `enableManualRenderingMode:format:maximumFrameCount:error:` and
17/// `renderOffline:toBuffer:error:`.
18/// Note that this is not a comprehensive list, and the underlying audio units could
19/// return other error codes (e.g. see kAudioUnitErr_* in AudioToolbox/AUComponent.h) from these
20/// methods as applicable.
21///
22/// AVAudioEngineManualRenderingErrorInvalidMode
23/// The operation cannot be performed because the engine is either not in manual
24/// rendering mode or the right variant of it.
25///
26/// AVAudioEngineManualRenderingErrorInitialized
27/// The operation cannot be performed because the engine is initialized (i.e. not stopped).
28///
29/// AVAudioEngineManualRenderingErrorNotRunning
30/// The operation cannot be performed because the engine is not running (i.e. not started).
31///
32/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioenginemanualrenderingerror?language=objc)
33// NS_ENUM
34#[repr(transparent)]
35#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
36pub struct AVAudioEngineManualRenderingError(pub OSStatus);
37impl AVAudioEngineManualRenderingError {
38    #[doc(alias = "AVAudioEngineManualRenderingErrorInvalidMode")]
39    pub const InvalidMode: Self = Self(-80800);
40    #[doc(alias = "AVAudioEngineManualRenderingErrorInitialized")]
41    pub const Initialized: Self = Self(-80801);
42    #[doc(alias = "AVAudioEngineManualRenderingErrorNotRunning")]
43    pub const NotRunning: Self = Self(-80802);
44}
45
46unsafe impl Encode for AVAudioEngineManualRenderingError {
47    const ENCODING: Encoding = OSStatus::ENCODING;
48}
49
50unsafe impl RefEncode for AVAudioEngineManualRenderingError {
51    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
52}
53
54/// Status codes returned from the render call to the engine operating in manual rendering mode.
55///
56/// AVAudioEngineManualRenderingStatusError
57/// An error occurred when rendering and no data was returned. See the returned error code
58/// for the description of the error.
59///
60/// AVAudioEngineManualRenderingStatusSuccess
61/// All of the requested data was returned successfully.
62///
63/// AVAudioEngineManualRenderingStatusInsufficientDataFromInputNode
64/// Applicable only to the input node, when it provides input data for rendering
65/// (see `AVAudioInputNode(setManualRenderingInputPCMFormat:inputBlock:)`).
66/// Indicates that not enough input data was returned by the input node to satisfy the
67/// render request at the current time. The output buffer may contain data rendered by other
68/// active sources in the engine's processing graph.
69///
70/// AVAudioEngineManualRenderingStatusCannotDoInCurrentContext
71/// The operation could not be performed now, but the client could retry later if needed.
72/// This is usually to guard a realtime render operation (e.g. rendering through
73/// `manualRenderingBlock`) when a reconfiguration of the engine's internal state
74/// is in progress.
75///
76/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioenginemanualrenderingstatus?language=objc)
77// NS_ENUM
78#[repr(transparent)]
79#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
80pub struct AVAudioEngineManualRenderingStatus(pub NSInteger);
81impl AVAudioEngineManualRenderingStatus {
82    #[doc(alias = "AVAudioEngineManualRenderingStatusError")]
83    pub const Error: Self = Self(-1);
84    #[doc(alias = "AVAudioEngineManualRenderingStatusSuccess")]
85    pub const Success: Self = Self(0);
86    #[doc(alias = "AVAudioEngineManualRenderingStatusInsufficientDataFromInputNode")]
87    pub const InsufficientDataFromInputNode: Self = Self(1);
88    #[doc(alias = "AVAudioEngineManualRenderingStatusCannotDoInCurrentContext")]
89    pub const CannotDoInCurrentContext: Self = Self(2);
90}
91
92unsafe impl Encode for AVAudioEngineManualRenderingStatus {
93    const ENCODING: Encoding = NSInteger::ENCODING;
94}
95
96unsafe impl RefEncode for AVAudioEngineManualRenderingStatus {
97    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
98}
99
100/// By default, the engine is connected to an audio device and automatically renders in realtime.
101/// It can also be configured to operate in manual rendering mode, i.e. not connected to an
102/// audio device and rendering in response to requests from the client.
103///
104/// AVAudioEngineManualRenderingModeOffline
105/// The engine operates in an offline mode without any realtime constraints.
106///
107/// AVAudioEngineManualRenderingModeRealtime
108/// The engine operates under realtime constraints, i.e. it will not make any blocking call
109/// (e.g. calling libdispatch, blocking on a mutex, allocating memory etc.) while rendering.
110/// Note that only the block based render mechanism can be used in this mode
111/// (see `AVAudioEngine(manualRenderingBlock)`.
112///
113/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioenginemanualrenderingmode?language=objc)
114// NS_ENUM
115#[repr(transparent)]
116#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
117pub struct AVAudioEngineManualRenderingMode(pub NSInteger);
118impl AVAudioEngineManualRenderingMode {
119    #[doc(alias = "AVAudioEngineManualRenderingModeOffline")]
120    pub const Offline: Self = Self(0);
121    #[doc(alias = "AVAudioEngineManualRenderingModeRealtime")]
122    pub const Realtime: Self = Self(1);
123}
124
125unsafe impl Encode for AVAudioEngineManualRenderingMode {
126    const ENCODING: Encoding = NSInteger::ENCODING;
127}
128
129unsafe impl RefEncode for AVAudioEngineManualRenderingMode {
130    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
131}
132
133/// Block to render the engine when operating in manual rendering mode
134///
135/// Parameter `numberOfFrames`: The number of PCM sample frames to be rendered
136///
137/// Parameter `outBuffer`: The PCM buffer to which the engine must render the audio.
138/// The buffer pointers (outBuffer->mBuffers[x].mData) may be null on entry, in which case
139/// the block will render into a memory it owns and modify the mData pointers to point to that
140/// memory. The block is responsible for preserving the validity of that memory until it is next
141/// called to render, or `AVAudioEngine(stop)` is called.
142///
143/// Parameter `outError`: On exit, if an error occurs during rendering, a description of the error (see
144/// `AVAudioEngineManualRenderingError` for the possible errors)
145///
146/// Returns: One of the status codes from `AVAudioEngineManualRenderingStatus`. Irrespective of the
147/// returned status code, on exit, the output buffer's mDataByteSize
148/// (outBuffer->mBuffers[x].mDataByteSize) will indicate the number of PCM data bytes rendered by
149/// the engine.
150///
151/// Use this if you want to render the engine from a realtime context when it is operating in
152/// the manual rendering mode. See `AVAudioEngine(manualRenderingBlock)` for details.
153///
154/// Note that when using AVAudioEngine manual rendering with
155/// `AVAudioEngineManualRenderingModeRealtime`, calling into the engine or related classes
156/// from a non-realtime thread (e.g. for setting or getting node properties), can cause
157/// `AVAudioEngineManualRenderingBlock` on the IO thread to return
158/// `AVAudioEngineManualRenderingStatusCannotDoInCurrentContext`.
159///
160/// This is because interacting with some of these properties requires synchronization between
161/// the realtime and calling threads.
162///
163/// In such a case, the client could implement their own synchronization between their realtime
164/// and non-realtime threads and retry calling `AVAudioEngineManualRenderingBlock`.
165///
166/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioenginemanualrenderingblock?language=objc)
167#[cfg(all(
168    feature = "AVAudioTypes",
169    feature = "block2",
170    feature = "objc2-core-audio-types"
171))]
172pub type AVAudioEngineManualRenderingBlock = *mut block2::Block<
173    dyn Fn(
174        AVAudioFrameCount,
175        NonNull<AudioBufferList>,
176        *mut OSStatus,
177    ) -> AVAudioEngineManualRenderingStatus,
178>;
179
180extern_class!(
181    /// An AVAudioEngine contains a group of connected AVAudioNodes ("nodes"), each of which performs
182    /// an audio signal generation, processing, or input/output task.
183    ///
184    /// Nodes are created separately and attached to the engine.
185    ///
186    /// The engine supports dynamic connection, disconnection and removal of nodes while running,
187    /// with only minor limitations:
188    /// - all dynamic reconnections must occur upstream of a mixer
189    /// - while removals of effects will normally result in the automatic connection of the adjacent
190    /// nodes, removal of a node which has differing input vs. output channel counts, or which
191    /// is a mixer, is likely to result in a broken graph.
192    ///
193    /// By default, the engine is connected to an audio device and automatically renders in realtime.
194    /// It can also be configured to operate in manual rendering mode, i.e. not connected to an
195    /// audio device and rendering in response to requests from the client, normally at or
196    /// faster than realtime rate.
197    ///
198    /// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioengine?language=objc)
199    #[unsafe(super(NSObject))]
200    #[derive(Debug, PartialEq, Eq, Hash)]
201    pub struct AVAudioEngine;
202);
203
204unsafe impl NSObjectProtocol for AVAudioEngine {}
205
206impl AVAudioEngine {
207    extern_methods!(
208        /// Initialize a new engine.
209        ///
210        /// On creation, the engine is by default connected to an audio device and automatically renders
211        /// in realtime. It can be configured to operate in manual rendering mode through
212        /// `enableManualRenderingMode:format:maximumFrameCount:error:`.
213        #[unsafe(method(init))]
214        #[unsafe(method_family = init)]
215        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
216
217        #[cfg(feature = "AVAudioNode")]
218        /// Take ownership of a new node.
219        ///
220        /// Parameter `node`: The node to be attached to the engine.
221        ///
222        /// To support the instantiation of arbitrary AVAudioNode subclasses, instances are created
223        /// externally to the engine, but are not usable until they are attached to the engine via
224        /// this method. Thus the idiom, without ARC, is:
225        ///
226        /// ```text
227        /// // when building engine:
228        /// AVAudioNode *_player;    // member of controller class (for example)
229        /// ...
230        /// _player = [[AVAudioPlayerNode alloc] init];
231        /// [engine attachNode: _player];
232        /// ...
233        /// // when destroying engine (without ARC)
234        /// [_player release];
235        /// ```
236        #[unsafe(method(attachNode:))]
237        #[unsafe(method_family = none)]
238        pub unsafe fn attachNode(&self, node: &AVAudioNode);
239
240        #[cfg(feature = "AVAudioNode")]
241        /// Detach a node previously attached to the engine.
242        ///
243        /// If necessary, the engine will safely disconnect the node before detaching it.
244        #[unsafe(method(detachNode:))]
245        #[unsafe(method_family = none)]
246        pub unsafe fn detachNode(&self, node: &AVAudioNode);
247
248        #[cfg(all(
249            feature = "AVAudioFormat",
250            feature = "AVAudioNode",
251            feature = "AVAudioTypes"
252        ))]
253        /// Establish a connection between two nodes.
254        ///
255        /// Parameter `node1`: The source node
256        ///
257        /// Parameter `node2`: The destination node
258        ///
259        /// Parameter `bus1`: The output bus on the source node
260        ///
261        /// Parameter `bus2`: The input bus on the destination node
262        ///
263        /// Parameter `format`: If non-nil, the format of the source node's output bus is set to this
264        /// format. In all cases, the format of the destination node's input bus is set to
265        /// match that of the source node's output bus.
266        ///
267        /// Nodes have input and output buses (AVAudioNodeBus). Use this method to establish
268        /// one-to-one connections betweeen nodes. Connections made using this method are always
269        /// one-to-one, never one-to-many or many-to-one.
270        ///
271        /// Note that any pre-existing connection(s) involving the source's output bus or the
272        /// destination's input bus will be broken.
273        #[unsafe(method(connect:to:fromBus:toBus:format:))]
274        #[unsafe(method_family = none)]
275        pub unsafe fn connect_to_fromBus_toBus_format(
276            &self,
277            node1: &AVAudioNode,
278            node2: &AVAudioNode,
279            bus1: AVAudioNodeBus,
280            bus2: AVAudioNodeBus,
281            format: Option<&AVAudioFormat>,
282        );
283
284        #[cfg(all(feature = "AVAudioFormat", feature = "AVAudioNode"))]
285        /// Establish a connection between two nodes
286        ///
287        /// This calls connect:to:fromBus:toBus:format: using bus 0 on the source node,
288        /// and bus 0 on the destination node, except in the case of a destination which is a mixer,
289        /// in which case the destination is the mixer's nextAvailableInputBus.
290        #[unsafe(method(connect:to:format:))]
291        #[unsafe(method_family = none)]
292        pub unsafe fn connect_to_format(
293            &self,
294            node1: &AVAudioNode,
295            node2: &AVAudioNode,
296            format: Option<&AVAudioFormat>,
297        );
298
299        #[cfg(all(
300            feature = "AVAudioConnectionPoint",
301            feature = "AVAudioFormat",
302            feature = "AVAudioNode",
303            feature = "AVAudioTypes"
304        ))]
305        /// Establish connections between a source node and multiple destination nodes.
306        ///
307        /// Parameter `sourceNode`: The source node
308        ///
309        /// Parameter `destNodes`: An array of AVAudioConnectionPoint objects specifying destination
310        /// nodes and busses
311        ///
312        /// Parameter `sourceBus`: The output bus on source node
313        ///
314        /// Parameter `format`: If non-nil, the format of the source node's output bus is set to this
315        /// format. In all cases, the format of the destination nodes' input bus is set to
316        /// match that of the source node's output bus
317        ///
318        /// Use this method to establish connections from a source node to multiple destination nodes.
319        /// Connections made using this method are either one-to-one (when a single destination
320        /// connection is specified) or one-to-many (when multiple connections are specified), but
321        /// never many-to-one.
322        ///
323        /// To incrementally add a new connection to a source node, use this method with an array
324        /// of AVAudioConnectionPoint objects comprising of pre-existing connections (obtained from
325        /// `outputConnectionPointsForNode:outputBus:`) and the new connection.
326        ///
327        /// Note that any pre-existing connection involving the destination's input bus will be
328        /// broken. And, any pre-existing connection on source node which is not a part of the
329        /// specified destination connection array will also be broken.
330        ///
331        /// Also note that when the output of a node is split into multiple paths, all the paths
332        /// must render at the same rate until they reach a common mixer.
333        /// In other words, starting from the split node until the common mixer node where all split
334        /// paths terminate, you cannot have:
335        /// - any AVAudioUnitTimeEffect
336        /// - any sample rate conversion
337        #[unsafe(method(connect:toConnectionPoints:fromBus:format:))]
338        #[unsafe(method_family = none)]
339        pub unsafe fn connect_toConnectionPoints_fromBus_format(
340            &self,
341            source_node: &AVAudioNode,
342            dest_nodes: &NSArray<AVAudioConnectionPoint>,
343            source_bus: AVAudioNodeBus,
344            format: Option<&AVAudioFormat>,
345        );
346
347        #[cfg(all(feature = "AVAudioNode", feature = "AVAudioTypes"))]
348        /// Remove a connection between two nodes.
349        ///
350        /// Parameter `node`: The node whose input is to be disconnected
351        ///
352        /// Parameter `bus`: The destination's input bus to disconnect
353        #[unsafe(method(disconnectNodeInput:bus:))]
354        #[unsafe(method_family = none)]
355        pub unsafe fn disconnectNodeInput_bus(&self, node: &AVAudioNode, bus: AVAudioNodeBus);
356
357        #[cfg(feature = "AVAudioNode")]
358        /// Remove a connection between two nodes.
359        ///
360        /// Parameter `node`: The node whose inputs are to be disconnected
361        ///
362        /// Connections are broken on each of the node's input busses.
363        #[unsafe(method(disconnectNodeInput:))]
364        #[unsafe(method_family = none)]
365        pub unsafe fn disconnectNodeInput(&self, node: &AVAudioNode);
366
367        #[cfg(all(feature = "AVAudioNode", feature = "AVAudioTypes"))]
368        /// Remove a connection between two nodes.
369        ///
370        /// Parameter `node`: The node whose output is to be disconnected
371        ///
372        /// Parameter `bus`: The source's output bus to disconnect
373        #[unsafe(method(disconnectNodeOutput:bus:))]
374        #[unsafe(method_family = none)]
375        pub unsafe fn disconnectNodeOutput_bus(&self, node: &AVAudioNode, bus: AVAudioNodeBus);
376
377        #[cfg(feature = "AVAudioNode")]
378        /// Remove a connection between two nodes.
379        ///
380        /// Parameter `node`: The node whose outputs are to be disconnected
381        ///
382        /// Connections are broken on each of the node's output busses.
383        #[unsafe(method(disconnectNodeOutput:))]
384        #[unsafe(method_family = none)]
385        pub unsafe fn disconnectNodeOutput(&self, node: &AVAudioNode);
386
387        /// Prepare the engine for starting.
388        ///
389        /// This method preallocates many of the resources the engine requires in order to start.
390        /// Use it to responsively start audio input or output.
391        ///
392        /// On AVAudioSession supported platforms, this method may cause the audio session to be implicitly activated. Activating the audio session (implicitly or explicitly) may cause other audio sessions to be interrupted or ducked depending on the session's configuration. It is recommended to configure and activate the app's audio session before preparing the engine.
393        /// See https://developer.apple.com/library/archive/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html for details.
394        #[unsafe(method(prepare))]
395        #[unsafe(method_family = none)]
396        pub unsafe fn prepare(&self);
397
398        /// Start the engine.
399        ///
400        /// Returns: YES for success
401        ///
402        /// Calls prepare if it has not already been called since stop.
403        ///
404        /// When the engine is rendering to/from an audio device, starts the audio hardware via the
405        /// AVAudioInputNode and/or AVAudioOutputNode instances in the engine. Audio begins to flow
406        /// through the engine.
407        /// Reasons for potential failure to start in this mode include:
408        /// 1. There is problem in the structure of the graph. Input can't be routed to output or to a
409        /// recording tap through converter type nodes.
410        /// 2. An AVAudioSession error.
411        /// 3. The driver failed to start the hardware.
412        ///
413        /// In manual rendering mode, prepares the engine to render when requested by the client.
414        ///
415        /// On AVAudioSession supported platforms, this method may cause the audio session to be implicitly activated. It is recommended to configure and activate the app's audio session before starting the engine. For more information, see the `prepare` method above.
416        #[unsafe(method(startAndReturnError:_))]
417        #[unsafe(method_family = none)]
418        pub unsafe fn startAndReturnError(&self) -> Result<(), Retained<NSError>>;
419
420        /// Pause the engine.
421        ///
422        /// When the engine is rendering to/from an audio device, stops the audio hardware and the flow
423        /// of audio through the engine. When operating in this mode, it is recommended that the engine
424        /// be paused or stopped (as applicable) when not in use, to minimize power consumption.
425        ///
426        /// Pausing the engine does not deallocate the resources allocated by prepare. Resume the
427        /// engine by invoking start again.
428        #[unsafe(method(pause))]
429        #[unsafe(method_family = none)]
430        pub unsafe fn pause(&self);
431
432        /// reset
433        /// Reset all of the nodes in the engine.
434        ///
435        /// This will reset all of the nodes in the engine. This is useful, for example, for silencing
436        /// reverb and delay tails.
437        ///
438        /// In manual rendering mode, the render timeline is reset to a sample time of zero.
439        #[unsafe(method(reset))]
440        #[unsafe(method_family = none)]
441        pub unsafe fn reset(&self);
442
443        /// When the engine is rendering to/from an audio device, stops the audio hardware and the
444        /// engine. When operating in this mode, it is recommended that the engine be paused or stopped
445        /// (as applicable) when not in use, to minimize power consumption.
446        ///
447        /// Stopping the engine releases the resources allocated by prepare.
448        #[unsafe(method(stop))]
449        #[unsafe(method_family = none)]
450        pub unsafe fn stop(&self);
451
452        #[cfg(all(
453            feature = "AVAudioConnectionPoint",
454            feature = "AVAudioNode",
455            feature = "AVAudioTypes"
456        ))]
457        /// Get connection information on a node's input bus.
458        ///
459        /// Parameter `node`: The node whose input connection is being queried.
460        ///
461        /// Parameter `bus`: The node's input bus on which the connection is being queried.
462        ///
463        /// Returns: An AVAudioConnectionPoint object with connection information on the node's
464        /// specified input bus.
465        ///
466        /// Connections are always one-to-one or one-to-many, never many-to-one.
467        ///
468        /// Returns nil if there is no connection on the node's specified input bus.
469        #[unsafe(method(inputConnectionPointForNode:inputBus:))]
470        #[unsafe(method_family = none)]
471        pub unsafe fn inputConnectionPointForNode_inputBus(
472            &self,
473            node: &AVAudioNode,
474            bus: AVAudioNodeBus,
475        ) -> Option<Retained<AVAudioConnectionPoint>>;
476
477        #[cfg(all(
478            feature = "AVAudioConnectionPoint",
479            feature = "AVAudioNode",
480            feature = "AVAudioTypes"
481        ))]
482        /// Get connection information on a node's output bus.
483        ///
484        /// Parameter `node`: The node whose output connections are being queried.
485        ///
486        /// Parameter `bus`: The node's output bus on which connections are being queried.
487        ///
488        /// Returns: An array of AVAudioConnectionPoint objects with connection information on the node's
489        /// specified output bus.
490        ///
491        /// Connections are always one-to-one or one-to-many, never many-to-one.
492        ///
493        /// Returns an empty array if there are no connections on the node's specified output bus.
494        #[unsafe(method(outputConnectionPointsForNode:outputBus:))]
495        #[unsafe(method_family = none)]
496        pub unsafe fn outputConnectionPointsForNode_outputBus(
497            &self,
498            node: &AVAudioNode,
499            bus: AVAudioNodeBus,
500        ) -> Retained<NSArray<AVAudioConnectionPoint>>;
501
502        #[cfg(feature = "objc2-audio-toolbox")]
503        #[cfg(not(target_os = "watchos"))]
504        /// The MusicSequence previously attached to the engine (if any).
505        #[unsafe(method(musicSequence))]
506        #[unsafe(method_family = none)]
507        pub unsafe fn musicSequence(&self) -> MusicSequence;
508
509        #[cfg(feature = "objc2-audio-toolbox")]
510        #[cfg(not(target_os = "watchos"))]
511        /// Setter for [`musicSequence`][Self::musicSequence].
512        #[unsafe(method(setMusicSequence:))]
513        #[unsafe(method_family = none)]
514        pub unsafe fn setMusicSequence(&self, music_sequence: MusicSequence);
515
516        #[cfg(all(feature = "AVAudioIONode", feature = "AVAudioNode"))]
517        /// The engine's singleton output node.
518        ///
519        /// Audio output is performed via an output node. The engine creates a singleton on demand when
520        /// this property is first accessed. Connect another node to the input of the output node, or
521        /// obtain a mixer that is connected there by default, using the "mainMixerNode" property.
522        ///
523        /// When the engine is rendering to/from an audio device, the AVAudioSesssion category and/or
524        /// availability of hardware determine whether an app can perform output. Check the output
525        /// format of output node (i.e. hardware format) for non-zero sample rate and channel count to
526        /// see if output is enabled.
527        /// Trying to perform output through the output node when it is not enabled or available will
528        /// cause the engine to throw an error (when possible) or an exception.
529        ///
530        /// In manual rendering mode, the output format of the output node will determine the
531        /// render format of the engine. It can be changed through
532        /// `enableManualRenderingMode:format:maximumFrameCount:error:`.
533        #[unsafe(method(outputNode))]
534        #[unsafe(method_family = none)]
535        pub unsafe fn outputNode(&self) -> Retained<AVAudioOutputNode>;
536
537        #[cfg(all(feature = "AVAudioIONode", feature = "AVAudioNode"))]
538        /// The engine's singleton input node.
539        ///
540        /// Audio input is performed via an input node. The engine creates a singleton on demand when
541        /// this property is first accessed. To receive input, connect another node from the output of
542        /// the input node, or create a recording tap on it.
543        ///
544        /// When the engine is rendering to/from an audio device, the AVAudioSesssion category and/or
545        /// availability of hardware determine whether an app can perform input. Check for the input node's
546        /// input format (i.e. hardware format) for non-zero sample rate and channel count to see if input is enabled.
547        /// Trying to perform input through the input node when it is not enabled or available will
548        /// cause the engine to throw an error (when possible) or an exception.
549        ///
550        /// Note that if the engine has at any point previously had its inputNode enabled and permission to
551        /// record was granted, then any time the engine is running, the mic-in-use indicator will appear.
552        ///
553        /// For applications which may need to dynamically switch between output-only and input-output
554        /// modes, it may be advantageous to use two engine instances.
555        ///
556        /// In manual rendering mode, the input node can be used to synchronously supply data to
557        /// the engine while it is rendering (see
558        /// `AVAudioInputNode(setManualRenderingInputPCMFormat:inputBlock:)`.
559        #[unsafe(method(inputNode))]
560        #[unsafe(method_family = none)]
561        pub unsafe fn inputNode(&self) -> Retained<AVAudioInputNode>;
562
563        #[cfg(all(feature = "AVAudioMixerNode", feature = "AVAudioNode"))]
564        /// The engine's optional singleton main mixer node.
565        ///
566        /// The engine will construct a singleton main mixer and connect it to the outputNode on demand,
567        /// when this property is first accessed. You can then connect additional nodes to the mixer.
568        ///
569        /// If the client has never explicitly set the connection format between the mainMixerNode and
570        /// the outputNode, the engine will always set/update the format to track the format of the outputNode
571        /// on (re)start, even after an AVAudioEngineConfigurationChangeNotification.
572        /// Otherwise, it's the client's responsibility to set/update this connection format after an
573        /// AVAudioEngineConfigurationChangeNotification.
574        ///
575        /// By default, the mixer's output format (sample rate and channel count) will track the format
576        /// of the output node. You may however make the connection explicitly with a different format.
577        #[unsafe(method(mainMixerNode))]
578        #[unsafe(method_family = none)]
579        pub unsafe fn mainMixerNode(&self) -> Retained<AVAudioMixerNode>;
580
581        /// The engine's running state.
582        #[unsafe(method(isRunning))]
583        #[unsafe(method_family = none)]
584        pub unsafe fn isRunning(&self) -> bool;
585
586        /// When auto shutdown is enabled, the engine can start and stop the audio hardware dynamically,
587        /// to conserve power. This is the enforced behavior on watchOS and can be optionally enabled on
588        /// other platforms.
589        ///
590        /// To conserve power, it is advised that the client pause/stop the engine when not in use.
591        /// But when auto shutdown is enabled, the engine will stop the audio hardware if it was running
592        /// idle for a certain duration, and restart it later when required.
593        /// Note that, because this operation is dynamic, it may affect the start times of the source
594        /// nodes (e.g. `AVAudioPlayerNode`), if the engine has to resume from its shutdown state.
595        ///
596        /// On watchOS, auto shutdown is always enabled. On other platforms, it is disabled by
597        /// default, but the client can enable it if needed.
598        ///
599        /// This property is applicable only when the engine is rendering to/from an audio device. If
600        /// the value is changed when the engine is in manual rendering mode, it will take effect
601        /// whenever the engine is switched to render to/from the audio device.
602        #[unsafe(method(isAutoShutdownEnabled))]
603        #[unsafe(method_family = none)]
604        pub unsafe fn isAutoShutdownEnabled(&self) -> bool;
605
606        /// Setter for [`isAutoShutdownEnabled`][Self::isAutoShutdownEnabled].
607        #[unsafe(method(setAutoShutdownEnabled:))]
608        #[unsafe(method_family = none)]
609        pub unsafe fn setAutoShutdownEnabled(&self, auto_shutdown_enabled: bool);
610
611        #[cfg(feature = "AVAudioNode")]
612        /// Set of all nodes attached to the engine.
613        #[unsafe(method(attachedNodes))]
614        #[unsafe(method_family = none)]
615        pub unsafe fn attachedNodes(&self) -> Retained<NSSet<AVAudioNode>>;
616
617        #[cfg(all(feature = "AVAudioFormat", feature = "AVAudioTypes"))]
618        /// Set the engine to operate in a manual rendering mode with the specified render format and
619        /// maximum frame count.
620        ///
621        /// Parameter `mode`: The manual rendering mode to use.
622        ///
623        /// Parameter `pcmFormat`: The format of the output PCM audio data from the engine.
624        ///
625        /// Parameter `maximumFrameCount`: The maximum number of PCM sample frames the engine will be asked to produce in any single
626        /// render call.
627        ///
628        /// Parameter `outError`: On exit, if the engine cannot switch to the manual rendering mode, a description of the
629        /// error (see `AVAudioEngineManualRenderingError` for the possible errors).
630        ///
631        /// Returns: YES for success.
632        ///
633        /// Use this method to configure the engine to render in response to requests from the client.
634        ///
635        /// The engine must be in a stopped state before calling this method.
636        /// The render format must be a PCM format and match the format of the buffer to which
637        /// the engine is asked to render (see `renderOffline:toBuffer:error:`).
638        ///
639        /// It is advised to enable manual rendering mode soon after the engine is created, and
640        /// before accessing any of mainMixerNode, inputNode or outputNode of the engine.
641        /// Otherwise, accessing or interacting with the engine before enabling manual rendering
642        /// mode could have the unintended side-effect of configuring the hardware for device-rendering
643        /// mode.
644        ///
645        /// The input data in manual rendering mode can be supplied through the source nodes, e.g.
646        /// `AVAudioPlayerNode`, `AVAudioInputNode` etc.
647        ///
648        /// When switching to manual rendering mode, the engine:
649        /// 1. Switches the input and output nodes to manual rendering mode. Their input and output
650        /// formats may change.
651        /// 2. Removes any taps previously installed on the input and output nodes.
652        /// 3. Maintains all the engine connections as is.
653        ///
654        /// Reasons for potential failure when switching to manual rendering mode include:
655        /// - Engine is not in a stopped state.
656        #[unsafe(method(enableManualRenderingMode:format:maximumFrameCount:error:_))]
657        #[unsafe(method_family = none)]
658        pub unsafe fn enableManualRenderingMode_format_maximumFrameCount_error(
659            &self,
660            mode: AVAudioEngineManualRenderingMode,
661            pcm_format: &AVAudioFormat,
662            maximum_frame_count: AVAudioFrameCount,
663        ) -> Result<(), Retained<NSError>>;
664
665        /// Set the engine to render to/from an audio device.
666        ///
667        /// When disabling the manual rendering mode, the engine:
668        /// 1. Stops and resets itself (see `stop` and `reset`).
669        /// 2. Switches the output/input nodes to render to/from an audio device. Their input and
670        /// output formats may change.
671        /// 3. Removes any taps previously installed on the input and output nodes.
672        /// 4. Maintains all the engine connections as is.
673        ///
674        /// Calling this method when the engine is already rendering to/from an audio device has no
675        /// effect.
676        #[unsafe(method(disableManualRenderingMode))]
677        #[unsafe(method_family = none)]
678        pub unsafe fn disableManualRenderingMode(&self);
679
680        #[cfg(all(
681            feature = "AVAudioTypes",
682            feature = "block2",
683            feature = "objc2-core-audio-types"
684        ))]
685        /// Block to render the engine operating in manual rendering mode
686        ///
687        /// This block based render call must be used to render the engine when operating in
688        /// `AVAudioEngineManualRenderingModeRealtime`. In this mode, the engine operates under
689        /// realtime constraints and will not make any blocking call (e.g. calling libdispatch, blocking
690        /// on a mutex, allocating memory etc.) while rendering.
691        ///
692        /// Before invoking the rendering functionality, client must fetch this block and cache the
693        /// result. The block can then be called from a realtime context, without any possibility of
694        /// blocking.
695        ///
696        /// When rendering in `AVAudioEngineManualRenderingModeOffline`, either this block based render
697        /// call or    `renderOffline:toBuffer:error:` ObjC method can be used.
698        /// All the rules outlined in `renderOffline:toBuffer:error:` are applicable here as well.
699        #[unsafe(method(manualRenderingBlock))]
700        #[unsafe(method_family = none)]
701        pub unsafe fn manualRenderingBlock(&self) -> AVAudioEngineManualRenderingBlock;
702
703        /// Whether or not the engine is operating in manual rendering mode, i.e. not connected
704        /// to an audio device and rendering in response to the requests from the client
705        #[unsafe(method(isInManualRenderingMode))]
706        #[unsafe(method_family = none)]
707        pub unsafe fn isInManualRenderingMode(&self) -> bool;
708
709        /// The manual rendering mode configured on the engine
710        ///
711        /// This property is meaningful only when the engine is operating in manual rendering mode,
712        /// i.e. when `isInManualRenderingMode` returns true.
713        #[unsafe(method(manualRenderingMode))]
714        #[unsafe(method_family = none)]
715        pub unsafe fn manualRenderingMode(&self) -> AVAudioEngineManualRenderingMode;
716
717        #[cfg(feature = "AVAudioFormat")]
718        /// The render format of the engine in manual rendering mode.
719        ///
720        /// Querying this property when the engine is not in manual rendering mode will return an
721        /// invalid format, with zero sample rate and channel count.
722        #[unsafe(method(manualRenderingFormat))]
723        #[unsafe(method_family = none)]
724        pub unsafe fn manualRenderingFormat(&self) -> Retained<AVAudioFormat>;
725
726        #[cfg(feature = "AVAudioTypes")]
727        /// The maximum number of PCM sample frames the engine can produce in any single render call in
728        /// the manual rendering mode.
729        ///
730        /// Querying this property when the engine is not in manual rendering mode will return zero.
731        #[unsafe(method(manualRenderingMaximumFrameCount))]
732        #[unsafe(method_family = none)]
733        pub unsafe fn manualRenderingMaximumFrameCount(&self) -> AVAudioFrameCount;
734
735        #[cfg(feature = "AVAudioTypes")]
736        /// Indicates where the engine is on its render timeline in manual rendering mode.
737        ///
738        /// The timeline in manual rendering mode starts at a sample time of zero, and is in terms
739        /// of the render format's sample rate. Resetting the engine (see `reset`) will reset the
740        /// timeline back to zero.
741        #[unsafe(method(manualRenderingSampleTime))]
742        #[unsafe(method_family = none)]
743        pub unsafe fn manualRenderingSampleTime(&self) -> AVAudioFramePosition;
744
745        #[cfg(all(
746            feature = "AVAudioFormat",
747            feature = "AVAudioNode",
748            feature = "objc2-audio-toolbox"
749        ))]
750        #[cfg(not(target_os = "watchos"))]
751        /// Establish a MIDI only connection between two nodes.
752        ///
753        /// Parameter `sourceNode`: The source node.
754        ///
755        /// Parameter `destinationNode`: The destination node.
756        ///
757        /// Parameter `format`: If non-nil, the format of the source node's output bus is set to this format.
758        /// In all cases, the format of the source nodes' output bus has to match with the
759        /// destination nodes' output bus format.
760        /// Although the output bus of the source is not in use, the format needs to be set
761        /// in order to be able to use the sample rate for MIDI event timing calculations.
762        ///
763        /// Parameter `tapBlock`: If non-nil, this block is called from the source node's `AUMIDIOutputEventBlock`
764        /// on the realtime thread. The host can tap the MIDI data of the source node through
765        /// this block. May be nil.
766        ///
767        /// Use this method to establish a MIDI only connection between a source node and a
768        /// destination node that has MIDI input capability.
769        ///
770        /// The source node can only be a AVAudioUnit node of type `kAudioUnitType_MIDIProcessor`.
771        /// The destination node types can be `kAudioUnitType_MusicDevice`,
772        /// `kAudioUnitType_MusicEffect` or `kAudioUnitType_MIDIProcessor`.
773        ///
774        /// Note that any pre-existing MIDI connection involving the destination will be broken.
775        ///
776        /// Any client installed block on the source node's audio unit `AUMIDIOutputEventBlock`
777        /// will be overwritten when making the MIDI connection.
778        #[deprecated]
779        #[unsafe(method(connectMIDI:to:format:block:))]
780        #[unsafe(method_family = none)]
781        pub unsafe fn connectMIDI_to_format_block(
782            &self,
783            source_node: &AVAudioNode,
784            destination_node: &AVAudioNode,
785            format: Option<&AVAudioFormat>,
786            tap_block: AUMIDIOutputEventBlock,
787        );
788
789        #[cfg(all(
790            feature = "AVAudioFormat",
791            feature = "AVAudioNode",
792            feature = "objc2-audio-toolbox"
793        ))]
794        #[cfg(not(target_os = "watchos"))]
795        /// Establish a MIDI only connection between a source node and multiple destination nodes.
796        ///
797        /// Parameter `sourceNode`: The source node.
798        ///
799        /// Parameter `destinationNodes`: An array of AVAudioNodes specifying destination nodes.
800        ///
801        /// Parameter `format`: If non-nil, the format of the source node's output bus is set to this format.
802        /// In all cases, the format of the source nodes' output bus has to match with the
803        /// destination nodes' output bus format.
804        /// Although the output bus of the source is not in use, the format needs to be set
805        /// in order to be able to use the sample rate for MIDI event timing calculations.
806        ///
807        /// Parameter `tapBlock`: If non-nil, this block is called from the source node's `AUMIDIOutputEventBlock`
808        /// on the realtime thread. The host can tap the MIDI data of the source node through
809        /// this block. May be nil.
810        ///
811        /// Use this method to establish a MIDI only connection between a source node and
812        /// multiple destination nodes.
813        ///
814        /// The source node can only be a AVAudioUnit node of type `kAudioUnitType_MIDIProcessor`.
815        /// The destination node types can be `kAudioUnitType_MusicDevice`,
816        /// `kAudioUnitType_MusicEffect` or `kAudioUnitType_MIDIProcessor`.
817        ///
818        /// MIDI connections made using this method are either one-to-one (when a single
819        /// destination connection is specified) or one-to-many (when multiple connections are
820        /// specified), but never many-to-one.
821        ///
822        /// Note that any pre-existing connection involving the destination will be broken.
823        ///
824        /// Any client installed block on the source node's audio unit `AUMIDIOutputEventBlock`
825        /// will be overwritten when making the MIDI connection.
826        #[deprecated]
827        #[unsafe(method(connectMIDI:toNodes:format:block:))]
828        #[unsafe(method_family = none)]
829        pub unsafe fn connectMIDI_toNodes_format_block(
830            &self,
831            source_node: &AVAudioNode,
832            destination_nodes: &NSArray<AVAudioNode>,
833            format: Option<&AVAudioFormat>,
834            tap_block: AUMIDIOutputEventBlock,
835        );
836
837        #[cfg(feature = "AVAudioNode")]
838        /// Remove a MIDI connection between two nodes.
839        ///
840        /// Parameter `sourceNode`: The node whose MIDI output is to be disconnected.
841        ///
842        /// Parameter `destinationNode`: The node whose MIDI input is to be disconnected.
843        ///
844        /// If a tap block is installed on the source node, it will be removed when the last
845        /// connection from the source node is removed.
846        #[unsafe(method(disconnectMIDI:from:))]
847        #[unsafe(method_family = none)]
848        pub unsafe fn disconnectMIDI_from(
849            &self,
850            source_node: &AVAudioNode,
851            destination_node: &AVAudioNode,
852        );
853
854        #[cfg(feature = "AVAudioNode")]
855        /// Remove a MIDI connection between one source node and multiple destination nodes.
856        ///
857        /// Parameter `sourceNode`: The node whose MIDI output is to be disconnected.
858        ///
859        /// Parameter `destinationNodes`: An array of AVAudioNodes specifying nodes whose MIDI input is to be disconnected.
860        ///
861        /// If a tap block is installed on the source node, it will be removed when the last
862        /// connection from the source node is removed.
863        #[unsafe(method(disconnectMIDI:fromNodes:))]
864        #[unsafe(method_family = none)]
865        pub unsafe fn disconnectMIDI_fromNodes(
866            &self,
867            source_node: &AVAudioNode,
868            destination_nodes: &NSArray<AVAudioNode>,
869        );
870
871        #[cfg(feature = "AVAudioNode")]
872        /// Disconnects all input MIDI connections of this node.
873        ///
874        /// Parameter `node`: The node whose MIDI input is to be disconnected.
875        #[unsafe(method(disconnectMIDIInput:))]
876        #[unsafe(method_family = none)]
877        pub unsafe fn disconnectMIDIInput(&self, node: &AVAudioNode);
878
879        #[cfg(feature = "AVAudioNode")]
880        /// Disconnects all output MIDI connections of this node.
881        ///
882        /// Parameter `node`: The node whose MIDI outputs are to be disconnected.
883        #[unsafe(method(disconnectMIDIOutput:))]
884        #[unsafe(method_family = none)]
885        pub unsafe fn disconnectMIDIOutput(&self, node: &AVAudioNode);
886    );
887}
888
889/// Methods declared on superclass `NSObject`.
890impl AVAudioEngine {
891    extern_methods!(
892        #[unsafe(method(new))]
893        #[unsafe(method_family = new)]
894        pub unsafe fn new() -> Retained<Self>;
895    );
896}
897
898extern "C" {
899    /// A notification generated on engine configuration changes when rendering to/from an audio
900    /// device.
901    ///
902    /// Register for this notification on your engine instances, as follows:
903    ///
904    /// ```text
905    /// [[NSNotificationCenter defaultCenter] addObserver: myObject
906    /// selector:
907    /// sel!(handleInterruption:)
908    /// name:        AVAudioEngineConfigurationChangeNotification
909    /// object:      engine];
910    /// ```
911    ///
912    /// When the engine's I/O unit observes a change to the audio input or output hardware's
913    /// channel count or sample rate, the engine stops itself (see `AVAudioEngine(stop)`), and
914    /// issues this notification.
915    /// The nodes remain attached and connected with previously set formats. However, the app
916    /// must reestablish connections if the connection formats need to change (e.g. in an
917    /// input node chain, connections must follow the hardware sample rate, while in an output only
918    /// chain, the output node supports rate conversion).
919    ///
920    /// Note that the engine must not be deallocated from within the client's notification handler
921    /// because the callback happens on an internal dispatch queue and can deadlock while trying to
922    /// synchronously teardown the engine.
923    ///
924    /// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioengineconfigurationchangenotification?language=objc)
925    pub static AVAudioEngineConfigurationChangeNotification: &'static NSString;
926}