PhxDemo.Midi (phx_demo v0.1.1)

Copy Markdown View Source

MIDI background music: music: "song.mid" is rendered to WAV with fluidsynth and the WAV then goes through the normal music pipeline (loop/trim, fades, ducking; see PhxDemo.Render).

The WAV is cached under <output_dir>/.tts_cache, next to narration clips, keyed on the MIDI bytes, the soundfont (path, size and mtime) and the sample rate. Re-recording an unchanged demo doesn't run fluidsynth again.

Soundfont

Resolved in this order:

  1. the :soundfont setting (use PhxDemo.Case, soundfont: "..." or config :phx_demo, :soundfont, "...")
  2. the soundfont downloaded by mix phx_demo.soundfont.setup
  3. common system locations (/usr/share/sounds/sf2/FluidR3_GM.sf2, /opt/homebrew/share/soundfonts/..., ...)

Summary

Functions

Cache key for a rendered MIDI: a hash of the MIDI bytes, the soundfont's path, size and mtime, and the sample rate.

Pre-flight for MIDI music. Returns :ok when music isn't MIDI, otherwise checks that fluidsynth is on PATH and a soundfont resolves. settings is a map with atom or string keys (PhxDemo.settings/1, or a timeline's).

Where mix phx_demo.soundfont.setup puts the default soundfont.

The fluidsynth argument list for rendering midi to out.

Whether a music: value is a MIDI file, judged by extension.

Renders midi to a WAV in cache_dir (or reuses the cached one). Returns {:ok, wav_path} or {:error, message}. Options: :soundfont (the setting), :rate.

Finds a soundfont. setting is the :soundfont setting (a path, or nil/false). Options (for testing): :downloaded and :system, to override those locations.

Directory for the soundfont downloaded by mix phx_demo.soundfont.setup.

Common system soundfont locations, in search order.

Functions

cache_key(midi_bytes, soundfont_path, arg, rate \\ 44100)

Cache key for a rendered MIDI: a hash of the MIDI bytes, the soundfont's path, size and mtime, and the sample rate.

check(settings, opts \\ [])

Pre-flight for MIDI music. Returns :ok when music isn't MIDI, otherwise checks that fluidsynth is on PATH and a soundfont resolves. settings is a map with atom or string keys (PhxDemo.settings/1, or a timeline's).

check!(settings, opts \\ [])

Like check/2, raising PhxDemo.Env.Error.

downloaded_soundfont()

Where mix phx_demo.soundfont.setup puts the default soundfont.

fluidsynth_args(soundfont, midi, out, rate \\ 44100)

The fluidsynth argument list for rendering midi to out.

midi?(path)

Whether a music: value is a MIDI file, judged by extension.

iex> PhxDemo.Midi.midi?("a/song.MID")
true

iex> PhxDemo.Midi.midi?("song.midi")
true

iex> PhxDemo.Midi.midi?("song.mp3")
false

iex> PhxDemo.Midi.midi?(false)
false

render_wav(midi, cache_dir, opts \\ [])

Renders midi to a WAV in cache_dir (or reuses the cached one). Returns {:ok, wav_path} or {:error, message}. Options: :soundfont (the setting), :rate.

resolve_soundfont(setting, opts \\ [])

Finds a soundfont. setting is the :soundfont setting (a path, or nil/false). Options (for testing): :downloaded and :system, to override those locations.

Returns {:ok, path}, or {:error, message}. An explicit setting that points at a missing file is an error; it doesn't fall through.

soundfont_dir()

Directory for the soundfont downloaded by mix phx_demo.soundfont.setup.

system_soundfonts()

Common system soundfont locations, in search order.