objc2_avf_audio/generated/
AVAudioPlayerNode.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
7use crate::*;
8
9/// Options controlling buffer scheduling.
10///
11///
12/// The buffer loops indefinitely.
13///
14/// The buffer interrupts any buffer already playing.
15///
16/// The buffer interrupts any buffer already playing, at its loop point.
17///
18/// API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0))
19///
20/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioplayernodebufferoptions?language=objc)
21// NS_OPTIONS
22#[repr(transparent)]
23#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
24pub struct AVAudioPlayerNodeBufferOptions(pub NSUInteger);
25bitflags::bitflags! {
26    impl AVAudioPlayerNodeBufferOptions: NSUInteger {
27        #[doc(alias = "AVAudioPlayerNodeBufferLoops")]
28        const Loops = 1<<0;
29        #[doc(alias = "AVAudioPlayerNodeBufferInterrupts")]
30        const Interrupts = 1<<1;
31        #[doc(alias = "AVAudioPlayerNodeBufferInterruptsAtLoop")]
32        const InterruptsAtLoop = 1<<2;
33    }
34}
35
36unsafe impl Encode for AVAudioPlayerNodeBufferOptions {
37    const ENCODING: Encoding = NSUInteger::ENCODING;
38}
39
40unsafe impl RefEncode for AVAudioPlayerNodeBufferOptions {
41    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
42}
43
44/// Specifies when the completion handler must be invoked.
45///
46///
47/// The buffer or file data has been consumed by the player.
48///
49/// The buffer or file data has been rendered (i.e. output) by the player. This
50/// does not account for any signal processing latencies downstream of the player
51/// in the engine (see `AVAudioNode(outputPresentationLatency)`).
52///
53/// Applicable only when the engine is rendering to/from an audio device.
54/// The buffer or file has finished playing. This accounts for both (small) signal
55/// processing latencies downstream of the player in the engine, as well as
56/// (possibly significant) latency in the audio playback device.
57///
58/// API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
59///
60/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioplayernodecompletioncallbacktype?language=objc)
61// NS_ENUM
62#[repr(transparent)]
63#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
64pub struct AVAudioPlayerNodeCompletionCallbackType(pub NSInteger);
65impl AVAudioPlayerNodeCompletionCallbackType {
66    #[doc(alias = "AVAudioPlayerNodeCompletionDataConsumed")]
67    pub const DataConsumed: Self = Self(0);
68    #[doc(alias = "AVAudioPlayerNodeCompletionDataRendered")]
69    pub const DataRendered: Self = Self(1);
70    #[doc(alias = "AVAudioPlayerNodeCompletionDataPlayedBack")]
71    pub const DataPlayedBack: Self = Self(2);
72}
73
74unsafe impl Encode for AVAudioPlayerNodeCompletionCallbackType {
75    const ENCODING: Encoding = NSInteger::ENCODING;
76}
77
78unsafe impl RefEncode for AVAudioPlayerNodeCompletionCallbackType {
79    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
80}
81
82/// Buffer or file completion callback handler.
83///
84/// Parameter `callbackType`: Indicates the type of buffer or file completion when the callback is invoked.
85///
86/// AVAudioPlayerNode issues this callback to inform the client about the specific type of
87/// buffer or file completion. See `AVAudioPlayerNodeCompletionCallbackType` for more details.
88///
89/// Note that the `AVAudioNodeCompletionHandler` callback from some of the player's scheduling
90/// methods (e.g. `scheduleBuffer:completionHandler:`) is equivalent to the
91/// AVAudioPlayerNodeCompletionHandler callback for `AVAudioPlayerNodeCompletionDataConsumed`.
92///
93/// In general the callbacks arrive on a non-main thread and it is the client's responsibility
94/// to handle them in a thread-safe manner.
95///
96/// Setting or getting properties on an AVAudioPlayerNode while the AVAudioEngine is running requires
97/// some synchronisation between the calling threads internally. If you want to call player node API within this
98/// completion handler block, calls should be synchronised to the same thread/queue.
99///
100/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioplayernodecompletionhandler?language=objc)
101#[cfg(feature = "block2")]
102pub type AVAudioPlayerNodeCompletionHandler =
103    *mut block2::DynBlock<dyn Fn(AVAudioPlayerNodeCompletionCallbackType)>;
104
105extern_class!(
106    /// Play buffers or segments of audio files.
107    ///
108    /// AVAudioPlayerNode supports scheduling the playback of `AVAudioBuffer` instances,
109    /// or segments of audio files opened via `AVAudioFile`. Buffers and segments may be
110    /// scheduled at specific points in time, or to play immediately following preceding segments.
111    ///
112    /// FORMATS
113    ///
114    /// Normally, you will want to configure the node's output format with the same number of
115    /// channels as are in the files and buffers to be played. Otherwise, channels will be dropped
116    /// or added as required. It is usually better to use an `AVAudioMixerNode` to
117    /// do this.
118    ///
119    /// Similarly, when playing file segments, the node will sample rate convert if necessary, but
120    /// it is often preferable to configure the node's output sample rate to match that of the file(s)
121    /// and use a mixer to perform the rate conversion.
122    ///
123    /// When playing buffers, there is an implicit assumption that the buffers are at the same
124    /// sample rate as the node's output format.
125    ///
126    /// TIMELINES
127    ///
128    /// The usual `AVAudioNode` sample times (as observed by `lastRenderTime`)
129    /// have an arbitrary zero point. AVAudioPlayerNode superimposes a second "player timeline" on
130    /// top of this, to reflect when the player was started, and intervals during which it was
131    /// paused. The methods `nodeTimeForPlayerTime:` and `playerTimeForNodeTime:`
132    /// convert between the two.
133    ///
134    /// This class' `stop` method unschedules all previously scheduled buffers and
135    /// file segments, and returns the player timeline to sample time 0.
136    ///
137    /// TIMESTAMPS
138    ///
139    /// The "schedule" methods all take an `AVAudioTime` "when" parameter. This is
140    /// interpreted as follows:
141    ///
142    /// 1. nil:
143    /// - if there have been previous commands, the new one is played immediately following the
144    /// last one.
145    /// - otherwise, if the node is playing, the event is played in the very near future.
146    /// - otherwise, the command is played at sample time 0.
147    /// 2. sample time:
148    /// - relative to the node's start time (which begins at 0 when the node is started).
149    /// 3. host time:
150    /// - ignored unless the sample time is invalid when the engine is rendering to an audio
151    /// device.
152    /// - ignored in manual rendering mode.
153    ///
154    /// ERRORS
155    ///
156    /// The "schedule" methods can fail if:
157    ///
158    /// 1. a buffer's channel count does not match that of the node's output format.
159    /// 2. a file can't be accessed.
160    /// 3. an AVAudioTime specifies neither a valid sample time or host time.
161    /// 4. a segment's start frame or frame count is negative.
162    ///
163    /// BUFFER/FILE COMPLETION HANDLERS
164    ///
165    /// The buffer or file completion handlers (see scheduling methods) are a means to schedule
166    /// more data if available on the player node. See `AVAudioPlayerNodeCompletionCallbackType`
167    /// for details on the different buffer/file completion callback types.
168    ///
169    /// Note that a player should not be stopped from within a completion handler callback because
170    /// it can deadlock while trying to unschedule previously scheduled buffers.
171    ///
172    /// OFFLINE RENDERING
173    ///
174    /// When a player node is used with the engine operating in the manual rendering mode, the
175    /// buffer/file completion handlers, `lastRenderTime` and the latencies (`latency` and
176    /// `outputPresentationLatency`) can be used to track how much data the player has rendered and
177    /// how much more data is left to render.
178    ///
179    /// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioplayernode?language=objc)
180    #[unsafe(super(AVAudioNode, NSObject))]
181    #[derive(Debug, PartialEq, Eq, Hash)]
182    #[cfg(feature = "AVAudioNode")]
183    pub struct AVAudioPlayerNode;
184);
185
186#[cfg(all(feature = "AVAudioMixing", feature = "AVAudioNode"))]
187extern_conformance!(
188    unsafe impl AVAudio3DMixing for AVAudioPlayerNode {}
189);
190
191#[cfg(all(feature = "AVAudioMixing", feature = "AVAudioNode"))]
192extern_conformance!(
193    unsafe impl AVAudioMixing for AVAudioPlayerNode {}
194);
195
196#[cfg(all(feature = "AVAudioMixing", feature = "AVAudioNode"))]
197extern_conformance!(
198    unsafe impl AVAudioStereoMixing for AVAudioPlayerNode {}
199);
200
201#[cfg(feature = "AVAudioNode")]
202extern_conformance!(
203    unsafe impl NSObjectProtocol for AVAudioPlayerNode {}
204);
205
206#[cfg(feature = "AVAudioNode")]
207impl AVAudioPlayerNode {
208    extern_methods!(
209        #[unsafe(method(init))]
210        #[unsafe(method_family = init)]
211        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
212
213        #[cfg(all(
214            feature = "AVAudioBuffer",
215            feature = "AVAudioTypes",
216            feature = "block2"
217        ))]
218        /// Schedule playing samples from an AVAudioBuffer.
219        ///
220        /// Parameter `buffer`: the buffer to play
221        ///
222        /// Parameter `completionHandler`: called after the buffer has been consumed by the player or the player is stopped. may be nil.
223        ///
224        /// Schedules the buffer to be played following any previously scheduled commands.
225        ///
226        /// It is possible for the completionHandler to be called before rendering begins
227        /// or before the buffer is played completely.
228        ///
229        /// # Safety
230        ///
231        /// `completion_handler` must be a valid pointer or null.
232        #[unsafe(method(scheduleBuffer:completionHandler:))]
233        #[unsafe(method_family = none)]
234        pub unsafe fn scheduleBuffer_completionHandler(
235            &self,
236            buffer: &AVAudioPCMBuffer,
237            completion_handler: AVAudioNodeCompletionHandler,
238        );
239
240        #[cfg(all(feature = "AVAudioBuffer", feature = "block2"))]
241        /// Schedule playing samples from an AVAudioBuffer.
242        ///
243        /// Parameter `buffer`: the buffer to play
244        ///
245        /// Parameter `callbackType`: option to specify when the completion handler must be called
246        ///
247        /// Parameter `completionHandler`: called after the buffer has been consumed by the player or has finished playing back or
248        /// the player is stopped. may be nil.
249        ///
250        /// Schedules the buffer to be played following any previously scheduled commands.
251        ///
252        /// # Safety
253        ///
254        /// `completion_handler` must be a valid pointer or null.
255        #[unsafe(method(scheduleBuffer:completionCallbackType:completionHandler:))]
256        #[unsafe(method_family = none)]
257        pub unsafe fn scheduleBuffer_completionCallbackType_completionHandler(
258            &self,
259            buffer: &AVAudioPCMBuffer,
260            callback_type: AVAudioPlayerNodeCompletionCallbackType,
261            completion_handler: AVAudioPlayerNodeCompletionHandler,
262        );
263
264        #[cfg(all(
265            feature = "AVAudioBuffer",
266            feature = "AVAudioTime",
267            feature = "AVAudioTypes",
268            feature = "block2"
269        ))]
270        /// Schedule playing samples from an AVAudioBuffer.
271        ///
272        /// Parameter `buffer`: the buffer to play
273        ///
274        /// Parameter `when`: the time at which to play the buffer. see the discussion of timestamps, above.
275        ///
276        /// Parameter `options`: options for looping, interrupting other buffers, etc.
277        ///
278        /// Parameter `completionHandler`: called after the buffer has been consumed by the player or the player is stopped. may be nil.
279        ///
280        /// It is possible for the completionHandler to be called before rendering begins
281        /// or before the buffer is played completely.
282        ///
283        /// # Safety
284        ///
285        /// `completion_handler` must be a valid pointer or null.
286        #[unsafe(method(scheduleBuffer:atTime:options:completionHandler:))]
287        #[unsafe(method_family = none)]
288        pub unsafe fn scheduleBuffer_atTime_options_completionHandler(
289            &self,
290            buffer: &AVAudioPCMBuffer,
291            when: Option<&AVAudioTime>,
292            options: AVAudioPlayerNodeBufferOptions,
293            completion_handler: AVAudioNodeCompletionHandler,
294        );
295
296        #[cfg(all(feature = "AVAudioBuffer", feature = "AVAudioTime", feature = "block2"))]
297        /// Schedule playing samples from an AVAudioBuffer.
298        ///
299        /// Parameter `buffer`: the buffer to play
300        ///
301        /// Parameter `when`: the time at which to play the buffer. see the discussion of timestamps, above.
302        ///
303        /// Parameter `options`: options for looping, interrupting other buffers, etc.
304        ///
305        /// Parameter `callbackType`: option to specify when the completion handler must be called
306        ///
307        /// Parameter `completionHandler`: called after the buffer has been consumed by the player or has finished playing back or
308        /// the player is stopped. may be nil.
309        ///
310        /// # Safety
311        ///
312        /// `completion_handler` must be a valid pointer or null.
313        #[unsafe(method(scheduleBuffer:atTime:options:completionCallbackType:completionHandler:))]
314        #[unsafe(method_family = none)]
315        pub unsafe fn scheduleBuffer_atTime_options_completionCallbackType_completionHandler(
316            &self,
317            buffer: &AVAudioPCMBuffer,
318            when: Option<&AVAudioTime>,
319            options: AVAudioPlayerNodeBufferOptions,
320            callback_type: AVAudioPlayerNodeCompletionCallbackType,
321            completion_handler: AVAudioPlayerNodeCompletionHandler,
322        );
323
324        #[cfg(all(
325            feature = "AVAudioFile",
326            feature = "AVAudioTime",
327            feature = "AVAudioTypes",
328            feature = "block2"
329        ))]
330        /// Schedule playing of an entire audio file.
331        ///
332        /// Parameter `file`: the file to play
333        ///
334        /// Parameter `when`: the time at which to play the file. see the discussion of timestamps, above.
335        ///
336        /// Parameter `completionHandler`: called after the file has been consumed by the player or the player is stopped. may be nil.
337        ///
338        /// It is possible for the completionHandler to be called before rendering begins
339        /// or before the file is played completely.
340        ///
341        /// # Safety
342        ///
343        /// `completion_handler` must be a valid pointer or null.
344        #[unsafe(method(scheduleFile:atTime:completionHandler:))]
345        #[unsafe(method_family = none)]
346        pub unsafe fn scheduleFile_atTime_completionHandler(
347            &self,
348            file: &AVAudioFile,
349            when: Option<&AVAudioTime>,
350            completion_handler: AVAudioNodeCompletionHandler,
351        );
352
353        #[cfg(all(feature = "AVAudioFile", feature = "AVAudioTime", feature = "block2"))]
354        /// Schedule playing of an entire audio file.
355        ///
356        /// Parameter `file`: the file to play
357        ///
358        /// Parameter `when`: the time at which to play the file. see the discussion of timestamps, above.
359        ///
360        /// Parameter `callbackType`: option to specify when the completion handler must be called
361        ///
362        /// Parameter `completionHandler`: called after the file has been consumed by the player or has finished playing back or
363        /// the player is stopped. may be nil.
364        ///
365        /// # Safety
366        ///
367        /// `completion_handler` must be a valid pointer or null.
368        #[unsafe(method(scheduleFile:atTime:completionCallbackType:completionHandler:))]
369        #[unsafe(method_family = none)]
370        pub unsafe fn scheduleFile_atTime_completionCallbackType_completionHandler(
371            &self,
372            file: &AVAudioFile,
373            when: Option<&AVAudioTime>,
374            callback_type: AVAudioPlayerNodeCompletionCallbackType,
375            completion_handler: AVAudioPlayerNodeCompletionHandler,
376        );
377
378        #[cfg(all(
379            feature = "AVAudioFile",
380            feature = "AVAudioTime",
381            feature = "AVAudioTypes",
382            feature = "block2"
383        ))]
384        /// Schedule playing a segment of an audio file.
385        ///
386        /// Parameter `file`: the file to play
387        ///
388        /// Parameter `startFrame`: the starting frame position in the stream
389        ///
390        /// Parameter `numberFrames`: the number of frames to play
391        ///
392        /// Parameter `when`: the time at which to play the region. see the discussion of timestamps, above.
393        ///
394        /// Parameter `completionHandler`: called after the segment has been consumed by the player or the player is stopped. may be nil.
395        ///
396        /// It is possible for the completionHandler to be called before rendering begins
397        /// or before the segment is played completely.
398        ///
399        /// # Safety
400        ///
401        /// `completion_handler` must be a valid pointer or null.
402        #[unsafe(method(scheduleSegment:startingFrame:frameCount:atTime:completionHandler:))]
403        #[unsafe(method_family = none)]
404        pub unsafe fn scheduleSegment_startingFrame_frameCount_atTime_completionHandler(
405            &self,
406            file: &AVAudioFile,
407            start_frame: AVAudioFramePosition,
408            number_frames: AVAudioFrameCount,
409            when: Option<&AVAudioTime>,
410            completion_handler: AVAudioNodeCompletionHandler,
411        );
412
413        #[cfg(all(
414            feature = "AVAudioFile",
415            feature = "AVAudioTime",
416            feature = "AVAudioTypes",
417            feature = "block2"
418        ))]
419        /// Schedule playing a segment of an audio file.
420        ///
421        /// Parameter `file`: the file to play
422        ///
423        /// Parameter `startFrame`: the starting frame position in the stream
424        ///
425        /// Parameter `numberFrames`: the number of frames to play
426        ///
427        /// Parameter `when`: the time at which to play the region. see the discussion of timestamps, above.
428        ///
429        /// Parameter `callbackType`: option to specify when the completion handler must be called
430        ///
431        /// Parameter `completionHandler`: called after the segment has been consumed by the player or has finished playing back or
432        /// the player is stopped. may be nil.
433        ///
434        /// # Safety
435        ///
436        /// `completion_handler` must be a valid pointer or null.
437        #[unsafe(method(scheduleSegment:startingFrame:frameCount:atTime:completionCallbackType:completionHandler:))]
438        #[unsafe(method_family = none)]
439        pub unsafe fn scheduleSegment_startingFrame_frameCount_atTime_completionCallbackType_completionHandler(
440            &self,
441            file: &AVAudioFile,
442            start_frame: AVAudioFramePosition,
443            number_frames: AVAudioFrameCount,
444            when: Option<&AVAudioTime>,
445            callback_type: AVAudioPlayerNodeCompletionCallbackType,
446            completion_handler: AVAudioPlayerNodeCompletionHandler,
447        );
448
449        /// Clear all of the node's previously scheduled events and stop playback.
450        ///
451        /// All of the node's previously scheduled events are cleared, including any that are in the
452        /// middle of playing. The node's sample time (and therefore the times to which new events are
453        /// to be scheduled) is reset to 0, and will not proceed until the node is started again (via
454        /// play or playAtTime).
455        ///
456        /// Note that pausing or stopping all the players connected to an engine does not pause or stop
457        /// the engine or the underlying hardware. The engine must be explicitly paused or stopped for
458        /// the hardware to stop.
459        #[unsafe(method(stop))]
460        #[unsafe(method_family = none)]
461        pub unsafe fn stop(&self);
462
463        #[cfg(feature = "AVAudioTypes")]
464        /// Prepares previously scheduled file regions or buffers for playback.
465        ///
466        /// Parameter `frameCount`: The number of sample frames of data to be prepared before returning.
467        #[unsafe(method(prepareWithFrameCount:))]
468        #[unsafe(method_family = none)]
469        pub unsafe fn prepareWithFrameCount(&self, frame_count: AVAudioFrameCount);
470
471        /// Start or resume playback immediately.
472        ///
473        /// equivalent to playAtTime:nil
474        #[unsafe(method(play))]
475        #[unsafe(method_family = none)]
476        pub unsafe fn play(&self);
477
478        #[cfg(feature = "AVAudioTime")]
479        /// Start or resume playback at a specific time.
480        ///
481        /// Parameter `when`: the node time at which to start or resume playback. nil signifies "now".
482        ///
483        /// This node is initially paused. Requests to play buffers or file segments are enqueued, and
484        /// any necessary decoding begins immediately. Playback does not begin, however, until the player
485        /// has started playing, via this method.
486        ///
487        /// Note that providing an AVAudioTime which is past (before lastRenderTime) will cause the
488        /// player to begin playback immediately.
489        ///
490        /// E.g. To start a player X seconds in future:
491        /// <pre>
492        /// // start engine and player
493        /// NSError *nsErr = nil;
494        /// [_engine startAndReturnError:
495        /// &nsErr
496        /// ];
497        /// if (!nsErr) {
498        /// const float kStartDelayTime = 0.5; // sec
499        /// AVAudioFormat *outputFormat = [_player outputFormatForBus:0];
500        /// AVAudioFramePosition startSampleTime = _player.lastRenderTime.sampleTime + kStartDelayTime * outputFormat.sampleRate;
501        /// AVAudioTime *startTime = [AVAudioTime timeWithSampleTime:startSampleTime atRate:outputFormat.sampleRate];
502        /// [_player playAtTime:startTime];
503        /// }
504        /// </pre>
505        #[unsafe(method(playAtTime:))]
506        #[unsafe(method_family = none)]
507        pub unsafe fn playAtTime(&self, when: Option<&AVAudioTime>);
508
509        /// Pause playback.
510        ///
511        /// The player's sample time does not advance while the node is paused.
512        ///
513        /// Note that pausing or stopping all the players connected to an engine does not pause or stop
514        /// the engine or the underlying hardware. The engine must be explicitly paused or stopped for
515        /// the hardware to stop.
516        #[unsafe(method(pause))]
517        #[unsafe(method_family = none)]
518        pub unsafe fn pause(&self);
519
520        #[cfg(feature = "AVAudioTime")]
521        /// Convert from player time to node time.
522        ///
523        /// Parameter `playerTime`: a time relative to the player's start time
524        ///
525        /// Returns: a node time
526        ///
527        /// This method and its inverse `playerTimeForNodeTime:` are discussed in the
528        /// introduction to this class.
529        ///
530        /// If the player is not playing when this method is called, nil is returned.
531        #[unsafe(method(nodeTimeForPlayerTime:))]
532        #[unsafe(method_family = none)]
533        pub unsafe fn nodeTimeForPlayerTime(
534            &self,
535            player_time: &AVAudioTime,
536        ) -> Option<Retained<AVAudioTime>>;
537
538        #[cfg(feature = "AVAudioTime")]
539        /// Convert from node time to player time.
540        ///
541        /// Parameter `nodeTime`: a node time
542        ///
543        /// Returns: a time relative to the player's start time
544        ///
545        /// This method and its inverse `nodeTimeForPlayerTime:` are discussed in the
546        /// introduction to this class.
547        ///
548        /// If the player is not playing when this method is called, nil is returned.
549        #[unsafe(method(playerTimeForNodeTime:))]
550        #[unsafe(method_family = none)]
551        pub unsafe fn playerTimeForNodeTime(
552            &self,
553            node_time: &AVAudioTime,
554        ) -> Option<Retained<AVAudioTime>>;
555
556        /// Indicates whether or not the player is playing.
557        #[unsafe(method(isPlaying))]
558        #[unsafe(method_family = none)]
559        pub unsafe fn isPlaying(&self) -> bool;
560    );
561}
562
563/// Methods declared on superclass `NSObject`.
564#[cfg(feature = "AVAudioNode")]
565impl AVAudioPlayerNode {
566    extern_methods!(
567        #[unsafe(method(new))]
568        #[unsafe(method_family = new)]
569        pub unsafe fn new() -> Retained<Self>;
570    );
571}