<commit_before>defmodule PortMidi.Input do
  @on_load {:init, 0}
  def init do
    :ok = :portmidi
    |> :code.priv_dir
    |> :filename.join("portmidi_in")
    |> :erlang.load_nif(0)
  end

  # Client implementation
  #######################

  def start_link(device_name) do
    :ok = device_name |> open
    {:ok, self}
  end

  def open(device_name), do:
    device_name
    |> String.to_char_list
    |> do_open

  def listen, do:
    {:ok, Task.async(&do_listen/0).pid}

  def do_listen do
    if poll == :read do
      read |> send_to_listeners
    end

    unless terminated?, do: do_listen
  end

  def send_to_listeners(data) do
    PortMidi.Listeners.list
    |> Enum.each( &(send(&1, data)) )
  end

  def stop(task), do:
    send(task, :stop)

  def terminated? do
    receive do
      :stop -> true
    after
      0 -> false
    end
  end

  def poll, do:
    do_poll

  def read, do:
    do_read

  # NIFs implementation
  #####################

  def do_poll, do:
    raise "NIF library not loaded"

  def do_read, do:
    raise "NIF library not loaded"

  def do_open(_device_name), do:
    raise "NIF library not loaded"

  def do_listen(_stream), do:
    raise "NIF library not loaded"
end<commit_msg>turn Input into simple API<commit_after>defmodule PortMidi.Input do
  alias PortMidi.Input.Server

  def start_link(device_name) do
    Server.start_link device_name
  end

  def listen(input, pid) do
    PortMidi.Listeners.register(input, pid)
  end

  def stop(input) do
    Server.stop(input)
  end
end

{'fraction_non_alphanumeric': 0.125, 'fraction_numerical': 0.0032722513089005235, 'mean_word_length': 2.8513853904282116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4514612', 'n_tokens_mistral': 566, 'n_tokens_neox': 538, 'n_words': 147}