2020-10-10 18:30:28 +00:00
|
|
|
defmodule MIDITools.Player do
|
2020-10-10 17:36:07 +00:00
|
|
|
use GenServer
|
|
|
|
|
|
|
|
# Client API
|
|
|
|
|
|
|
|
def start_link(arg \\ nil) do
|
|
|
|
GenServer.start_link(__MODULE__, arg, name: __MODULE__)
|
|
|
|
end
|
|
|
|
|
2020-10-10 18:30:28 +00:00
|
|
|
def set_schedule(schedule, end_time) do
|
|
|
|
GenServer.call(__MODULE__, {:set_schedule, schedule, end_time})
|
2020-10-10 17:36:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def play do
|
|
|
|
GenServer.call(__MODULE__, :play)
|
|
|
|
end
|
|
|
|
|
2020-10-10 18:30:28 +00:00
|
|
|
def set_repeat(repeat) do
|
|
|
|
GenServer.call(__MODULE__, {:set_repeat, repeat})
|
|
|
|
end
|
|
|
|
|
2020-10-10 17:36:07 +00:00
|
|
|
# Server callbacks
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def init(_arg) do
|
|
|
|
{:ok, synth} = MIDISynth.start_link([])
|
|
|
|
epoch = Timex.epoch() |> Timex.to_datetime()
|
2020-10-10 18:30:28 +00:00
|
|
|
|
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
timer: nil,
|
|
|
|
schedule: [],
|
|
|
|
schedule_left: [],
|
|
|
|
start_time: epoch,
|
|
|
|
end_time: 0,
|
|
|
|
synth: synth,
|
|
|
|
repeat: false
|
|
|
|
}}
|
2020-10-10 17:36:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2020-10-10 18:30:28 +00:00
|
|
|
def handle_call({:set_schedule, schedule, end_time}, _from, state) do
|
|
|
|
{:reply, :ok, %{state | schedule: schedule, schedule_left: schedule, end_time: end_time}}
|
2020-10-10 17:36:07 +00:00
|
|
|
end
|
|
|
|
|
2020-10-10 18:30:28 +00:00
|
|
|
def handle_call(:play, _from, %{timer: timer} = state) when timer != nil do
|
|
|
|
{:reply, :already_started, state}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_call(:play, _from, %{schedule: schedule} = state) do
|
2020-10-10 17:36:07 +00:00
|
|
|
start_time = Timex.now()
|
2020-10-10 18:30:28 +00:00
|
|
|
timer = start_timer(schedule, start_time)
|
|
|
|
{:reply, :ok, %{state | timer: timer, start_time: start_time, schedule_left: schedule}}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_call({:set_repeat, repeat}, _from, state) do
|
|
|
|
{:reply, :ok, %{state | repeat: repeat}}
|
2020-10-10 17:36:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info(
|
|
|
|
:play,
|
2020-10-10 18:30:28 +00:00
|
|
|
%{
|
|
|
|
schedule_left: schedule_left,
|
|
|
|
start_time: start_time,
|
|
|
|
synth: synth,
|
|
|
|
repeat: repeat,
|
|
|
|
end_time: end_time,
|
|
|
|
schedule: schedule
|
|
|
|
} = state
|
2020-10-10 17:36:07 +00:00
|
|
|
) do
|
2020-10-10 18:30:28 +00:00
|
|
|
{timer, schedule_left} = play_next_command(schedule_left, start_time, synth)
|
|
|
|
state = %{state | timer: timer, schedule_left: schedule_left}
|
|
|
|
|
|
|
|
if repeat and schedule_left == [] do
|
|
|
|
start_time = DateTime.add(start_time, end_time, :millisecond)
|
|
|
|
timer = start_timer(schedule, start_time)
|
|
|
|
{:noreply, %{state | start_time: start_time, timer: timer, schedule_left: schedule}}
|
|
|
|
else
|
|
|
|
{:noreply, state}
|
|
|
|
end
|
2020-10-10 17:36:07 +00:00
|
|
|
end
|
|
|
|
|
2020-10-10 18:30:28 +00:00
|
|
|
# Private functions
|
2020-10-10 17:36:07 +00:00
|
|
|
|
2020-10-10 18:30:28 +00:00
|
|
|
defp start_timer([], _start_time), do: nil
|
|
|
|
|
|
|
|
defp start_timer([{offset, _command} | _], start_time) do
|
|
|
|
next_time = DateTime.add(start_time, offset, :millisecond)
|
|
|
|
delay = max(Timex.diff(next_time, Timex.now(), :millisecond), 0)
|
|
|
|
Process.send_after(self(), :play, delay)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp play_next_command([], _start_time, _synth), do: {nil, []}
|
|
|
|
|
|
|
|
defp play_next_command([{offset, command} | next_schedule] = schedule_left, start_time, synth) do
|
2020-10-10 17:36:07 +00:00
|
|
|
next_time = DateTime.add(start_time, offset, :millisecond)
|
|
|
|
micro_diff = Timex.diff(next_time, Timex.now(), :microsecond)
|
|
|
|
|
|
|
|
if micro_diff < 500 do
|
|
|
|
# Play command, and try to play next command too.
|
|
|
|
MIDISynth.midi(synth, command)
|
2020-10-10 18:30:28 +00:00
|
|
|
play_next_command(next_schedule, start_time, synth)
|
2020-10-10 17:36:07 +00:00
|
|
|
else
|
|
|
|
# Command is too far in the future, schedule next timer.
|
2020-10-10 18:30:28 +00:00
|
|
|
delay = max(ceil(micro_diff / 1000), 0)
|
|
|
|
timer = Process.send_after(self(), :play, delay)
|
|
|
|
{timer, schedule_left}
|
2020-10-10 17:36:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|