Current section
Files
Jump to
Current section
Files
midi_synth
README.md
README.md
# MIDISynth[](https://hex.pm/packages/midi_synth)[](https://circleci.com/gh/fhunleth/midi_synth)Play music in Elixir.## Trying it outFirst, install [FluidSynth](http://www.fluidsynth.org/).On Linux:```shsudo apt install libfluidsynth-dev```On OSX:```shbrew install fluidsynth```Next, either clone this library or pull it in as a dependency to your Elixirproject. The following is an example of the dependency for your `mix.exs`:```elixir {:midi_synth, "~> 0.4.0"}```Fetch dependencies and build as you normally would. The first build willdownload the `FluidR3_GM.sf2` soundfont file. This is a CreativeCommons-licensed data file that contains all of the [GeneralMIDI](https://en.wikipedia.org/wiki/General_MIDI) instruments. It is a goodplace to start, but can be changed later on.## Try it out using IExStart IEx by running `iex -S mix` from a shell prompt.`MIDISynth` is a `GenServer` and must be started first. The library comes withwith helpers to make playing simple things easy. For more complicated uses,you'll want to build on this library.OK, let's play a note. Notes are numbered sequentially. Middle C is note 60.Here's how to play middle C for 100 ms.```elixiriex> {:ok, synth} = MIDISynth.start_link([]){:ok, #PID<0.226.0>}iex> MIDISynth.Keyboard.play(synth, 60, 100)```You can play the same note with a different velocity. The velocities range from1 to 127. Here's how to play middle C for 100 ms with velocity mezzo-forte: 80.```elixiriex> MIDISynth.Keyboard.play(synth, 60, 100, 80)```If you don't like the piano, try switching the instrument to something else. Forexample, trumpets ([General MIDIinstrument](https://www.midi.org/specifications-old/item/gm-level-1-sound-set)57) are nice:```elixiriex> MIDISynth.Keyboard.change_program(synth, 57)iex> MIDISynth.Keyboard.play(synth, 60, 500)```Percussion instruments can be played only through MIDI channel 9.For example, this is how to play a cowbell sound:```elixiriex> MIDISynth.Keyboard.play(synth, 56, 0, 127, 9)```The real value of this library is the ability to send raw MIDI messages to theFluidSynth library. The Elixir code barely scratches the surface of what'spossible. If you're comfortable with raw [MIDIcommands](https://www.midi.org/specifications/item/table-1-summary-of-midi-message),try this out:```elixiriex> MIDISynth.midi(synth, <<0x90, 60, 127>>)iex> MIDISynth.midi(synth, <<0x80, 60, 127>>)```See `MIDISynth.Command` for help with encoding messages, and please feel free toadd more.## LicenseThe Elixir and C code are covered by the Apache 2 License.