objc2_avf_audio/generated/
AVAudioConverter.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::*;
6use objc2_foundation::*;
7
8use crate::*;
9
10/// values for the primeMethod property. See further discussion under AVAudioConverterPrimeInfo.
11///
12/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioconverterprimemethod?language=objc)
13// NS_ENUM
14#[repr(transparent)]
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
16pub struct AVAudioConverterPrimeMethod(pub NSInteger);
17impl AVAudioConverterPrimeMethod {
18    /// Primes with leading + trailing input frames.
19    #[doc(alias = "AVAudioConverterPrimeMethod_Pre")]
20    pub const Pre: Self = Self(0);
21    /// Only primes with trailing (zero latency). Leading frames are assumed to be silence.
22    #[doc(alias = "AVAudioConverterPrimeMethod_Normal")]
23    pub const Normal: Self = Self(1);
24    /// Acts in "latency" mode. Both leading and trailing frames assumed to be silence.
25    #[doc(alias = "AVAudioConverterPrimeMethod_None")]
26    pub const None: Self = Self(2);
27}
28
29unsafe impl Encode for AVAudioConverterPrimeMethod {
30    const ENCODING: Encoding = NSInteger::ENCODING;
31}
32
33unsafe impl RefEncode for AVAudioConverterPrimeMethod {
34    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
35}
36
37/// This struct is the value of the primeInfo property and specifies priming information.
38///
39/// When using convertToBuffer:error:withInputFromBlock: (either a single call or a series of calls), some
40/// conversions, particularly involving sample-rate conversion, ideally require a certain
41/// number of input frames previous to the normal start input frame and beyond the end of
42/// the last expected input frame in order to yield high-quality results.
43///
44/// These are expressed in the leadingFrames and trailingFrames members of the structure.
45///
46/// The very first call to convertToBuffer:error:withInputFromBlock:, or first call after
47/// reset, will request additional input frames beyond those normally
48/// expected in the input proc callback to fulfill this first AudioConverterFillComplexBuffer()
49/// request. The number of additional frames requested, depending on the prime method, will
50/// be approximately:
51///
52/// Prime method                        | Additional frames
53/// ------------------------------------|----------------------
54/// AVAudioConverterPrimeMethod_Pre     | leadingFrames + trailingFrames
55/// AVAudioConverterPrimeMethod_Normal  | trailingFrames
56/// AVAudioConverterPrimeMethod_None    | 0
57///
58/// Thus, in effect, the first input proc callback(s) may provide not only the leading
59/// frames, but also may "read ahead" by an additional number of trailing frames depending
60/// on the prime method.
61///
62/// AVAudioConverterPrimeMethod_None is useful in a real-time application processing live input,
63/// in which case trailingFrames (relative to input sample rate) of through latency will be
64/// seen at the beginning of the output of the AudioConverter.  In other real-time
65/// applications such as DAW systems, it may be possible to provide these initial extra
66/// audio frames since they are stored on disk or in memory somewhere and
67/// AVAudioConverterPrimeMethod_Pre may be preferable.  The default method is
68/// AVAudioConverterPrimeMethod_Normal, which requires no pre-seeking of the input stream and
69/// generates no latency at the output.
70///
71/// Field: leadingFrames
72/// Specifies the number of leading (previous) input frames, relative to the normal/desired
73/// start input frame, required by the converter to perform a high quality conversion. If
74/// using AVAudioConverterPrimeMethod_Pre, the client should "pre-seek" the input stream provided
75/// through the input proc by leadingFrames. If no frames are available previous to the
76/// desired input start frame (because, for example, the desired start frame is at the very
77/// beginning of available audio), then provide "leadingFrames" worth of initial zero frames
78/// in the input proc.  Do not "pre-seek" in the default case of
79/// AVAudioConverterPrimeMethod_Normal or when using AVAudioConverterPrimeMethod_None.
80///
81/// Field: trailingFrames
82/// Specifies the number of trailing input frames (past the normal/expected end input frame)
83/// required by the converter to perform a high quality conversion.  The client should be
84/// prepared to provide this number of additional input frames except when using
85/// AVAudioConverterPrimeMethod_None. If no more frames of input are available in the input stream
86/// (because, for example, the desired end frame is at the end of an audio file), then zero
87/// (silent) trailing frames will be synthesized for the client.
88///
89/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioconverterprimeinfo?language=objc)
90#[cfg(feature = "AVAudioTypes")]
91#[repr(C)]
92#[derive(Clone, Copy, Debug, PartialEq)]
93pub struct AVAudioConverterPrimeInfo {
94    pub leadingFrames: AVAudioFrameCount,
95    pub trailingFrames: AVAudioFrameCount,
96}
97
98#[cfg(feature = "AVAudioTypes")]
99unsafe impl Encode for AVAudioConverterPrimeInfo {
100    const ENCODING: Encoding = Encoding::Struct(
101        "AVAudioConverterPrimeInfo",
102        &[<AVAudioFrameCount>::ENCODING, <AVAudioFrameCount>::ENCODING],
103    );
104}
105
106#[cfg(feature = "AVAudioTypes")]
107unsafe impl RefEncode for AVAudioConverterPrimeInfo {
108    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
109}
110
111/// You must return one of these codes from your AVAudioConverterInputBlock.
112///
113/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioconverterinputstatus?language=objc)
114// NS_ENUM
115#[repr(transparent)]
116#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
117pub struct AVAudioConverterInputStatus(pub NSInteger);
118impl AVAudioConverterInputStatus {
119    /// This is the normal case where you supply data to the converter.
120    #[doc(alias = "AVAudioConverterInputStatus_HaveData")]
121    pub const HaveData: Self = Self(0);
122    /// If you are out of data for now, set *ioNumberOfPackets = 0 and return
123    /// AVAudioConverterInputStatus_NoDataNow; it is possible that some of the supplied input data
124    /// may not be converted to output immediately, but instead may be converted to output only
125    /// if/when more input is provided or the end-of-stream is indicated with the
126    /// AVAudioConverterInputStatus_EndOfStream status code.
127    #[doc(alias = "AVAudioConverterInputStatus_NoDataNow")]
128    pub const NoDataNow: Self = Self(1);
129    /// If you are at the end of stream, set *ioNumberOfPackets = 0 and return
130    /// AVAudioConverterInputStatus_EndOfStream.
131    #[doc(alias = "AVAudioConverterInputStatus_EndOfStream")]
132    pub const EndOfStream: Self = Self(2);
133}
134
135unsafe impl Encode for AVAudioConverterInputStatus {
136    const ENCODING: Encoding = NSInteger::ENCODING;
137}
138
139unsafe impl RefEncode for AVAudioConverterInputStatus {
140    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
141}
142
143/// These values are returned from convertToBuffer:error:withInputFromBlock:
144///
145/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioconverteroutputstatus?language=objc)
146// NS_ENUM
147#[repr(transparent)]
148#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
149pub struct AVAudioConverterOutputStatus(pub NSInteger);
150impl AVAudioConverterOutputStatus {
151    /// All of the requested data was returned.
152    #[doc(alias = "AVAudioConverterOutputStatus_HaveData")]
153    pub const HaveData: Self = Self(0);
154    /// Not enough input was available to satisfy the request at the current time. The output buffer
155    /// contains as much as could be converted.
156    #[doc(alias = "AVAudioConverterOutputStatus_InputRanDry")]
157    pub const InputRanDry: Self = Self(1);
158    /// The end of stream has been reached. No data was returned.
159    #[doc(alias = "AVAudioConverterOutputStatus_EndOfStream")]
160    pub const EndOfStream: Self = Self(2);
161    /// An error occurred.
162    #[doc(alias = "AVAudioConverterOutputStatus_Error")]
163    pub const Error: Self = Self(3);
164}
165
166unsafe impl Encode for AVAudioConverterOutputStatus {
167    const ENCODING: Encoding = NSInteger::ENCODING;
168}
169
170unsafe impl RefEncode for AVAudioConverterOutputStatus {
171    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
172}
173
174/// A block which will be called by convertToBuffer:error:withInputFromBlock: to get input data as needed.
175///
176///
177/// Parameter `inNumberOfPackets`: This will be the number of packets required to complete the request. You may supply more or
178/// less that this amount. If less, then the input block will get called again.
179///
180///
181/// Parameter `outStatus`: The block must set the appropriate AVAudioConverterInputStatus enum value.
182///
183/// If you have supplied data, set outStatus to AVAudioConverterInputStatus_HaveData and return
184/// an AVAudioBuffer.
185///
186/// If you are out of data for now, set outStatus to AVAudioConverterInputStatus_NoDataNow and
187/// return nil, and the conversion routine will return as much output as could be converted with
188/// the input already supplied.
189///
190/// If you are at the end of stream, set outStatus to AVAudioConverterInputStatus_EndOfStream,
191/// and return nil.
192///
193///
194/// Returns: An AVAudioBuffer containing data to be converted, or nil if at end of stream or no data is
195/// available. The data in the returned buffer must not be cleared or re-filled until the input
196/// block is called again or the conversion has finished.
197///
198/// convertToBuffer:error:withInputFromBlock: will return as much output as could be converted
199/// with the input already supplied.
200///
201/// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioconverterinputblock?language=objc)
202#[cfg(all(
203    feature = "AVAudioBuffer",
204    feature = "AVAudioTypes",
205    feature = "block2"
206))]
207pub type AVAudioConverterInputBlock = *mut block2::Block<
208    dyn Fn(AVAudioPacketCount, NonNull<AVAudioConverterInputStatus>) -> *mut AVAudioBuffer,
209>;
210
211extern_class!(
212    /// Converts streams of audio between various formats.
213    ///
214    /// See also [Apple's documentation](https://developer.apple.com/documentation/avfaudio/avaudioconverter?language=objc)
215    #[unsafe(super(NSObject))]
216    #[derive(Debug, PartialEq, Eq, Hash)]
217    pub struct AVAudioConverter;
218);
219
220unsafe impl NSObjectProtocol for AVAudioConverter {}
221
222impl AVAudioConverter {
223    extern_methods!(
224        #[cfg(feature = "AVAudioFormat")]
225        /// Initialize from input and output formats.
226        ///
227        /// Parameter `fromFormat`: The input format.
228        ///
229        /// Parameter `toFormat`: The output format.
230        ///
231        /// Returns nil if the format conversion is not possible.
232        #[unsafe(method(initFromFormat:toFormat:))]
233        #[unsafe(method_family = init)]
234        pub unsafe fn initFromFormat_toFormat(
235            this: Allocated<Self>,
236            from_format: &AVAudioFormat,
237            to_format: &AVAudioFormat,
238        ) -> Option<Retained<Self>>;
239
240        /// Resets the converter so that a new stream may be converted.
241        #[unsafe(method(reset))]
242        #[unsafe(method_family = none)]
243        pub unsafe fn reset(&self);
244
245        #[cfg(feature = "AVAudioFormat")]
246        /// The format of the input audio stream. (NB. AVAudioFormat includes the channel layout)
247        #[unsafe(method(inputFormat))]
248        #[unsafe(method_family = none)]
249        pub unsafe fn inputFormat(&self) -> Retained<AVAudioFormat>;
250
251        #[cfg(feature = "AVAudioFormat")]
252        /// The format of the output audio stream. (NB. AVAudioFormat includes the channel layout)
253        #[unsafe(method(outputFormat))]
254        #[unsafe(method_family = none)]
255        pub unsafe fn outputFormat(&self) -> Retained<AVAudioFormat>;
256
257        /// An array of integers indicating from which input to derive each output.
258        ///
259        /// The array has size equal to the number of output channels. Each element's value is the input
260        /// channel number, starting with zero, that is to be copied to that output. A negative value
261        /// means that the output channel will have no source and will be silent. Setting a channel map
262        /// overrides channel mapping due to any channel layouts in the input and output formats that
263        /// may have been supplied.
264        #[unsafe(method(channelMap))]
265        #[unsafe(method_family = none)]
266        pub unsafe fn channelMap(&self) -> Retained<NSArray<NSNumber>>;
267
268        /// Setter for [`channelMap`][Self::channelMap].
269        #[unsafe(method(setChannelMap:))]
270        #[unsafe(method_family = none)]
271        pub unsafe fn setChannelMap(&self, channel_map: &NSArray<NSNumber>);
272
273        /// Decoders require some data in the form of a magicCookie in order to decode properly.
274        /// Encoders will produce a magicCookie.
275        #[unsafe(method(magicCookie))]
276        #[unsafe(method_family = none)]
277        pub unsafe fn magicCookie(&self) -> Option<Retained<NSData>>;
278
279        /// Setter for [`magicCookie`][Self::magicCookie].
280        #[unsafe(method(setMagicCookie:))]
281        #[unsafe(method_family = none)]
282        pub unsafe fn setMagicCookie(&self, magic_cookie: Option<&NSData>);
283
284        /// If YES and channel remapping is necessary, then channels will be mixed as
285        /// appropriate instead of remapped. Default value is NO.
286        #[unsafe(method(downmix))]
287        #[unsafe(method_family = none)]
288        pub unsafe fn downmix(&self) -> bool;
289
290        /// Setter for [`downmix`][Self::downmix].
291        #[unsafe(method(setDownmix:))]
292        #[unsafe(method_family = none)]
293        pub unsafe fn setDownmix(&self, downmix: bool);
294
295        /// Setting YES will turn on dither, if dither makes sense in given the current formats
296        /// and settings. Default value is NO.
297        #[unsafe(method(dither))]
298        #[unsafe(method_family = none)]
299        pub unsafe fn dither(&self) -> bool;
300
301        /// Setter for [`dither`][Self::dither].
302        #[unsafe(method(setDither:))]
303        #[unsafe(method_family = none)]
304        pub unsafe fn setDither(&self, dither: bool);
305
306        /// An AVAudioQuality value as defined in AVAudioSettings.h.
307        #[unsafe(method(sampleRateConverterQuality))]
308        #[unsafe(method_family = none)]
309        pub unsafe fn sampleRateConverterQuality(&self) -> NSInteger;
310
311        /// Setter for [`sampleRateConverterQuality`][Self::sampleRateConverterQuality].
312        #[unsafe(method(setSampleRateConverterQuality:))]
313        #[unsafe(method_family = none)]
314        pub unsafe fn setSampleRateConverterQuality(
315            &self,
316            sample_rate_converter_quality: NSInteger,
317        );
318
319        /// An AVSampleRateConverterAlgorithmKey value as defined in AVAudioSettings.h.
320        #[unsafe(method(sampleRateConverterAlgorithm))]
321        #[unsafe(method_family = none)]
322        pub unsafe fn sampleRateConverterAlgorithm(&self) -> Option<Retained<NSString>>;
323
324        /// Setter for [`sampleRateConverterAlgorithm`][Self::sampleRateConverterAlgorithm].
325        #[unsafe(method(setSampleRateConverterAlgorithm:))]
326        #[unsafe(method_family = none)]
327        pub unsafe fn setSampleRateConverterAlgorithm(
328            &self,
329            sample_rate_converter_algorithm: Option<&NSString>,
330        );
331
332        /// Indicates the priming method to be used by the sample rate converter or decoder.
333        #[unsafe(method(primeMethod))]
334        #[unsafe(method_family = none)]
335        pub unsafe fn primeMethod(&self) -> AVAudioConverterPrimeMethod;
336
337        /// Setter for [`primeMethod`][Self::primeMethod].
338        #[unsafe(method(setPrimeMethod:))]
339        #[unsafe(method_family = none)]
340        pub unsafe fn setPrimeMethod(&self, prime_method: AVAudioConverterPrimeMethod);
341
342        #[cfg(feature = "AVAudioTypes")]
343        /// Indicates the the number of priming frames.
344        #[unsafe(method(primeInfo))]
345        #[unsafe(method_family = none)]
346        pub unsafe fn primeInfo(&self) -> AVAudioConverterPrimeInfo;
347
348        #[cfg(feature = "AVAudioTypes")]
349        /// Setter for [`primeInfo`][Self::primeInfo].
350        #[unsafe(method(setPrimeInfo:))]
351        #[unsafe(method_family = none)]
352        pub unsafe fn setPrimeInfo(&self, prime_info: AVAudioConverterPrimeInfo);
353
354        #[cfg(feature = "AVAudioBuffer")]
355        /// Perform a simple conversion. That is, a conversion which does not involve codecs or sample rate conversion.
356        ///
357        /// Parameter `inputBuffer`: The input buffer.
358        ///
359        /// Parameter `outputBuffer`: The output buffer.
360        ///
361        /// Parameter `outError`: An error if the conversion fails.
362        ///
363        /// Returns: YES is returned on success, NO when an error has occurred.
364        ///
365        /// The output buffer's frameCapacity should be at least at large as the inputBuffer's frameLength.
366        /// If the conversion involves a codec or sample rate conversion, you instead must use
367        /// convertToBuffer:error:withInputFromBlock:.
368        #[unsafe(method(convertToBuffer:fromBuffer:error:_))]
369        #[unsafe(method_family = none)]
370        pub unsafe fn convertToBuffer_fromBuffer_error(
371            &self,
372            output_buffer: &AVAudioPCMBuffer,
373            input_buffer: &AVAudioPCMBuffer,
374        ) -> Result<(), Retained<NSError>>;
375
376        #[cfg(all(
377            feature = "AVAudioBuffer",
378            feature = "AVAudioTypes",
379            feature = "block2"
380        ))]
381        /// Perform any supported conversion.
382        ///
383        /// Parameter `inputBlock`: A block which will be called to get input data as needed. See description for AVAudioConverterInputBlock.
384        ///
385        /// Parameter `outputBuffer`: The output buffer.
386        ///
387        /// Parameter `outError`: An error if the conversion fails.
388        ///
389        /// Returns: An AVAudioConverterOutputStatus is returned.
390        ///
391        /// It attempts to fill the buffer to its capacity. On return, the buffer's length indicates the number of
392        /// sample frames successfully converted.
393        #[unsafe(method(convertToBuffer:error:withInputFromBlock:))]
394        #[unsafe(method_family = none)]
395        pub unsafe fn convertToBuffer_error_withInputFromBlock(
396            &self,
397            output_buffer: &AVAudioBuffer,
398            out_error: Option<&mut Option<Retained<NSError>>>,
399            input_block: AVAudioConverterInputBlock,
400        ) -> AVAudioConverterOutputStatus;
401    );
402}
403
404/// Methods declared on superclass `NSObject`.
405impl AVAudioConverter {
406    extern_methods!(
407        #[unsafe(method(init))]
408        #[unsafe(method_family = init)]
409        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
410
411        #[unsafe(method(new))]
412        #[unsafe(method_family = new)]
413        pub unsafe fn new() -> Retained<Self>;
414    );
415}
416
417/// Encoding.
418impl AVAudioConverter {
419    extern_methods!(
420        /// bitRate in bits per second. Only applies when encoding.
421        #[unsafe(method(bitRate))]
422        #[unsafe(method_family = none)]
423        pub unsafe fn bitRate(&self) -> NSInteger;
424
425        /// Setter for [`bitRate`][Self::bitRate].
426        #[unsafe(method(setBitRate:))]
427        #[unsafe(method_family = none)]
428        pub unsafe fn setBitRate(&self, bit_rate: NSInteger);
429
430        /// When encoding, an AVEncoderBitRateStrategyKey value constant as defined in AVAudioSettings.h. Returns nil if not encoding.
431        #[unsafe(method(bitRateStrategy))]
432        #[unsafe(method_family = none)]
433        pub unsafe fn bitRateStrategy(&self) -> Option<Retained<NSString>>;
434
435        /// Setter for [`bitRateStrategy`][Self::bitRateStrategy].
436        #[unsafe(method(setBitRateStrategy:))]
437        #[unsafe(method_family = none)]
438        pub unsafe fn setBitRateStrategy(&self, bit_rate_strategy: Option<&NSString>);
439
440        /// The maximum size of an output packet, in bytes.
441        ///
442        /// When encoding it is useful to know how large a packet can be in order to allocate a buffer to receive the output.
443        #[unsafe(method(maximumOutputPacketSize))]
444        #[unsafe(method_family = none)]
445        pub unsafe fn maximumOutputPacketSize(&self) -> NSInteger;
446
447        /// When encoding, an NSArray of NSNumber of all bit rates provided by the codec. Returns nil if not encoding.
448        #[unsafe(method(availableEncodeBitRates))]
449        #[unsafe(method_family = none)]
450        pub unsafe fn availableEncodeBitRates(&self) -> Option<Retained<NSArray<NSNumber>>>;
451
452        /// When encoding, an NSArray of NSNumber of bit rates that can be applied based on the current formats and settings. Returns nil if not encoding.
453        #[unsafe(method(applicableEncodeBitRates))]
454        #[unsafe(method_family = none)]
455        pub unsafe fn applicableEncodeBitRates(&self) -> Option<Retained<NSArray<NSNumber>>>;
456
457        /// When encoding, an NSArray of NSNumber of all output sample rates provided by the codec. Returns nil if not encoding.
458        #[unsafe(method(availableEncodeSampleRates))]
459        #[unsafe(method_family = none)]
460        pub unsafe fn availableEncodeSampleRates(&self) -> Option<Retained<NSArray<NSNumber>>>;
461
462        /// When encoding, an NSArray of NSNumber of output sample rates that can be applied based on the current formats and settings. Returns nil if not encoding.
463        #[unsafe(method(applicableEncodeSampleRates))]
464        #[unsafe(method_family = none)]
465        pub unsafe fn applicableEncodeSampleRates(&self) -> Option<Retained<NSArray<NSNumber>>>;
466
467        /// When encoding, an NSArray of NSNumber of all output channel layout tags provided by the codec. Returns nil if not encoding.
468        #[unsafe(method(availableEncodeChannelLayoutTags))]
469        #[unsafe(method_family = none)]
470        pub unsafe fn availableEncodeChannelLayoutTags(
471            &self,
472        ) -> Option<Retained<NSArray<NSNumber>>>;
473    );
474}