oboe/
audio_stream_base.rs

1//use oboe_sys as ffi;
2use num_traits::FromPrimitive;
3
4use std::fmt::{self, Display};
5
6use super::{
7    AudioFormat, ChannelCount, ContentType, Direction, InputPreset, PerformanceMode,
8    RawAudioStreamBase, SampleRateConversionQuality, SessionId, SharingMode, Usage,
9};
10
11/**
12 * Base trait containing parameters for audio streams and builders.
13 */
14pub trait AudioStreamBase {
15    /**
16     * Get actual number of channels
17     */
18    fn get_channel_count(&self) -> ChannelCount;
19
20    /**
21     * Get actual stream direction
22     *
23     * `Direction::Input` or `Direction::Output`.
24     */
25    fn get_direction(&self) -> Direction;
26
27    /**
28     * Get the actual sample rate for the stream
29     */
30    fn get_sample_rate(&self) -> i32;
31
32    /**
33     * Get the number of frames in each callback
34     */
35    fn get_frames_per_callback(&self) -> i32;
36
37    /**
38     * Get the audio sample format (e.g. F32 or I16)
39     */
40    fn get_format(&self) -> AudioFormat;
41
42    /**
43     * Query the maximum number of frames that can be filled without blocking.
44     * If the stream has been closed the last known value will be returned.
45     */
46    fn get_buffer_size_in_frames(&self) -> i32;
47
48    /**
49     * Get the capacity in number of frames
50     */
51    fn get_buffer_capacity_in_frames(&self) -> i32;
52
53    /**
54     * Get the sharing mode of the stream
55     */
56    fn get_sharing_mode(&self) -> SharingMode;
57
58    /**
59     * Get the performance mode of the stream
60     */
61    fn get_performance_mode(&self) -> PerformanceMode;
62
63    /**
64     * Get the device identifier of the stream
65     */
66    fn get_device_id(&self) -> i32;
67
68    /**
69     * Get the usage for this stream
70     */
71    fn get_usage(&self) -> Usage;
72
73    /**
74     * Get the stream's content type
75     */
76    fn get_content_type(&self) -> ContentType;
77
78    /**
79     * Get the stream's input preset
80     */
81    fn get_input_preset(&self) -> InputPreset;
82
83    /**
84     * Get the stream's session ID allocation strategy (None or Allocate)
85     */
86    fn get_session_id(&self) -> SessionId;
87
88    /**
89     * Return true if can convert channel counts to achieve optimal results.
90     */
91    fn is_channel_conversion_allowed(&self) -> bool;
92
93    /**
94     * Return true if  Oboe can convert data formats to achieve optimal results.
95     */
96    fn is_format_conversion_allowed(&self) -> bool;
97
98    /**
99     * Get whether and how Oboe can convert sample rates to achieve optimal results.
100     */
101    fn get_sample_rate_conversion_quality(&self) -> SampleRateConversionQuality;
102}
103
104impl<T: RawAudioStreamBase> AudioStreamBase for T {
105    fn get_channel_count(&self) -> ChannelCount {
106        FromPrimitive::from_i32(self._raw_base().mChannelCount).unwrap()
107    }
108
109    fn get_direction(&self) -> Direction {
110        FromPrimitive::from_i32(self._raw_base().mDirection).unwrap()
111    }
112
113    fn get_sample_rate(&self) -> i32 {
114        self._raw_base().mSampleRate
115    }
116
117    fn get_frames_per_callback(&self) -> i32 {
118        self._raw_base().mFramesPerCallback
119    }
120
121    fn get_format(&self) -> AudioFormat {
122        FromPrimitive::from_i32(self._raw_base().mFormat).unwrap()
123    }
124
125    fn get_buffer_size_in_frames(&self) -> i32 {
126        self._raw_base().mBufferSizeInFrames
127    }
128
129    fn get_buffer_capacity_in_frames(&self) -> i32 {
130        self._raw_base().mBufferCapacityInFrames
131    }
132
133    fn get_sharing_mode(&self) -> SharingMode {
134        FromPrimitive::from_i32(self._raw_base().mSharingMode).unwrap()
135    }
136
137    fn get_performance_mode(&self) -> PerformanceMode {
138        FromPrimitive::from_i32(self._raw_base().mPerformanceMode).unwrap()
139    }
140
141    fn get_device_id(&self) -> i32 {
142        self._raw_base().mDeviceId
143    }
144
145    fn get_usage(&self) -> Usage {
146        FromPrimitive::from_i32(self._raw_base().mUsage).unwrap()
147    }
148
149    fn get_content_type(&self) -> ContentType {
150        FromPrimitive::from_i32(self._raw_base().mContentType).unwrap()
151    }
152
153    fn get_input_preset(&self) -> InputPreset {
154        FromPrimitive::from_i32(self._raw_base().mInputPreset).unwrap()
155    }
156
157    fn get_session_id(&self) -> SessionId {
158        FromPrimitive::from_i32(self._raw_base().mSessionId).unwrap()
159    }
160
161    fn is_channel_conversion_allowed(&self) -> bool {
162        self._raw_base().mChannelConversionAllowed
163    }
164
165    fn is_format_conversion_allowed(&self) -> bool {
166        self._raw_base().mFormatConversionAllowed
167    }
168
169    fn get_sample_rate_conversion_quality(&self) -> SampleRateConversionQuality {
170        FromPrimitive::from_i32(self._raw_base().mSampleRateConversionQuality).unwrap()
171    }
172}
173
174pub(crate) fn audio_stream_base_fmt<T: AudioStreamBase>(
175    base: &T,
176    f: &mut fmt::Formatter<'_>,
177) -> fmt::Result {
178    "DeviceId: ".fmt(f)?;
179    base.get_device_id().fmt(f)?;
180    "\nSessionId: ".fmt(f)?;
181    fmt::Debug::fmt(&base.get_session_id(), f)?;
182    "\nDirection: ".fmt(f)?;
183    fmt::Debug::fmt(&base.get_direction(), f)?;
184    if base.get_direction() == Direction::Input {
185        "\nInput preset: ".fmt(f)?;
186        fmt::Debug::fmt(&base.get_input_preset(), f)?;
187    }
188    "\nBuffer capacity in frames: ".fmt(f)?;
189    base.get_buffer_capacity_in_frames().fmt(f)?;
190    "\nBuffer size in frames: ".fmt(f)?;
191    base.get_buffer_size_in_frames().fmt(f)?;
192    "\nFrames per callback: ".fmt(f)?;
193    base.get_frames_per_callback().fmt(f)?;
194    "\nSample rate: ".fmt(f)?;
195    base.get_sample_rate().fmt(f)?;
196    "\nSample rate conversion quality: ".fmt(f)?;
197    fmt::Debug::fmt(&base.get_sample_rate_conversion_quality(), f)?;
198    "\nChannel count: ".fmt(f)?;
199    fmt::Debug::fmt(&base.get_channel_count(), f)?;
200    if base.is_channel_conversion_allowed() {
201        " (conversion allowed)".fmt(f)?;
202    }
203    "\nFormat: ".fmt(f)?;
204    fmt::Debug::fmt(&base.get_format(), f)?;
205    if base.is_format_conversion_allowed() {
206        " (conversion allowed)".fmt(f)?;
207    }
208    "\nSharing mode: ".fmt(f)?;
209    fmt::Debug::fmt(&base.get_sharing_mode(), f)?;
210    "\nPerformance mode: ".fmt(f)?;
211    fmt::Debug::fmt(&base.get_performance_mode(), f)?;
212    "\nUsage: ".fmt(f)?;
213    fmt::Debug::fmt(&base.get_usage(), f)?;
214    "\nContent type: ".fmt(f)?;
215    fmt::Debug::fmt(&base.get_content_type(), f)?;
216    '\n'.fmt(f)
217}