| 1 |
use cubeb_backend::{ChannelLayout, SampleFormat}; |
| 2 |
use std::mem; |
| 3 |
use std::os::raw::{c_int, c_void}; |
| 4 |
|
| 5 |
extern crate audio_mixer; |
| 6 |
pub use self::audio_mixer::Channel; |
| 7 |
|
| 8 |
const CHANNEL_ORDER: [audio_mixer::Channel; audio_mixer::Channel::count()] = [ |
| 9 |
audio_mixer::Channel::FrontLeft, |
| 10 |
audio_mixer::Channel::FrontRight, |
| 11 |
audio_mixer::Channel::FrontCenter, |
| 12 |
audio_mixer::Channel::LowFrequency, |
| 13 |
audio_mixer::Channel::BackLeft, |
| 14 |
audio_mixer::Channel::BackRight, |
| 15 |
audio_mixer::Channel::FrontLeftOfCenter, |
| 16 |
audio_mixer::Channel::FrontRightOfCenter, |
| 17 |
audio_mixer::Channel::BackCenter, |
| 18 |
audio_mixer::Channel::SideLeft, |
| 19 |
audio_mixer::Channel::SideRight, |
| 20 |
audio_mixer::Channel::TopCenter, |
| 21 |
audio_mixer::Channel::TopFrontLeft, |
| 22 |
audio_mixer::Channel::TopFrontCenter, |
| 23 |
audio_mixer::Channel::TopFrontRight, |
| 24 |
audio_mixer::Channel::TopBackLeft, |
| 25 |
audio_mixer::Channel::TopBackCenter, |
| 26 |
audio_mixer::Channel::TopBackRight, |
| 27 |
audio_mixer::Channel::Silence, |
| 28 |
audio_mixer::Channel::Discrete, |
| 29 |
]; |
| 30 |
|
| 31 |
pub fn get_channel_order(channel_layout: ChannelLayout) -> Vec<audio_mixer::Channel> { |
| 32 |
let mut map = channel_layout.bits(); |
| 33 |
let mut order = Vec::new(); |
| 34 |
let mut channel_index: usize = 0; |
| 35 |
while map != 0 { |
| 36 |
if map & 1 == 1 { |
| 37 |
order.push(CHANNEL_ORDER[channel_index]); |
| 38 |
} |
| 39 |
map >>= 1; |
| 40 |
channel_index += 1; |
| 41 |
} |
| 42 |
order |
| 43 |
} |
| 44 |
|
| 45 |
fn get_default_channel_order(channel_count: usize) -> Vec<audio_mixer::Channel> { |
| 46 |
assert_ne!(channel_count, 0); |
| 47 |
let mut channels = Vec::with_capacity(channel_count); |
| 48 |
for channel in CHANNEL_ORDER.iter().take(channel_count) { |
| 49 |
channels.push(*channel); |
| 50 |
} |
| 51 |
|
| 52 |
if channel_count > CHANNEL_ORDER.len() { |
| 53 |
channels.extend(vec![ |
| 54 |
audio_mixer::Channel::Silence; |
| 55 |
channel_count - CHANNEL_ORDER.len() |
| 56 |
]); |
| 57 |
} |
| 58 |
|
| 59 |
channels |
| 60 |
} |
| 61 |
|
| 62 |
#[derive(Debug)] |
| 63 |
enum MixerType { |
| 64 |
IntegerMixer(audio_mixer::Mixer<i16>), |
| 65 |
FloatMixer(audio_mixer::Mixer<f32>), |
| 66 |
} |
| 67 |
|
| 68 |
impl MixerType { |
| 69 |
fn new( |
| 70 |
format: SampleFormat, |
| 71 |
input_channels: &[audio_mixer::Channel], |
| 72 |
output_channels: &[audio_mixer::Channel], |
| 73 |
) -> Self { |
| 74 |
match format { |
| 75 |
SampleFormat::S16LE | SampleFormat::S16BE | SampleFormat::S16NE => { |
| 76 |
cubeb_log!("Create an integer type(i16) mixer"); |
| 77 |
Self::IntegerMixer(audio_mixer::Mixer::<i16>::new( |
| 78 |
input_channels, |
| 79 |
output_channels, |
| 80 |
)) |
| 81 |
} |
| 82 |
SampleFormat::Float32LE | SampleFormat::Float32BE | SampleFormat::Float32NE => { |
| 83 |
cubeb_log!("Create an floating type(f32) mixer"); |
| 84 |
Self::FloatMixer(audio_mixer::Mixer::<f32>::new( |
| 85 |
input_channels, |
| 86 |
output_channels, |
| 87 |
)) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
fn sample_size(&self) -> usize { |
| 93 |
match self { |
| 94 |
MixerType::IntegerMixer(_) => mem::size_of::<i16>(), |
| 95 |
MixerType::FloatMixer(_) => mem::size_of::<f32>(), |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
fn input_channels(&self) -> &[Channel] { |
| 100 |
match self { |
| 101 |
MixerType::IntegerMixer(m) => m.input_channels(), |
| 102 |
MixerType::FloatMixer(m) => m.input_channels(), |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
fn output_channels(&self) -> &[Channel] { |
| 107 |
match self { |
| 108 |
MixerType::IntegerMixer(m) => m.output_channels(), |
| 109 |
MixerType::FloatMixer(m) => m.output_channels(), |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
fn mix( |
| 114 |
&self, |
| 115 |
input_buffer_ptr: *const (), |
| 116 |
input_buffer_size: usize, |
| 117 |
output_buffer_ptr: *mut (), |
| 118 |
output_buffer_size: usize, |
| 119 |
frames: usize, |
| 120 |
) { |
| 121 |
use std::slice; |
| 122 |
|
| 123 |
// Check input buffer size. |
| 124 |
let size_needed = frames * self.input_channels().len() * self.sample_size(); |
| 125 |
assert!(input_buffer_size >= size_needed); |
| 126 |
// Check output buffer size. |
| 127 |
let size_needed = frames * self.output_channels().len() * self.sample_size(); |
| 128 |
assert!(output_buffer_size >= size_needed); |
| 129 |
|
| 130 |
match self { |
| 131 |
MixerType::IntegerMixer(m) => { |
| 132 |
let in_buf_ptr = input_buffer_ptr as *const i16; |
| 133 |
let out_buf_ptr = output_buffer_ptr as *mut i16; |
| 134 |
let input_buffer = unsafe { |
| 135 |
slice::from_raw_parts(in_buf_ptr, frames * self.input_channels().len()) |
| 136 |
}; |
| 137 |
let output_buffer = unsafe { |
| 138 |
slice::from_raw_parts_mut(out_buf_ptr, frames * self.output_channels().len()) |
| 139 |
}; |
| 140 |
let mut in_buf = input_buffer.chunks(self.input_channels().len()); |
| 141 |
let mut out_buf = output_buffer.chunks_mut(self.output_channels().len()); |
| 142 |
for _ in 0..frames { |
| 143 |
m.mix(in_buf.next().unwrap(), out_buf.next().unwrap()); |
| 144 |
} |
| 145 |
} |
| 146 |
MixerType::FloatMixer(m) => { |
| 147 |
let in_buf_ptr = input_buffer_ptr as *const f32; |
| 148 |
let out_buf_ptr = output_buffer_ptr as *mut f32; |
| 149 |
let input_buffer = unsafe { |
| 150 |
slice::from_raw_parts(in_buf_ptr, frames * self.input_channels().len()) |
| 151 |
}; |
| 152 |
let output_buffer = unsafe { |
| 153 |
slice::from_raw_parts_mut(out_buf_ptr, frames * self.output_channels().len()) |
| 154 |
}; |
| 155 |
let mut in_buf = input_buffer.chunks(self.input_channels().len()); |
| 156 |
let mut out_buf = output_buffer.chunks_mut(self.output_channels().len()); |
| 157 |
for _ in 0..frames { |
| 158 |
m.mix(in_buf.next().unwrap(), out_buf.next().unwrap()); |
| 159 |
} |
| 160 |
} |
| 161 |
}; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
#[derive(Debug)] |
| 166 |
pub struct Mixer { |
| 167 |
mixer: MixerType, |
| 168 |
// Only accessed from callback thread. |
| 169 |
buffer: Vec<u8>, |
| 170 |
} |
| 171 |
|
| 172 |
impl Mixer { |
| 173 |
pub fn new( |
| 174 |
format: SampleFormat, |
| 175 |
in_channel_count: usize, |
| 176 |
input_layout: ChannelLayout, |
| 177 |
out_channel_count: usize, |
| 178 |
mut output_channels: Vec<audio_mixer::Channel>, |
| 179 |
) -> Self { |
| 180 |
assert!(in_channel_count > 0); |
| 181 |
assert!(out_channel_count > 0); |
| 182 |
|
| 183 |
cubeb_log!( |
| 184 |
"Creating a mixer with input channel count: {}, input layout: {:?},\ |
| 185 |
out channel count: {}, output channels: {:?}", |
| 186 |
in_channel_count, |
| 187 |
input_layout, |
| 188 |
out_channel_count, |
| 189 |
output_channels |
| 190 |
); |
| 191 |
|
| 192 |
let input_channels = if in_channel_count as u32 != input_layout.bits().count_ones() { |
| 193 |
cubeb_log!( |
| 194 |
"Mismatch between input channels and layout. Applying default layout instead" |
| 195 |
); |
| 196 |
get_default_channel_order(in_channel_count) |
| 197 |
} else { |
| 198 |
get_channel_order(input_layout) |
| 199 |
}; |
| 200 |
|
| 201 |
// When having one or two channel, force mono or stereo. Some devices (namely, |
| 202 |
// Bose QC35, mark 1 and 2), expose a single channel mapped to the right for |
| 203 |
// some reason. Some devices (e.g., builtin speaker on MacBook Pro 2018) map |
| 204 |
// the channel layout to the undefined channels. |
| 205 |
if out_channel_count == 1 { |
| 206 |
output_channels = vec![audio_mixer::Channel::FrontCenter]; |
| 207 |
} else if out_channel_count == 2 { |
| 208 |
output_channels = vec![ |
| 209 |
audio_mixer::Channel::FrontLeft, |
| 210 |
audio_mixer::Channel::FrontRight, |
| 211 |
]; |
| 212 |
} |
| 213 |
|
| 214 |
let all_silence = vec![audio_mixer::Channel::Silence; out_channel_count]; |
| 215 |
if output_channels.is_empty() |
| 216 |
|| out_channel_count != output_channels.len() |
| 217 |
|| all_silence == output_channels |
| 218 |
|| Self::duplicate_channel_present(&output_channels) |
| 219 |
{ |
| 220 |
cubeb_log!("Use invalid layout. Apply default layout instead"); |
| 221 |
output_channels = get_default_channel_order(out_channel_count); |
| 222 |
} |
| 223 |
|
| 224 |
Self { |
| 225 |
mixer: MixerType::new(format, &input_channels, &output_channels), |
| 226 |
buffer: Vec::new(), |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
pub fn update_buffer_size(&mut self, frames: usize) -> bool { |
| 231 |
let size_needed = frames * self.mixer.input_channels().len() * self.mixer.sample_size(); |
| 232 |
let elements_needed = size_needed / mem::size_of::<u8>(); |
| 233 |
if self.buffer.len() < elements_needed { |
| 234 |
self.buffer.resize(elements_needed, 0); |
| 235 |
true |
| 236 |
} else { |
| 237 |
false |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
pub fn get_buffer_mut_ptr(&mut self) -> *mut u8 { |
| 242 |
self.buffer.as_mut_ptr() |
| 243 |
} |
| 244 |
|
| 245 |
// `update_buffer_size` must be called before this. |
| 246 |
pub fn mix(&self, frames: usize, dest_buffer: *mut c_void, dest_buffer_size: usize) -> c_int { |
| 247 |
let (src_buffer_ptr, src_buffer_size) = self.get_buffer_info(); |
| 248 |
self.mixer.mix( |
| 249 |
src_buffer_ptr as *const (), |
| 250 |
src_buffer_size, |
| 251 |
dest_buffer as *mut (), |
| 252 |
dest_buffer_size, |
| 253 |
frames, |
| 254 |
); |
| 255 |
0 |
| 256 |
} |
| 257 |
|
| 258 |
fn get_buffer_info(&self) -> (*const u8, usize) { |
| 259 |
( |
| 260 |
self.buffer.as_ptr(), |
| 261 |
self.buffer.len() * mem::size_of::<u8>(), |
| 262 |
) |
| 263 |
} |
| 264 |
|
| 265 |
fn duplicate_channel_present(channels: &[audio_mixer::Channel]) -> bool { |
| 266 |
let mut bitmap: u32 = 0; |
| 267 |
for channel in channels { |
| 268 |
if channel != &Channel::Silence && channel != &Channel::Discrete { |
| 269 |
if (bitmap & channel.bitmask()) != 0 { |
| 270 |
return true; |
| 271 |
} |
| 272 |
bitmap |= channel.bitmask(); |
| 273 |
} |
| 274 |
} |
| 275 |
false |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
// This test gives a clear channel order of the ChannelLayout passed from cubeb interface. |
| 280 |
#[test] |
| 281 |
fn test_get_channel_order() { |
| 282 |
assert_eq!( |
| 283 |
get_channel_order(ChannelLayout::MONO), |
| 284 |
[Channel::FrontCenter] |
| 285 |
); |
| 286 |
assert_eq!( |
| 287 |
get_channel_order(ChannelLayout::MONO_LFE), |
| 288 |
[Channel::FrontCenter, Channel::LowFrequency] |
| 289 |
); |
| 290 |
assert_eq!( |
| 291 |
get_channel_order(ChannelLayout::STEREO), |
| 292 |
[Channel::FrontLeft, Channel::FrontRight] |
| 293 |
); |
| 294 |
assert_eq!( |
| 295 |
get_channel_order(ChannelLayout::STEREO_LFE), |
| 296 |
[ |
| 297 |
Channel::FrontLeft, |
| 298 |
Channel::FrontRight, |
| 299 |
Channel::LowFrequency |
| 300 |
] |
| 301 |
); |
| 302 |
assert_eq!( |
| 303 |
get_channel_order(ChannelLayout::_3F), |
| 304 |
[ |
| 305 |
Channel::FrontLeft, |
| 306 |
Channel::FrontRight, |
| 307 |
Channel::FrontCenter |
| 308 |
] |
| 309 |
); |
| 310 |
assert_eq!( |
| 311 |
get_channel_order(ChannelLayout::_3F_LFE), |
| 312 |
[ |
| 313 |
Channel::FrontLeft, |
| 314 |
Channel::FrontRight, |
| 315 |
Channel::FrontCenter, |
| 316 |
Channel::LowFrequency |
| 317 |
] |
| 318 |
); |
| 319 |
assert_eq!( |
| 320 |
get_channel_order(ChannelLayout::_2F1), |
| 321 |
[Channel::FrontLeft, Channel::FrontRight, Channel::BackCenter] |
| 322 |
); |
| 323 |
assert_eq!( |
| 324 |
get_channel_order(ChannelLayout::_2F1_LFE), |
| 325 |
[ |
| 326 |
Channel::FrontLeft, |
| 327 |
Channel::FrontRight, |
| 328 |
Channel::LowFrequency, |
| 329 |
Channel::BackCenter |
| 330 |
] |
| 331 |
); |
| 332 |
assert_eq!( |
| 333 |
get_channel_order(ChannelLayout::_3F1), |
| 334 |
[ |
| 335 |
Channel::FrontLeft, |
| 336 |
Channel::FrontRight, |
| 337 |
Channel::FrontCenter, |
| 338 |
Channel::BackCenter |
| 339 |
] |
| 340 |
); |
| 341 |
assert_eq!( |
| 342 |
get_channel_order(ChannelLayout::_3F1_LFE), |
| 343 |
[ |
| 344 |
Channel::FrontLeft, |
| 345 |
Channel::FrontRight, |
| 346 |
Channel::FrontCenter, |
| 347 |
Channel::LowFrequency, |
| 348 |
Channel::BackCenter |
| 349 |
] |
| 350 |
); |
| 351 |
assert_eq!( |
| 352 |
get_channel_order(ChannelLayout::_2F2), |
| 353 |
[ |
| 354 |
Channel::FrontLeft, |
| 355 |
Channel::FrontRight, |
| 356 |
Channel::SideLeft, |
| 357 |
Channel::SideRight |
| 358 |
] |
| 359 |
); |
| 360 |
assert_eq!( |
| 361 |
get_channel_order(ChannelLayout::_2F2_LFE), |
| 362 |
[ |
| 363 |
Channel::FrontLeft, |
| 364 |
Channel::FrontRight, |
| 365 |
Channel::LowFrequency, |
| 366 |
Channel::SideLeft, |
| 367 |
Channel::SideRight |
| 368 |
] |
| 369 |
); |
| 370 |
assert_eq!( |
| 371 |
get_channel_order(ChannelLayout::QUAD), |
| 372 |
[ |
| 373 |
Channel::FrontLeft, |
| 374 |
Channel::FrontRight, |
| 375 |
Channel::BackLeft, |
| 376 |
Channel::BackRight |
| 377 |
] |
| 378 |
); |
| 379 |
assert_eq!( |
| 380 |
get_channel_order(ChannelLayout::QUAD_LFE), |
| 381 |
[ |
| 382 |
Channel::FrontLeft, |
| 383 |
Channel::FrontRight, |
| 384 |
Channel::LowFrequency, |
| 385 |
Channel::BackLeft, |
| 386 |
Channel::BackRight |
| 387 |
] |
| 388 |
); |
| 389 |
assert_eq!( |
| 390 |
get_channel_order(ChannelLayout::_3F2), |
| 391 |
[ |
| 392 |
Channel::FrontLeft, |
| 393 |
Channel::FrontRight, |
| 394 |
Channel::FrontCenter, |
| 395 |
Channel::SideLeft, |
| 396 |
Channel::SideRight |
| 397 |
] |
| 398 |
); |
| 399 |
assert_eq!( |
| 400 |
get_channel_order(ChannelLayout::_3F2_LFE), |
| 401 |
[ |
| 402 |
Channel::FrontLeft, |
| 403 |
Channel::FrontRight, |
| 404 |
Channel::FrontCenter, |
| 405 |
Channel::LowFrequency, |
| 406 |
Channel::SideLeft, |
| 407 |
Channel::SideRight |
| 408 |
] |
| 409 |
); |
| 410 |
assert_eq!( |
| 411 |
get_channel_order(ChannelLayout::_3F2_BACK), |
| 412 |
[ |
| 413 |
Channel::FrontLeft, |
| 414 |
Channel::FrontRight, |
| 415 |
Channel::FrontCenter, |
| 416 |
Channel::BackLeft, |
| 417 |
Channel::BackRight |
| 418 |
] |
| 419 |
); |
| 420 |
assert_eq!( |
| 421 |
get_channel_order(ChannelLayout::_3F2_LFE_BACK), |
| 422 |
[ |
| 423 |
Channel::FrontLeft, |
| 424 |
Channel::FrontRight, |
| 425 |
Channel::FrontCenter, |
| 426 |
Channel::LowFrequency, |
| 427 |
Channel::BackLeft, |
| 428 |
Channel::BackRight |
| 429 |
] |
| 430 |
); |
| 431 |
assert_eq!( |
| 432 |
get_channel_order(ChannelLayout::_3F3R_LFE), |
| 433 |
[ |
| 434 |
Channel::FrontLeft, |
| 435 |
Channel::FrontRight, |
| 436 |
Channel::FrontCenter, |
| 437 |
Channel::LowFrequency, |
| 438 |
Channel::BackCenter, |
| 439 |
Channel::SideLeft, |
| 440 |
Channel::SideRight |
| 441 |
] |
| 442 |
); |
| 443 |
assert_eq!( |
| 444 |
get_channel_order(ChannelLayout::_3F4_LFE), |
| 445 |
[ |
| 446 |
Channel::FrontLeft, |
| 447 |
Channel::FrontRight, |
| 448 |
Channel::FrontCenter, |
| 449 |
Channel::LowFrequency, |
| 450 |
Channel::BackLeft, |
| 451 |
Channel::BackRight, |
| 452 |
Channel::SideLeft, |
| 453 |
Channel::SideRight |
| 454 |
] |
| 455 |
); |
| 456 |
} |
| 457 |
|
| 458 |
#[test] |
| 459 |
fn test_get_default_channel_order() { |
| 460 |
for len in 1..CHANNEL_ORDER.len() + 10 { |
| 461 |
let channels = get_default_channel_order(len); |
| 462 |
if len <= CHANNEL_ORDER.len() { |
| 463 |
assert_eq!(channels, &CHANNEL_ORDER[..len]); |
| 464 |
} else { |
| 465 |
let silences = vec![audio_mixer::Channel::Silence; len - CHANNEL_ORDER.len()]; |
| 466 |
assert_eq!(channels[..CHANNEL_ORDER.len()], CHANNEL_ORDER); |
| 467 |
assert_eq!(&channels[CHANNEL_ORDER.len()..], silences.as_slice()); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
#[test] |
| 473 |
fn test_non_silent_duplicate_channels() { |
| 474 |
let duplicate = [ |
| 475 |
Channel::FrontLeft, |
| 476 |
Channel::Silence, |
| 477 |
Channel::FrontRight, |
| 478 |
Channel::FrontCenter, |
| 479 |
Channel::Silence, |
| 480 |
Channel::FrontRight, |
| 481 |
]; |
| 482 |
assert!(Mixer::duplicate_channel_present(&duplicate)); |
| 483 |
|
| 484 |
let non_duplicate = [ |
| 485 |
Channel::FrontLeft, |
| 486 |
Channel::Silence, |
| 487 |
Channel::FrontRight, |
| 488 |
Channel::FrontCenter, |
| 489 |
Channel::Silence, |
| 490 |
Channel::Silence, |
| 491 |
]; |
| 492 |
assert!(!Mixer::duplicate_channel_present(&non_duplicate)); |
| 493 |
|
| 494 |
let duplicate = [ |
| 495 |
Channel::FrontLeft, |
| 496 |
Channel::Discrete, |
| 497 |
Channel::FrontRight, |
| 498 |
Channel::FrontCenter, |
| 499 |
Channel::Discrete, |
| 500 |
Channel::FrontRight, |
| 501 |
]; |
| 502 |
assert!(Mixer::duplicate_channel_present(&duplicate)); |
| 503 |
|
| 504 |
let non_duplicate = [ |
| 505 |
Channel::FrontLeft, |
| 506 |
Channel::Discrete, |
| 507 |
Channel::FrontRight, |
| 508 |
Channel::FrontCenter, |
| 509 |
Channel::Discrete, |
| 510 |
Channel::Discrete, |
| 511 |
]; |
| 512 |
assert!(!Mixer::duplicate_channel_present(&non_duplicate)); |
| 513 |
} |