{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$comment": "Copyright (c) 2026 Nenad Micic. SPDX-License-Identifier: Apache-2.0",
  "title": "JotTune score v1",
  "description": "Structural contract for editable score-v1 JSON. Compilation checks UTF-8/byte budgets, rational bounds, map ordering, stream and voice references, pitch range and event endpoints. Native inspection and replay check declarations, DSP parameter ranges, render budgets and saved audio. Passing this schema alone does not verify a song.",
  "type": "object",
  "additionalProperties": false,
  "required": ["version", "fs", "clock", "declarations", "streams", "voices", "events"],
  "properties": {
    "version": {"const": 1},
    "name": {"allOf": [{"$ref": "#/definitions/line"}, {"minLength": 1}]},
    "about": {"type": "array", "minItems": 0, "maxItems": 1048576, "items": {"$ref": "#/definitions/line"}},
    "fs": {"enum": [44100, 48000]},
    "clock": {"$ref": "#/definitions/clock"},
    "metadata": {"$ref": "#/definitions/metadata"},
    "declarations": {"type": "string", "description": "Native instrument/effect declarations and recipe rows."},
    "streams": {"type": "array", "minItems": 1, "maxItems": 16, "items": {"$ref": "#/definitions/identifier"}, "uniqueItems": true},
    "voices": {"type": "array", "minItems": 1, "maxItems": 64, "items": {"$ref": "#/definitions/identifier"}},
    "events": {"type": "array", "minItems": 0, "maxItems": 8192, "items": {"$ref": "#/definitions/event"}}
  },
  "definitions": {
    "integer": {"type": "integer", "minimum": -9007199254740991, "maximum": 9007199254740991},
    "rational": {
      "description": "Integer or quoted numerator/denominator in quarter-note units or BPM. The compiler bounds the numerator and the denominator (1..4096).",
      "oneOf": [{"$ref": "#/definitions/integer"}, {"type": "string", "pattern": "^-?(0|[1-9][0-9]*)/[1-9][0-9]*$", "maxLength": 22}]
    },
    "identifier": {"type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_-]*$"},
    "line": {"type": "string", "pattern": "^[^\\r\\n\\u0000]*$"},
    "label": {"type": "string", "pattern": "^[A-Za-z0-9._:+/@-]{1,160}$"},
    "metadata": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "title": {"type": "string", "minLength": 1, "maxLength": 60},
        "collection": {"type": "string", "minLength": 1, "maxLength": 200},
        "author": {"type": "string", "minLength": 1, "maxLength": 200},
        "made_by": {"enum": ["agent", "human", "mixed"]},
        "genre": {"anyOf": [{"$ref": "#/definitions/label"}, {"type": "null"}]},
        "tempo": {
          "anyOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["min_bpm", "max_bpm"],
              "properties": {
                "min_bpm": {"type": "number", "exclusiveMinimum": 0, "maximum": 9007199254740991},
                "max_bpm": {"type": "number", "exclusiveMinimum": 0, "maximum": 9007199254740991}
              }
            },
            {"type": "null"}
          ]
        },
        "model": {
          "anyOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["id", "source"],
              "properties": {"id": {"$ref": "#/definitions/label"}, "source": {"enum": ["client-reported", "user-stated"]}}
            },
            {"type": "null"}
          ]
        },
        "utc_day": {"type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$", "format": "date"},
        "license": {"$ref": "#/definitions/label"},
        "renderer_release": {"type": "string", "pattern": "^[A-Za-z0-9._-]{1,128}$"},
        "prompt_id": {"$ref": "#/definitions/label"}
      },
      "description": "The player checks NFC, whitespace, control characters, calendar dates and tempo ordering for portable TL metadata. License is a declaration of the song’s terms."
    },
    "clock": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bars", "meter", "tempo", "ramp", "tail_ns"],
      "properties": {
        "bars": {"type": "integer", "minimum": 1, "maximum": 65536},
        "meter": {
          "type": "array",
          "minItems": 1,
          "maxItems": 128,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["bar", "quarters"],
            "properties": {"bar": {"type": "integer", "minimum": 1, "maximum": 65536}, "quarters": {"$ref": "#/definitions/rational"}}
          }
        },
        "tempo": {
          "type": "array",
          "minItems": 1,
          "maxItems": 128,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["bar", "bpm"],
            "properties": {"bar": {"type": "integer", "minimum": 1, "maximum": 65537}, "bpm": {"$ref": "#/definitions/rational"}}
          }
        },
        "ramp": {"enum": ["step", "linear"]},
        "tail_ns": {"type": "integer", "minimum": 0, "maximum": 600000000000}
      }
    },
    "pitch": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["midi"],
          "properties": {"midi": {"type": "integer", "minimum": 0, "maximum": 127}}
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["note"],
          "properties": {"note": {"type": "string", "pattern": "^[A-G][#b]?-?[0-9]$"}}
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["millihz"],
          "properties": {"millihz": {"$ref": "#/definitions/integer"}}
        }
      ]
    },
    "event": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {"type": "string"},
        "at": {"$ref": "#/definitions/rational"},
        "stream": {"$ref": "#/definitions/identifier"},
        "voice": {"type": "integer", "minimum": 0, "maximum": 63},
        "duration": {"$ref": "#/definitions/rational"},
        "pitch": {"$ref": "#/definitions/pitch"},
        "param": {"$ref": "#/definitions/identifier"},
        "bus": {"enum": ["chorus", "delay", "reverb", "duck", "flanger"]},
        "bits": {"type": "integer", "minimum": 1, "maximum": 24},
        "rate_div": {"type": "integer", "minimum": 1, "maximum": 64},
        "enabled": {"type": "integer", "minimum": 0, "maximum": 1},
        "text": {"$ref": "#/definitions/line"},
        "velocity_q15": {"$ref": "#/definitions/integer"},
        "input": {"$ref": "#/definitions/integer"},
        "value_q15": {"$ref": "#/definitions/integer"},
        "cutoff_millihz": {"$ref": "#/definitions/integer"},
        "q_milli": {"$ref": "#/definitions/integer"},
        "level_q15": {"$ref": "#/definitions/integer"},
        "x_mm": {"$ref": "#/definitions/integer"},
        "y_mm": {"$ref": "#/definitions/integer"},
        "z_mm": {"$ref": "#/definitions/integer"},
        "yaw_mdeg": {"$ref": "#/definitions/integer"},
        "id": {"$ref": "#/definitions/integer"},
        "value": {"$ref": "#/definitions/integer"}
      },
      "oneOf": [
        {
          "properties": {"kind": {"const": "note"}},
          "required": ["kind", "at", "stream", "voice", "duration", "pitch", "velocity_q15"],
          "maxProperties": 7
        },
        {
          "properties": {"kind": {"const": "on"}},
          "required": ["kind", "at", "stream", "voice", "pitch", "velocity_q15"],
          "maxProperties": 6
        },
        {"properties": {"kind": {"const": "off"}}, "required": ["kind", "at", "stream", "voice"], "maxProperties": 4},
        {
          "properties": {"kind": {"const": "pitch"}},
          "required": ["kind", "at", "stream", "voice", "pitch"],
          "maxProperties": 5
        },
        {
          "properties": {"kind": {"const": "ctl"}},
          "required": ["kind", "at", "stream", "voice", "input", "value_q15"],
          "maxProperties": 6
        },
        {
          "properties": {"kind": {"const": "filter"}},
          "required": ["kind", "at", "stream", "voice", "cutoff_millihz", "q_milli"],
          "maxProperties": 6
        },
        {
          "properties": {"kind": {"const": "send"}},
          "required": ["kind", "at", "stream", "voice", "bus", "level_q15"],
          "maxProperties": 6
        },
        {
          "properties": {"kind": {"const": "crush"}},
          "required": ["kind", "at", "stream", "voice", "bits", "rate_div"],
          "maxProperties": 6
        },
        {
          "properties": {"kind": {"const": "pos"}},
          "required": ["kind", "at", "stream", "voice", "x_mm", "y_mm", "z_mm"],
          "maxProperties": 7
        },
        {
          "properties": {"kind": {"const": "move"}},
          "required": ["kind", "at", "stream", "voice", "duration", "x_mm", "y_mm", "z_mm"],
          "maxProperties": 8
        },
        {"properties": {"kind": {"const": "yaw"}}, "required": ["kind", "at", "stream", "yaw_mdeg"], "maxProperties": 4},
        {
          "properties": {"kind": {"const": "gen"}},
          "required": ["kind", "at", "stream", "id", "param", "value"],
          "maxProperties": 6
        },
        {
          "properties": {"kind": {"const": "lane"}},
          "required": ["kind", "at", "stream", "id", "enabled"],
          "maxProperties": 5
        },
        {"properties": {"kind": {"const": "mark"}}, "required": ["kind", "at", "stream", "text"], "maxProperties": 4}
      ]
    }
  }
}
