Add generic event module with begin and end times.

Add module for converting events to schedule.
This commit is contained in:
Pim Kunis 2020-10-11 11:55:01 +02:00
parent e12fd31c92
commit 3947d6fa2a
2 changed files with 37 additions and 0 deletions

27
lib/midi_tools/event.ex Normal file
View file

@ -0,0 +1,27 @@
defmodule MIDITools.Event do
defmodule Note do
defstruct channel: 0, tone: 0, start_time: 0, end_time: 0, velocity: 0
def new(channel, tone, start_time, end_time, velocity) do
%__MODULE__{
channel: channel,
tone: tone,
start_time: start_time,
end_time: end_time,
velocity: velocity
}
end
end
def convert(%Note{
channel: channel,
tone: tone,
start_time: start_time,
end_time: end_time,
velocity: velocity
}) do
note_on = MIDISynth.Command.note_on(channel, tone, velocity)
note_off = MIDISynth.Command.note_off(channel, tone)
[{start_time, note_on}, {end_time, note_off}]
end
end

View file

@ -0,0 +1,10 @@
defmodule MIDITools.Schedule do
def convert_events(events) do
events
|> Enum.flat_map(&MIDITools.Event.convert/1)
|> Enum.reduce(%{}, fn {time, midi}, acc ->
Map.update(acc, time, midi, &<<midi::binary, &1::binary>>)
end)
|> Enum.sort()
end
end