Code Monkey home page Code Monkey logo

midi_fundsp's Introduction

midi_fundsp

Live performance synthesizer library

This crate assembles and integrates code from the midir, midi-msg, and cpal crates to enable the easy creation of live synthesizer software using fundsp for sound synthesis.

Using the crate involves setting up the following:

  • An input thread to monitor MIDI events
  • An output thread generating sounds that correspond to those events
  • A table of fundsp sounds for the output thread to employ
  • A SegQueue that enables those threads to communicate

Putting these pieces together yields the following introductory example program:

use std::sync::{Arc, Mutex};

use crossbeam_queue::SegQueue;
use crossbeam_utils::atomic::AtomicCell;
use midi_fundsp::{
    io::{get_first_midi_device, start_input_thread, start_output_thread},
    sounds::options,
};
use midir::MidiInput;
use read_input::{shortcut::input, InputBuild};

fn main() -> anyhow::Result<()> {
    let mut midi_in = MidiInput::new("midir reading input")?;
    let in_port = get_first_midi_device(&mut midi_in)?;
    let midi_msgs = Arc::new(SegQueue::new());
    let quit = Arc::new(AtomicCell::new(false));
    start_input_thread(midi_msgs.clone(), midi_in, in_port, quit.clone());
    start_output_thread::<10>(midi_msgs, Arc::new(Mutex::new(options())));
    input::<String>().msg("Press any key to exit\n").get();
    Ok(())
}

The first four lines set up:

  • A handle to the first MIDI input device it finds
  • A messaging queue to connect the input and output threads
  • A flag to instruct the threads to quit

The next two lines call start_input_thread() and start_output_thread() to start the corresponding threads. The table of fundsp sounds comes from midi_fundsp::sounds::options(), but a user can easily assemble their own custom table of sounds as well.

Once the threads start, the program continues until the user enters a key, handling any incoming MIDI events as they arrive.

Other example programs show how to send different sounds to the left and right stereo channels and how to change the selection of synthesizer sound and MIDI input device while running.

Notes

  • Always compile with --release. Sound quality is poor when compiled with --debug.
  • The following MIDI messages are currently supported:
    • Note On
    • Note Off
    • Pitch Bend
    • Program Change
      • Program change numbers correspond to indices in the ProgramTable
      • These can originate either from a MIDI input device or from software
    • All Notes Off
    • All Sound Off
  • See CHANGELOG.md for updates.

Acknowledgements

  • Thank you to the authors of fundsp, midir, midi-msg, and cpal, who made it possible and practical for me to create this crate.
  • Special thanks to Sami Perttu, author of fundsp, for help resolving some technical issues and for incorporating pull requests into fundsp that made this crate possible.

License

Licensed under either of

at your option.

Contributions

I am very interested in receiving contributions to this library. Here are types of contributions I envision, and how I would like contributors to proceed:

  • Creating additional synthesizer sounds
    • Add a function with type signature Fn(&SharedMidiState) -> Box<dyn AudioUnit64> to sounds.rs. Add your function and a suitable description to the table generated by options() in that same file. Then make a pull request to include it.
  • Handling additional MIDI messages
    • Open an issue describing the MIDI message you would like to handle, and conceptually speaking how you would like to handle it in the context of this library.
    • From there, we will discuss the best way forward.
  • Other ideas
    • Open an issue describing your idea, and we will discuss it from there.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

midi_fundsp's People

Contributors

gjf2a avatar jpursell avatar blacryu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.