-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from l0calh05t/midi
Add bindings for `libraries/Midi/Midi_c.h`
- Loading branch information
Showing
6 changed files
with
158 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
|
||
.vscode | ||
**/*.rs.bk | ||
Cargo.lock | ||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//! Very basic program to test MIDI bindings of the Bela | ||
|
||
extern crate bela_sys; | ||
extern crate libc; | ||
extern crate nix; | ||
|
||
use bela_sys::midi::*; | ||
use bela_sys::{BelaContext, BelaInitSettings}; | ||
use nix::sys::signal; | ||
use std::{mem, ptr, thread, time}; | ||
|
||
static mut MIDI: *mut Midi = ptr::null_mut(); | ||
|
||
extern "C" fn setup(_context: *mut BelaContext, _user_data: *mut std::os::raw::c_void) -> bool { | ||
unsafe { | ||
assert!(MIDI.is_null()); | ||
MIDI = Midi_new(b"hw:0,0,0\0".as_ptr()); | ||
} | ||
true | ||
} | ||
|
||
extern "C" fn cleanup(_context: *mut BelaContext, _user_data: *mut std::os::raw::c_void) { | ||
unsafe { | ||
Midi_delete(MIDI); | ||
MIDI = ptr::null_mut(); | ||
} | ||
} | ||
|
||
extern "C" fn render(_context: *mut BelaContext, _user_data: *mut std::os::raw::c_void) { | ||
unsafe { | ||
if MIDI.is_null() { | ||
return; | ||
} | ||
|
||
while Midi_availableMessages(MIDI) > 0 { | ||
let mut msg_buf = [0u8; 3]; | ||
let msg_len = Midi_getMessage(MIDI, msg_buf.as_mut_ptr()) as usize; | ||
assert!(msg_len <= 3); | ||
if msg_len != 0 && (msg_buf[0] & 0b1111_0000) == 0b1001_0000 { | ||
bela_sys::rt_printf(b"Note on\n\0".as_ptr()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
extern "C" fn signal_handler(_signal: std::os::raw::c_int) { | ||
unsafe { | ||
bela_sys::Bela_requestStop(); | ||
} | ||
} | ||
|
||
fn main() { | ||
unsafe { | ||
let mut settings = { | ||
let mut settings: mem::MaybeUninit<BelaInitSettings> = mem::MaybeUninit::uninit(); | ||
bela_sys::Bela_defaultSettings(settings.as_mut_ptr()); | ||
settings.assume_init() | ||
}; | ||
bela_sys::Bela_setVerboseLevel(1); | ||
settings.setup = Some(setup); | ||
settings.render = Some(render); | ||
settings.cleanup = Some(cleanup); | ||
settings.verbose = 1; | ||
settings.highPerformanceMode = 1; | ||
settings.analogOutputsPersist = 0; | ||
settings.uniformSampleRate = 1; | ||
|
||
if bela_sys::Bela_initAudio(&mut settings, ptr::null_mut()) != 0 { | ||
println!("settings.render: {:?}", &settings.render); | ||
panic!("lol fail"); | ||
} | ||
|
||
if bela_sys::Bela_startAudio() != 0 { | ||
println!("Unable to start real-time audio"); | ||
bela_sys::Bela_stopAudio(); | ||
bela_sys::Bela_cleanupAudio(); | ||
panic!("Aborting"); | ||
} | ||
|
||
let handler = signal::SigHandler::Handler(signal_handler); | ||
let flags = signal::SaFlags::empty(); | ||
let set = signal::SigSet::empty(); | ||
let sig_action = signal::SigAction::new(handler, flags, set); | ||
|
||
signal::sigaction(signal::SIGINT, &sig_action).unwrap(); | ||
signal::sigaction(signal::SIGTERM, &sig_action).unwrap(); | ||
|
||
while bela_sys::Bela_stopRequested() == 0 { | ||
thread::sleep(time::Duration::new(1, 0)); | ||
} | ||
|
||
bela_sys::Bela_stopAudio(); | ||
bela_sys::Bela_cleanupAudio(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)] | ||
|
||
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); | ||
|
||
#[cfg(feature = "midi")] | ||
pub mod midi { | ||
include!(concat!(env!("OUT_DIR"), "/midi_bindings.rs")); | ||
} |