summaryrefslogtreecommitdiffstats
path: root/third_party/rust/coremidi/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/coremidi/examples
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/coremidi/examples')
-rw-r--r--third_party/rust/coremidi/examples/endpoints.rs24
-rw-r--r--third_party/rust/coremidi/examples/notifications.rs31
-rw-r--r--third_party/rust/coremidi/examples/properties.rs25
-rw-r--r--third_party/rust/coremidi/examples/receive.rs71
-rw-r--r--third_party/rust/coremidi/examples/send.rs85
-rw-r--r--third_party/rust/coremidi/examples/virtual-destination.rs20
-rw-r--r--third_party/rust/coremidi/examples/virtual-source.rs32
7 files changed, 288 insertions, 0 deletions
diff --git a/third_party/rust/coremidi/examples/endpoints.rs b/third_party/rust/coremidi/examples/endpoints.rs
new file mode 100644
index 0000000000..1411d1d2aa
--- /dev/null
+++ b/third_party/rust/coremidi/examples/endpoints.rs
@@ -0,0 +1,24 @@
+extern crate coremidi;
+
+fn main() {
+ println!("System destinations:");
+
+ for (i, destination) in coremidi::Destinations.into_iter().enumerate() {
+ let display_name = get_display_name(&destination);
+ println!("[{}] {}", i, display_name);
+ }
+
+ println!();
+ println!("System sources:");
+
+ for (i, source) in coremidi::Sources.into_iter().enumerate() {
+ let display_name = get_display_name(&source);
+ println!("[{}] {}", i, display_name);
+ }
+}
+
+fn get_display_name(endpoint: &coremidi::Endpoint) -> String {
+ endpoint
+ .display_name()
+ .unwrap_or_else(|| "[Unknown Display Name]".to_string())
+}
diff --git a/third_party/rust/coremidi/examples/notifications.rs b/third_party/rust/coremidi/examples/notifications.rs
new file mode 100644
index 0000000000..ef58c43d26
--- /dev/null
+++ b/third_party/rust/coremidi/examples/notifications.rs
@@ -0,0 +1,31 @@
+extern crate core_foundation;
+extern crate coremidi;
+
+use coremidi::{Client, Notification};
+
+use core_foundation::runloop::{kCFRunLoopDefaultMode, CFRunLoopRunInMode};
+
+fn main() {
+ println!("Logging MIDI Client Notifications");
+ println!("Will Quit Automatically After 10 Seconds");
+ println!();
+
+ let _client = Client::new_with_notifications("example-client", print_notification).unwrap();
+
+ // As the MIDIClientCreate docs say (https://developer.apple.com/documentation/coremidi/1495360-midiclientcreate),
+ // notifications will be delivered on the run loop that was current when
+ // Client was created.
+ //
+ // In order to actually receive the notifications, a run loop must be
+ // running. Since this sample app does not use an app framework like
+ // UIApplication or NSApplication, it does not have a run loop running yet.
+ // So we start one that lasts for 10 seconds with the following line.
+ //
+ // You may not have to do this in your app - see https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW24
+ // for information about when run loops are running automatically.
+ unsafe { CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, 0) };
+}
+
+fn print_notification(notification: &Notification) {
+ println!("Received Notification: {:?} \r", notification);
+}
diff --git a/third_party/rust/coremidi/examples/properties.rs b/third_party/rust/coremidi/examples/properties.rs
new file mode 100644
index 0000000000..b673a22d9b
--- /dev/null
+++ b/third_party/rust/coremidi/examples/properties.rs
@@ -0,0 +1,25 @@
+extern crate coremidi;
+
+use coremidi::{Client, PacketList, Properties, PropertyGetter, PropertySetter};
+
+fn main() {
+ let client = Client::new("Example Client").unwrap();
+
+ let callback = |packet_list: &PacketList| {
+ println!("{}", packet_list);
+ };
+
+ // Creates a virtual destination, then gets its properties
+ let destination = client
+ .virtual_destination("Example Destination", callback)
+ .unwrap();
+
+ println!("Created Virtual Destination...");
+
+ // How to get a property
+ let name: String = Properties::name().value_from(&destination).unwrap();
+ println!("With Name: {}", name);
+
+ // How to set a property
+ Properties::private().set_value(&destination, true).unwrap();
+}
diff --git a/third_party/rust/coremidi/examples/receive.rs b/third_party/rust/coremidi/examples/receive.rs
new file mode 100644
index 0000000000..93bb9c23f2
--- /dev/null
+++ b/third_party/rust/coremidi/examples/receive.rs
@@ -0,0 +1,71 @@
+extern crate coremidi;
+
+use std::env;
+
+fn main() {
+ let source_index = get_source_index();
+ println!("Source index: {}", source_index);
+
+ let source = coremidi::Source::from_index(source_index).unwrap();
+ println!("Source display name: {}", source.display_name().unwrap());
+
+ let client = coremidi::Client::new("Example Client").unwrap();
+
+ let callback = |packet_list: &coremidi::PacketList| {
+ println!("{}", packet_list);
+ };
+
+ let input_port = client.input_port("Example Port", callback).unwrap();
+ input_port.connect_source(&source).unwrap();
+
+ let mut input_line = String::new();
+ println!("Press Enter to Finish");
+ std::io::stdin()
+ .read_line(&mut input_line)
+ .expect("Failed to read line");
+
+ input_port.disconnect_source(&source).unwrap();
+}
+
+fn get_source_index() -> usize {
+ let mut args_iter = env::args();
+ let tool_name = args_iter
+ .next()
+ .and_then(|path| {
+ path.split(std::path::MAIN_SEPARATOR)
+ .last()
+ .map(|v| v.to_string())
+ })
+ .unwrap_or_else(|| "receive".to_string());
+
+ match args_iter.next() {
+ Some(arg) => match arg.parse::<usize>() {
+ Ok(index) => {
+ if index >= coremidi::Sources::count() {
+ println!("Source index out of range: {}", index);
+ std::process::exit(-1);
+ }
+ index
+ }
+ Err(_) => {
+ println!("Wrong source index: {}", arg);
+ std::process::exit(-1);
+ }
+ },
+ None => {
+ println!("Usage: {} <source-index>", tool_name);
+ println!();
+ println!("Available Sources:");
+ print_sources();
+ std::process::exit(-1);
+ }
+ }
+}
+
+fn print_sources() {
+ for (i, source) in coremidi::Sources.into_iter().enumerate() {
+ if let Some(display_name) = source.display_name() {
+ println!("[{}] {}", i, display_name)
+ }
+ }
+}
diff --git a/third_party/rust/coremidi/examples/send.rs b/third_party/rust/coremidi/examples/send.rs
new file mode 100644
index 0000000000..804710c734
--- /dev/null
+++ b/third_party/rust/coremidi/examples/send.rs
@@ -0,0 +1,85 @@
+extern crate coremidi;
+
+use std::env;
+use std::thread;
+use std::time::Duration;
+
+fn main() {
+ let destination_index = get_destination_index();
+ println!("Destination index: {}", destination_index);
+
+ let destination = coremidi::Destination::from_index(destination_index).unwrap();
+ println!(
+ "Destination display name: {}",
+ destination.display_name().unwrap()
+ );
+
+ let client = coremidi::Client::new("Example Client").unwrap();
+ let output_port = client.output_port("Example Port").unwrap();
+
+ let note_on = create_note_on(0, 64, 127);
+ let note_off = create_note_off(0, 64, 127);
+
+ for i in 0..10 {
+ println!("[{}] Sending note ...", i);
+
+ output_port.send(&destination, &note_on).unwrap();
+
+ thread::sleep(Duration::from_millis(1000));
+
+ output_port.send(&destination, &note_off).unwrap();
+ }
+}
+
+fn get_destination_index() -> usize {
+ let mut args_iter = env::args();
+ let tool_name = args_iter
+ .next()
+ .and_then(|path| {
+ path.split(std::path::MAIN_SEPARATOR)
+ .last()
+ .map(|v| v.to_string())
+ })
+ .unwrap_or_else(|| "send".to_string());
+
+ match args_iter.next() {
+ Some(arg) => match arg.parse::<usize>() {
+ Ok(index) => {
+ if index >= coremidi::Destinations::count() {
+ println!("Destination index out of range: {}", index);
+ std::process::exit(-1);
+ }
+ index
+ }
+ Err(_) => {
+ println!("Wrong destination index: {}", arg);
+ std::process::exit(-1);
+ }
+ },
+ None => {
+ println!("Usage: {} <destination-index>", tool_name);
+ println!();
+ println!("Available Destinations:");
+ print_destinations();
+ std::process::exit(-1);
+ }
+ }
+}
+
+fn print_destinations() {
+ for (i, destination) in coremidi::Destinations.into_iter().enumerate() {
+ if let Some(display_name) = destination.display_name() {
+ println!("[{}] {}", i, display_name)
+ }
+ }
+}
+
+fn create_note_on(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
+ let data = &[0x90 | (channel & 0x0f), note & 0x7f, velocity & 0x7f];
+ coremidi::PacketBuffer::new(0, data)
+}
+
+fn create_note_off(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
+ let data = &[0x80 | (channel & 0x0f), note & 0x7f, velocity & 0x7f];
+ coremidi::PacketBuffer::new(0, data)
+}
diff --git a/third_party/rust/coremidi/examples/virtual-destination.rs b/third_party/rust/coremidi/examples/virtual-destination.rs
new file mode 100644
index 0000000000..989dcebb46
--- /dev/null
+++ b/third_party/rust/coremidi/examples/virtual-destination.rs
@@ -0,0 +1,20 @@
+extern crate coremidi;
+
+fn main() {
+ let client = coremidi::Client::new("Example Client").unwrap();
+
+ let callback = |packet_list: &coremidi::PacketList| {
+ println!("{}", packet_list);
+ };
+
+ let _destination = client
+ .virtual_destination("Example Destination", callback)
+ .unwrap();
+
+ let mut input_line = String::new();
+ println!("Created Virtual Destination \"Example Destination\"");
+ println!("Press Enter to Finish");
+ std::io::stdin()
+ .read_line(&mut input_line)
+ .expect("Failed to read line");
+}
diff --git a/third_party/rust/coremidi/examples/virtual-source.rs b/third_party/rust/coremidi/examples/virtual-source.rs
new file mode 100644
index 0000000000..28867162ce
--- /dev/null
+++ b/third_party/rust/coremidi/examples/virtual-source.rs
@@ -0,0 +1,32 @@
+extern crate coremidi;
+
+use std::thread;
+use std::time::Duration;
+
+fn main() {
+ let client = coremidi::Client::new("Example Client").unwrap();
+ let source = client.virtual_source("Example Source").unwrap();
+
+ let note_on = create_note_on(0, 64, 127);
+ let note_off = create_note_off(0, 64, 127);
+
+ for i in 0..10 {
+ println!("[{}] Sending note...", i);
+
+ source.received(&note_on).unwrap();
+
+ thread::sleep(Duration::from_millis(1000));
+
+ source.received(&note_off).unwrap();
+ }
+}
+
+fn create_note_on(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
+ let data = &[0x90 | (channel & 0x0f), note & 0x7f, velocity & 0x7f];
+ coremidi::PacketBuffer::new(0, data)
+}
+
+fn create_note_off(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
+ let data = &[0x80 | (channel & 0x0f), note & 0x7f, velocity & 0x7f];
+ coremidi::PacketBuffer::new(0, data)
+}