opusic_c/
encoder.rs

1use crate::{sys, mem, ErrorCode, Application, Channels, SampleRate, Bandwidth, Bitrate, Signal, InbandFec, FrameDuration};
2
3use mem::alloc::vec::Vec;
4
5///OPUS encoder
6pub struct Encoder {
7    inner: mem::Unique<sys::OpusEncoder>,
8    channels: Channels,
9}
10
11impl Encoder {
12    ///Creates new encoder instance
13    pub fn new(channels: Channels, rate: SampleRate, app: Application) -> Result<Self, ErrorCode> {
14        let size = unsafe {
15            sys::opus_encoder_get_size(channels as _)
16        };
17
18        if size == 0 {
19            return Err(ErrorCode::Internal);
20        }
21
22        let mut encoder = match mem::Unique::new(size as _) {
23            Some(inner) => Encoder {
24                inner,
25                channels,
26            },
27            None => return Err(ErrorCode::AllocFail)
28        };
29
30        let result = unsafe {
31            sys::opus_encoder_init(encoder.inner.as_mut(), rate as _, channels as _, app as _)
32        };
33
34        map_sys_error!(result => encoder)
35    }
36
37    #[inline(always)]
38    ///Returns channels number
39    ///
40    ///When encoding, it is used to determine frame size as `input.len() / channels`
41    pub fn channels(&self) -> Channels {
42        self.channels
43    }
44
45    ///Encodes an Opus frame, returning number of bytes written.
46    ///
47    ///If more than 1 channel is configured, then input must be interleaved.
48    ///
49    ///Input size must correspond to sampling rate.
50    ///For example, at 48 kHz allowed frame sizes are 120, 240, 480, 960, 1920, and 2880.
51    ///Passing in a duration of less than 10 ms (480 samples at 48 kHz) will prevent the encoder from using the LPC or hybrid modes.
52    pub fn encode_to(&mut self, input: &[u16], output: &mut [mem::MaybeUninit<u8>]) -> Result<usize, ErrorCode> {
53        let result = unsafe {
54            sys::opus_encode(self.inner.as_mut(),
55                             input.as_ptr() as _, (input.len() / (self.channels as usize)) as _,
56                             output.as_mut_ptr() as _, output.len() as _)
57        };
58
59        map_sys_error!(result => result as _)
60    }
61
62    #[inline(always)]
63    ///Encodes an Opus frame, returning number of bytes written.
64    ///
65    ///Refer to `encode_to` for details
66    pub fn encode_to_slice(&mut self, input: &[u16], output: &mut [u8]) -> Result<usize, ErrorCode> {
67        self.encode_to(input, unsafe { mem::transmute(output) })
68    }
69
70    #[inline(always)]
71    ///Encodes an Opus frame, returning number of bytes written.
72    ///
73    ///Vector will be written into spare capacity, modifying its length on success.
74    ///
75    ///It is user responsibility to reserve correct amount of space
76    ///
77    ///Refer to `encode_to` for details
78    pub fn encode_to_vec(&mut self, input: &[u16], output: &mut Vec<u8>) -> Result<usize, ErrorCode> {
79        let initial_len = output.len();
80        let result = self.encode_to(input, output.spare_capacity_mut())?;
81        unsafe {
82            output.set_len(initial_len + result);
83        }
84        Ok(result)
85    }
86
87    ///Encodes an Opus frame, returning number of bytes written.
88    ///
89    ///If more than 1 channel is configured, then input must be interleaved.
90    ///
91    ///Input size must correspond to sampling rate.
92    ///For example, at 48 kHz allowed frame sizes are 120, 240, 480, 960, 1920, and 2880.
93    ///Passing in a duration of less than 10 ms (480 samples at 48 kHz) will prevent the encoder from using the LPC or hybrid modes.
94    ///
95    ///## Note
96    ///
97    ///When using float API, input with a normal range of +/-1.0 should be preferred.
98    ///Samples with a range beyond +/-1.0 are supported
99    ///but will be clipped by decoders using the integer API and should only be used
100    ///if it is known that the far end supports extended dynamic range
101    pub fn encode_float_to(&mut self, input: &[f32], output: &mut [mem::MaybeUninit<u8>]) -> Result<usize, ErrorCode> {
102        let result = unsafe {
103            sys::opus_encode_float(self.inner.as_mut(),
104                                   input.as_ptr(), (input.len() / (self.channels as usize)) as _,
105                                   output.as_mut_ptr() as _, output.len() as _)
106        };
107
108        map_sys_error!(result => result as _)
109    }
110
111    #[inline(always)]
112    ///Encodes an Opus frame, returning number of bytes written.
113    ///
114    ///Refer to `encode_to` for details
115    pub fn encode_float_to_slice(&mut self, input: &[f32], output: &mut [u8]) -> Result<usize, ErrorCode> {
116        self.encode_float_to(input, unsafe { mem::transmute(output) })
117    }
118
119    #[inline(always)]
120    ///Encodes an Opus frame, returning number of bytes written.
121    ///
122    ///Vector will be written into spare capacity, modifying its length on success.
123    ///
124    ///It is user responsibility to reserve correct amount of space
125    ///
126    ///Refer to `encode_to` for details
127    pub fn encode_float_to_vec(&mut self, input: &[f32], output: &mut Vec<u8>) -> Result<usize, ErrorCode> {
128        let initial_len = output.len();
129        let result = self.encode_float_to(input, output.spare_capacity_mut())?;
130        unsafe {
131            output.set_len(initial_len + result);
132        }
133        Ok(result)
134    }
135
136    #[inline]
137    ///Resets state to initial state
138    pub fn reset(&mut self) -> Result<(), ErrorCode> {
139        let result = unsafe {
140            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_RESET_STATE)
141        };
142
143        map_sys_error!(result => ())
144    }
145
146    #[inline]
147    ///Gets the total samples of delay added by the entire codec.
148    ///
149    ///From the perspective of a decoding application the real data begins this many samples late.
150    pub fn get_look_ahead(&mut self) -> Result<u32, ErrorCode> {
151        let mut value: i32 = 0;
152        let result = unsafe {
153            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_LOOKAHEAD_REQUEST, &mut value)
154        };
155
156        map_sys_error!(result => match value.is_negative() {
157            false => value as _,
158            true => return Err(ErrorCode::unknown())
159        })
160    }
161
162    #[inline]
163    ///Gets the encoder's bitrate configuration.
164    pub fn get_bitrate(&mut self) -> Result<Bitrate, ErrorCode> {
165        let mut value: i32 = 0;
166        let result = unsafe {
167            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_BITRATE_REQUEST, &mut value)
168        };
169
170        map_sys_error!(result => value.into())
171    }
172
173    #[inline]
174    ///Configures the encoder's bitrate
175    pub fn set_bitrate(&mut self, value: Bitrate) -> Result<(), ErrorCode> {
176        let value: i32 = value.into();
177        let result = unsafe {
178            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_BITRATE_REQUEST, value)
179        };
180
181        map_sys_error!(result => ())
182    }
183
184    #[inline]
185    ///Determine if variable bitrate (VBR) is enabled in the encoder.
186    pub fn get_vbr(&mut self) -> Result<bool, ErrorCode> {
187        let mut value: i32 = 0;
188        let result = unsafe {
189            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_VBR_REQUEST, &mut value)
190        };
191
192        map_sys_error!(result => value == 1)
193    }
194
195    #[inline]
196    ///Enables or disables variable bitrate (VBR) in the encoder.
197    ///
198    ///The configured bitrate may not be met exactly because frames must be an integer number of bytes in length.
199    pub fn set_vbr(&mut self, value: bool) -> Result<(), ErrorCode> {
200        let value: i32 = match value {
201            true => 1,
202            false => 0
203        };
204        let result = unsafe {
205            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_VBR_REQUEST, value)
206        };
207
208        map_sys_error!(result => ())
209    }
210
211    #[inline]
212    ///Determine if constrained VBR is enabled in the encoder.
213    pub fn get_vbr_constraint(&mut self) -> Result<bool, ErrorCode> {
214        let mut value: i32 = 0;
215        let result = unsafe {
216            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_VBR_CONSTRAINT_REQUEST, &mut value)
217        };
218
219        map_sys_error!(result => value == 1)
220    }
221
222    #[inline]
223    ///Enables or disables constrained VBR in the encoder.
224    ///
225    ///This setting is ignored when the encoder is in CBR mode.
226    ///
227    ///## Note
228    ///
229    ///Only the MDCT mode of Opus currently heeds the constraint. Speech mode ignores it
230    ///completely, hybrid mode may fail to obey it if the LPC layer uses more bitrate than the
231    ///constraint would have permitted.
232    pub fn set_vbr_constraint(&mut self, value: bool) -> Result<(), ErrorCode> {
233        let value: i32 = match value {
234            true => 1,
235            false => 0
236        };
237        let result = unsafe {
238            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_VBR_CONSTRAINT_REQUEST, value)
239        };
240
241        map_sys_error!(result => ())
242    }
243
244    #[inline]
245    ///Gets the encoder's forced channel configuration (if set).
246    pub fn get_force_channels(&mut self) -> Result<Option<Channels>, ErrorCode> {
247        let mut value: i32 = 0;
248        let result = unsafe {
249            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_FORCE_CHANNELS_REQUEST, &mut value)
250        };
251
252        map_sys_error!(result => match value {
253            1 => Some(Channels::Mono),
254            2 => Some(Channels::Stereo),
255            _ => None,
256        })
257    }
258
259    #[inline]
260    ///Configures mono/stereo forcing in the encoder (or disables it by specifying None).
261    ///
262    ///This can force the encoder to produce packets encoded as either mono or stereo, regardless
263    ///of the format of the input audio. This is useful when the caller knows that the input signal
264    ///is currently a mono source embedded in a stereo stream.
265    pub fn set_force_channels(&mut self, value: Option<Channels>) -> Result<(), ErrorCode> {
266        let value = match value {
267            Some(value) => value as i32,
268            None => sys::OPUS_AUTO
269        };
270        let result = unsafe {
271            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_FORCE_CHANNELS_REQUEST, value)
272        };
273
274        map_sys_error!(result => ())
275    }
276
277    #[inline]
278    ///Gets the encoder's complexity configuration.
279    pub fn get_complexity(&mut self) -> Result<u8, ErrorCode> {
280        let mut value: i32 = 0;
281        let result = unsafe {
282            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_COMPLEXITY_REQUEST, &mut value)
283        };
284
285        map_sys_error!(result => value as _)
286    }
287
288    #[inline]
289    ///Configures the encoder's computational complexity.
290    ///
291    ///The supported range is 0-10 inclusive with 10 representing the highest complexity.
292    pub fn set_complexity(&mut self, value: u8) -> Result<(), ErrorCode> {
293        let result = unsafe {
294            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_COMPLEXITY_REQUEST, value as i32)
295        };
296
297        map_sys_error!(result => ())
298    }
299
300    #[inline]
301    ///Gets the encoder's configured signal type.
302    pub fn get_signal(&mut self) -> Result<Signal, ErrorCode> {
303        let mut value: i32 = 0;
304        let result = unsafe {
305            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_SIGNAL_REQUEST, &mut value)
306        };
307
308        map_sys_error!(result => value.into())
309    }
310
311    #[inline]
312    ///Configures the type of signal being encoded.
313    ///
314    ///This is a hint which helps the encoder's mode selection.
315    pub fn set_signal(&mut self, value: Signal) -> Result<(), ErrorCode> {
316        let result = unsafe {
317            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_SIGNAL_REQUEST, value as i32)
318        };
319
320        map_sys_error!(result => ())
321    }
322
323    #[inline]
324    ///Gets the encoder's configured application.
325    pub fn get_application(&mut self) -> Result<Application, ErrorCode> {
326        let mut value: i32 = 0;
327        let result = unsafe {
328            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_APPLICATION_REQUEST, &mut value)
329        };
330
331        map_sys_error!(result => match Application::from_sys(value) {
332            Some(value) => value,
333            None => return Err(ErrorCode::unknown())
334        })
335    }
336
337    #[inline]
338    ///Configures the encoder's intended application.
339    ///
340    ///The initial value is a mandatory argument to encoder constructor.
341    pub fn set_application(&mut self, value: Application) -> Result<(), ErrorCode> {
342        let result = unsafe {
343            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_APPLICATION_REQUEST, value as i32)
344        };
345
346        map_sys_error!(result => ())
347    }
348
349    #[inline]
350    ///Gets the encoder's configured bandpass
351    pub fn get_bandwidth(&mut self) -> Result<Bandwidth, ErrorCode> {
352        let mut value: i32 = 0;
353        let result = unsafe {
354            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_BANDWIDTH_REQUEST, &mut value)
355        };
356
357        map_sys_error!(result => value.into())
358    }
359
360    #[inline]
361    ///Sets the encoder's bandpass to a specific value.
362    ///
363    ///This prevents the encoder from automatically selecting the bandpass based on the available
364    ///bitrate. If an application knows the bandpass of the input audio it is providing, it should
365    ///normally use `set_max_bandwidth` instead, which still gives the encoder the freedom to
366    ///reduce the bandpass when the bitrate becomes too low, for better overall quality.
367    pub fn set_bandwidth(&mut self, value: Bandwidth) -> Result<(), ErrorCode> {
368        let result = unsafe {
369            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_BANDWIDTH_REQUEST, value as i32)
370        };
371
372        map_sys_error!(result => ())
373    }
374
375    #[inline]
376    ///Gets the encoder's configured maximum allowed bandpass.
377    pub fn get_max_bandwidth(&mut self) -> Result<Bandwidth, ErrorCode> {
378        let mut value: i32 = 0;
379        let result = unsafe {
380            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_MAX_BANDWIDTH_REQUEST, &mut value)
381        };
382
383        map_sys_error!(result => value.into())
384    }
385
386    #[inline]
387    ///Configures the maximum bandpass that the encoder will select automatically.
388    ///
389    ///Applications should normally use this instead of `set_bandwidth` (leaving that set to the
390    ///default, `Bandwidth::Auto`). This allows the application to set an upper bound based on the type of
391    ///input it is providing, but still gives the encoder the freedom to reduce the bandpass when
392    ///the bitrate becomes too low, for better overall quality.
393    pub fn set_max_bandwidth(&mut self, value: Bandwidth) -> Result<(), ErrorCode> {
394        let result = unsafe {
395            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_MAX_BANDWIDTH_REQUEST, value as i32)
396        };
397
398        map_sys_error!(result => ())
399    }
400
401    #[inline]
402    ///Gets encoder's configured use of inband forward error correction.
403    pub fn get_inband_fec(&mut self) -> Result<InbandFec, ErrorCode> {
404        let mut value: i32 = 0;
405        let result = unsafe {
406            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_INBAND_FEC_REQUEST, &mut value)
407        };
408
409        map_sys_error!(result => match value {
410            0 => InbandFec::Off,
411            1 => InbandFec::Mode1,
412            2 => InbandFec::Mode2,
413            _ => return Err(ErrorCode::unknown()),
414        })
415    }
416
417    #[inline]
418    ///Configures the encoder's use of inband forward error correction (FEC).
419    ///
420    ///## Note
421    ///
422    ///This is only applicable to the LPC layer
423    pub fn set_inband_fec(&mut self, value: InbandFec) -> Result<(), ErrorCode> {
424        let result = unsafe {
425            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_INBAND_FEC_REQUEST, value as i32)
426        };
427
428        map_sys_error!(result => ())
429    }
430
431    #[inline]
432    ///Gets the encoder's configured packet loss percentage.
433    pub fn get_packet_loss(&mut self) -> Result<u8, ErrorCode> {
434        let mut value: i32 = 0;
435        let result = unsafe {
436            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_PACKET_LOSS_PERC_REQUEST, &mut value)
437        };
438
439        map_sys_error!(result => value as _)
440    }
441
442    #[inline]
443    ///Configures the encoder's expected packet loss percentage (Allowed values are 0..=100).
444    ///
445    ///Higher values trigger progressively more loss resistant behavior in the encoder at the
446    ///expense of quality at a given bitrate in the absence of packet loss, but greater quality
447    ///under loss.
448    pub fn set_packet_loss(&mut self, value: u8) -> Result<(), ErrorCode> {
449        let result = unsafe {
450            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_PACKET_LOSS_PERC_REQUEST, value as i32)
451        };
452
453        map_sys_error!(result => ())
454    }
455
456    #[inline]
457    ///Gets the encoder's configured prediction status.
458    pub fn get_prediction_disabled(&mut self) -> Result<bool, ErrorCode> {
459        let mut value: i32 = 0;
460        let result = unsafe {
461            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_PREDICTION_DISABLED_REQUEST, &mut value)
462        };
463
464        map_sys_error!(result => value == 1)
465    }
466
467    #[inline]
468    ///If set to `true`, disables almost all use of prediction, making frames almost completely independent.
469    ///
470    ///This reduces quality.
471    pub fn set_prediction_disabled(&mut self, value: bool) -> Result<(), ErrorCode> {
472        let value: i32 = match value {
473            true => 1,
474            false => 0,
475        };
476
477        let result = unsafe {
478            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_PREDICTION_DISABLED_REQUEST, value)
479        };
480
481        map_sys_error!(result => ())
482    }
483
484    #[inline]
485    ///Gets the encoder's configured signal depth.
486    pub fn get_lsb_depth(&mut self) -> Result<u8, ErrorCode> {
487        let mut value: i32 = 0;
488        let result = unsafe {
489            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_LSB_DEPTH_REQUEST, &mut value)
490        };
491
492        map_sys_error!(result => value as _)
493    }
494
495    #[inline]
496    ///Configures the depth of signal being encoded (Defaults to 24) in range 8 to 24.
497    ///
498    ///This is a hint which helps the encoder identify silence and near-silence. It represents the
499    ///number of significant bits of linear intensity below which the signal contains ignorable
500    ///quantization or other noise.
501    ///
502    ///For example, 14 would be an appropriate setting for G.711 u-law input.
503    ///16 would be appropriate for 16-bit linear pcm input with `encode_float`.
504    ///
505    ///When using `encode` instead of `encode_float`, or when libopus is compiled for
506    ///fixed-point, the encoder uses the minimum of the value set here and the value 16.
507    pub fn set_lsb_depth(&mut self, value: u8) -> Result<(), ErrorCode> {
508        let result = unsafe {
509            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_LSB_DEPTH_REQUEST, value as i32)
510        };
511
512        map_sys_error!(result => ())
513    }
514
515    #[inline]
516    ///Gets the encoder's configured use of variable duration frames.
517    pub fn get_frame_duration(&mut self) -> Result<FrameDuration, ErrorCode> {
518        let mut value: i32 = 0;
519        let result = unsafe {
520            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_EXPERT_FRAME_DURATION_REQUEST, &mut value)
521        };
522
523        map_sys_error!(result => match value {
524            sys::OPUS_FRAMESIZE_ARG => FrameDuration::SizeArg,
525            sys::OPUS_FRAMESIZE_2_5_MS => FrameDuration::Size2_5,
526            sys::OPUS_FRAMESIZE_5_MS => FrameDuration::Size5,
527            sys::OPUS_FRAMESIZE_10_MS => FrameDuration::Size10,
528            sys::OPUS_FRAMESIZE_20_MS => FrameDuration::Size20,
529            sys::OPUS_FRAMESIZE_40_MS => FrameDuration::Size40,
530            sys::OPUS_FRAMESIZE_60_MS => FrameDuration::Size60,
531            sys::OPUS_FRAMESIZE_80_MS => FrameDuration::Size80,
532            sys::OPUS_FRAMESIZE_100_MS => FrameDuration::Size100,
533            sys::OPUS_FRAMESIZE_120_MS => FrameDuration::Size120,
534            _ => return Err(ErrorCode::unknown()),
535        })
536    }
537
538    #[inline]
539    ///Configures the encoder's use of variable duration frames.
540    ///
541    ///When variable duration is enabled, the encoder is free to use a shorter frame size than the
542    ///one requested in the `encode` call. It is then the user's responsibility to verify how
543    ///much audio was encoded by checking the ToC byte of the encoded packet. The part of the audio
544    ///that was not encoded needs to be resent to the encoder for the next call. Do not use this
545    ///option unless you really know what you are doing.
546    pub fn set_frame_duration(&mut self, value: FrameDuration) -> Result<(), ErrorCode> {
547        let result = unsafe {
548            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_EXPERT_FRAME_DURATION_REQUEST, value as i32)
549        };
550
551        map_sys_error!(result => ())
552    }
553
554    #[inline]
555    ///Gets the encoder's configured Deep Redundancy (DRED) maximum number of frames.
556    pub fn get_dred_duration(&mut self) -> Result<u8, ErrorCode> {
557        let mut value: i32 = 0;
558        let result = unsafe {
559            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_DRED_DURATION_REQUEST, &mut value)
560        };
561
562        map_sys_error!(result => value as _)
563    }
564
565    #[inline]
566    ///Configures value of Deep Redundancy (DRED) in range 0..=104
567    ///
568    ///If non-zero, enables DRED and use the specified maximum number of 10-ms redundant frames.
569    pub fn set_dred_duration(&mut self, value: u8) -> Result<(), ErrorCode> {
570        let result = unsafe {
571            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_DRED_DURATION_REQUEST, value as i32)
572        };
573
574        map_sys_error!(result => ())
575    }
576
577    #[inline]
578    ///Gets configured sample rate of this instance
579    pub fn get_sample_rate(&mut self) -> Result<SampleRate, ErrorCode> {
580        let mut value: i32 = 0;
581        let result = unsafe {
582            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_SAMPLE_RATE_REQUEST, &mut value)
583        };
584
585        map_sys_error!(result => match value {
586            8000 => SampleRate::Hz8000,
587            12000 => SampleRate::Hz12000,
588            16000 => SampleRate::Hz16000,
589            24000 => SampleRate::Hz24000,
590            48000 => SampleRate::Hz48000,
591            _ => return Err(ErrorCode::unknown())
592        })
593    }
594
595    #[inline]
596    ///Access encoder's DTX value
597    pub fn get_dtx(&mut self) -> Result<bool, ErrorCode> {
598        let mut value: i32 = 0;
599        let result = unsafe {
600            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_DTX_REQUEST, &mut value)
601        };
602
603        map_sys_error!(result => value == 1)
604    }
605
606    #[inline]
607    ///Configures the encoder's use of discontinuous transmission (DTX).
608    ///
609    ///This is only applicable to the LPC layer
610    pub fn set_dtx(&mut self, value: bool) -> Result<(), ErrorCode> {
611        let value: i32 = match value {
612            true => 1,
613            false => 0,
614        };
615
616        let result = unsafe {
617            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_DTX_REQUEST, value)
618        };
619
620        map_sys_error!(result => ())
621    }
622
623    #[inline]
624    ///Gets the encoder's configured phase inversion status.
625    pub fn get_phase_inversion_disabled(&mut self) -> Result<bool, ErrorCode> {
626        let mut value: i32 = 0;
627        let result = unsafe {
628            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST, &mut value)
629        };
630
631        map_sys_error!(result => value == 1)
632    }
633
634    #[inline]
635    ///Configures phase inversion.
636    ///
637    ///If set to `true`, disables the use of phase inversion for intensity stereo, improving the quality
638    ///of mono downmixes, but slightly reducing normal stereo quality.
639    pub fn set_phase_inversion_disabled(&mut self, value: bool) -> Result<(), ErrorCode> {
640        let value: i32 = match value {
641            true => 1,
642            false => 0,
643        };
644
645        let result = unsafe {
646            sys::opus_encoder_ctl(self.inner.as_mut(), sys::OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST, value)
647        };
648
649        map_sys_error!(result => ())
650    }
651}
652
653unsafe impl Send for Encoder {}