@@ -9,7 +9,7 @@ use std::ffi::{CStr, CString};
99use std:: os:: raw:: { c_char, c_double, c_void} ;
1010use std:: ptr:: null_mut;
1111use std:: sync:: {
12- atomic:: { AtomicBool , Ordering } ,
12+ atomic:: { AtomicBool , AtomicU32 , Ordering } ,
1313 Arc , Mutex , MutexGuard , Weak ,
1414} ;
1515
@@ -100,6 +100,7 @@ pub struct SampleRate {
100100pub struct CallbackInfo {
101101 pub buffer_index : i32 ,
102102 pub system_time : ai:: ASIOTimeStamp ,
103+ pub callback_flag : u32 ,
103104}
104105
105106/// Holds the pointer to the callbacks that come from cpal
@@ -319,6 +320,9 @@ pub struct CallbackId(usize);
319320/// parameters.
320321static BUFFER_CALLBACK : Mutex < Vec < ( CallbackId , BufferCallback ) > > = Mutex :: new ( Vec :: new ( ) ) ;
321322
323+ /// Used to identify when to clear buffers.
324+ static CALLBACK_FLAG : AtomicU32 = AtomicU32 :: new ( 0 ) ;
325+
322326/// Indicates that ASIOOutputReady should be called
323327static CALL_OUTPUT_READY : AtomicBool = AtomicBool :: new ( false ) ;
324328
@@ -1047,9 +1051,13 @@ extern "C" fn buffer_switch_time_info(
10471051 // This lock is probably unavoidable, but locks in the audio stream are not great.
10481052 let mut bcs = BUFFER_CALLBACK . lock ( ) . unwrap ( ) ;
10491053 let asio_time: & mut AsioTime = unsafe { & mut * ( time as * mut AsioTime ) } ;
1054+ // Alternates: 0, 1, 0, 1, ...
1055+ let callback_flag = CALLBACK_FLAG . fetch_xor ( 1 , Ordering :: Relaxed ) ;
1056+
10501057 let callback_info = CallbackInfo {
10511058 buffer_index : double_buffer_index,
10521059 system_time : asio_time. time_info . system_time ,
1060+ callback_flag,
10531061 } ;
10541062 for & mut ( _, ref mut bc) in bcs. iter_mut ( ) {
10551063 bc. run ( & callback_info) ;
0 commit comments