Skip to content

A Claude Code plugin that turns a story into a finished anime

Ruby
6.9 KB
Raw Blame
require "json"
require "yaml"
require "fileutils"
require_relative "shell"
require_relative "ffmpeg"

module Media
  # Sound effects over a finished video (the full-song preview). prompts/<name>/sfx.yml has
  #   source: / out:          the video to add sounds to (only read) and the new file
  #   rel_db:                 default level of a cue, in dB relative to the music's loudness under it (−4 ≈ clearly audible, still under it)
  #   peak_db / max_gain_db:  caps: a cue never peaks above peak_db (−6) nor gets boosted more than max_gain_db (+18)
  #   sounds: { name => { prompt:, duration:, loop:, prompt_influence: } }   generated with ElevenLabs Sound Effects v3,
  #           or { tone: Hz, duration: }: a steady sine synthesized with ffmpeg (a monitor flatline; the model only makes rhythmic beeps)
  #   cues:   [{ at: song s, sound:, rel_db:, rate: (pitch/speed), len: (s, cut + fade), fade: (s) }, ...]
  # Sounds are cached in output/<name>/sounds/<sound>.wav with a sidecar json; a sound is generated again only when its prompt,
  # model, duration, loop or prompt influence changed (or FORCE=1 with ONLY=a,b). scripts/sfx_mix.py trims their leading silence, sets each cue's level
  # against the music in its window and limits the sum; the video stream is copied untouched.
  class Sfx < Shell
    ROOT = File.expand_path("../..", __dir__)

    attr_reader :name, :dir

    def initialize(name = ENV.fetch("SFX", "mv2-sfx"))
      @name = name
      @dir = File.join(ROOT, "output", name)
      FileUtils.mkdir_p(File.join(@dir, "sounds"))
    end

    def config = @config ||= YAML.safe_load_file(File.join(ROOT, "prompts", name, "sfx.yml"))
    def source = File.expand_path(config.fetch("source"), ROOT)
    def out = File.expand_path(config.fetch("out"), ROOT)
    def sounds = config.fetch("sounds")
    def wav(sound) = File.join(dir, "sounds", "#{sound}.wav")

    # Generate every sound that is missing or stale. only: names to force (with force: true).
    def generate(only: nil, force: false, client: nil)
      unknown = config.fetch("cues").map { |c| c.fetch("sound") }.uniq - sounds.keys
      raise "cues use undefined sounds: #{unknown.join(", ")}" if unknown.any?
      model = nil
      sounds.filter_map do |sound, spec|
        want = if spec["tone"]
                 { "tone" => spec["tone"], "duration" => spec.fetch("duration", 2) }
               else
                 prompt = spec.fetch("prompt")
                 negative = spec["negative"] || config["negative"]
                 prompt = "#{prompt}. Avoid: #{negative}" unless negative.to_s.strip.empty?
                 warn "[sfx] #{sound}: seed is ignored by ElevenLabs SFX v3" if spec.key?("seed")
                 { "model" => Fal::Models::ElevenlabsSfx.endpoint, "prompt" => prompt,
                   "duration" => spec.fetch("duration", 2), "loop" => spec.fetch("loop", false),
                   "prompt_influence" => spec.fetch("prompt_influence", 0.3) }
               end
        meta = File.join(dir, "sounds", "#{sound}.json")
        have = File.exist?(meta) && File.exist?(wav(sound)) ? JSON.parse(File.read(meta)) : {}
        stale = have.slice(*want.keys) != want
        next unless stale || (force && (only.nil? || only.include?(sound)))
        if spec["tone"]
          # A steady sine (e.g. a monitor flatline, which the SFX model always turns into rhythmic beeps): synthesized, no fal call.
          run("ffmpeg", "-y", "-v", "error", "-f", "lavfi", "-i", "sine=frequency=#{want["tone"]}:sample_rate=48000:duration=#{want["duration"]}",
              "-af", "volume=-6dB,afade=t=in:d=0.004,afade=t=out:st=#{want["duration"] - 0.02}:d=0.02", "-ac", "2", wav(sound))
          File.write(meta, JSON.pretty_generate(want))
          next sound
        end
        warn "[sfx] #{sound} generating…"
        client ||= Fal::Client.new
        model ||= Fal::Models::ElevenlabsSfx.new(client: client)
        duration = want["duration"]
        generated_duration = want["loop"] == true && duration.is_a?(Numeric) ? [duration, 30].min : duration
        res = model.generate(prompt: want["prompt"], duration: generated_duration,
                             loop: want["loop"], prompt_influence: want["prompt_influence"])
        url = res.output.fetch("audio").fetch("url")
        original = File.join(dir, "sounds", "#{sound}.mp3")
        client.download(url, original)
        # Decode before looping: repeated MP3 encoder padding otherwise shortens the final PCM duration.
        pending = File.join(dir, "sounds", "#{sound}.pending.wav")
        run("ffmpeg", "-y", "-v", "error", "-i", original, "-t", generated_duration,
            "-ar", "48000", "-ac", "2", "-c:a", "pcm_s16le", pending)
        if want["loop"]
          looped = File.join(dir, "sounds", "#{sound}.looped.wav")
          run("ffmpeg", "-y", "-v", "error", "-stream_loop", "-1", "-i", pending,
              "-t", duration, "-c:a", "pcm_s16le", looped)
          File.rename(looped, pending)
        end
        File.rename(pending, wav(sound))
        File.write(meta, JSON.pretty_generate(want.merge("request_id" => res.request_id, "url" => url)))
        sound
      end
    end

    # `voice_dip_db:` in sfx.yml: the cues dip under the scene's voice track, and their levels are read from the mix made
    # before any voice lift (03_mix_ref.wav, Steps::Mix), so a louder voice is not followed by louder effects.
    def voice_args
      dip = config["voice_dip_db"] or return []
      scene = File.dirname(source)
      voice = File.join(scene, "03_dialogue.wav")
      ref = File.join(scene, "03_mix_ref.wav")
      [*(File.exist?(ref) ? ["--level-ref", ref] : []), *(File.exist?(voice) ? ["--voice", voice, "--voice-dip-db", dip.to_s] : [])]
    end

    # Mix the cues over the source video's audio -> out (video copied). Returns [out, per-cue level report].
    def mix
      missing = config.fetch("cues").map { |c| c["sound"] }.uniq.reject { |s| File.exist?(wav(s)) }
      raise "sounds not generated yet (rake sfx:gen): #{missing.join(", ")}" if missing.any?
      cues = config.fetch("cues").map do |c|
        { "at" => c.fetch("at"), "wav" => wav(c.fetch("sound")), "sound" => c["sound"], "rel_db" => c.fetch("rel_db", config.fetch("rel_db", -4)),
          "rate" => c.fetch("rate", 1.0), "len" => c["len"], "fade" => c.fetch("fade", 0.05) }
      end
      File.write(path("cues.json"), JSON.pretty_generate(cues))
      music = path("music.wav")
      run("ffmpeg", "-y", "-v", "error", "-i", source, "-vn", "-ac", "2", "-ar", "48000", "-c:a", "pcm_s16le", music)
      report = JSON.parse(run(Media::Python.new.executable, File.join(ROOT, "tools", "python", "sfx_mix.py"), music, path("cues.json"), path("mix.wav"), *voice_args,
                              "--floor-db", config.fetch("floor_db", -30).to_s, "--peak-db", config.fetch("peak_db", -6).to_s,
                              "--max-gain-db", config.fetch("max_gain_db", 18).to_s, quiet: true))
      FFmpeg.new.mux(source, path("mix.wav"), out)
      [out, report]
    end

    def path(file) = File.join(dir, file)
  end
end