lucidstack / ex-portmidi

Elixir bindings to the portmidi library
MIT License
35 stars 12 forks source link

Speed problems #2

Closed jimm closed 8 years ago

jimm commented 8 years ago

I'm using what I think is the simplest "through" configuration in PortMidi, but the throughput is very slow. Am I doing something wrong?

defmodule Sysex do
  def main(in_name \\ "MidiPipe Output 1", out_name \\ "SimpleSynth virtual input") do
    {:ok, input} = PortMidi.open(:input, in_name)
    {:ok, output} = PortMidi.open(:output, out_name)
    listener = spawn_link(Sysex, :loop, [input, output])
    PortMidi.listen(input, listener)
    :timer.sleep(20_000)       # this will be Process.sleep in future Elixir
    send(listener, :stop)
    PortMidi.close(:input, input)
    PortMidi.close(:output, output)
  end

  def loop(input, output) do
    receive do
      {^input, bytes} ->
        PortMidi.write(output, bytes)
        loop(input, output)
      :stop ->
        :ok
    end
  end
end

When I connect MidiPipe (http://www.subtlesoft.square7.net/MidiPipe.html) directly to SimpleSynth (http://notahat.com/simplesynth/) by connection MidiPipe's output to the SimpleSynth virtual input, the output keeps up with me as I play a virtual keyboard in MP with my mouse, running the mouse over the keyboard back and forth.

When I switch MP to use its own virtual output and connect to that as an input, and use the code above, it can only keep up with four or five notes a second.

What am I doing wrong?

Also, since this may not be an issue with PortMidi, please let me know if there is a better place to be asking these questions.

lucidstack commented 8 years ago

Hi @jimm, sorry for taking so long to reply!

I think I've tackled the issue. As I had initially built this library for a very trivial use case, I hadn't considered performance, and didn't properly fiddle with the buffer size values.

Now I have (hopefully!) set up all the proper values, and playing SimpleSynth from MidiPipe works perfectly on my Macbook. Thank you for pointing me to those apps by the way, really nice stuff for debugging.

Could you give a try to the with-buffer-size branch? You can fetch that branch by having this in your mix.exs:

  defp deps do
    [{:portmidi, github: "lucidstack/ex-portmidi", branch: "with-buffer-size"}]
  end

Please let me know how it goes, and don't hesitate to contact me for further questions or issues of any kind.

jimm commented 8 years ago

That did it! Thank you, @lucidstack.

lucidstack commented 8 years ago

Fantastic! I've just merged to master and pushed to Hex, so feel free to...

  defp deps do
    [{:portmidi, "~> 5.0.0"}]
  end

Have a nice day! 👋