5
2

ElixirでDJコントローラーLチカ

Last updated at Posted at 2024-06-29

前提条件

  • DJコントローラー DDJ-FLX4
  • OS Ubuntu22.04
  • portmidiをインストールできる環境(他のOSでもできるかも)

環境作成

portmidiのインストール

$ sudo apt install libportmidi-dev libportmidi0

コードを書く

完成版はgithubに有ります

portmidiの設定をする

mix.exs
defmodule Hoge.MixProject do
  use Mix.Project

  def project do
    [
      app: :hoge,
      version: "0.1.0",
      elixir: "~> 1.16",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
+     extra_applications: [:logger, :portmidi]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
+      {:portmidi, "~> 5.0"}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

Lチカの本体

lib/hoge.ex
defmodule Hoge do
  @moduledoc """
  Documentation for `Hoge`.
  """


  def hello() do
    # MIDIデバイスの一覧 デバッグ用
    PortMidi.devices
    |> IO.inspect()

    #上記一覧からデバイスを指定してMIDIデバイスを開く 例:"DDJ-FLX4 MIDI 1"
    {_, output} = PortMidi.open(:output, "DDJ-FLX4 MIDI 1")
    |> IO.inspect()
    
    #Lチカを100回繰り返す
    Enum.each(1..100, fn _ -> led(output) end)

    # MIDIデバイスを閉じる
    PortMidi.close(:output, output)

    :world
  end
  
  def led(output) do
    # LチカのMIDIのデータの作成と送信
    ["B0","02", "7f"]
    |> send_midi(output)
    Process.sleep(200)
    ["B0","02", "00"]
    |> send_midi(output)
    Process.sleep(200)
    ["B1","02", "7f"]
    |> send_midi(output)
    Process.sleep(200)
    ["B1","02", "00"]
    |> send_midi(output)
    Process.sleep(200)
  end

  def send_midi(v, output) do
    # 16進数を10進数に変換してMIDIデータを送信

    data = Enum.map(v, fn x ->  h(x)  end)
    |> List.to_tuple()
    |> IO.inspect()
    PortMidi.write(output, data)
  end

  def h(v) do
   # 16進数を10進数に変換
    {a, _} = Integer.parse(v, 16)
    a
  end
end

動作動画

参考資料

DDJ-FLX4 MIDIメッセージ一覧

5
2
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
5
2