Skip to content

Commit

Permalink
Merge pull request RustAudio#397 from mitchmindtree/timestamp
Browse files Browse the repository at this point in the history
[WIP] Timestamp and `StreamInstant` APIs
  • Loading branch information
mitchmindtree authored May 5, 2020
2 parents e4eb716 + 46afc8f commit fe22704
Show file tree
Hide file tree
Showing 9 changed files with 620 additions and 86 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ hound = "3.4"
ringbuf = "0.1.6"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["audiosessiontypes", "audioclient", "coml2api", "combaseapi", "debug", "devpkey", "handleapi", "ksmedia", "mmdeviceapi", "objbase", "std", "synchapi", "winbase", "winuser"] }
winapi = { version = "0.3", features = ["audiosessiontypes", "audioclient", "coml2api", "combaseapi", "debug", "devpkey", "handleapi", "ksmedia", "mmdeviceapi", "objbase", "profileapi", "std", "synchapi", "winbase", "winuser"] }
asio-sys = { version = "0.1", path = "asio-sys", optional = true }
parking_lot = "0.9"

Expand All @@ -34,6 +34,7 @@ libc = "0.2.65"
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
coreaudio-rs = { version = "0.9.1", default-features = false, features = ["audio_unit", "core_audio"] }
core-foundation-sys = "0.6.2" # For linking to CoreFoundation.framework and handling device name `CFString`s.
mach = "0.3" # For access to mach_timebase type.

[target.'cfg(target_os = "emscripten")'.dependencies]
stdweb = { version = "0.1.3", default-features = false }
28 changes: 22 additions & 6 deletions asio-sys/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -889,7 +896,7 @@ extern "C" fn asio_message(
// Informs the driver whether the application is interested in time code info. If an
// application does not need to know about time code, the driver has less work to do.
// TODO: Provide an option for this?
0
1
}

_ => 0, // Unknown/unhandled message type.
Expand All @@ -909,8 +916,13 @@ 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 asio_time: &mut AsioTime = unsafe { &mut *(time as *mut AsioTime) };
let callback_info = CallbackInfo {
buffer_index: double_buffer_index,
system_time: asio_time.time_info.system_time,
};
for &mut (_, ref mut bc) in bcs.iter_mut() {
bc.run(double_buffer_index);
bc.run(&callback_info);
}
time
}
Expand Down Expand Up @@ -952,6 +964,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>()
Expand Down
Loading

0 comments on commit fe22704

Please sign in to comment.