{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://pointcast.xyz/sunday/score.schema.json",
  "title": "Tone Bloom Rainy Sunday score v1",
  "description": "Portable deterministic note events for the four Rainy Sunday instruments. Local synthesis only; no audio recordings or executable code.",
  "$comment": "This structural schema is paired with validateScore in /sunday/protocol.mjs. That runtime validator also requires durationMs to equal the maximum event end time, every event to end by 120000ms, and no more than twelve overlapping notes. Those aggregate arithmetic invariants are not expressible in standard JSON Schema. Runtime normalization sorts events and rounds velocity to three decimals.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema", "version", "engine", "instrument", "durationMs", "events"],
  "properties": {
    "schema": { "const": "tone-bloom.score.v1" },
    "version": { "const": 1 },
    "engine": { "const": "sunday-audio.v1" },
    "instrument": { "enum": ["window", "seam", "cup", "clock"] },
    "durationMs": { "type": "integer", "minimum": 50, "maximum": 120000 },
    "events": {
      "type": "array",
      "minItems": 1,
      "maxItems": 256,
      "items": { "$ref": "#/$defs/event" }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "instrument": { "enum": ["window", "seam", "clock"] } }, "required": ["instrument"] },
      "then": {
        "properties": {
          "events": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": { "durationMs": { "type": "integer", "maximum": 6000 } }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "event": {
      "type": "object",
      "additionalProperties": false,
      "required": ["atMs", "midi", "velocity", "durationMs"],
      "properties": {
        "atMs": { "type": "integer", "minimum": 0, "maximum": 119950 },
        "midi": { "type": "integer", "minimum": 36, "maximum": 96 },
        "velocity": { "type": "number", "minimum": 0, "maximum": 1 },
        "durationMs": { "type": "integer", "minimum": 50, "maximum": 16000 }
      }
    }
  },
  "examples": [
    {
      "schema": "tone-bloom.score.v1",
      "version": 1,
      "engine": "sunday-audio.v1",
      "instrument": "window",
      "durationMs": 2800,
      "events": [
        { "atMs": 0, "midi": 72, "velocity": 0.7, "durationMs": 2200 },
        { "atMs": 600, "midi": 79, "velocity": 0.5, "durationMs": 2200 }
      ]
    }
  ]
}
