@wildshard/shard-format 0.4.6 → 0.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildshard/shard-format",
3
- "version": "0.4.6",
3
+ "version": "0.4.8",
4
4
  "description": "The raw shardfile format: the spec, the validator and a validate CLI. The public, versioned contract every other repo builds on.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -7,24 +7,44 @@ import type { AudioShape } from './audio-mp4.ts'
7
7
  // AUDIO AU5: measure one recording from its own bytes, with no decode and no
8
8
  // ffmpeg. The strict playback index (`aacIndex.ts`) reads an admitted m4a; a
9
9
  // plain box walk (`audio-mp4.ts`) reads an AAC file the index refuses (no
10
- // `edts`, as six of Pine Hollow's beds); a RIFF walk reads PCM WAV.
10
+ // `edts`, as six of Pine Hollow's beds); a RIFF walk reads PCM WAV. The bytes
11
+ // decide the container, never the URL: AU7's build keeps a source's URL, so a
12
+ // `.wav` or `.flac` URL can serve AAC. Any other container (FLAC, AIFF, Ogg,
13
+ // MP3, WebM, CAF, or bytes no sniff knows) still counts, and is unreadable.
11
14
 
12
15
  /** What a recording is for: its URL says so (`/assets/music/` or `/assets/sfx/`). */
13
16
  type RecordingRole = 'music' | 'sfx'
14
17
 
18
+ /** The container a recording's bytes are in, sniffed from its first bytes. */
19
+ type AudioContainer =
20
+ | 'mp4'
21
+ | 'wav'
22
+ | 'flac'
23
+ | 'aiff'
24
+ | 'ogg'
25
+ | 'mp3'
26
+ | 'webm'
27
+ | 'caf'
28
+ | 'unknown'
29
+
15
30
  /** One recording, measured from its own bytes. */
16
31
  interface RecordingMeasure extends AudioShape {
17
32
  readonly url: string
18
33
  readonly hash: string
19
34
  readonly bytes: number
20
35
  readonly role: RecordingRole
36
+ /** What the bytes are, whatever the URL's extension says. */
37
+ readonly container: AudioContainer
21
38
  /** `aac` is AAC in mp4; `wav` is PCM WAV; `unreadable` is neither. */
22
39
  readonly codec: 'aac' | 'wav' | 'unreadable'
23
40
  /** Why the strict playback index refused it, when it did. */
24
41
  readonly reason?: string
25
42
  }
26
43
 
27
- type MeasureBase = Pick<RecordingMeasure, 'url' | 'hash' | 'bytes' | 'role'>
44
+ type MeasureBase = Pick<
45
+ RecordingMeasure,
46
+ 'url' | 'hash' | 'bytes' | 'role' | 'container'
47
+ >
28
48
 
