Zongzi.Score.RecordMap (zongzi v0.3.0)

Copy Markdown

Generic Record compiler and binary search engine.

Compiles a series of positioned Records into a left-closed, right-open interval tuple and provides binary search over it.

Compiled events

Each compiled event must include start_pos and end_pos fields. Remaining fields are freely populated by the reducer.

Example

# TempoMap's compile
reducer = fn start_tick, end_tick, event, current_sec ->
  with {:ok, strategy} <- Tempo.build_segment_from_event(...) do
    duration = Tempo.duration_sec(strategy)
    {:ok, %{start_pos: start_tick, end_pos: end_tick, start_sec: current_sec, strategy: strategy},
     current_sec + duration}
  end
end

RecordMap.compile(tempo_events, reducer, 0.0)

Summary

Types

A compiled event.

Reducer function signature.

t()

Functions

Compiles a list of Records into a binary-searchable tuple.

Finds the interval containing target_pos in the compiled tuple.

Types

compiled_event()

@type compiled_event() :: %{
  :start_pos => Zongzi.Score.Record.position(),
  :end_pos => Zongzi.Score.Record.end_position(),
  optional(atom()) => term()
}

A compiled event.

Must include start_pos and end_pos for binary search.

reducer()

Reducer function signature.

Receives the interval's start position, end position, Record value, and accumulated state. Returns {:ok, compiled_event, new_acc} or {:error, reason}.

t()

@type t() :: tuple()

Functions

compile(records_arg, reducer, initial_acc)

@spec compile(Zongzi.Score.Record.records(), reducer(), term()) ::
  {:ok, t()} | {:error, term()}

Compiles a list of Records into a binary-searchable tuple.

Returns {:ok, compiled_tuple} or {:error, reason}.

find_by_position(tuple, target_pos)

@spec find_by_position(t(), Zongzi.Score.Record.position()) :: compiled_event()

Finds the interval containing target_pos in the compiled tuple.

Intervals are left-closed, right-open [start_pos, end_pos). When target_pos falls outside all intervals, returns the last interval.