{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://tapestra.app/schema/tapestra-score-1.json",
  "title": "tapestra-score/1",
  "description": "A whole Tapestra performance as data: accompaniment sections, chords, piano notes, pad hits, effect changes and captions on a tick timeline. Mirrors Score.fromJson() and Score.validate() in the app (lib/core/score.dart) and the checks of EngineService.importScore(). Spec: https://tapestra.app/format.html",
  "type": "object",
  "required": ["format", "events"],
  "properties": {
    "format": { "const": "tapestra-score/1", "description": "Must be exactly this string, or the score is rejected." },
    "title": { "type": "string", "description": "Shown in the Scores list. Default: \"Untitled\"." },
    "style": {
      "description": "Accompaniment style id (assets/styles/<id>.json). Default: \"8beat_pop\". Import rejects any other id.",
      "enum": ["8beat_pop", "16beat_funk", "pop_ballad", "bossa_nova", "jazz_swing", "vienna_waltz", "slow_rock", "disco"]
    },
    "bpm": { "type": "number", "description": "Tempo in beats per minute. Default 120. Values outside 30–300 are clamped, not rejected." },
    "ppq": {
      "type": "number",
      "exclusiveMinimum": 0,
      "description": "Ticks per quarter note, used to turn ticks into time (ms per tick = 60000 / bpm / ppq). Default 24, which is what the styles use and what every example uses. Decimals are truncated. The app does not check it, but a ppq of 0 or less cannot play, so the schema rejects it."
    },
    "key": { "type": "string", "description": "Display and AI hint only; the engine ignores it. Default \"C\"." },
    "events": {
      "type": "array",
      "minItems": 1,
      "description": "Events in any order (the app sorts them by t). Import fails with \"no events\" when empty and with \"no section event — the band never starts\" when no event is a section.",
      "items": { "$ref": "#/$defs/event" },
      "contains": { "type": "object", "required": ["section"] }
    }
  },
  "$defs": {
    "event": {
      "description": "An object with a tick \"t\" and one kind. The kind is chosen by the first of these keys present: section, chord, n, pad, fx, caption. Other keys of the object are then ignored.",
      "type": "object",
      "required": ["t"],
      "properties": {
        "t": { "type": "number", "minimum": 0, "description": "Tick position from the start (decimals are truncated)." }
      },
      "if": { "required": ["section"] },
      "then": { "$ref": "#/$defs/section" },
      "else": {
        "if": { "required": ["chord"] },
        "then": { "$ref": "#/$defs/chord" },
        "else": {
          "if": { "required": ["n"] },
          "then": { "$ref": "#/$defs/note" },
          "else": {
            "if": { "required": ["pad"] },
            "then": { "$ref": "#/$defs/pad" },
            "else": {
              "if": { "required": ["fx"] },
              "then": { "$ref": "#/$defs/fx" },
              "else": {
                "if": { "required": ["caption"] },
                "then": { "$ref": "#/$defs/caption" },
                "else": false
              }
            }
          }
        }
      }
    },
    "section": {
      "description": "Starts the band (first section event) or requests a section change. Fills, BREAK and ENDING cut in on the next beat; MAIN_A and MAIN_B on the next bar line. ENDING plays out and stops the band.",
      "properties": {
        "section": { "enum": ["INTRO", "MAIN_A", "MAIN_B", "FILL_A", "FILL_B", "BREAK", "ENDING"] }
      }
    },
    "chord": {
      "description": "Sets the accompaniment chord. Root A–G, optional # or b; the chord is minor when the rest starts with \"m\" but not \"maj\". Anything after the quality (7, maj7, sus4 …) is accepted and ignored: the band plays major or minor triads.",
      "properties": {
        "chord": { "type": "string", "pattern": "^\\s*[A-G][#b]?[^\\n]*$" }
      }
    },
    "note": {
      "description": "A note on a MIDI channel (0 = piano, 9 = drums). Numbers are clamped (note 0–127, v 1–127, ch 0–15, d at least 1).",
      "properties": {
        "n": {
          "oneOf": [
            { "type": "number", "description": "MIDI note number; 60 = C4." },
            { "type": "string", "pattern": "^\\s*[A-G][#b]?-?[0-9]\\s*$", "description": "Note name with a one-digit octave, C4 = 60: C5, F#3, Bb2, C-1." }
          ]
        },
        "v": { "type": "number", "description": "Velocity 1–127. Default 100." },
        "d": { "type": "number", "description": "Length in ticks. Default ppq / 2." },
        "ch": { "type": "number", "description": "MIDI channel 0–15. Default 0 (piano)." }
      }
    },
    "pad": {
      "description": "Hits pad number \"pad\" (0-based) of the kit that is loaded. Import rejects indexes outside 0–15; indexes past the end of the current kit (built-in kits have 8 pads) are skipped at play time.",
      "properties": {
        "pad": { "type": "number", "minimum": 0, "exclusiveMaximum": 16 },
        "v": { "type": "number", "description": "Velocity 1–127. Default 110." },
        "d": { "type": "number", "description": "Hold time in ticks before release. Default ppq / 4." }
      }
    },
    "fx": {
      "description": "Switches a master-bus effect on or off and/or sets its parameters. Parameter keys that are unknown for the effect, or not numbers, are ignored.",
      "properties": {
        "fx": { "enum": ["eq", "compressor", "pitch", "flanger", "delay", "reverb", "bass", "bassBoost"] },
        "on": { "type": "boolean" }
      },
      "allOf": [
        { "if": { "properties": { "fx": { "const": "eq" } } }, "then": { "$ref": "#/$defs/fxParams/eq" } },
        { "if": { "properties": { "fx": { "const": "compressor" } } }, "then": { "$ref": "#/$defs/fxParams/compressor" } },
        { "if": { "properties": { "fx": { "const": "pitch" } } }, "then": { "$ref": "#/$defs/fxParams/pitch" } },
        { "if": { "properties": { "fx": { "const": "flanger" } } }, "then": { "$ref": "#/$defs/fxParams/flanger" } },
        { "if": { "properties": { "fx": { "const": "delay" } } }, "then": { "$ref": "#/$defs/fxParams/delay" } },
        { "if": { "properties": { "fx": { "const": "reverb" } } }, "then": { "$ref": "#/$defs/fxParams/reverb" } },
        { "if": { "properties": { "fx": { "enum": ["bass", "bassBoost"] } } }, "then": { "$ref": "#/$defs/fxParams/bass" } }
      ]
    },
    "fxParams": {
      "eq": { "properties": { "band0": { "type": "number" }, "band1": { "type": "number" }, "band2": { "type": "number" }, "band3": { "type": "number" }, "band4": { "type": "number" } } },
      "compressor": { "properties": { "threshold": { "type": "number" }, "ratio": { "type": "number" }, "attack": { "type": "number" }, "release": { "type": "number" }, "makeup": { "type": "number" } } },
      "pitch": { "properties": { "semitones": { "type": "number" }, "mix": { "type": "number" } } },
      "flanger": { "properties": { "rate": { "type": "number" }, "depth": { "type": "number" }, "feedback": { "type": "number" }, "mix": { "type": "number" } } },
      "delay": { "properties": { "time": { "type": "number" }, "feedback": { "type": "number" }, "mix": { "type": "number" }, "pingpong": { "type": "number" } } },
      "reverb": { "properties": { "room": { "type": "number" }, "damp": { "type": "number" }, "width": { "type": "number" }, "mix": { "type": "number" } } },
      "bass": { "properties": { "gain": { "type": "number" } } }
    },
    "caption": {
      "description": "Text shown while the score plays. A plain string is English. An object maps locale codes (en, zh_Hant, zh_Hans …) to text; the app picks the exact locale, then the language, then en.",
      "properties": {
        "caption": {
          "oneOf": [
            { "type": "string" },
            { "type": "object", "additionalProperties": { "type": "string" } }
          ]
        }
      }
    }
  }
}
