fixes compile errors for coreaudio timestamp work

This commit is contained in:
JoshuaBatty 2020-05-01 15:19:28 +02:00
parent 54e5e95705
commit 590d37b103
2 changed files with 25 additions and 16 deletions

View File

@ -34,6 +34,7 @@ libc = "0.2.65"
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
coreaudio-rs = { version = "0.9.1", default-features = false, features = ["audio_unit", "core_audio"] } 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. 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] [target.'cfg(target_os = "emscripten")'.dependencies]
stdweb = { version = "0.1.3", default-features = false } stdweb = { version = "0.1.3", default-features = false }

View File

@ -483,7 +483,7 @@ impl Device {
config: &StreamConfig, config: &StreamConfig,
sample_format: SampleFormat, sample_format: SampleFormat,
mut data_callback: D, mut data_callback: D,
_error_callback: E, mut error_callback: E,
) -> Result<Stream, BuildStreamError> ) -> Result<Stream, BuildStreamError>
where where
D: FnMut(&Data, &InputCallbackInfo) + Send + 'static, D: FnMut(&Data, &InputCallbackInfo) + Send + 'static,
@ -646,7 +646,7 @@ impl Device {
// TODO: Perhaps loop over all buffers instead? // TODO: Perhaps loop over all buffers instead?
let AudioBuffer { let AudioBuffer {
mNumberChannels: _num_channels, mNumberChannels: channels,
mDataByteSize: data_byte_size, mDataByteSize: data_byte_size,
mData: data, mData: data,
} = buffers[0]; } = buffers[0];
@ -656,8 +656,14 @@ impl Device {
let data = Data::from_parts(data, len, sample_format); let data = Data::from_parts(data, len, sample_format);
// TODO: Need a better way to get delay, for now we assume a double-buffer offset. // TODO: Need a better way to get delay, for now we assume a double-buffer offset.
let callback = host_time_to_stream_instant(args.time_stamp.mHostTime); let callback = match host_time_to_stream_instant(args.time_stamp.mHostTime) {
let buffer_frames = len / channels; Err(err) => {
error_callback(err.into());
return Err(());
}
Ok(cb) => cb,
};
let buffer_frames = len / channels as usize;
let delay = frames_to_duration(buffer_frames, sample_rate); let delay = frames_to_duration(buffer_frames, sample_rate);
let capture = callback let capture = callback
.sub(delay) .sub(delay)
@ -683,7 +689,7 @@ impl Device {
config: &StreamConfig, config: &StreamConfig,
sample_format: SampleFormat, sample_format: SampleFormat,
mut data_callback: D, mut data_callback: D,
_error_callback: E, mut error_callback: E,
) -> Result<Stream, BuildStreamError> ) -> Result<Stream, BuildStreamError>
where where
D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static, D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static,
@ -718,9 +724,15 @@ impl Device {
let len = (data_byte_size as usize / bytes_per_channel) as usize; let len = (data_byte_size as usize / bytes_per_channel) as usize;
let mut data = Data::from_parts(data, len, sample_format); let mut data = Data::from_parts(data, len, sample_format);
let callback = host_time_to_stream_instant(args.time_stamp.mHostTime); let callback = match host_time_to_stream_instant(args.time_stamp.mHostTime) {
Err(err) => {
error_callback(err.into());
return Err(());
}
Ok(cb) => cb,
};
// TODO: Need a better way to get delay, for now we assume a double-buffer offset. // TODO: Need a better way to get delay, for now we assume a double-buffer offset.
let buffer_frames = len / channels; let buffer_frames = len / channels as usize;
let delay = frames_to_duration(buffer_frames, sample_rate); let delay = frames_to_duration(buffer_frames, sample_rate);
let playback = callback let playback = callback
.add(delay) .add(delay)
@ -745,17 +757,13 @@ impl Device {
fn host_time_to_stream_instant( fn host_time_to_stream_instant(
m_host_time: u64, m_host_time: u64,
) -> Result<crate::StreamInstant, BackendSpecificError> { ) -> Result<crate::StreamInstant, BackendSpecificError> {
let mut info: coreaudio::sys::mach_timebase_info_data_t = unimplemented!(); let mut info: mach::mach_time::mach_timebase_info = Default::default();
let res = coreaudio::sys::mach_timebase_info(&mut info); let res = unsafe { mach::mach_time::mach_timebase_info(&mut info) };
if res != SUCCESS { check_os_status(res)?;
let description = unimplemented!(); let nanos = m_host_time * info.numer as u64 / info.denom as u64;
let err = BackendSpecificError { description };
return Err(err.into());
}
let nanos = m_host_time * info.numer / info.denom;
let secs = nanos / 1_000_000_000; let secs = nanos / 1_000_000_000;
let subsec_nanos = nanos - secs * 1_000_000_000; let subsec_nanos = nanos - secs * 1_000_000_000;
crate::StreamInstant::new(secs, subsec_nanos) Ok(crate::StreamInstant::new(secs as i64, subsec_nanos as u32))
} }
// Convert the given duration in frames at the given sample rate to a `std::time::Duration`. // Convert the given duration in frames at the given sample rate to a `std::time::Duration`.