Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elixir, MIDI and LiveView

Elixir, MIDI and LiveView

(Paris.Ex meetup)

Thibaut Barrère

June 18, 2019
Tweet

More Decks by Thibaut Barrère

Other Decks in Programming

Transcript

  1. @thibaut_barrere Consultant Ruby / Elixir / Ansible Kiba ETL /

    Kiba Pro Remote 100% (Charente Maritime)
  2. Note On {0x90 + channel, note, velocity} Note Off {0x80

    + channel, note, release_velocity}
  3. # Start a process for MIDI event queue {:ok, pid}

    = PortMidi.open(:output, "Kontakt Virtual Input") note = 48 # C-4 velocity = 127 # Send "NOTE ON" PortMidi.write(pid, {0x90, note, velocity}) # Send "NOTE OFF" PortMidi.write(pid, {0x80, note})
  4. Translation def note_number_to_latin_name(midi_note, middle_c \\ @default_middle_c) do remap_note(midi_note, @latin_names, middle_c)

    end defp remap_note(midi_note, names, middle_c) do offset = get_base_offset(middle_c) octave = div(midi_note + offset, 12) octave_note = rem(midi_note + offset, 12) octave_note = names |> Enum.at(octave_note) "#{octave_note}#{octave}" end defp get_base_offset("C4"), do: -12
  5. Phoenix LiveView <svg> <%= for note <- notes do %>

    <rect x="..." y="0" width="10" height="20" fill="<%= note.color %>"> <% end %> </svg> SVG? ✔
  6. def note_on({channel, note, velocity}, device, portmidi \\ PortMidi) do Phoenix.PubSub.broadcast(Widgets.PubSub,

    "notes", {:note_on, note}) :ok = portmidi.write(device, {0x90 + channel, note, velocity}) end