4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ElixirAdvent Calendar 2024

Day 20

Elixirで作る「DDJ-FLX4のジョグ対応ブロックくずし」 その6 対策をする、そしてプロジェクトを3分割のつづきddj_client編

Last updated at Posted at 2024-12-19

前回のつづきです

ddj_client

ddj_client/lib/ddj_client.ex
defmodule DdjClient do
  @moduledoc """
  Documentation for `DdjClient`.
  """

  alias Socket.Web

  def hello do
    socket =
      Web.connect!("localhost", 4000, path: "/socket/websocket?token=undefined&vsn=2.0.0")
      |> join()

    {_, input} = PortMidi.open(:input, "DDJ-FLX4 MIDI 1")
    PortMidi.listen(input, self())
    midi_in(input, socket)
    PortMidi.close(:input, input)

    :world
  end

  def midi_in(input, socket) do
    receive do
      v ->
        jog(v, socket)
    end

    midi_in(input, socket)
  end

  def jog({_, [{{176, 33, 65}, _}]}, socket) do
    IO.inspect("右 1")
    send_data(socket, "1.0")
  end

  def jog({_, [{{176, 33, 63}, _}]}, socket) do
    IO.inspect("左 -1")
    send_data(socket, "-1.0")
  end

  def jog(_, _) do
    nil
  end

  def send_data(socket, data) do
    data = """
    ["3","4","ddj:lobby","new_msg",{"body":"#{data}"}]
    """

    Web.send!(socket, {:text, data})
    socket
  end

  def join(socket) do
    phx_join = """
    ["3","3","ddj:lobby","phx_join",{}]
    """

    Web.send!(socket, {:text, phx_join})
    socket
  end
end
  • hello

    • WebSocketサーバーに接続
    • DDJ-FLX4を開く
    • DDJ-FLX4の受信開始
    • midi_in(受信ループ)開始
    • DDJ-FLX4を閉じる今は事実無効
  • midi_in

    • jog関数を呼ぶ
    • midi_inを呼んでループ
  • jog

    • ジョグを回したら1.0又は-1.0をサーバーに送る

つづく
次回はサーバー編です

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?