lv2-midi-1.2.0/.cargo_vcs_info.json0000644000000001121374457221200125160ustar { "git": { "sha1": "c93bfe859a849c5189a27c62c659bd3b724a39b6" } } lv2-midi-1.2.0/Cargo.toml0000644000000023061374457221200105230ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "lv2-midi" version = "1.2.0" authors = ["Jan-Oliver 'Janonard' Opdenhövel "] description = "rust-lv2's MIDI processing library" readme = "README.md" license = "MIT OR Apache-2.0" repository = "https://github.com/RustAudio/rust-lv2" [dependencies.lv2-atom] version = "2.0.0" [dependencies.lv2-sys] version = "2.0.0" [dependencies.urid] version = "0.1.0" [dependencies.wmidi] version = "3.1.0" optional = true [dev-dependencies.lv2-core] version = "3.0.0" [dev-dependencies.lv2-units] version = "0.1.3" [badges.maintenance] status = "passively-maintained" [badges.travis-ci] branch = "master" repository = "RustAudio/rust-lv2" lv2-midi-1.2.0/Cargo.toml.orig010064400017500001750000000011431374457150200142140ustar 00000000000000[package] name = "lv2-midi" version = "1.2.0" authors = ["Jan-Oliver 'Janonard' Opdenhövel "] edition = "2018" license = "MIT OR Apache-2.0" description = "rust-lv2's MIDI processing library" readme = "README.md" repository = "https://github.com/RustAudio/rust-lv2" [badges] travis-ci = { repository = "RustAudio/rust-lv2", branch = "master" } maintenance = { status = "passively-maintained" } [dependencies] lv2-atom = "2.0.0" lv2-sys = "2.0.0" urid = "0.1.0" [dev-dependencies] lv2-units = "0.1.3" lv2-core = "3.0.0" [dependencies.wmidi] version = "3.1.0" optional = truelv2-midi-1.2.0/LICENSE-APACHE010064400017500001731000000251371364504410600132540ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. lv2-midi-1.2.0/LICENSE-MIT010064400017500001731000000017761364504410600127670ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.lv2-midi-1.2.0/README.md010064400017500001750000000031141374457150200126040ustar 00000000000000## Rust-LV2's MIDI processing library. The MIDI processing library of [`rust-lv2`](https://crates.io/crates/lv2), a safe, fast, and ergonomic framework to create [LV2 plugins](http://lv2plug.in/) for audio processing, written in Rust. This is a addon to [`lv2-atom`](https://crates.io/crates/lv2-atom) that adds the `MidiEvent` atom type. There is also an optional dependency to [`wmidi`](https://crates.io/crates/wmidi), which introduces the `WMidiEvent` atom type, which allows you to directly read and write the events defined by `wmidi`. ## Documentation The original LV2 API (in the `C` programming language) is documented by ["the LV2 book"](https://lv2plug.in/book/). This book is in the process of being translated to Rust along with the development of `rust-lv2` [(link)](https://janonard.github.io/rust-lv2-book/) and describes how to properly use `rust-lv2`. ## Features There are two optional features: * `host`: Some of the types defined by some crates are only useful for testing or LV2 hosts. Since the goal of this framework is to provide an easy way to create plugins, these aren't necessary and therefore gated behind that feature. * `wmidi`: Add [`wmidi`](https://crates.io/crates/wmidi) as an optional dependency to `lv2-midi`, which enables a shortcut to read and write MIDI events directly with the types defined by this crate. ## License Licensed under either of * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) at your option.lv2-midi-1.2.0/src/lib.rs010064400017500001731000000067761364504410600132430ustar 00000000000000//! MIDI message extension for `lv2_atom`. //! //! This crate adds a new atom type for the `lv2_atom` crate: The `MidiEvent`, a message conformant to the [MIDI specification](https://www.midi.org/specifications-old/item/the-midi-1-0-specification). Due to the one-crate-per-spec policy of the `rust-lv2` project, this relatively small crate isn't integrated into the main atom crate. //! //! If compiled with the optional `wmidi` dependency, the crate also has an additional module containing the `WMidiEvent`. This atom uses the `MidiMessage` type defined in by `wmidi` instead of byte slices. //! //! # Example //! //! This example showcases a MIDI event processor that modulates every played note up a forth, using the `wmidi` optional dependency. //! //! ``` //! use lv2_core::prelude::*; //! use lv2_atom::prelude::*; //! use lv2_midi::prelude::*; //! use lv2_units::prelude::*; //! use wmidi::*; //! use urid::*; //! //! #[derive(URIDCollection)] //! struct MyURIDs { //! atom: AtomURIDCollection, //! midi: MidiURIDCollection, //! units: UnitURIDCollection, //! } //! //! #[derive(PortCollection)] //! struct MyPorts { //! input: InputPort, //! output: OutputPort, //! } //! //! /// Something like a plugin's run method. //! fn run(ports: &mut MyPorts, urids: MyURIDs) { //! // Retrieving the input and output sequence. //! let input_sequence = ports.input.read(urids.atom.sequence, urids.units.beat).unwrap(); //! let mut output_sequence = ports.output.init( //! urids.atom.sequence, //! TimeStampURID::Frames(urids.units.frame) //! ).unwrap(); //! //! for (timestamp, atom) in input_sequence { //! // If the atom encodes a message... //! if let Some(message) = atom.read(urids.midi.wmidi, ()) { //! // Calculate the message to send. //! let message_to_send = match message { //! MidiMessage::NoteOn(channel, note, velocity) => { //! let note = note.step(5).unwrap(); // modulate up five half-steps. //! MidiMessage::NoteOn(channel, note, velocity) //! }, //! MidiMessage::NoteOff(channel, note, velocity) => { //! let note = note.step(5).unwrap(); // modulate up five half-steps. //! MidiMessage::NoteOff(channel, note, velocity) //! } //! _ => message, //! }; //! // Write the modulated message or forward it. //! output_sequence.init(timestamp, urids.midi.wmidi, message_to_send); //! } else { //! // Forward the other, uninterpreted message. //! output_sequence.forward(timestamp, atom); //! } //! } //! } //! ``` extern crate lv2_atom as atom; extern crate lv2_sys as sys; use urid::*; pub mod raw; #[cfg(feature = "wmidi")] pub mod wmidi_binding; /// Collection with the URIDs of all `UriBound`s in this crate. #[derive(URIDCollection)] pub struct MidiURIDCollection { pub raw: URID, #[cfg(feature = "wmidi")] pub wmidi: URID, #[cfg(feature = "wmidi")] pub sysex_wmidi: URID, } /// Prelude for wildcard use, containing many important types. pub mod prelude { pub use crate::raw::MidiEvent; #[cfg(feature = "wmidi")] pub use crate::wmidi_binding::SystemExclusiveWMidiEvent; #[cfg(feature = "wmidi")] pub use crate::wmidi_binding::WMidiEvent; pub use crate::MidiURIDCollection; } lv2-midi-1.2.0/src/raw.rs010064400017500001731000000020331364504410600132440ustar 00000000000000//! Atom types to give raw access to MIDI messages. //! //! This implementation is very low-level; Basically an alias for a chunk. It should only be used by those who don't want additional dependencies or want to modify messages directly. //! //! If you just want to use MIDI messages in your plugin, you should use the optional `wmidi` feature. use atom::prelude::*; use urid::*; /// Midi Event. /// /// This low-level implementation is basically the same as a chunk atom: It reads a slice of bytes and writes with a `ByteWriter`. pub struct MidiEvent; unsafe impl UriBound for MidiEvent { const URI: &'static [u8] = sys::LV2_MIDI__MidiEvent; } impl<'a, 'b> Atom<'a, 'b> for MidiEvent where 'a: 'b, { type ReadParameter = (); type ReadHandle = &'a [u8]; type WriteParameter = (); type WriteHandle = FramedMutSpace<'a, 'b>; fn read(body: Space<'a>, _: ()) -> Option<&'a [u8]> { body.data() } fn init(frame: FramedMutSpace<'a, 'b>, _: ()) -> Option> { Some(frame) } } lv2-midi-1.2.0/src/wmidi_binding.rs010064400017500001731000000150361364504410600152650ustar 00000000000000//! Atom types to make direct usage of the `wmidi` crate. //! //! This is the high-level and recommended module for MIDI message handling. The contained atom type can convert the raw MIDI message to rustic enumerations and back. //! //! If you want to have raw, low-level access to the messages, you should use the [raw module](../raw/index.html). use atom::prelude::*; use std::convert::TryFrom; use urid::*; /// Midi event. /// /// This atom implementation makes direct use of the `wmidi::MidiMessage` type as it's reading handle as well as it's writing parameter. /// /// This means that the message has to be fully constructed before it can be written as an atom. For most messages this isn't a problem since they have a static size, but system exclusive messages have a dynamic size. /// /// If you want to write system exclusive messages, you should use the [`SystemExclusiveWMidiEvent`](struct.SystemExclusiveWMidiEvent.html). It can read any message, but specializes on writing system exclusive messages with a custom writer. pub struct WMidiEvent; unsafe impl UriBound for WMidiEvent { const URI: &'static [u8] = sys::LV2_MIDI__MidiEvent; } impl<'a, 'b> Atom<'a, 'b> for WMidiEvent where 'a: 'b, { type ReadParameter = (); type ReadHandle = wmidi::MidiMessage<'a>; type WriteParameter = wmidi::MidiMessage<'static>; type WriteHandle = (); fn read(space: Space<'a>, _: ()) -> Option> { space .data() .and_then(|bytes| wmidi::MidiMessage::try_from(bytes).ok()) } fn init(mut frame: FramedMutSpace<'a, 'b>, message: wmidi::MidiMessage<'b>) -> Option<()> { frame .allocate(message.bytes_size(), false) .and_then(|(_, space)| message.copy_to_slice(space).ok()) .map(|_| ()) } } /// System exclusive MIDI event. /// /// This atom is an alternative to [`WMidiEvent`](struct.WMidiEvent.html): It can only write system exclusive messages, but doesn't require the message to be already constructed in memory. Instead, it uses a writing handle similar to a chunk's writing handle. /// /// Although it can only write system exclusive message, it can read any other message too. pub struct SystemExclusiveWMidiEvent; unsafe impl UriBound for SystemExclusiveWMidiEvent { const URI: &'static [u8] = sys::LV2_MIDI__MidiEvent; } impl<'a, 'b> Atom<'a, 'b> for SystemExclusiveWMidiEvent where 'a: 'b, { type ReadParameter = (); type ReadHandle = wmidi::MidiMessage<'a>; type WriteParameter = (); type WriteHandle = Writer<'a, 'b>; fn read(space: Space<'a>, _: ()) -> Option> { WMidiEvent::read(space, ()) } fn init(frame: FramedMutSpace<'a, 'b>, _: ()) -> Option> { let mut writer = Writer { frame }; writer.write::(&0xf0); Some(writer) } } /// System exclusive message writer. /// /// This writing handle is similar to a chunk's `ByteWriter`: You can allocate space, write raw bytes and generic values. /// /// The "start of system exclusive" status byte is written by [`SystemExclusiveWMidiEvent::init`](struct.SystemExclusiveWMidiEvent.html#method.init) method and the "end of system exclusive" status byte is written when the writer is dropped. pub struct Writer<'a, 'b> { frame: FramedMutSpace<'a, 'b>, } impl<'a, 'b> Writer<'a, 'b> { pub fn allocate(&mut self, size: usize) -> Option<&'a mut [u8]> { self.frame.allocate(size, false).map(|(_, slice)| slice) } pub fn write_raw(&mut self, data: &[u8]) -> Option<&'a mut [u8]> { self.frame.write_raw(data, false) } pub fn write(&mut self, instance: &T) -> Option<&'a mut T> where T: Unpin + Copy + Send + Sync + Sized + 'static, { (&mut self.frame as &mut dyn MutSpace).write(instance, false) } } impl<'a, 'b> Drop for Writer<'a, 'b> { fn drop(&mut self) { self.write::(&0xf7); } } #[cfg(test)] mod tests { use crate::wmidi_binding::*; use atom::space::RootMutSpace; use std::convert::TryFrom; use std::mem::size_of; use wmidi::*; #[test] fn test_midi_event() { let map = HashURIDMapper::new(); let urid = map.map_type::().unwrap(); let mut raw_space: Box<[u8]> = Box::new([0; 256]); let reference_message = MidiMessage::NoteOn(Channel::Ch1, Note::A0, Velocity::try_from(125).unwrap()); // writing { let mut space = RootMutSpace::new(raw_space.as_mut()); (&mut space as &mut dyn MutSpace) .init(urid, reference_message.clone()) .unwrap(); } // verifying { let (header, raw_space) = raw_space.split_at(size_of::()); let header = unsafe { &*(header.as_ptr() as *const sys::LV2_Atom) }; assert_eq!(header.type_, urid); assert_eq!(header.size as usize, 3); let (message, _) = raw_space.split_at(3); let message = MidiMessage::try_from(message).unwrap(); assert_eq!(message, reference_message); } // reading { let space = Space::from_reference(raw_space.as_ref()); let message = WMidiEvent::read(space.split_atom_body(urid).unwrap().0, ()).unwrap(); assert_eq!(message, reference_message); } } #[test] fn test_sysex_event() { let map = HashURIDMapper::new(); let urid = map.map_type::().unwrap(); let mut raw_space: Box<[u8]> = Box::new([0; 256]); // writing { let mut space = RootMutSpace::new(raw_space.as_mut()); let mut writer = (&mut space as &mut dyn MutSpace).init(urid, ()).unwrap(); writer.write_raw(&[1, 2, 3, 4]); } // verifying { let (header, raw_space) = raw_space.split_at(size_of::()); let header = unsafe { &*(header.as_ptr() as *const sys::LV2_Atom) }; assert_eq!(header.type_, urid); assert_eq!(header.size as usize, 6); let (message, _) = raw_space.split_at(6); assert_eq!(message, &[0xf0, 1, 2, 3, 4, 0xf7]); } // reading { let space = Space::from_reference(raw_space.as_ref()); let message = WMidiEvent::read(space.split_atom_body(urid).unwrap().0, ()).unwrap(); assert_eq!( message, MidiMessage::SysEx(unsafe { U7::from_bytes_unchecked(&[1, 2, 3, 4]) }) ); } } }