29
49
  interface WavFormat {
30
50
  readonly channels: number
@@ -45,13 +65,25 @@ const WAV_BYTE_RATE = 8
45
65
  const WAV_BLOCK = 12
46
66
  const WAV_FMT_MIN = 16
47
67
  const PAD = 2
68
+ const FORM_TYPE = 8
69
+ const NEXT = 1
70
+ const MP3_SYNC = 255
71
+ const MP3_SYNC_HIGH = 224
72
+ const UTF16_BOM_LOW = 254
73
+ const UNKNOWN_REASON = 'bytes in no audio container AU5 knows; they still count'
48
74
 
49
75
  const ROLE_PATTERN = /^\/assets\/(?<role>music|sfx)\//u
50
- const RECORDING_PATTERN = /^\/assets\/(?:music|sfx)\/.+\.(?:m4a|wav)$/u
76
+ const AUDIO_EXTENSION =
77
+ /\.(?:m4a|mp4|aac|wav|wave|flac|aif|aiff|aifc|caf|mp3|ogg|oga|opus|webm|weba)$/iu
78
+
79
+ /** Whether a public URL sits in an audio folder (`/assets/music/` or `/assets/sfx/`). */
80
+ function isAudioFolderUrl(url: string): boolean {
81
+ return ROLE_PATTERN.test(url)
82
+ }
51
83
 
52
- /** Whether a public URL is a recording AU5 counts. */
84
+ /** Whether a public URL names an audio file in an audio folder, by any audio extension. */
53
85
  function isRecordingUrl(url: string): boolean {
54
- return RECORDING_PATTERN.test(url)
86
+ return isAudioFolderUrl(url) && AUDIO_EXTENSION.test(url)
55
87
  }
56
88
 
57
89
  function recordingRole(url: string): RecordingRole {
@@ -62,6 +94,54 @@ function riffTag(raw: Uint8Array, at: number): string {
62
94
  return String.fromCodePoint(...raw.subarray(at, at + TAG_BYTES))
63
95
  }
64
96
 
97
+ function isMp3(raw: Uint8Array): boolean {
98
+ // An ID3 tag, or an MPEG audio (or ADTS) frame sync: byte 255, then 3 high
99
+ // bits set, but not 255 254, a UTF-16 text file's byte-order mark.
100
+ const second = raw[NEXT] ?? NONE
101
+ return (
102
+ riffTag(raw, NONE).startsWith('ID3') ||
103
+ (raw[NONE] === MP3_SYNC &&
104
+ second >= MP3_SYNC_HIGH &&
105
+ second !== UTF16_BOM_LOW)
106
+ )
107
+ }
108
+
109
+ /** First-four-byte magics; WebM's is the EBML header. */
110
+ const CONTAINER_TAGS: ReadonlyMap<string, AudioContainer> = new Map([
111
+ ['fLaC', 'flac'],
112
+ ['OggS', 'ogg'],
113
+ ['caff', 'caf'],
114
+ ['\u001AE\u00DF\u00A3', 'webm'],
115
+ ])
116
+
117
+ /** RIFF and IFF files: the outer tag, then the form type at byte 8. */
118
+ const FORM_TAGS: ReadonlyMap<string, AudioContainer> = new Map([
119
+ ['RIFF WAVE', 'wav'],
120
+ ['FORM AIFF', 'aiff'],
121
+ ['FORM AIFC', 'aiff'],
122
+ ])
123
+
124
+ /** The container of a recording's bytes, from their magic numbers alone. */
125
+ function audioContainer(raw: Uint8Array): AudioContainer {
126
+ const head = riffTag(raw, NONE)
127
+ const form = FORM_TAGS.get(`${head} ${riffTag(raw, FORM_TYPE)}`)
128
+ if (form !== undefined) {
129
+ return form
130
+ }
131
+ if (riffTag(raw, TAG_BYTES) === 'ftyp') {
132
+ return 'mp4'
133
+ }
134
+ return CONTAINER_TAGS.get(head) ?? (isMp3(raw) ? 'mp3' : 'unknown')
135
+ }
136
+
137
+ /** Whether a mounted file in an audio folder is a recording AU5 counts: an audio extension, or audio bytes. */
138
+ function isRecordingMount(url: string, raw: Uint8Array): boolean {
139
+ return (
140
+ isRecordingUrl(url) ||
141
+ (isAudioFolderUrl(url) && audioContainer(raw) !== 'unknown')
142
+ )
143
+ }
144
+
65
145
  /** The `fmt ` fields; a byte rate that is not rate times block size reads as 0, so the file is unreadable. */
66
146
  function wavFormat(view: DataView, body: number): WavFormat {
67
147
  const rate = view.getUint32(body + WAV_RATE, true)
@@ -153,32 +233,63 @@ function strictAac(raw: Uint8Array): AudioShape {
153
233
  }
154
234
  }
155
235
 
236
+ function unreadable(base: MeasureBase, reason: string): RecordingMeasure {
237
+ const empty = { channels: NONE, sampleRate: NONE, seconds: NONE }
238
+ return { ...base, ...empty, kbps: NONE, codec: 'unreadable', reason }
239
+ }
240
+
156
241
  function aacMeasure(base: MeasureBase, raw: Uint8Array): RecordingMeasure {
157
242
  try {
158
- return { ...base, codec: 'aac', ...strictAac(raw) }
243
+ return { ...base, container: 'mp4', codec: 'aac', ...strictAac(raw) }
159
244
  } catch (error: unknown) {
160
245
  const reason = error instanceof Error ? error.message : String(error)
161
246
  const loose = looseAac(raw)
162
- if (loose === undefined) {
163
- const empty = { channels: NONE, sampleRate: NONE, seconds: NONE }
164
- return { ...base, ...empty, kbps: NONE, codec: 'unreadable', reason }
165
- }
166
- return { ...base, codec: 'aac', ...loose, reason }
247
+ return loose === undefined
248
+ ? unreadable(base, reason)
249
+ : { ...base, container: 'mp4', codec: 'aac', ...loose, reason }
250
+ }
251
+ }
252
+
253
+ /** A recording whose bytes are not WAV: AAC in mp4, or a container AU5 does not read. */
254
+ function otherMeasure(base: MeasureBase, raw: Uint8Array): RecordingMeasure {
255
+ if (base.container === 'mp4') {
256
+ return aacMeasure(base, raw)
257
+ }
258
+ if (base.container === 'unknown') {
259
+ // An mp4 with no leading `ftyp` may still read as AAC.
260
+ const row = aacMeasure(base, raw)
261
+ return row.codec === 'aac' ? row : unreadable(base, UNKNOWN_REASON)
167
262
  }
263
+ const name = base.container.toUpperCase()
264
+ return unreadable(
265
+ base,
266
+ `${name} bytes, not AAC in mp4 or WAV; they still count`,
267
+ )
168
268
  }
169
269
 
170
- /** Measure one recording at `url` from its bytes. */
270
+ /** Measure one recording at `url` from its bytes: the bytes pick the container, not the URL's extension. */
171
271
  function measureRecording(
172
272
  url: string,
173
273
  hash: string,
174
274
  raw: Uint8Array,
175
275
  ): RecordingMeasure {
176
- const base = { url, hash, bytes: raw.length, role: recordingRole(url) }
276
+ const container = audioContainer(raw)
277
+ const role = recordingRole(url)
278
+ const base = { url, hash, bytes: raw.length, role, container }
279
+ if (container !== 'wav') {
280
+ return otherMeasure(base, raw)
281
+ }
177
282
  const wav = wavShape(raw)
178
283
  return wav === undefined
179
- ? aacMeasure(base, raw)
284
+ ? unreadable(base, 'a RIFF WAVE file with no readable fmt and data')
180
285
  : { ...base, codec: 'wav', ...wav }
181
286
  }
182
287
 
183
- export type { RecordingMeasure, RecordingRole }
184
- export { isRecordingUrl, measureRecording }
288
+ export type { AudioContainer, RecordingMeasure, RecordingRole }
289
+ export {
290
+ audioContainer,
291
+ isAudioFolderUrl,
292
+ isRecordingMount,
293
+ isRecordingUrl,
294
+ measureRecording,
295
+ }
@@ -107,9 +107,12 @@ function formatProblem(
107
107
 
108
108
  function formatFinding(row: Readonly<RecordingMeasure>): AudioFinding[] {
109
109
  if (row.codec === 'unreadable') {
110
- const why = row.reason ?? 'unknown'
111
- const message = `${row.url} is not AAC-LC 48 kHz mono or stereo (${why})`
112
- return [{ code: 'audio-unreadable', level: 'red', rule: 'format', message }]
110
+ const code =
111
+ row.container === 'mp4' || row.container === 'wav'
112
+ ? 'audio-unreadable'
113
+ : 'audio-container'
114
+ const message = `${row.url} is not AAC-LC 48 kHz mono or stereo (${row.reason ?? 'unknown'})`
115
+ return [{ code, level: 'red', rule: 'format', message }]
113
116
  }
114
117
  if (row.codec === 'aac' && row.reason !== undefined) {
115
118
  const message = `${row.url} is AAC outside the strict playback index (${row.reason})`
@@ -5,9 +5,10 @@ import memoryGates from './memoryGates.json' with { type: 'json' };
5
5
  export const CHUNK_SIZE = 500; // metres, square
6
6
  /** Half a cell width in metres. */
7
7
  export const CHUNK_HALF: number = CHUNK_SIZE / 2;
8
- /** Signed placement radius of the finite platform catalogue (SF88); residency budgets stay independent. */
9
- export const GRID_RADIUS = 2;
10
- /** Number of placements, including open plots, in the finite square platform. */
8
+ /** Signed placement radius of the finite platform catalogue (SF88): 7×7 since J314, J305's 5×5 at its centre and the
9
+ * outer ring void until a shard is placed there; residency budgets stay independent. */
10
+ export const GRID_RADIUS = 3;
11
+ /** Most placements, including open plots, the finite square platform holds; a grid may leave cells empty. */
11
12
  export const GRID_CELL_COUNT: number = (2 * GRID_RADIUS + 1) ** 2;
12
13
  /** Legal midpoint entry opening in metres. */
13
14
  export const ENTRY_WIDTH = 8;
@@ -223,15 +223,16 @@ const gridShape = strictObject({
223
223
  emptyNeighbour,
224
224
  })
225
225
 
226
- /** Placement data, independent of package content and solo coordinates. */
226
+ /** Placement data, independent of package content and solo coordinates. Since J314 a
227
+ * cell may stay empty: the void a new shard's first upload is placed into. */
227
228
  const GridCatalogueSchema: GenericSchema<GridCatalogueInput, GridCatalogue> =
228
229
  pipe(
229
230
  gridShape,
230
231
  check(
231
232
  // oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- valibot's check takes the schema's own type
232
233
  (grid: InferOutput<typeof gridShape>): boolean =>
233
- uniquePlaces(grid.cells) && grid.cells.length === GRID_CELL_COUNT,
234
- 'unique stable instance identities and complete signed cell layout',
234
+ uniquePlaces(grid.cells),
235
+ 'unique stable instance identities and cells',
235
236
  ),
236
237
  )
237
238
 
@@ -101,6 +101,8 @@ const PERFORMANCE_CODES = {
101
101
  const AUDIO_CODES = {
102
102
  'audio-unreadable':
103
103
  'A recording is not readable as AAC-LC 48 kHz mono or stereo (red).',
104
+ 'audio-container':
105
+ "A recording's bytes are FLAC, AIFF, Ogg, MP3, WebM, CAF or no known container, not AAC in mp4 or WAV; they still count toward the budget (red).",
104
106
  'audio-strict-index':
105
107
  'A recording is AAC outside the strict playback index (amber).',
106
108
  'audio-wav': 'A recording is PCM WAV; ship AAC-LC 48 kHz (amber).',
@@ -1,16 +1,22 @@
1
- import { isRecordingUrl, measureRecording } from './audio-measure.ts'
1
+ import {
2
+ isAudioFolderUrl,
3
+ isRecordingMount,
4
+ measureRecording,
5
+ } from './audio-measure.ts'
2
6
  import { audioReport, isoDay } from './audio-rules.ts'
3
7
 
4
8
  import type { RecordingMeasure } from './audio-measure.ts'
5
9
  import type { AudioReport } from './audio-rules.ts'
6
- import type { CookData } from './cook-data.ts'
10
+ import type { CookData, CookMount } from './cook-data.ts'
7
11
  import type { OutputIndex } from './outputIndex.ts'
8
12
  import type { Shardfile } from './shardSchema.ts'
9
13
 
10
14
  // AUDIO AU5: a validated product's own recordings, and its report against the
11
15
  // audio rules. Validation has already checked every byte's hash and size; this
12
16
  // only measures. The recordings are the cook's audio mounts (`/assets/music`
13
- // and `/assets/sfx`, under `mounts/` or in the blob they equal) and the audio
17
+ // and `/assets/sfx`, under `mounts/` or in the blob they equal; any audio
18
+ // extension, or any audio bytes whatever the extension, so a `.flac` or `.aiff`
19
+ // counts and only non-audio files such as `music.json` don't) and the audio
14
20
  // section's non-commons `recordings` blobs. Commons recordings are shared, so
15
21
  // they are not the shard's own.
16
22
 
@@ -18,7 +24,9 @@ import type { Shardfile } from './shardSchema.ts'
18
24
  interface ProductAudioInput {
19
25
  readonly cook: CookData | undefined
20
26
  readonly read: (path: string, maxBytes: number) => Uint8Array
21
- readonly shard: Readonly<Pick<Shardfile, 'audio'>>
27
+ readonly shard: Readonly<{
28
+ audio: Readonly<Pick<Shardfile['audio'], 'recordings'>>
29
+ }>
22
30
  readonly assets: ReadonlyMap<string, Uint8Array>
23
31
  }
24
32
 
@@ -30,24 +38,15 @@ function productRecordings(
30
38
  input: Readonly<ProductAudioInput>,
31
39
  ): RecordingMeasure[] {
32
40
  const mounts = (input.cook?.mounts ?? [])
33
- .filter((mount: Readonly<{ url: string }>): boolean =>
34
- isRecordingUrl(mount.url),
35
- )
36
- .map(
37
- (
38
- mount: Readonly<{
39
- url: string
40
- path: string
41
- hash: string
42
- bytes: number
43
- }>,
44
- ): RecordingMeasure =>
45
- measureRecording(
46
- mount.url,
47
- mount.hash,
48
- input.read(mount.path, mount.bytes),
49
- ),
41
+ .filter((mount: Readonly<CookMount>): boolean =>
42
+ isAudioFolderUrl(mount.url),
50
43
  )
44
+ .flatMap((mount: Readonly<CookMount>): RecordingMeasure[] => {
45
+ const raw = input.read(mount.path, mount.bytes)
46
+ return isRecordingMount(mount.url, raw)
47
+ ? [measureRecording(mount.url, mount.hash, raw)]
48
+ : []
49
+ })
51
50
  const bound = Object.entries(input.shard.audio.recordings ?? {}).flatMap(
52
51
  ([url, ref]: readonly [string, string]): RecordingMeasure[] => {
53
52
  const bytes = ref.startsWith(COMMONS) ? undefined : input.assets.get(ref)