Add tests for player and event.
This commit is contained in:
parent
362826eb2f
commit
2802e5fbf7
6 changed files with 85 additions and 16 deletions
|
@ -11,8 +11,15 @@ defmodule MIDITools.Event do
|
||||||
|
|
||||||
defstruct channel: 0, tone: 0, start_time: 0, end_time: 0, velocity: 0
|
defstruct channel: 0, tone: 0, start_time: 0, end_time: 0, velocity: 0
|
||||||
|
|
||||||
@spec new(MIDISynth.Command.channel(), non_neg_integer(), non_neg_integer(), non_neg_integer(), MIDISynth.Command.velocity()) :: %Note{}
|
@spec new(
|
||||||
def new(channel, tone, start_time, end_time, velocity) when start_time >= 0 and end_time > start_time do
|
MIDISynth.Command.channel(),
|
||||||
|
non_neg_integer(),
|
||||||
|
non_neg_integer(),
|
||||||
|
non_neg_integer(),
|
||||||
|
MIDISynth.Command.velocity()
|
||||||
|
) :: %Note{}
|
||||||
|
def new(channel, tone, start_time, end_time, velocity)
|
||||||
|
when start_time >= 0 and end_time > start_time do
|
||||||
%__MODULE__{
|
%__MODULE__{
|
||||||
channel: channel,
|
channel: channel,
|
||||||
tone: tone,
|
tone: tone,
|
||||||
|
@ -30,7 +37,8 @@ defmodule MIDITools.Event do
|
||||||
|
|
||||||
defstruct channel: 0, time: 0, program: 0
|
defstruct channel: 0, time: 0, program: 0
|
||||||
|
|
||||||
@spec new(MIDISynth.Command.channel(), non_neg_integer(), non_neg_integer()) :: %ChangeProgram{}
|
@spec new(MIDISynth.Command.channel(), non_neg_integer(), non_neg_integer()) ::
|
||||||
|
%ChangeProgram{}
|
||||||
def new(channel, time, program) do
|
def new(channel, time, program) do
|
||||||
%__MODULE__{channel: channel, time: time, program: program}
|
%__MODULE__{channel: channel, time: time, program: program}
|
||||||
end
|
end
|
||||||
|
|
|
@ -66,7 +66,7 @@ defmodule MIDITools.Player do
|
||||||
@doc """
|
@doc """
|
||||||
Resume playback on the player after it has been paused.
|
Resume playback on the player after it has been paused.
|
||||||
"""
|
"""
|
||||||
@spec resume() :: :ok, {:error, :not_paused}
|
@spec(resume() :: :ok, {:error, :not_paused})
|
||||||
def resume do
|
def resume do
|
||||||
GenServer.call(__MODULE__, :resume)
|
GenServer.call(__MODULE__, :resume)
|
||||||
end
|
end
|
||||||
|
@ -100,8 +100,7 @@ defmodule MIDITools.Player do
|
||||||
start_time = Timex.now()
|
start_time = Timex.now()
|
||||||
timer = start_timer(schedule, start_time)
|
timer = start_timer(schedule, start_time)
|
||||||
|
|
||||||
{:reply, :ok,
|
{:reply, :ok, %{reset(state) | timer: timer, start_time: start_time}}
|
||||||
%{reset(state) | timer: timer, start_time: start_time}}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_call({:set_repeat, repeat}, _from, state) do
|
def handle_call({:set_repeat, repeat}, _from, state) do
|
||||||
|
|
2
mix.exs
2
mix.exs
|
@ -29,7 +29,7 @@ defmodule MIDITools.MixProject do
|
||||||
[
|
[
|
||||||
{:midi_synth, "~> 0.4.0"},
|
{:midi_synth, "~> 0.4.0"},
|
||||||
{:timex, "~> 3.6"},
|
{:timex, "~> 3.6"},
|
||||||
{:ex_doc, "~> 0.22", only: :dev, runtime: false},
|
{:ex_doc, "~> 0.22", only: :dev, runtime: false}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
28
test/event_test.exs
Normal file
28
test/event_test.exs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
defmodule MIDITools.EventTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
doctest MIDITools.Event
|
||||||
|
|
||||||
|
alias MIDITools.Event
|
||||||
|
|
||||||
|
test "note event" do
|
||||||
|
assert %Event.Note{channel: 0, tone: 60, start_time: 1, end_time: 1000, velocity: 127} =
|
||||||
|
Event.Note.new(0, 60, 1, 1000, 127)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "change program event" do
|
||||||
|
assert %Event.ChangeProgram{channel: 0, time: 1, program: 40} =
|
||||||
|
Event.ChangeProgram.new(0, 1, 40)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "note event conversion" do
|
||||||
|
note = Event.Note.new(0, 60, 1, 1000, 127)
|
||||||
|
[note_on, note_off] = Event.convert(note)
|
||||||
|
assert {1, <<0x90, 60, 127>>} = note_on
|
||||||
|
assert {1000, <<0x80, 60, 64>>} = note_off
|
||||||
|
end
|
||||||
|
|
||||||
|
test "change program event conversion" do
|
||||||
|
change_program = Event.ChangeProgram.new(0, 1, 40)
|
||||||
|
assert [{1, <<0xC0, 40>>}] = Event.convert(change_program)
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,8 +0,0 @@
|
||||||
defmodule MIDIToolsTest do
|
|
||||||
use ExUnit.Case
|
|
||||||
doctest MIDITools
|
|
||||||
|
|
||||||
test "greets the world" do
|
|
||||||
assert MIDITools.hello() == :world
|
|
||||||
end
|
|
||||||
end
|
|
42
test/player_test.exs
Normal file
42
test/player_test.exs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
defmodule MIDITools.PlayerTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
doctest MIDITools.Player
|
||||||
|
|
||||||
|
alias MIDITools.Player
|
||||||
|
|
||||||
|
setup do
|
||||||
|
{:ok, _pid} = Player.start_link()
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
setup_all do
|
||||||
|
events = Enum.map(1..4, &MIDITools.Event.Note.new(9, 51, &1 * 500, (&1 + 1) * 500, 127))
|
||||||
|
duration = 2000
|
||||||
|
[events: events, duration: duration]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "play", %{events: events, duration: duration} do
|
||||||
|
assert :ok = Player.generate_schedule(events, duration)
|
||||||
|
assert :ok = Player.play()
|
||||||
|
Process.sleep(2500)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "pause & resume", %{events: events, duration: duration} do
|
||||||
|
Player.generate_schedule(events, duration)
|
||||||
|
Player.play()
|
||||||
|
Process.sleep(1100)
|
||||||
|
assert :ok = Player.pause()
|
||||||
|
Process.sleep(500)
|
||||||
|
assert :ok = Player.resume()
|
||||||
|
Process.sleep(1400)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "pause & resume edge cases", %{events: events, duration: duration} do
|
||||||
|
Player.generate_schedule(events, duration)
|
||||||
|
assert {:error, :not_started} = Player.pause()
|
||||||
|
assert {:error, :not_paused} = Player.resume()
|
||||||
|
Player.play()
|
||||||
|
Player.pause()
|
||||||
|
assert {:error, :already_paused} = Player.pause()
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue