1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
//! Constants and routines for handing channel mapping // This file is part of the PulseAudio Rust language binding. // // Copyright (c) 2017 Lyndon Brown // // This library is free software; you can redistribute it and/or modify it under the terms of the // GNU Lesser General Public License as published by the Free Software Foundation; either version // 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License along with this library; // if not, see <http://www.gnu.org/licenses/>. //! # Overview //! //! Channel maps provide a way to associate channels in a stream with a specific speaker position. //! This relieves applications of having to make sure their channel order is identical to the final //! output. //! //! # Initialisation //! //! A channel map consists of an array of [`Position`] values, one for each channel. This array is //! stored together with a channel count in a [`Map`] structure. //! //! Before filling the structure, the application must initialise it using [`Map::init`]. There are //! also a number of convenience functions for standard channel mappings: //! //! * [`Map::init_mono`]: Create a channel map with only mono audio. //! * [`Map::init_stereo`]: Create a standard stereo mapping. //! * [`Map::init_auto`]: Create a standard channel map for a specific number of channels. //! * [`Map::init_extend`]: Similar to [`Map::init_auto`] but synthesize a channel map if no //! predefined one is known for the specified number of channels. //! //! [`Position`]: enum.Position.html //! [`Map`]: struct.Map.html //! [`Map::init`]: struct.Map.html#method.init //! [`Map::init_mono`]: struct.Map.html#method.init_mono //! [`Map::init_stereo`]: struct.Map.html#method.init_stereo //! [`Map::init_auto`]: struct.Map.html#method.init_auto //! [`Map::init_extend`]: struct.Map.html#method.init_extend use std; use libc; use capi; use std::os::raw::c_char; use std::ffi::{CStr, CString}; pub use capi::pa_channel_map_def_t as MapDef; /// A mask of channel positions. pub type PositionMask = capi::channelmap::pa_channel_position_mask_t; /// Position mask covering all positions pub const POSITION_MASK_ALL: PositionMask = 0xffffffffffffffffu64; /// A list of channel labels /// /// Note, certain aliases, specifically `Left`, `Right`, `Center` and `Subwoofer`, available in the /// equivalent C enum are not provided here, since Rust does not allow aliases. #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Position { Invalid = -1, Mono = 0, /// Apple, Dolby call this 'Left' FrontLeft, /// Apple, Dolby call this 'Right' FrontRight, /// Apple, Dolby call this 'Center' FrontCenter, /// Microsoft calls this 'Back Center', Apple calls this 'Center Surround', /// Dolby calls this 'Surround Rear Center' RearCenter, /// Microsoft calls this 'Back Left', Apple calls this 'Left Surround', /// Dolby calls this 'Surround Rear Left' RearLeft, /// Microsoft calls this 'Back Right', Apple calls this 'Right Surround', /// Dolby calls this 'Surround Rear Right' RearRight, /// Aka subwoofer. Microsoft calls this 'Low Frequency', /// Apple calls this 'LFEScreen' Lfe, /// Apple, Dolby call this 'Left Center' FrontLeftOfCenter, /// Apple, Dolby call this 'Right Center' FrontRightOfCenter, /// Apple calls this 'Left Surround Direct', /// Dolby calls this 'Surround Left' SideLeft, /// Apple calls this 'Right Surround Direct', /// Dolby calls this 'Surround Right' SideRight, Aux0, Aux1, Aux2, Aux3, Aux4, Aux5, Aux6, Aux7, Aux8, Aux9, Aux10, Aux11, Aux12, Aux13, Aux14, Aux15, Aux16, Aux17, Aux18, Aux19, Aux20, Aux21, Aux22, Aux23, Aux24, Aux25, Aux26, Aux27, Aux28, Aux29, Aux30, Aux31, /// Apple calls this 'Top Center Surround' TopCenter, /// Apple calls this 'Vertical Height Left' TopFrontLeft, /// Apple calls this 'Vertical Height Right' TopFrontRight, /// Apple calls this 'Vertical Height Center' TopFrontCenter, /// Microsoft and Apple call this 'Top Back Left' TopRearLeft, /// Microsoft and Apple call this 'Top Back Right' TopRearRight, /// Microsoft and Apple call this 'Top Back Center' TopRearCenter, } impl Default for Position { fn default() -> Self { Position::Invalid } } impl From<Position> for capi::pa_channel_position_t { fn from(p: Position) -> Self { unsafe { std::mem::transmute(p) } } } impl From<capi::pa_channel_position_t> for Position { fn from(p: capi::pa_channel_position_t) -> Self { unsafe { std::mem::transmute(p) } } } /// A channel map which can be used to attach labels to specific channels of a stream. These values /// are relevant for conversion and mixing of streams. #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct Map { /// Number of channels mapped pub channels: u8, /// Channel labels pub map: [Position; ::sample::CHANNELS_MAX], } impl Default for Map { fn default() -> Self { Self { channels: 0, map: [Position::Invalid; ::sample::CHANNELS_MAX], } } } /// The maximum length of strings returned by [`Map::print`], as per the underlying C function. /// Please note that this value can change with any release without warning and without being /// considered API or ABI breakage. You should not use this definition anywhere where it might /// become part of an ABI. /// /// [`Map::print`]: struct.Map.html#method.print pub const PRINT_MAX: usize = capi::PA_CHANNEL_MAP_SNPRINT_MAX; impl Position { /// Makes a bit mask from a channel position. pub fn to_mask(self) -> PositionMask { if self == Position::Invalid { return 0; } (1 as PositionMask) << (self as PositionMask) } /// Return a text label for the specified channel position pub fn to_string(pos: Self) -> Option<&'static CStr> { let ptr = unsafe { capi::pa_channel_position_to_string(pos.into()) }; if ptr.is_null() { return None; } Some(unsafe { CStr::from_ptr(ptr) }) } /// Return a human readable text label for the specified channel position. pub fn to_pretty_string(pos: Self) -> Option<&'static CStr> { let ptr = unsafe { capi::pa_channel_position_to_pretty_string(pos.into()) }; if ptr.is_null() { return None; } Some(unsafe { CStr::from_ptr(ptr) }) } /// The inverse of [`to_string`](#method.to_string). pub fn from_string(s: &str) -> Self { // Warning: New CStrings will be immediately freed if not bound to a variable, leading to // as_ptr() giving dangling pointers! let c_str = CString::new(s.clone()).unwrap(); unsafe { capi::pa_channel_position_from_string(c_str.as_ptr()).into() } } } impl Map { /// Parse a channel position list or well-known mapping name into a channel map structure. This /// turns the output of [`print`](#method.print) and [`to_name`](#method.to_name) back into a /// `Map`. pub fn new_from_string(s: &str) -> Self { // Warning: New CStrings will be immediately freed if not bound to a variable, leading to // as_ptr() giving dangling pointers! let c_str = CString::new(s.clone()).unwrap(); let mut map: Self = Self::default(); unsafe { capi::pa_channel_map_parse(std::mem::transmute(&mut map), c_str.as_ptr()) }; map } /// Initialize the specified channel map and return a pointer to it. The map will have a defined /// state but [`is_valid`](#method.is_valid) will fail for it. pub fn init(&mut self) -> &mut Self { unsafe { capi::pa_channel_map_init(std::mem::transmute(&self)) }; self } /// Initialize the specified channel map for monaural audio and return a pointer to it. pub fn init_mono(&mut self) -> &mut Self { unsafe { capi::pa_channel_map_init_mono(std::mem::transmute(&self)) }; self } /// Initialize the specified channel map for stereophonic audio and return a pointer to it. pub fn init_stereo(&mut self) -> &mut Self { unsafe { capi::pa_channel_map_init_stereo(std::mem::transmute(&self)) }; self } /// Initialize the specified channel map for the specified number of channels using default /// labels and return a pointer to it. /// /// This call will fail (return `None`) if there is no default channel map known for this /// specific number of channels and mapping. pub fn init_auto(&mut self, channels: u32, def: MapDef) -> Option<&mut Self> { debug_assert!(channels as usize <= ::sample::CHANNELS_MAX); unsafe { if capi::pa_channel_map_init_auto(std::mem::transmute(&self), channels, def).is_null() { return None; } }; Some(self) } /// Similar to [`init_auto`](#method.init_auto) but instead of failing if no default mapping is /// known with the specified parameters it will synthesize a mapping based on a known mapping /// with fewer channels and fill up the rest with AUX0...AUX31 channels. pub fn init_extend(&mut self, channels: u32, def: MapDef) -> &mut Self { debug_assert!(channels as usize <= ::sample::CHANNELS_MAX); unsafe { capi::pa_channel_map_init_extend(std::mem::transmute(&self), channels, def) }; self } /// Make a human readable string from the map. pub fn print(&self) -> Option<String> { let tmp = unsafe { libc::malloc(PRINT_MAX) as *mut c_char }; if tmp.is_null() { return None; } unsafe { capi::pa_channel_map_snprint(tmp, PRINT_MAX, std::mem::transmute(self)); let ret = Some(CStr::from_ptr(tmp).to_string_lossy().into_owned()); libc::free(tmp as *mut libc::c_void); ret } } /// Compare whether or not two maps are equal. pub fn is_equal_to(&self, to: &Self) -> bool { unsafe { capi::pa_channel_map_equal(std::mem::transmute(self), std::mem::transmute(to)) == 1 } } /// Check whether or not the map is considered valid. pub fn is_valid(&self) -> bool { unsafe { capi::pa_channel_map_valid(std::mem::transmute(self)) != 0 } } /// Checks whether or not the specified map is compatible with the specified sample spec. pub fn is_compatible_with_sample_spec(&self, ss: &::sample::Spec) -> bool { unsafe { capi::pa_channel_map_compatible(std::mem::transmute(self), std::mem::transmute(ss)) != 0 } } /// Checks whether every channel defined in `of` is also defined in self. pub fn is_superset_of(&self, of: &Self) -> bool { unsafe { capi::pa_channel_map_superset(std::mem::transmute(self), std::mem::transmute(of)) != 0 } } /// Checks whether or not it makes sense to apply a volume "balance" with this mapping, i.e. if /// there are left/right channels available. pub fn can_balance(&self) -> bool { unsafe { capi::pa_channel_map_can_balance(std::mem::transmute(self)) != 0 } } /// Checks whether or not it makes sense to apply a volume "fade" (i.e. "balance" between front /// and rear) with this mapping, i.e. if there are front/rear channels available. pub fn can_fade(&self) -> bool { unsafe { capi::pa_channel_map_can_fade(std::mem::transmute(self)) != 0 } } /// Checks whether or not it makes sense to apply a volume "lfe balance" (i.e. "balance" between /// LFE and non-LFE channels) with this mapping, i.e. if there are LFE and non-LFE channels /// available. pub fn can_lfe_balance(&self) -> bool { unsafe { capi::pa_channel_map_can_lfe_balance(std::mem::transmute(self)) != 0 } } /// Tries to find a well-known channel mapping name for this channel mapping, i.e. "stereo", /// "surround-71" and so on. This name can be parsed with /// [`new_from_string`](#method.new_from_string). pub fn to_name(&self) -> Option<&'static CStr> { let ptr = unsafe { capi::pa_channel_map_to_name(std::mem::transmute(self)) }; if ptr.is_null() { return None; } Some(unsafe { CStr::from_ptr(ptr) }) } /// Similar to [`to_name`](#method.to_name), but returning prettier, human readable text labels, /// i.e. "Stereo", "Surround 7.1" and so on. pub fn to_pretty_name(&self) -> Option<&'static CStr> { let ptr = unsafe { capi::pa_channel_map_to_pretty_name(std::mem::transmute(self)) }; if ptr.is_null() { return None; } Some(unsafe { CStr::from_ptr(ptr) }) } /// Checks whether or not the specified channel position is available at least once in the map. pub fn has_position(&self, p: Position) -> bool { unsafe { capi::pa_channel_map_has_position(std::mem::transmute(self), p.into()) != 0 } } /// Generates a bit mask from a map. pub fn get_mask(&self) -> PositionMask { unsafe { capi::pa_channel_map_mask(std::mem::transmute(self)) } } }