#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
mod sys {
include!(concat!(env!("OUT_DIR"), "/link_rs.rs"));
}
use sys::*;
use std::os::raw::c_void;
pub struct Link {
wlink: *mut WLink,
}
impl Drop for Link {
fn drop(&mut self) {
unsafe { Link_destroy(self.wlink) }
}
}
impl Link {
pub fn new(bpm: f64) -> Link {
Link { wlink: unsafe { Link_create(bpm) } }
}
pub fn is_enabled(&self) -> bool {
unsafe { Link_isEnabled(self.wlink) }
}
pub fn enable(&mut self, enable: bool) {
unsafe { Link_enable(self.wlink, enable) }
}
pub fn is_start_stop_sync_enabled(&self) -> bool {
unsafe { Link_isStartStopSyncEnabled(self.wlink) }
}
pub fn enable_start_stop_sync(&mut self, enable: bool) {
unsafe { Link_enableStartStopSync(self.wlink, enable) }
}
pub fn num_peers(&self) -> usize {
unsafe { Link_numPeers(self.wlink) }
}
pub fn set_num_peers_callback(&mut self, callback: extern fn(usize)) {
unsafe {
let cb = callback as unsafe extern fn(usize);
Link_setNumPeersCallback(self.wlink, Some(cb));
}
}
pub fn set_tempo_callback(&mut self, callback: extern fn(f64)) {
unsafe {
let cb = callback as unsafe extern fn(f64);
Link_setTempoCallback(self.wlink, Some(cb));
}
}
pub fn set_start_stop_callback(&mut self, callback: extern fn(bool)) {
unsafe {
let cb = callback as unsafe extern fn(bool);
Link_setStartStopCallback(self.wlink, Some(cb));
}
}
pub fn clock(&self) -> Clock {
Clock { wc: unsafe { Link_clock(self.wlink) } }
}
pub fn with_audio_session_state<F>(&self, f: F)
where F: FnMut(SessionState)
{
let user_data = &f as *const _ as *mut c_void;
unsafe {
Link_withAudioSessionState(self.wlink, Some(closure_wrapper::<F>), user_data);
}
extern fn closure_wrapper<F>(closure: *mut c_void, wss: *mut WSessionState)
where F: FnMut(SessionState)
{
let opt_closure = closure as *mut Option<F>;
unsafe {
let mut fnx = (*opt_closure).take().unwrap();
let ss = SessionState { wss };
fnx(ss);
}
}
}
pub fn commit_audio_session_state(&mut self, ss: SessionState) {
unsafe { Link_commitAudioSessionState(self.wlink, ss.wss) }
}
pub fn with_app_session_state<F>(&self, f: F)
where F: FnMut(SessionState)
{
let user_data = &f as *const _ as *mut c_void;
unsafe {
Link_withAppSessionState(self.wlink, Some(closure_wrapper::<F>), user_data);
}
extern fn closure_wrapper<F>(closure: *mut c_void, wss: *mut WSessionState)
where F: FnMut(SessionState)
{
let opt_closure = closure as *mut Option<F>;
unsafe {
let mut fnx = (*opt_closure).take().unwrap();
let ss = SessionState { wss };
fnx(ss);
}
}
}
pub fn commit_app_session_state(&mut self, ss: SessionState) {
unsafe { Link_commitAppSessionState(self.wlink, ss.wss) }
}
}
pub struct SessionState {
wss: *mut WSessionState,
}
impl SessionState {
pub fn tempo(&self) -> f64 {
unsafe { SessionState_tempo(self.wss) }
}
pub fn set_tempo(&mut self, bpm: f64, at_time: i64) {
unsafe { SessionState_setTempo(self.wss, bpm, at_time) }
}
pub fn beat_at_time(&self, time: i64, quantum: f64) -> f64 {
unsafe { SessionState_beatAtTime(self.wss, time, quantum) }
}
pub fn phase_at_time(&self, time: i64, quantum: f64) -> f64 {
unsafe { SessionState_phaseAtTime(self.wss, time, quantum) }
}
pub fn time_at_beat(&self, beat: f64, quantum: f64) -> i64 {
unsafe { SessionState_timeAtBeat(self.wss, beat, quantum) }
}
pub fn request_beat_at_time(&mut self, beat: f64, time: i64, quantum: f64) {
unsafe { SessionState_requestBeatAtTime(self.wss, beat, time, quantum) }
}
pub fn force_beat_at_time(&mut self, beat: f64, time: i64, quantum: f64) {
unsafe { SessionState_forceBeatAtTime(self.wss, beat, time, quantum) }
}
pub fn set_is_playing(&mut self, is_playing: bool, time: i64) {
unsafe { SessionState_setIsPlaying(self.wss, is_playing, time) }
}
pub fn is_playing(&self) -> bool {
unsafe { SessionState_isPlaying(self.wss) }
}
pub fn time_for_is_playing(&self) -> i64 {
unsafe { SessionState_timeForIsPlaying(self.wss) }
}
pub fn request_beat_at_start_playing_time(&mut self, beat: f64, quantum: f64) {
unsafe { SessionState_requestBeatAtStartPlayingTime(self.wss, beat, quantum) }
}
pub fn set_is_playing_and_request_beat_at_time(&mut self,
is_playing: bool, time: i64, beat: f64, quantum: f64) {
unsafe { SessionState_setIsPlayingAndRequestBeatAtTime(self.wss,
is_playing, time, beat, quantum) }
}
}
pub struct Clock {
wc: *mut WClock,
}
impl Drop for Clock{
fn drop(&mut self) {
unsafe { Clock_destroy(self.wc) }
}
}
impl Clock {
#[cfg(target_os = "macos")]
pub fn ticks_to_micros(&self, ticks: u64) -> i64 {
unsafe { Clock_ticksToMicros(self.wc, ticks) }
}
#[cfg(target_os = "macos")]
pub fn micros_to_ticks(&self, micros: i64) -> u64 {
unsafe { Clock_microsToTicks(self.wc, micros) }
}
#[cfg(target_os = "macos")]
pub fn ticks(&self) -> u64 {
unsafe { Clock_ticks(self.wc) }
}
pub fn micros(&self) -> i64 {
unsafe { Clock_micros(self.wc) }
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}