WIP Add timestamp implementation to ASIO backend

Currently unchecked and untested, though most of the groundwork should
be laid.
This commit is contained in:
mitchmindtree 2020-04-27 22:07:23 +02:00 • committed by JoshuaBatty
parent 4579daad7b
commit 22eef32898
2 changed files with 87 additions and 24 deletions

View file

@ -84,8 +84,15 @@ pub struct SampleRate {
pub rate: u32,
}
/// Information provided to the BufferCallback.
#[derive(Debug)]
pub struct CallbackInfo {
pub buffer_index: i32,
pub system_time: ai::ASIOTimeStamp,
}
/// Holds the pointer to the callbacks that come from cpal
struct BufferCallback(Box<dyn FnMut(i32) + Send>);
struct BufferCallback(Box<dyn FnMut(&CallbackInfo) + Send>);
/// Input and Output streams.
///
@ -355,9 +362,9 @@ impl Asio {
impl BufferCallback {
/// Calls the inner callback.
fn run(&mut self, index: i32) {
fn run(&mut self, callback_info: &CallbackInfo) {
let cb = &mut self.0;
cb(index);
cb(callback_info);
}
}
@ -610,7 +617,7 @@ impl Driver {
/// Returns an ID uniquely associated with the given callback so that it may be removed later.
pub fn add_callback<F>(&self, callback: F) -> CallbackId
where
F: 'static + FnMut(i32) + Send,
F: 'static + FnMut(&CallbackInfo) + Send,
{
let mut bc = BUFFER_CALLBACK.lock().unwrap();
let id = bc
@ -909,8 +916,15 @@ extern "C" fn buffer_switch_time_info(
) -> *mut ai::ASIOTime {
// This lock is probably unavoidable, but locks in the audio stream are not great.
let mut bcs = BUFFER_CALLBACK.lock().unwrap();
let time: &mut AsioTime = unsafe {
&mut *(time as *mut AsioTime)
};
let callback_info = CallbackInfo {
buffer_index: double_buffer_index,
system_time: time.time_info.system_time,
};
for &mut (_, ref mut bc) in bcs.iter_mut() {
bc.run(double_buffer_index);
bc.run(&callback_info);
}
time
}
@ -952,6 +966,10 @@ fn check_type_sizes() {
std::mem::size_of::<AsioTimeCode>(),
std::mem::size_of::<ai::ASIOTimeCode>()
);
assert_eq!(
std::mem::size_of::<AsioTimeInfo>(),
std::mem::size_of::<ai::AsioTimeInfo>(),
);
assert_eq!(
std::mem::size_of::<AsioTime>(),
std::mem::size_of::<ai::ASIOTime>()