mlua-pulse 0.1.0

Lua-friendly music composition and audio export bindings built on tunes and mlua
Documentation
local pulse = require("pulse")

local title = "hardcore-edm"
local bpm = 150.0
local beat = 60.0 / bpm
local export_options = {
  sample_rate = 44100,
  normalize = "rms",
  target_db = -14.0,
  true_peak_db = -1.0,
  midi_velocity_gain = 1.9,
  midi_min_velocity = 0.64,
}

local function rep(value, count)
  local values = {}
  for index = 1, count do
    values[index] = value
  end
  return values
end

local function degree(scale, value)
  local unique = #scale - 1
  local zero = value - 1
  local octave = math.floor(zero / unique)
  local position = (zero % unique) + 1
  return pulse.transpose(scale[position], octave * 12)
end

local function line(scale, degrees)
  local notes = {}
  for index, value in ipairs(degrees) do
    notes[index] = degree(scale, value)
  end
  return notes
end

local function machine(kick, hats, volume)
  return pulse.drum_machine("808")
    :steps(16)
    :step_duration(beat / 4.0)
    :kick(kick)
    :snare("----x-------x---")
    :hihat(hats)
    :open_hihat("--x---x---x---x-")
    :clap("----x-------x---")
    :grid()
    :repeat_times(3)
    :volume(volume)
    :effect("compressor", "aggressive")
end

local f_minor = pulse.scale("F3", "minor")
local c_phrygian = pulse.scale("C4", "phrygian")

local intro = pulse.phrase()
  :add(
    pulse.sequence()
      :chords({pulse.chord("F2", "min7"), pulse.chord("Db2", "maj7"), pulse.chord("Ab2", "maj7"), pulse.chord("Eb2", "dominant7")})
      :durations(rep(beat * 4.0, 4))
      :instrument("dark_pad")
      :volume(0.42)
      :velocity(0.45)
      :effect("filter", { type = "low_pass", cutoff = 900.0, resonance = 0.28, slope = 24 })
      :effect("reverb", "ambient")
  )
  :add(
    pulse.sequence()
      :notes(line(c_phrygian, {8, 7, 5, 4, 3, 4, 5, 7, 8, 10, 8, 7, 5, 4, 3, 1}))
      :durations(rep(beat / 2.0, 16))
      :instrument("scream_lead")
      :volume(0.34)
      :pan(0.18)
      :velocity(0.55)
      :effect("filter", { type = "low_pass", cutoff = 2600.0, resonance = 0.18, slope = 24 })
      :effect("phaser", "deep")
  )
  :add_drum_grid(machine("x---x---x---x---", "x-x-x-x-x-x-x-x-", 0.36))
  :repeat_times(1)

local build = pulse.phrase()
  :add(
    pulse.sequence()
      :notes(line(c_phrygian, {1, 2, 3, 5, 7, 8, 10, 8, 7, 5, 3, 2, 1, 2, 3, 5, 7, 8, 10, 12, 10, 8, 7, 5, 3, 5, 7, 8, 10, 12, 13, 15}))
      :durations(rep(beat / 2.0, 32))
      :instrument("supersaw")
      :volume(0.42)
      :pan(-0.12)
      :velocity(0.58)
      :effect("filter", { type = "high_pass", cutoff = 280.0, resonance = 0.2 })
      :effect("delay", "eighth_note")
  )
  :add(
    pulse.sequence()
      :notes(line(f_minor, {1, 1, 5, 5, 6, 6, 8, 8, 1, 1, 5, 5, 6, 6, 8, 8}))
      :durations(rep(beat, 16))
      :instrument("bass_808")
      :volume(0.78)
      :velocity(0.82)
      :synth("wavetable")
      :effect("compressor", "bass")
  )
  :add_drum_grid(machine("x-x-x-x-x-x-x-x-", "xxxxxxxxxxxxxxxx", 0.46))
  :repeat_times(1)

local drop = pulse.phrase()
  :add(
    pulse.sequence()
      :notes(line(f_minor, {1, 1, 1, 5, 6, 6, 5, 3, 1, 1, 8, 6, 5, 5, 3, 1}))
      :durations(rep(beat, 16))
      :instrument("bass_808")
      :volume(0.9)
      :velocity(0.9)
      :synth("fm", "growl")
      :effect("filter", { type = "low_pass", cutoff = 1600.0, resonance = 0.7, slope = 24 })
      :effect("compressor", "bass")
  )
  :add(
    pulse.sequence()
      :chords({pulse.chord("F3", "minor"), pulse.chord("Db3", "major"), pulse.chord("Ab3", "major"), pulse.chord("Eb3", "dominant7")})
      :durations(rep(beat * 2.0, 4))
      :instrument("stab")
      :volume(0.5)
      :pan(0.12)
      :velocity(0.68)
      :effect("chorus", "thick")
  )
  :add(
    pulse.sequence()
      :notes(line(c_phrygian, {8, 8, 7, 5, 10, 8, 7, 5, 12, 10, 8, 7, 10, 8, 7, 5, 8, 8, 7, 5, 10, 8, 7, 5, 12, 10, 8, 7, 15, 12, 10, 8}))
      :durations(rep(beat / 2.0, 32))
      :instrument("supersaw")
      :volume(0.38)
      :pan(-0.18)
      :velocity(0.6)
      :effect("delay", "dotted_eighth")
  )
  :add_drum_grid(machine("x-xxx-xxx-xxx-xx", "xxxxxxxxxxxxxxxx", 0.6))
  :add_drum_grid(
    pulse.drum_grid()
      :track("hardcore_layers")
      :steps(16)
      :step_duration(beat / 4.0)
      :sound("kick_808_deep_layer", "x---x---x---x---")
      :sound("snare_909_snap_layer", "----x-------x---")
      :sound("crash_swell_layer", "x---------------")
      :repeat_times(3)
      :volume(0.38)
  )
  :repeat_times(3)

local break_section = pulse.phrase()
  :add(
    pulse.sequence()
      :chords({pulse.chord("Db3", "maj7"), pulse.chord("Eb3", "dominant7"), pulse.chord("F3", "min7"), pulse.chord("C3", "dominant7")})
      :durations(rep(beat * 4.0, 4))
      :instrument("warm_pad")
      :volume(0.44)
      :velocity(0.42)
      :effect("reverb", "ambient")
  )
  :add(
    pulse.sequence()
      :notes(line(f_minor, {1, 5, 6, 5, 1, 5, 6, 5}))
      :durations(rep(beat * 2.0, 8))
      :instrument("sub_bass")
      :volume(0.62)
      :velocity(0.7)
  )
  :repeat_times(1)

local song = pulse.song()
  :tempo(bpm)
  :add_phrase(intro)
  :add_phrase(build)
  :add_phrase(drop)
  :add_phrase(break_section)
  :add_phrase(drop)
  :master_effect("compressor", { threshold = 0.56, ratio = 2.4, attack = 0.012, release = 0.18, makeup_gain = 1.05 })
  :master_effect("eq", { low_gain = 1.08, mid_gain = 0.82, high_gain = 0.34, low_freq = 160.0, high_freq = 3600.0 })
  :master_effect("limiter", "brick_wall")

assert(title == "hardcore-edm")
song:export_wav("output/hardcore-edm.wav", export_options)
song:export_flac("output/hardcore-edm.flac", export_options)
song:export_midi("output/hardcore-edm.mid", export_options)