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::Block<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"))]
187unsafe impl AVAudio3DMixing for AVAudioPlayerNode {}
188
189#[cfg(all(feature = "AVAudioMixing", feature = "AVAudioNode"))]
190unsafe impl AVAudioMixing for AVAudioPlayerNode {}
191
192#[cfg(all(feature = "AVAudioMixing", feature = "AVAudioNode"))]
193unsafe impl AVAudioStereoMixing for AVAudioPlayerNode {}
194
195#[cfg(feature = "AVAudioNode")]
196unsafe impl NSObjectProtocol for AVAudioPlayerNode {}
197
198#[cfg(feature = "AVAudioNode")]
199impl AVAudioPlayerNode {
200 extern_methods!(
201 #[unsafe(method(init))]
202 #[unsafe(method_family = init)]
203 pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
204
205 #[cfg(all(
206 feature = "AVAudioBuffer",
207 feature = "AVAudioTypes",
208 feature = "block2"
209 ))]
210 /// Schedule playing samples from an AVAudioBuffer.
211 ///
212 /// Parameter `buffer`: the buffer to play
213 ///
214 /// Parameter `completionHandler`: called after the buffer has been consumed by the player or the player is stopped. may be nil.
215 ///
216 /// Schedules the buffer to be played following any previously scheduled commands.
217 ///
218 /// It is possible for the completionHandler to be called before rendering begins
219 /// or before the buffer is played completely.
220 #[unsafe(method(scheduleBuffer:completionHandler:))]
221 #[unsafe(method_family = none)]
222 pub unsafe fn scheduleBuffer_completionHandler(
223 &self,
224 buffer: &AVAudioPCMBuffer,
225 completion_handler: AVAudioNodeCompletionHandler,
226 );
227
228 #[cfg(all(feature = "AVAudioBuffer", feature = "block2"))]
229 /// Schedule playing samples from an AVAudioBuffer.
230 ///
231 /// Parameter `buffer`: the buffer to play
232 ///
233 /// Parameter `callbackType`: option to specify when the completion handler must be called
234 ///
235 /// Parameter `completionHandler`: called after the buffer has been consumed by the player or has finished playing back or
236 /// the player is stopped. may be nil.
237 ///
238 /// Schedules the buffer to be played following any previously scheduled commands.
239 #[unsafe(method(scheduleBuffer:completionCallbackType:completionHandler:))]
240 #[unsafe(method_family = none)]
241 pub unsafe fn scheduleBuffer_completionCallbackType_completionHandler(
242 &self,
243 buffer: &AVAudioPCMBuffer,
244 callback_type: AVAudioPlayerNodeCompletionCallbackType,
245 completion_handler: AVAudioPlayerNodeCompletionHandler,
246 );
247
248 #[cfg(all(
249 feature = "AVAudioBuffer",
250 feature = "AVAudioTime",
251 feature = "AVAudioTypes",
252 feature = "block2"
253 ))]
254 /// Schedule playing samples from an AVAudioBuffer.
255 ///
256 /// Parameter `buffer`: the buffer to play
257 ///
258 /// Parameter `when`: the time at which to play the buffer. see the discussion of timestamps, above.
259 ///
260 /// Parameter `options`: options for looping, interrupting other buffers, etc.
261 ///
262 /// Parameter `completionHandler`: called after the buffer has been consumed by the player or the player is stopped. may be nil.
263 ///
264 /// It is possible for the completionHandler to be called before rendering begins
265 /// or before the buffer is played completely.
266 #[unsafe(method(scheduleBuffer:atTime:options:completionHandler:))]
267 #[unsafe(method_family = none)]
268 pub unsafe fn scheduleBuffer_atTime_options_completionHandler(
269 &self,
270 buffer: &AVAudioPCMBuffer,
271 when: Option<&AVAudioTime>,
272 options: AVAudioPlayerNodeBufferOptions,
273 completion_handler: AVAudioNodeCompletionHandler,
274 );
275
276 #[cfg(all(feature = "AVAudioBuffer", feature = "AVAudioTime", feature = "block2"))]
277 /// Schedule playing samples from an AVAudioBuffer.
278 ///
279 /// Parameter `buffer`: the buffer to play
280 ///
281 /// Parameter `when`: the time at which to play the buffer. see the discussion of timestamps, above.
282 ///
283 /// Parameter `options`: options for looping, interrupting other buffers, etc.
284 ///
285 /// Parameter `callbackType`: option to specify when the completion handler must be called
286 ///
287 /// Parameter `completionHandler`: called after the buffer has been consumed by the player or has finished playing back or
288 /// the player is stopped. may be nil.
289 #[unsafe(method(scheduleBuffer:atTime:options:completionCallbackType:completionHandler:))]
290 #[unsafe(method_family = none)]
291 pub unsafe fn scheduleBuffer_atTime_options_completionCallbackType_completionHandler(
292 &self,
293 buffer: &AVAudioPCMBuffer,
294 when: Option<&AVAudioTime>,
295 options: AVAudioPlayerNodeBufferOptions,
296 callback_type: AVAudioPlayerNodeCompletionCallbackType,
297 completion_handler: AVAudioPlayerNodeCompletionHandler,
298 );
299
300 #[cfg(all(
301 feature = "AVAudioFile",
302 feature = "AVAudioTime",
303 feature = "AVAudioTypes",
304 feature = "block2"
305 ))]
306 /// Schedule playing of an entire audio file.
307 ///
308 /// Parameter `file`: the file to play
309 ///
310 /// Parameter `when`: the time at which to play the file. see the discussion of timestamps, above.
311 ///
312 /// Parameter `completionHandler`: called after the file has been consumed by the player or the player is stopped. may be nil.
313 ///
314 /// It is possible for the completionHandler to be called before rendering begins
315 /// or before the file is played completely.
316 #[unsafe(method(scheduleFile:atTime:completionHandler:))]
317 #[unsafe(method_family = none)]
318 pub unsafe fn scheduleFile_atTime_completionHandler(
319 &self,
320 file: &AVAudioFile,
321 when: Option<&AVAudioTime>,
322 completion_handler: AVAudioNodeCompletionHandler,
323 );
324
325 #[cfg(all(feature = "AVAudioFile", feature = "AVAudioTime", feature = "block2"))]
326 /// Schedule playing of an entire audio file.
327 ///
328 /// Parameter `file`: the file to play
329 ///
330 /// Parameter `when`: the time at which to play the file. see the discussion of timestamps, above.
331 ///
332 /// Parameter `callbackType`: option to specify when the completion handler must be called
333 ///
334 /// Parameter `completionHandler`: called after the file has been consumed by the player or has finished playing back or
335 /// the player is stopped. may be nil.
336 #[unsafe(method(scheduleFile:atTime:completionCallbackType:completionHandler:))]
337 #[unsafe(method_family = none)]
338 pub unsafe fn scheduleFile_atTime_completionCallbackType_completionHandler(
339 &self,
340 file: &AVAudioFile,
341 when: Option<&AVAudioTime>,
342 callback_type: AVAudioPlayerNodeCompletionCallbackType,
343 completion_handler: AVAudioPlayerNodeCompletionHandler,
344 );
345
346 #[cfg(all(
347 feature = "AVAudioFile",
348 feature = "AVAudioTime",
349 feature = "AVAudioTypes",
350 feature = "block2"
351 ))]
352 /// Schedule playing a segment of an audio file.
353 ///
354 /// Parameter `file`: the file to play
355 ///
356 /// Parameter `startFrame`: the starting frame position in the stream
357 ///
358 /// Parameter `numberFrames`: the number of frames to play
359 ///
360 /// Parameter `when`: the time at which to play the region. see the discussion of timestamps, above.
361 ///
362 /// Parameter `completionHandler`: called after the segment has been consumed by the player or the player is stopped. may be nil.
363 ///
364 /// It is possible for the completionHandler to be called before rendering begins
365 /// or before the segment is played completely.
366 #[unsafe(method(scheduleSegment:startingFrame:frameCount:atTime:completionHandler:))]
367 #[unsafe(method_family = none)]
368 pub unsafe fn scheduleSegment_startingFrame_frameCount_atTime_completionHandler(
369 &self,
370 file: &AVAudioFile,
371 start_frame: AVAudioFramePosition,
372 number_frames: AVAudioFrameCount,
373 when: Option<&AVAudioTime>,
374 completion_handler: AVAudioNodeCompletionHandler,
375 );
376
377 #[cfg(all(
378 feature = "AVAudioFile",
379 feature = "AVAudioTime",
380 feature = "AVAudioTypes",
381 feature = "block2"
382 ))]
383 /// Schedule playing a segment of an audio file.
384 ///
385 /// Parameter `file`: the file to play
386 ///
387 /// Parameter `startFrame`: the starting frame position in the stream
388 ///
389 /// Parameter `numberFrames`: the number of frames to play
390 ///
391 /// Parameter `when`: the time at which to play the region. see the discussion of timestamps, above.
392 ///
393 /// Parameter `callbackType`: option to specify when the completion handler must be called
394 ///
395 /// Parameter `completionHandler`: called after the segment has been consumed by the player or has finished playing back or
396 /// the player is stopped. may be nil.
397 #[unsafe(method(scheduleSegment:startingFrame:frameCount:atTime:completionCallbackType:completionHandler:))]
398 #[unsafe(method_family = none)]
399 pub unsafe fn scheduleSegment_startingFrame_frameCount_atTime_completionCallbackType_completionHandler(
400 &self,
401 file: &AVAudioFile,
402 start_frame: AVAudioFramePosition,
403 number_frames: AVAudioFrameCount,
404 when: Option<&AVAudioTime>,
405 callback_type: AVAudioPlayerNodeCompletionCallbackType,
406 completion_handler: AVAudioPlayerNodeCompletionHandler,
407 );
408
409 /// Clear all of the node's previously scheduled events and stop playback.
410 ///
411 /// All of the node's previously scheduled events are cleared, including any that are in the
412 /// middle of playing. The node's sample time (and therefore the times to which new events are
413 /// to be scheduled) is reset to 0, and will not proceed until the node is started again (via
414 /// play or playAtTime).
415 ///
416 /// Note that pausing or stopping all the players connected to an engine does not pause or stop
417 /// the engine or the underlying hardware. The engine must be explicitly paused or stopped for
418 /// the hardware to stop.
419 #[unsafe(method(stop))]
420 #[unsafe(method_family = none)]
421 pub unsafe fn stop(&self);
422
423 #[cfg(feature = "AVAudioTypes")]
424 /// Prepares previously scheduled file regions or buffers for playback.
425 ///
426 /// Parameter `frameCount`: The number of sample frames of data to be prepared before returning.
427 #[unsafe(method(prepareWithFrameCount:))]
428 #[unsafe(method_family = none)]
429 pub unsafe fn prepareWithFrameCount(&self, frame_count: AVAudioFrameCount);
430
431 /// Start or resume playback immediately.
432 ///
433 /// equivalent to playAtTime:nil
434 #[unsafe(method(play))]
435 #[unsafe(method_family = none)]
436 pub unsafe fn play(&self);
437
438 #[cfg(feature = "AVAudioTime")]
439 /// Start or resume playback at a specific time.
440 ///
441 /// Parameter `when`: the node time at which to start or resume playback. nil signifies "now".
442 ///
443 /// This node is initially paused. Requests to play buffers or file segments are enqueued, and
444 /// any necessary decoding begins immediately. Playback does not begin, however, until the player
445 /// has started playing, via this method.
446 ///
447 /// Note that providing an AVAudioTime which is past (before lastRenderTime) will cause the
448 /// player to begin playback immediately.
449 ///
450 /// E.g. To start a player X seconds in future:
451 /// <pre>
452 /// // start engine and player
453 /// NSError *nsErr = nil;
454 /// [_engine startAndReturnError:
455 /// &nsErr
456 /// ];
457 /// if (!nsErr) {
458 /// const float kStartDelayTime = 0.5; // sec
459 /// AVAudioFormat *outputFormat = [_player outputFormatForBus:0];
460 /// AVAudioFramePosition startSampleTime = _player.lastRenderTime.sampleTime + kStartDelayTime * outputFormat.sampleRate;
461 /// AVAudioTime *startTime = [AVAudioTime timeWithSampleTime:startSampleTime atRate:outputFormat.sampleRate];
462 /// [_player playAtTime:startTime];
463 /// }
464 /// </pre>
465 #[unsafe(method(playAtTime:))]
466 #[unsafe(method_family = none)]
467 pub unsafe fn playAtTime(&self, when: Option<&AVAudioTime>);
468
469 /// Pause playback.
470 ///
471 /// The player's sample time does not advance while the node is paused.
472 ///
473 /// Note that pausing or stopping all the players connected to an engine does not pause or stop
474 /// the engine or the underlying hardware. The engine must be explicitly paused or stopped for
475 /// the hardware to stop.
476 #[unsafe(method(pause))]
477 #[unsafe(method_family = none)]
478 pub unsafe fn pause(&self);
479
480 #[cfg(feature = "AVAudioTime")]
481 /// Convert from player time to node time.
482 ///
483 /// Parameter `playerTime`: a time relative to the player's start time
484 ///
485 /// Returns: a node time
486 ///
487 /// This method and its inverse `playerTimeForNodeTime:` are discussed in the
488 /// introduction to this class.
489 ///
490 /// If the player is not playing when this method is called, nil is returned.
491 #[unsafe(method(nodeTimeForPlayerTime:))]
492 #[unsafe(method_family = none)]
493 pub unsafe fn nodeTimeForPlayerTime(
494 &self,
495 player_time: &AVAudioTime,
496 ) -> Option<Retained<AVAudioTime>>;
497
498 #[cfg(feature = "AVAudioTime")]
499 /// Convert from node time to player time.
500 ///
501 /// Parameter `nodeTime`: a node time
502 ///
503 /// Returns: a time relative to the player's start time
504 ///
505 /// This method and its inverse `nodeTimeForPlayerTime:` are discussed in the
506 /// introduction to this class.
507 ///
508 /// If the player is not playing when this method is called, nil is returned.
509 #[unsafe(method(playerTimeForNodeTime:))]
510 #[unsafe(method_family = none)]
511 pub unsafe fn playerTimeForNodeTime(
512 &self,
513 node_time: &AVAudioTime,
514 ) -> Option<Retained<AVAudioTime>>;
515
516 /// Indicates whether or not the player is playing.
517 #[unsafe(method(isPlaying))]
518 #[unsafe(method_family = none)]
519 pub unsafe fn isPlaying(&self) -> bool;
520 );
521}
522
523/// Methods declared on superclass `NSObject`.
524#[cfg(feature = "AVAudioNode")]
525impl AVAudioPlayerNode {
526 extern_methods!(
527 #[unsafe(method(new))]
528 #[unsafe(method_family = new)]
529 pub unsafe fn new() -> Retained<Self>;
530 );
531}