Skip to content

Instantly share code, notes, and snippets.

@kelytha

kelytha/main.rs Secret

Created March 15, 2020 18:48
Show Gist options
  • Save kelytha/24dfda03ab7eca203d34119a462e429c to your computer and use it in GitHub Desktop.
Save kelytha/24dfda03ab7eca203d34119a462e429c to your computer and use it in GitHub Desktop.
alsa-rs sequencer bug
use alsa::Seq;
use alsa::seq::{PortInfo, Event, EvNote, EvCtrl, PortType, EventType, PortCap};
use std::ffi::CString;
use std::io::stdin;
use std::time::Duration;
use std::thread::sleep;
fn main() {
let alsa_seq = Seq::open(None, None, true).unwrap();
let mut portinfo = PortInfo::empty().unwrap();
let cstr = CString::new("OUT").unwrap();
portinfo.set_capability(PortCap::READ | PortCap::SUBS_READ);
portinfo.set_type(PortType::MIDI_GENERIC | PortType::APPLICATION);
portinfo.set_name(&cstr);
alsa_seq.create_port(&portinfo).unwrap();
let outport = portinfo.get_port();
println!("Connect a rawMIDI receiver and press enter to continue...");
stdin().read_line(&mut String::new()).unwrap();
let mut e = create_note_on(outport, 0, 8);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_note_off(outport, 0, 8);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_cc(outport, 0, 8, 127);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_cc(outport, 0, 8, 0);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_sysex_shift_on(outport);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_sysex_shift_off(outport);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_cc(outport, 0, 8, 127);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_cc(outport, 0, 8, 0);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_note_on(outport, 0, 8);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_note_off(outport, 0, 8);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_cc(outport, 0, 8, 127);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
e = create_cc(outport, 0, 8, 0);
alsa_seq.event_output_direct(&mut e).unwrap();
sleep(Duration::from_millis(100));
}
fn create_note_on<'a>(port: i32, channel: u8, note: u8) -> Event<'a> {
let note = EvNote {
channel,
note,
duration: 0,
velocity: 127,
off_velocity: 0,
};
let mut e = Event::new(EventType::Noteon, &note);
e.set_subs();
e.set_direct();
e.set_source(port);
e
}
fn create_note_off<'a>(port: i32, channel: u8, note: u8) -> Event<'a> {
let note = EvNote {
channel,
note,
duration: 0,
velocity: 0,
off_velocity: 0,
};
let mut e = Event::new(EventType::Noteon, &note);
e.set_subs();
e.set_direct();
e.set_source(port);
e
}
fn create_cc<'a>(port: i32, channel: u8, param: u32, value: i32) -> Event<'a> {
let cc = EvCtrl {
channel,
param,
value,
};
let mut e = Event::new(EventType::Controller, &cc);
e.set_subs();
e.set_direct();
e.set_source(port);
e
}
fn create_sysex_shift_on<'a>(port: i32) -> Event<'a> {
let data = [
0xf0, 0x00, 0x21, 0x09, 0x15, 0x00, 0x4d, 0x50, 0x00, 0x01, 0x4d, 0x01, 0xf7,
];
let mut e = Event::new_ext(EventType::Sysex, &data[..]);
e.set_subs();
e.set_direct();
e.set_source(port);
e.into_owned()
}
fn create_sysex_shift_off<'a>(port: i32) -> Event<'a> {
let data = [
0xf0, 0x00, 0x21, 0x09, 0x15, 0x00, 0x4d, 0x50, 0x00, 0x01, 0x4d, 0x00, 0xf7,
];
let mut e = Event::new_ext(EventType::Sysex, &data[..]);
e.set_subs();
e.set_direct();
e.set_source(port);
e.into_owned()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment